Skip to content

Instantly share code, notes, and snippets.

@max1220
Created March 12, 2025 20:08
Show Gist options
  • Save max1220/b9251ad7f221a12d8f233318a45d597a to your computer and use it in GitHub Desktop.
Save max1220/b9251ad7f221a12d8f233318a45d597a to your computer and use it in GitHub Desktop.
Bash performance monitor script. Outputs CSV data ready for gnuplot!
#!/bin/bash
set -euo pipefail
# change to log output directory
#cd /var/log/sys_stats
cd sys_stats
# wrap stdin with timestamp and newline
function wrap() { echo -n "${EPOCHREALTIME},"; cat; echo; }
# write CSV files:
# log CPU usage
grep -F "cpu" /proc/stat | tr -s " " | cut -d " " -f 2- | tr " " "," | tr "\n" "," | wrap >> stat.log
# log CPU frequency
grep -F "MHz" /proc/cpuinfo | cut -d ":" -f2 | tr -d " " | tr "\n" "," | wrap >> freq.log
# log memory usage
grep -F "Mem" /proc/meminfo | cut -d ":" -f2 | tr -d " kB" | tr "\n" "," | wrap >> mem.log
# log swap usage
tail -n +2 /proc/swaps | tr "\t" " " | tr -s " " | cut -d " " -f 1,3,4 | tr " " "," | tr "\n" "," | wrap >> swap.log
# log disk statistics
grep -vF "loop" /proc/diskstats | tr -s " " | cut -d " " -f 4-15 | tr " " "," | tr "\n" "," | wrap >> disk_stats.log
# log disk space usage
df -l | grep -F "/dev/" | grep -vF "tmpfs" | tr -s " " | cut -d " " -f 1-4 | tr " " "," | tr "\n" "," | wrap >> disk_usage.log
# log network interface statistics
ip -s -j l show | jq -jc "[map(.) | .[] | [.ifname, .stats64.rx.bytes, .stats64.rx.packets, .stats64.rx.errors, .stats64.rx.dropped, .stats64.rx.over_errors, .stats64.rx.multicast, .stats64.tx.bytes, .stats64.tx.packets, .stats64.tx.errors, .stats64.tx.dropped, .stats64.tx.carrier_errors, .stats64.tx.collisions ]] | flatten | @csv" | wrap >> net.log
# log ping to localhost
ping -w1 -q -n -c5 -i 0.1 127.0.0.1 | tail -n2 | cut -d "=" -f2 | tr "/" " " | tr "\n" " " | cut -d " " -f 1,4,12-15 | tr " " "," | tr -d "\n" | wrap >> ping_localhost.log
# log ping to 8.8.8.8
ping -w1 -q -n -c5 -i 0.1 8.8.8.8 | tail -n2 | cut -d "=" -f2 | tr "/" " " | tr "\n" " " | cut -d " " -f 1,4,12-15 | tr " " "," | tr -d "\n" | wrap >> ping_googledns.log
# log ping to 10.42.42.4
ping -w1 -q -n -c5 -i 0.1 10.42.42.4 | tail -n2 | cut -d "=" -f2 | tr "/" " " | tr "\n" " " | cut -d " " -f 1,4,12-15 | tr " " "," | tr -d "\n" | wrap >> ping_wysewg.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment