CH-J Toolsquản lý máy chủ qua SSH
Menu
Mã nguồn đã công bố

CH-J Tools

Duyệt thư mục và tệp của một bản phát hành ứng dụng cụ thể.

Tải mã nguồn dạng ZIP
CH-J / Server Manager 872 tệp
core-20261030T220507Z-61e4558d889f Ngày thêm: · Ngày dựng:
linux_metrics.sh Ngày thêm: Chưa ghi nhận ngày 13,1 KB · 199 dòng
Mã Tải tệp xuống
1LC_ALL=C
2platform=$(uname -s 2>/dev/null || echo unknown)
3kernel=$(uname -r 2>/dev/null || echo unknown)
4host=$(hostname 2>/dev/null || echo unknown)
5cpu_arch=$(uname -m 2>/dev/null || echo unknown)
6cpu_usage=0
7stat_second=
8cpu_logical=0
9cpu_physical=0
10cpu_sockets=0
11cpu_model=unknown
12cpu_core_types=
13network_default=
15if [ -r /proc/uptime ]; then
16 uptime_s=$(awk '{print int($1)}' /proc/uptime)
17else
18 boot_epoch=$(sysctl -n kern.boottime 2>/dev/null | awk -F'[=,]' '{gsub(/[^0-9]/,"",$2); print $2}')
19 now_epoch=$(date +%s 2>/dev/null || echo 0)
20 if [ -n "$boot_epoch" ] && [ "$now_epoch" -ge "$boot_epoch" ] 2>/dev/null; then uptime_s=$((now_epoch-boot_epoch)); else uptime_s=0; fi
21fi
22if [ -r /proc/loadavg ]; then load1=$(awk '{print $1}' /proc/loadavg); else load1=$(uptime | sed -E 's/.*load averages?:[[:space:]]*([0-9.]+).*/\1/'); fi
24if [ -r /proc/meminfo ]; then
25 mem_total=$(awk '/^MemTotal:/{print $2*1024}' /proc/meminfo)
26 mem_avail=$(awk '/^MemAvailable:/{print $2*1024}' /proc/meminfo)
27 swap_total=$(awk '/^SwapTotal:/{print $2*1024}' /proc/meminfo)
28 swap_free=$(awk '/^SwapFree:/{print $2*1024}' /proc/meminfo)
29 cpu_first=$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=9 && i<=NF;i++)total+=$i; printf "%.0f:%.0f",idle,total; exit}' /proc/stat)
30 stat_first=$(grep '^cpu[0-9]' /proc/stat)
31 sleep 0.2
32 stat_second=$(grep '^cpu[0-9]' /proc/stat)
33 cpu_second=$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=9 && i<=NF;i++)total+=$i; printf "%.0f:%.0f",idle,total; exit}' /proc/stat)
34 cpu_usage=$(awk -v first="$cpu_first" -v second="$cpu_second" 'BEGIN{split(first,a,":");split(second,b,":");total=b[2]-a[2];idle=b[1]-a[1];if(total>0)printf "%.2f",100*(total-idle)/total;else print 0}')
35 cpu_logical=$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc 2>/dev/null || awk '/^processor[[:space:]]*:/{n++}END{print n+0}' /proc/cpuinfo)
36 cpu_model=$(awk -F: '/^(model name|Processor|Hardware)[[:space:]]*:/{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2);if($2!=""){print $2;exit}}' /proc/cpuinfo)
37 if command -v lscpu >/dev/null 2>&1; then
38 cpu_physical=$(lscpu -p=Core,Socket 2>/dev/null | awk -F, '!/^#/&&$1~/^[0-9]+$/{seen[$1":"$2]=1}END{for(k in seen)n++;print n+0}')
39 cpu_sockets=$(lscpu -p=Socket 2>/dev/null | awk -F, '!/^#/&&$1~/^[0-9]+$/{seen[$1]=1}END{for(k in seen)n++;print n+0}')
40 fi
41 [ "$cpu_physical" -gt 0 ] 2>/dev/null || cpu_physical=$cpu_logical
42 [ "$cpu_sockets" -gt 0 ] 2>/dev/null || cpu_sockets=1
43 if ls /sys/devices/system/cpu/cpu*/topology/core_type >/dev/null 2>&1; then
44 core_type_records=$(for type_file in /sys/devices/system/cpu/cpu*/topology/core_type; do topology=$(dirname "$type_file"); type=$(cat "$type_file" 2>/dev/null); core=$(cat "$topology/core_id" 2>/dev/null); package=$(cat "$topology/physical_package_id" 2>/dev/null); printf '%s:%s:%s\n' "$type" "$package" "$core"; done | sort -u)
45 efficiency_cores=$(printf '%s\n' "$core_type_records" | awk -F: '$1==1{n++}END{print n+0}')
46 performance_cores=$(printf '%s\n' "$core_type_records" | awk -F: '$1==2{n++}END{print n+0}')
47 [ "$performance_cores" -gt 0 ] && cpu_core_types="P-core:$performance_cores"
48 if [ "$efficiency_cores" -gt 0 ]; then [ -n "$cpu_core_types" ] && separator=, || separator=; cpu_core_types=$(printf '%s%sE-core:%s' "$cpu_core_types" "$separator" "$efficiency_cores"); fi
49 fi
50 if command -v ip >/dev/null 2>&1; then network_default=$(ip route show default 2>/dev/null | awk '{for(i=1;i<=NF;i++)if($i=="dev"){print $(i+1);exit}}'); fi
51else
52 mem_total=$(sysctl -n hw.memsize 2>/dev/null || echo 0)
53 page_size=$(vm_stat 2>/dev/null | awk '/page size of/{print $8}')
54 [ -n "$page_size" ] || page_size=4096
55 mem_avail=$(vm_stat 2>/dev/null | awk -v page="$page_size" '/Pages (free|inactive|speculative):/{gsub(/\./,"",$3);pages+=$3}END{printf "%.0f",pages*page}')
56 swap_total=0
57 swap_free=0
58 cpu_logical=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 0)
59 cpu_physical=$(sysctl -n hw.physicalcpu 2>/dev/null || echo "$cpu_logical")
60 cpu_sockets=1
61 cpu_model=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || sysctl -n hw.model 2>/dev/null || echo unknown)
62 cpu_usage=$(top -l 1 -n 0 2>/dev/null | awk '/CPU usage:/{idle=$7;gsub(/%/,"",idle)}END{if(idle!="")printf "%.2f",100-idle;else print 0}')
63 for level in 0 1 2; do
64 count=$(sysctl -n "hw.perflevel$level.physicalcpu" 2>/dev/null || echo 0)
65 if [ "$count" -gt 0 ] 2>/dev/null; then
66 name=$(sysctl -n "hw.perflevel$level.name" 2>/dev/null || echo "Cluster $level")
67 [ -n "$cpu_core_types" ] && separator=, || separator=
68 cpu_core_types=$(printf '%s%s%s:%s' "$cpu_core_types" "$separator" "$name" "$count")
69 fi
70 done
71 network_default=$(route -n get default 2>/dev/null | awk '/interface:/{print $2;exit}')
72fi
74[ -n "$cpu_model" ] || cpu_model=unknown
75cpu_model=$(printf '%s' "$cpu_model" | tr '\r\n|\036' ' ')
76cpu_core_types=$(printf '%s' "$cpu_core_types" | tr '\r\n|\036' ' ')
77disk=$(df -Pk / 2>/dev/null | awk 'NR==2{print $2*1024":"$3*1024":"$4*1024}')
78printf 'platform=%s\nkernel=%s\nhostname=%s\nuptimeSeconds=%s\nload1=%s\nmemoryTotal=%s\nmemoryAvailable=%s\nswapTotal=%s\nswapFree=%s\ndisk=%s\ncpuUsage=%s\ncpuArchitecture=%s\ncpuModel=%s\ncpuLogical=%s\ncpuPhysical=%s\ncpuSockets=%s\ncpuCoreTypes=%s\nnetworkDefault=%s\n' "$platform" "$kernel" "$host" "$uptime_s" "$load1" "$mem_total" "$mem_avail" "$swap_total" "$swap_free" "$disk" "$cpu_usage" "$cpu_arch" "$cpu_model" "$cpu_logical" "$cpu_physical" "$cpu_sockets" "$cpu_core_types" "$network_default"
80# Detailed memory and swap for the "Memory and virtual memory" section (kB fields converted to bytes).
81if [ -r /proc/meminfo ]; then
82 awk '$1 ~ /^(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapCached|Active|Inactive|Slab|SReclaimable|SUnreclaim|Shmem|KernelStack|PageTables|VmallocUsed|Percpu|Dirty|Writeback|SwapTotal|SwapFree|Zswap|Zswapped|CommitLimit|Committed_AS):$/ { key=$1; sub(/:$/, "", key); value=($3=="kB") ? $2*1024 : $2; printf "meminfo=%s|%.0f\n", key, value }' /proc/meminfo
83fi
84if [ -r /proc/swaps ]; then
85 awk 'NR>1 { printf "swapArea=%s|%s|%.0f|%.0f|%s\n", $1, $2, $3*1024, $4*1024, $5 }' /proc/swaps
86fi
87if [ -r /proc/vmstat ]; then
88 awk '$1=="pswpin" || $1=="pswpout" || $1=="pgmajfault" { printf "vmstat=%s|%s\n", $1, $2 }' /proc/vmstat
89fi
90for zram in /sys/block/zram*; do
91 [ -r "$zram/mm_stat" ] || continue
92 printf 'zram=%s|%s|%s|%s\n' "$(basename "$zram")" "$(cat "$zram/disksize" 2>/dev/null)" "$(cat "$zram/mm_stat" 2>/dev/null | tr -s ' ' ',')" "$(cat "$zram/comp_algorithm" 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p')"
93done
94if [ -r /sys/module/zswap/parameters/enabled ]; then
95 printf 'zswap=%s|%s|%s\n' "$(cat /sys/module/zswap/parameters/enabled)" "$(cat /sys/module/zswap/parameters/compressor 2>/dev/null)" "$(cat /sys/module/zswap/parameters/max_pool_percent 2>/dev/null)"
96fi
97printf 'pageSize=%s\n' "$(getconf PAGESIZE 2>/dev/null || echo 4096)"
99if [ -d /sys/class/net ]; then
100 for iface_path in /sys/class/net/*; do
101 [ -e "$iface_path" ] || continue
102 iface=$(basename "$iface_path")
103 [ "$iface" = lo ] && continue
104 state=$(cat "$iface_path/operstate" 2>/dev/null || echo unknown)
105 mac=$(cat "$iface_path/address" 2>/dev/null || true)
106 rx=$(cat "$iface_path/statistics/rx_bytes" 2>/dev/null || echo 0)
107 tx=$(cat "$iface_path/statistics/tx_bytes" 2>/dev/null || echo 0)
108 if command -v ip >/dev/null 2>&1; then addresses=$(ip -o addr show dev "$iface" scope global 2>/dev/null | awk '{print $4}' | paste -sd, -); else addresses=; fi
109 printf 'networkInterface=%s|%s|%s|%s|%s|%s\n' "$iface" "$state" "$mac" "$rx" "$tx" "$addresses"
110 done
111elif command -v ifconfig >/dev/null 2>&1; then
112 for iface in $(ifconfig -l 2>/dev/null); do
113 [ "$iface" = lo0 ] && continue
114 state=$(ifconfig "$iface" 2>/dev/null | awk '/status:/{print $2;exit}')
115 mac=$(ifconfig "$iface" 2>/dev/null | awk '/ether /{print $2;exit}')
116 addresses=$(ifconfig "$iface" 2>/dev/null | awk '/^[[:space:]]+inet /{print $2}/^[[:space:]]+inet6 /{print $2}' | paste -sd, -)
117 counters=$(netstat -ibn -I "$iface" 2>/dev/null | awk 'NR==2{print $7":"$10;exit}')
118 rx=$(printf '%s' "$counters" | cut -d: -f1); tx=$(printf '%s' "$counters" | cut -d: -f2)
119 [ -n "$state" ] || state=unknown; [ -n "$rx" ] || rx=0; [ -n "$tx" ] || tx=0
120 printf 'networkInterface=%s|%s|%s|%s|%s|%s\n' "$iface" "$state" "$mac" "$rx" "$tx" "$addresses"
121 done
122fi
124# Every CPU package: model, cores, usage from the two /proc/stat samples, frequency and temperature.
125if [ -n "$stat_second" ]; then
126 {
127 printf '%s\n' "$stat_first" | sed 's/^/A /'
128 printf '%s\n' "$stat_second" | sed 's/^/B /'
129 awk 'FNR==1{ n=FILENAME; kind=(n ~ /core_id$/) ? "C" : "P"; sub(/\/topology.*/,"",n); sub(/.*\/cpu/,"",n); print kind, n, $1 }' /sys/devices/system/cpu/cpu[0-9]*/topology/physical_package_id /sys/devices/system/cpu/cpu[0-9]*/topology/core_id 2>/dev/null
130 for hw in /sys/class/hwmon/hwmon*; do
131 [ "$(cat "$hw/name" 2>/dev/null)" = coretemp ] || continue
132 for label in "$hw"/temp*_label; do
133 case "$(cat "$label" 2>/dev/null)" in "Package id "*) printf 'H %s %s\n' "$(sed 's/[^0-9]//g' "$label")" "$(cat "${label%_label}_input" 2>/dev/null)";; esac
134 done
135 done
136 for hw in /sys/class/hwmon/hwmon*; do
137 case "$(cat "$hw/name" 2>/dev/null)" in k10temp|zenpower) printf '%s %s\n' "$(readlink -f "$hw/device" 2>/dev/null)" "$(cat "$hw/temp1_input" 2>/dev/null)";; esac
138 done | sort | awk '{ print "H", NR-1, $2 }'
139 } | awk '
140 FILENAME == "/proc/cpuinfo" {
141 i = index($0, ":"); if (!i) next
142 k = $0; sub(/[ \t]*:.*/, "", k); v = substr($0, i + 1); sub(/^[ \t]+/, "", v)
143 if (k == "processor") cur = v; else if (k == "model name" || k == "Processor") model[cur] = v; else if (k == "cpu MHz") mhz[cur] = v
144 next
145 }
146 $1 == "A" || $1 == "B" {
147 id = $2; sub(/^cpu/, "", id); idle = $6 + $7; tot = 0
148 for (i = 3; i <= 10 && i <= NF; i++) tot += $i
149 if ($1 == "A") { ia[id] = idle; ta[id] = tot } else { ib[id] = idle; tb[id] = tot; seen[id] = 1 }
150 next
151 }
152 $1 == "P" { pkg[$2] = $3; next }
153 $1 == "C" { core[$2] = $3; next }
154 $1 == "H" { temp[$2] = $3; next }
155 END {
156 for (id in seen) {
157 p = (id in pkg) ? pkg[id] : 0; logical[p]++
158 if (!((p, core[id]) in cores)) { cores[p, core[id]] = 1; physical[p]++ }
159 dt[p] += tb[id] - ta[id]; di[p] += ib[id] - ia[id]
160 if (model[id] != "") name[p] = model[id]
161 if (mhz[id] != "") { ms[p] += mhz[id]; mc[p]++ }
162 }
163 for (p in logical) {
164 n = name[p]; gsub(/\|/, " ", n)
165 printf "cpuPackage=%s|%s|%d|%d|%s|%s|%s\n", p, n, logical[p], physical[p], (dt[p] > 0 ? sprintf("%.2f", 100 * (dt[p] - di[p]) / dt[p]) : ""), (mc[p] ? sprintf("%.0f", ms[p] / mc[p]) : ""), ((p in temp) ? temp[p] : "")
166 }
167 }' - /proc/cpuinfo 2>/dev/null
168fi
170# Every display or accelerator device on the PCI bus, whatever its driver, plus SoC GPUs.
171# sysfs needs no root and no extra tools; nvidia-smi, if installed, adds what only the NVIDIA driver knows.
172gpu_first() { for gpu_file in "$@"; do if [ -r "$gpu_file" ]; then cat "$gpu_file" 2>/dev/null; return; fi; done; }
173if [ -d /sys/bus/pci/devices ]; then
174 for gpu_dev in $(awk '/^0x(03|12)/{ n=FILENAME; sub(/\/class$/,"",n); print n }' /sys/bus/pci/devices/*/class 2>/dev/null); do
175 gpu_id=$(basename "$gpu_dev")
176 gpu_driver=$(basename "$(readlink "$gpu_dev/driver" 2>/dev/null)" 2>/dev/null)
177 gpu_name=
178 if command -v lspci >/dev/null 2>&1; then gpu_name=$(lspci -vmm -s "$gpu_id" 2>/dev/null | awk -F'\t' '/^Vendor:/{v=$2}/^Device:/{d=$2}END{print v" "d}'); fi
179 gpu_clock=$(gpu_first "$gpu_dev"/hwmon/hwmon*/freq1_input)
180 if [ -z "$gpu_clock" ]; then
181 gpu_mhz=$(gpu_first "$gpu_dev"/drm/card[0-9]*/gt_cur_freq_mhz "$gpu_dev"/tile0/gt0/freq0/act_freq)
182 [ -n "$gpu_mhz" ] && gpu_clock=$((gpu_mhz * 1000000))
183 fi
184 printf 'gpu=%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n' "$gpu_id" "$(gpu_first "$gpu_dev/vendor")" "$(gpu_first "$gpu_dev/device")" "$gpu_driver" "$gpu_name" \
185 "$(gpu_first "$gpu_dev/gpu_busy_percent")" "$(gpu_first "$gpu_dev/mem_busy_percent")" "$(gpu_first "$gpu_dev/mem_info_vram_total")" "$(gpu_first "$gpu_dev/mem_info_vram_used")" \
186 "$(gpu_first "$gpu_dev"/hwmon/hwmon*/temp1_input)" "$(gpu_first "$gpu_dev"/hwmon/hwmon*/power1_average "$gpu_dev"/hwmon/hwmon*/power1_input)" "$(gpu_first "$gpu_dev"/hwmon/hwmon*/power1_cap)" "$gpu_clock" "$(gpu_first "$gpu_dev"/hwmon/hwmon*/pwm1)"
187 done
188fi
189for gpu_card in /sys/class/drm/card[0-9]*; do
190 case "$gpu_card" in *-*) continue;; esac
191 [ -e "$gpu_card/device" ] || continue
192 case "$(readlink -f "$gpu_card/device" 2>/dev/null)" in */pci*) continue;; esac
193 gpu_driver=$(basename "$(readlink "$gpu_card/device/driver" 2>/dev/null)" 2>/dev/null)
194 printf 'gpu=%s|||%s|%s|||||%s|||%s|\n' "$(basename "$gpu_card")" "$gpu_driver" "$gpu_driver" "$(gpu_first "$gpu_card"/device/hwmon/hwmon*/temp1_input)" "$(gpu_first "$gpu_card/gt_cur_freq_mhz")"
195done
196if command -v nvidia-smi >/dev/null 2>&1; then
197 if command -v timeout >/dev/null 2>&1; then gpu_limit="timeout 4"; else gpu_limit=; fi
198 $gpu_limit nvidia-smi --query-gpu=pci.bus_id,name,utilization.gpu,utilization.memory,memory.total,memory.used,temperature.gpu,power.draw,power.limit,clocks.current.graphics,fan.speed,driver_version --format=csv,noheader,nounits 2>/dev/null | tr -d '\r' | sed 's/, */|/g; s/^/nvidiaGpu=/'
199fi
SHA-256ac47175a7c2d32dabcb1f7538f0b1741a7c401b8363e91fc7678b467c5d866bf

CH-J Proprietary Software License 1.14 · Mã nguồn được cung cấp theo CH-J Proprietary Software License 1.14. Việc công khai mã nguồn không thay đổi các điều khoản giấy phép và không cấp thêm quyền nào.

SHA-256 của tệp nén77380d4805485ffa4027fbe7858b5836f197a4aa9d52358a43e2d12d018744df
Tệp