```
- #!/bin/bash
- if [ -n "$BASHRC_ALREADY_RUN" ]; then
- return
- fi
- export BASHRC_ALREADY_RUN=1
- uname -snrvm
- load1=$(awk '{printf "%.2f", $1}' /proc/loadavg)
- load5=$(awk '{printf "%.2f", $2}' /proc/loadavg)
- load15=$(awk '{printf "%.2f", $3}' /proc/loadavg)
- uptime=$(uptime -p | sed 's/up //')
- memory=$(free -m | awk '/Mem/{printf "%d%% of %.1fGB", $3/$2*100, $2/1024}')
- local_ip=$(ip addr show eth0 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1)
- if [ -z "$local_ip" ]; then
- local_ip=$(ip addr show eth1 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1)
- fi
- if [ -z "$local_ip" ]; then
- local_ip=$(ip addr show bridge0 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1)
- fi
- if [ -z "$local_ip" ]; then
- local_ip=$(hostname -I 2>/dev/null | awk '{print $1}' | head -1)
- fi
- ipv6_address=$(ip addr show 2>/dev/null | grep inet6 | grep -v 'scope link' | grep -v '::1/128' | head -1 | awk '{print $2}' | cut -d/ -f1)
- if [ -z "$ipv6_address" ]; then
- ipv6_address="N/A"
- fi
- # 系统温度检测
- temp="N/A"
- # 优先读取CPU温度(thermal_zone1)
- if [ -f "/sys/class/thermal/thermal_zone1/temp" ]; then
- temp_raw=$(cat "/sys/class/thermal/thermal_zone1/temp" 2>/dev/null | tr -d -c '0-9')
- if [ -n "$temp_raw" ] && [ "$temp_raw" -gt 0 ]; then
- temp=$((temp_raw/1000))
- temp="${temp}°C"
- fi
- fi
- # 如果没找到或显示为27°C,尝试其他CPU温度传感器
- if [ "$temp" = "N/A" ] || [ "$temp" = "27°C" ]; then
- # 尝试hwmon3下的CPU核心温度
- for sensor in /sys/class/hwmon/hwmon3/temp*_input; do
- if [ -f "$sensor" ]; then
- temp_raw=$(cat "$sensor" 2>/dev/null | tr -d -c '0-9')
- if [ -n "$temp_raw" ] && [ "$temp_raw" -gt 0 ]; then
- temp_value=$((temp_raw/1000))
- # 如果不是27度,就用这个值
- if [ $temp_value -ne 27 ] && [ $temp_value -ge 10 ] && [ $temp_value -le 100 ]; then
- temp="${temp_value}°C"
- break
- fi
- fi
- fi
- done
- fi
- volume_paths=()
- for i in {1..4}; do
- if [ -d "/volume$i" ] && mountpoint -q "/volume$i" 2>/dev/null; then
- volume_paths+=("/volume$i")
- fi
- done
- if [ ${#volume_paths[@]} -eq 0 ]; then
- storage_summary="0% of 0T"
- else
- total_size_kb=0
- total_used_kb=0
-
- for path in "${volume_paths[@]}"; do
- df_output=$(df -k "$path" 2>/dev/null | tail -1)
- if [ -n "$df_output" ]; then
- size_kb=$(echo "$df_output" | awk '{print $2}')
- used_kb=$(echo "$df_output" | awk '{print $3}')
- total_size_kb=$((total_size_kb + size_kb))
- total_used_kb=$((total_used_kb + used_kb))
- fi
- done
-
- if [ $total_size_kb -gt 0 ]; then
- usage_percent=$((total_used_kb * 100 / total_size_kb))
-
- total_size_tb=$(echo "scale=2; $total_size_kb / (1024 * 1024 * 1024)" | bc 2>/dev/null)
- if [ -z "$total_size_tb" ] || [ "$total_size_tb" = "0" ]; then
- total_size_tb=$(awk "BEGIN {printf "%.2f", $total_size_kb / (1024 * 1024 * 1024)}" 2>/dev/null || echo "0")
- fi
- storage_summary="${usage_percent}% of ${total_size_tb}T"
- else
- storage_summary="0% of 0T"
- fi
- fi
- cpu_model=$(grep -m1 'model name' /proc/cpuinfo | cut -d: -f2 | sed 's/^[ \t]*//' | head -c 30)
- cpu_cores=$(grep -c 'processor' /proc/cpuinfo)
- # CPU使用率 - 使用/proc/stat计算
- cpu_usage="0.0"
- # 从/proc/stat读取CPU时间信息
- if cpu_line=$(grep '^cpu ' /proc/stat 2>/dev/null); then
- # 移除开头的"cpu ",获取所有时间值
- cpu_times=$(echo "$cpu_line" | cut -d' ' -f2-)
-
- # 计算总时间和空闲时间
- total=0
- idle=0
- count=1
-
- for time_val in $cpu_times; do
- total=$((total + time_val))
- # 第4个和第5个值是idle和iowait(0-based索引,所以是第4和第5个)
- if [ $count -eq 4 ] || [ $count -eq 5 ]; then
- idle=$((idle + time_val))
- fi
- count=$((count + 1))
- done
-
- # 计算使用率百分比
- if [ $total -gt 0 ]; then
- # 使用bc进行浮点计算
- usage=$(echo "scale=1; ($total - $idle) * 100 / $total" | bc 2>/dev/null)
-
- # 如果bc不可用,使用awk
- if [ -z "$usage" ]; then
- usage=$(awk -v total=$total -v idle=$idle 'BEGIN{printf "%.1f", (total-idle)*100/total}')
- fi
-
- cpu_usage="$usage"
- fi
- fi
- network_iface=$(ip route | grep default | awk '{print $5}' | head -1)
- network_speed="N/A"
- if [ -n "$network_iface" ] && [ -f "/sys/class/net/$network_iface/speed" ]; then
- speed=$(cat "/sys/class/net/$network_iface/speed" 2>/dev/null)
- [ -n "$speed" ] && [ "$speed" -gt 0 ] && network_speed="${speed}Mbps"
- fi
- disk_health="N/A"
- first_disk=$(lsblk -d -o NAME | grep -E '^sd|^nvme' | head -1)
- if [ -n "$first_disk" ]; then
- smartctl_output=$(sudo -n smartctl -H "/dev/$first_disk" 2>&1)
-
- if echo "$smartctl_output" | grep -qi "test result.*passed\|PASSED"; then
- disk_health="PASSED"
- elif echo "$smartctl_output" | grep -qi "test result.*failed\|FAILED"; then
- disk_health="FAILED"
- elif echo "$smartctl_output" | grep -qi "SMART support is: Available"; then
- disk_health="AVAILABLE"
- elif echo "$smartctl_output" | grep -qi "command not found\|Permission denied"; then
- disk_health="ERROR"
- else
- disk_health="UNKNOWN"
- fi
- fi
- service_status=""
- if systemctl is-active --quiet smbd 2>/dev/null; then
- service_status="${service_status}smb:✓ "
- else
- service_status="${service_status}smb:✗ "
- fi
- if systemctl is-active --quiet nmbd 2>/dev/null; then
- service_status="${service_status}nmb:✓ "
- else
- service_status="${service_status}nmb:✗ "
- fi
- if systemctl is-active --quiet ssh 2>/dev/null; then
- service_status="${service_status}ssh:✓ "
- else
- service_status="${service_status}ssh:✗ "
- fi
- if systemctl is-active --quiet docker 2>/dev/null; then
- service_status="${service_status}dkr:✓ "
- else
- service_status="${service_status}dkr:✗ "
- fi
- logged_users=$(who | wc -l)
- last_login=$(last -n 1 | head -1 | awk '{print $1" from "$3}')
- docker_containers="N/A"
- if command -v docker &> /dev/null; then
- if docker info >/dev/null 2>&1; then
- running_containers=$(docker ps -q 2>/dev/null | wc -l)
- total_containers=$(docker ps -aq 2>/dev/null | wc -l)
- docker_containers="${running_containers}/${total_containers}"
- else
- docker_containers="无权限"
- fi
- else
- docker_containers="未安装"
- fi
- total_processes=$(ps -ef | wc -l)
- top_process=$(ps -eo pid,comm,%cpu --sort=-%cpu | head -2 | tail -1 | awk '{print $2"("$3"%)"}')
- swap_info=$(free -m | awk '/Swap/{printf "%d/%dMB (%.0f%%)", $3, $2, ($3/$2)*100}')
- last_update="N/A"
- if [ -f "/var/lib/apt/periodic/update-success-stamp" ]; then
- last_update=$(stat -c %y /var/lib/apt/periodic/update-success-stamp 2>/dev/null | cut -d' ' -f1)
- elif [ -f "/var/lib/dpkg/status" ]; then
- last_update=$(stat -c %y /var/lib/dpkg/status 2>/dev/null | cut -d' ' -f1)
- fi
- raid_status="N/A"
- if [ -f "/proc/mdstat" ]; then
- active_raids=$(grep -c "^md" /proc/mdstat)
- if [ "$active_raids" -gt 0 ]; then
- raid_list=$(grep "^md" /proc/mdstat | awk '{print $1}')
- raid_details=""
- for raid in $raid_list; do
- raid_line=$(grep -A1 "^$raid" /proc/mdstat | tail -1)
- raid_type=$(echo "$raid_line" | awk '{print $4}')
- raid_state=$(echo "$raid_line" | grep -o '\[[^]]*\]' | head -1)
- if [ -n "$raid_type" ] && [ -n "$raid_state" ]; then
- [ -n "$raid_details" ] && raid_details="${raid_details}, "
- raid_details="${raid_details}${raid_type}${raid_state}"
- fi
- done
-
- if [ -n "$raid_details" ]; then
- raid_status="${active_raids} RAID: ${raid_details}"
- else
- raid_status="${active_raids} RAID设备"
- fi
- else
- raid_status="无RAID"
- fi
- fi
- log_errors="N/A"
- if command -v journalctl &> /dev/null; then
- log_errors=$(journalctl --since="1 hour ago" 2>/dev/null | grep -c -i "error\|fail")
- fi
- echo -e "\e[32m _ _ _____ _____ ______ ______ _ _\e[0m"
- echo -e "\e[32m | | | || __ \| __ \| ____|| ____|| \ | |\e[0m"
- echo -e "\e[32m | | | || | | | |__) | |__ | |__ | \| |\e[0m"
- echo -e "\e[32m | | | || | -- | _ /| __| | __| | . \` |\e[0m"
- echo -e "\e[32m | |_| || |__| | | \ \| |____ | |____ | |\ |\e[0m"
- echo -e "\e[32m \___/ |_____/|_| \_\______|\______||_| \_|\e[0m"
- echo -e "\e[34m --------------------------------------------\e[0m"
- echo -e "\e[35m UGREEN DXP4800 POWER BY UGOS PRO\e[0m"
- echo -e "\e[34m --------------------------------------------\e[0m"
- echo -e " \e[36m▶ 系统核心\e[0m"
- echo -e " 运行时间: \e[32m${uptime}\e[0m"
- echo -e " CPU型号: \e[32m${cpu_model}\e[0m"
- echo -e " CPU负载: \e[32m${load1} (1min) ${load5} (5min) ${load15} (15min)\e[0m"
- echo -e " CPU使用率:\e[32m${cpu_usage}%\e[0m 核心数: \e[32m${cpu_cores}\e[0m"
- echo -e " \e[36m▶ 内存与进程\e[0m"
- echo -e " 内存已用: \e[32m${memory}\e[0m"
- echo -e " 交换空间: \e[32m${swap_info}\e[0m"
- echo -e " 进程总数: \e[32m${total_processes}\e[0m 高负载进程: \e[31m${top_process}\e[0m"
- echo -e " \e[36m▶ 存储信息\e[0m"
- echo -e " 空间存储: \e[32m${storage_summary}\e[0m"
- echo -e " 磁盘健康: \e[32m${disk_health}\e[0m"
- echo -e " RAID状态: \e[32m${raid_status}\e[0m"
- echo -e " \e[36m▶ 网络信息\e[0m"
- echo -e " 局域网IP: \e[32m${local_ip}\e[0m"
- echo -e " 网络接口: \e[32m${network_iface}\e[0m 速率: \e[32m${network_speed}\e[0m"
- echo -e " 公网IPv6: \e[32m${ipv6_address}\e[0m"
- echo -e " \e[36m▶ 服务状态\e[0m"
- echo -e " 服务状态: \e[32m${service_status}\e[0m"
- echo -e " Docker容器:\e[32m${docker_containers} (运行/总数)\e[0m"
- echo -e " 登录用户: \e[32m${logged_users}\e[0m 最近登录: \e[32m${last_login}\e[0m"
- echo -e " \e[36m▶ 系统监控\e[0m"
- echo -e " 系统温度: \e[32m${temp}\e[0m"
- echo -e " 最近更新: \e[32m${last_update}\e[0m"
- echo -e " 最近错误: \e[31m${log_errors} (1小时内)\e[0m"
- echo -e "\e[34m --------------------------------------------\e[0m"
复制代码
```
|