From 7a6328f97f492e7011e5c2668e07722defc6fe08 Mon Sep 17 00:00:00 2001 From: sky Date: Sun, 8 Dec 2024 19:08:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20simpleadmin/www/cgi-bin/ge?= =?UTF-8?q?t=5Fsystem=5Fstats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add get_system_stats --- simpleadmin/www/cgi-bin/get_system_stats | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 simpleadmin/www/cgi-bin/get_system_stats diff --git a/simpleadmin/www/cgi-bin/get_system_stats b/simpleadmin/www/cgi-bin/get_system_stats new file mode 100644 index 0000000..4049e9f --- /dev/null +++ b/simpleadmin/www/cgi-bin/get_system_stats @@ -0,0 +1,41 @@ +#!/bin/sh + +# Send HTTP header for plain text content +echo "Content-Type: text/plain" +echo "" + +# Read first CPU statistics +read cpu user nice system idle iowait irq softirq steal guest guest_nice < /proc/stat + +# Record first total usage time and idle time +prev_total=$((user+nice+system+idle+iowait+irq+softirq+steal)) +prev_idle=$idle + +# Wait for a short time to capture CPU usage +sleep 0.5 + +# Read second CPU statistics +read cpu user nice system idle iowait irq softirq steal guest guest_nice < /proc/stat + +# Calculate second total usage time +total=$((user+nice+system+idle+iowait+irq+softirq+steal)) +idle=$idle + +# Calculate time differences +total_diff=$((total-prev_total)) +idle_diff=$((idle-prev_idle)) + +# Calculate CPU usage percentage +cpu_usage=$(awk -v total_diff="$total_diff" -v idle_diff="$idle_diff" 'BEGIN{printf "%.1f", (total_diff-idle_diff)*100/total_diff}') + +# Get memory information +mem_info=$(free -m | grep "Mem:") +mem_total=$(echo "$mem_info" | awk '{print $2}') +mem_used=$(echo "$mem_info" | awk '{print $3}') +mem_usage=$(awk "BEGIN {printf \"%.1f\", ($mem_used * 100) / $mem_total}") + +# Output results +echo "CPU Usage: $cpu_usage" +echo "Memory Usage: $mem_usage" +echo "Memory Total: $((mem_total * 1024 * 1024))" +echo "Memory Used: $((mem_used * 1024 * 1024))" \ No newline at end of file