SysAdmin
This page has useful snippets for sysadmin tasks, shell scripting, and other related things used frequently(and unfrequently) for things like managing servers, firewalls, networking, monitoring and so on.
CPU Usage w/ no external tools
This snippet(s) gets the CPU usage of the system using /proc/stat over the
course of 0.5 seconds. this can be changed to 1 second for a more accurate
reading.
prev_idle=$(awk '/^cpu /{print $5}' /proc/stat)prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
sleep 0.5
idle=$(awk '/^cpu /{print $5}' /proc/stat)total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
diff_idle=$((idle - prev_idle))diff_total=$((total - prev_total))
usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)echo "$usage"Same command but in a single line:
prev_idle=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - prev_idle)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "$usage"Every Second
while truedo    prev_idle=$(awk '/^cpu /{print $5}' /proc/stat)    prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
    sleep 0.5
    idle=$(awk '/^cpu /{print $5}' /proc/stat)    total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
    diff_idle=$((idle - prev_idle))    diff_total=$((total - prev_total))
    usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)    echo "$usage%"doneOne liner for the above:
while true; do prev_idle=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - prev_idle)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "$usage%"; doneAlternative command to above
Does the same thing but differently
_STAT_PREV_IDLE=$(awk '/^cpu /{print $5}' /proc/stat)prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)while truedo    sleep 0.5    idle=$(awk '/^cpu /{print $5}' /proc/stat)    total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)    diff_idle=$((idle - prev_idle))    diff_total=$((total - prev_total))    usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)    echo "CPU Usage: $usage%"    prev_idle=$idle    prev_total=$totaldoneOne liner for the above:
_STAT_PREV_IDLE=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); while true; do sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - _STAT_PREV_IDLE)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "CPU Usage: $usage%"; _STAT_PREV_IDLE=$idle; prev_total=$total; doneMemory Usage w/ no external tools
while truedo    awk '/^MemTotal:/{total=$2}/^MemAvailable:/{avail=$2} END{used=total-avail; usage=(used/total)*100; used_gb=used/1024/1024; printf("RAM Usage: %.2f%%  %.2fGB\n", usage, used_gb)}' /proc/meminfo    sleep 0.5doneOne-liner for the above:
while true; do awk '/^MemTotal:/{total=$2}/^MemAvailable:/{avail=$2} END{used=total-avail; usage=(used/total)*100; used_gb=used/1024/1024; printf("RAM Usage: %.2f%%  %.2fGB\n", usage, used_gb)}' /proc/meminfo; sleep 0.5; done