Last active
November 6, 2025 08:42
-
-
Save devicezero/5e3408b7cd78f735b8a6ca3ff1393abc to your computer and use it in GitHub Desktop.
advanced system info (cpu, memory, storage)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| pagesize=$(getconf PAGESIZE 2>/dev/null || getconf PAGE_SIZE 2>/dev/null) | |
| [ -z "$pagesize" ] && pagesize=4096 | |
| echo "{" | |
| ### SYSTEM INFO ### | |
| echo "\"system\": {" | |
| hostname=$(hostname 2>/dev/null || cat /etc/hostname 2>/dev/null || echo "unknown") | |
| kernel=$(uname -r) | |
| arch=$(uname -m) | |
| if [ -f /etc/os-release ]; then | |
| os_name=$(grep '^PRETTY_NAME=' /etc/os-release | cut -d= -f2 | tr -d '"') | |
| else | |
| os_name=$(uname -s) | |
| fi | |
| printf "\"hostname\": \"%s\",\n" "$hostname" | |
| printf "\"kernel\": \"%s\",\n" "$kernel" | |
| printf "\"architecture\": \"%s\",\n" "$arch" | |
| printf "\"os\": \"%s\"\n" "$os_name" | |
| echo "}," | |
| ### CPU ### | |
| echo "\"cpu\": {" | |
| cpu_name=$(grep -m1 -E 'Model|Hardware|model name|Processor' /proc/cpuinfo | awk -F': ' '{print $2}') | |
| [ -z "$cpu_name" ] && cpu_name="unknown" | |
| cpu_cores=$(grep -c '^processor' /proc/cpuinfo 2>/dev/null); [ -z "$cpu_cores" ] && cpu_cores=1 | |
| read_idle_total() { | |
| awk '/^cpu /{ idle=$5+$6; total=0; for(i=2;i<=NF;i++) total+=$i; print idle, total; exit }' /proc/stat | |
| } | |
| set -- $(read_idle_total); idle1=$1; tot1=$2 | |
| sleep 1 | |
| set -- $(read_idle_total); idle2=$1; tot2=$2 | |
| cpu_usage=$(awk -v i1="$idle1" -v t1="$tot1" -v i2="$idle2" -v t2="$tot2" \ | |
| 'BEGIN{ dt=t2-t1; di=i2-i1; if (dt<=0) {printf "0.00"; exit} printf "%.2f", 100.0*(dt-di)/dt }') | |
| set -- $(awk '{print $1, $2, $3}' /proc/loadavg); l1=$1; l5=$2; l15=$3 | |
| printf "\"name\": \"%s\",\n" "$cpu_name" | |
| printf "\"cores\": %s,\n" "$cpu_cores" | |
| printf "\"usage_percent\": %s,\n" "$cpu_usage" | |
| printf "\"load_avg\": {\"1min\": %s, \"5min\": %s, \"15min\": %s}\n" "$l1" "$l5" "$l15" | |
| echo "}," | |
| ### MEMORY ### | |
| echo "\"memory\": {" | |
| getk() { awk -v k="$1" '($1==k":"){print $2; found=1} END{if(!found) print 0}' /proc/meminfo; } | |
| MemTotal=$(getk MemTotal) | |
| MemFree=$(getk MemFree) | |
| MemAvail=$(getk MemAvailable) | |
| Buffers=$(getk Buffers) | |
| Cached=$(getk Cached) | |
| SReclaimable=$(getk SReclaimable) | |
| Shmem=$(getk Shmem) | |
| SwapTotal=$(getk SwapTotal) | |
| SwapFree=$(getk SwapFree) | |
| cache_kb=$((Cached + SReclaimable - Shmem)); [ "$cache_kb" -lt 0 ] && cache_kb=0 | |
| used_excl_cache_kb=$((MemTotal - MemFree - Buffers - cache_kb)); [ "$used_excl_cache_kb" -lt 0 ] && used_excl_cache_kb=0 | |
| swap_used_kb=$((SwapTotal - SwapFree)) | |
| printf "\"total_kb\": %s,\n" "$MemTotal" | |
| printf "\"available_kb\": %s,\n" "$MemAvail" | |
| printf "\"free_kb\": %s,\n" "$MemFree" | |
| printf "\"buffers_kb\": %s,\n" "$Buffers" | |
| printf "\"cache_kb\": %s,\n" "$cache_kb" | |
| printf "\"used_excl_cache_kb\": %s,\n" "$used_excl_cache_kb" | |
| printf "\"swap_total_kb\": %s,\n" "$SwapTotal" | |
| printf "\"swap_used_kb\": %s\n" "$swap_used_kb" | |
| echo "}," | |
| ### STORAGE ### | |
| echo "\"storage\": [" | |
| df -B1 -P | awk 'NR==1{next} { | |
| if (n++) printf(",\n"); | |
| printf("{\"filesystem\":\"%s\",\"size_bytes\":%s,\"used_bytes\":%s,\"avail_bytes\":%s,\"mount\":\"%s\"}", | |
| $1,$2,$3,$4,$6) | |
| }' | |
| echo "]," | |
| ### TOP PROCESSES BY RSS ### | |
| tmpfile=$(mktemp) | |
| for d in /proc/[0-9]*; do | |
| pid=${d#/proc/} | |
| [ -r "$d/statm" ] || continue | |
| rss_pages=$(awk '{print $2+0}' "$d/statm" 2>/dev/null) | |
| [ -z "$rss_pages" ] && continue | |
| rss_kb=$((rss_pages * pagesize / 1024)) | |
| [ "$rss_kb" -le 0 ] && continue | |
| if [ -r "$d/cmdline" ]; then | |
| cmd=$(tr '\0' ' ' < "$d/cmdline") | |
| fi | |
| [ -z "$cmd" ] && [ -r "$d/comm" ] && cmd=$(cat "$d/comm") | |
| [ -z "$cmd" ] && cmd="unknown" | |
| cmd_esc=$(printf '%s' "$cmd" | sed 's/\\/\\\\/g; s/"/\\"/g') | |
| printf '%s\t%s\t%s\n' "$rss_kb" "$pid" "$cmd_esc" >> "$tmpfile" | |
| done | |
| echo "\"top_processes_by_rss\": [" | |
| count=0; max=15 | |
| while IFS="$(printf '\t')" read -r rss_kb pid cmd_esc; do | |
| [ -z "$rss_kb" ] && continue | |
| if [ $count -gt 0 ]; then printf ",\n"; fi | |
| printf "{\"pid\": %s, \"rss_kb\": %s, \"cmd\": \"%s\"}" "$pid" "$rss_kb" "$cmd_esc" | |
| count=$((count+1)) | |
| [ $count -ge $max ] && break | |
| done <<EOF | |
| $(sort -nr -k1,1 "$tmpfile") | |
| EOF | |
| echo "]" | |
| rm -f "$tmpfile" | |
| echo "}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment