Skip to content

Instantly share code, notes, and snippets.

@ostinelli
Last active November 19, 2017 01:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ostinelli/449632408c5f48a3331a to your computer and use it in GitHub Desktop.
Save ostinelli/449632408c5f48a3331a to your computer and use it in GitHub Desktop.
Monitor server CPU and memory.
#!/usr/bin/env bash
# settings
OUTPUT_FILE=monitor.csv
# build command
cmd=""
cpu_headers=""
for (( i=4; i<=$(nproc)+4; i++ ))
do
cmd="${cmd}NR==$i{printf \"%.1f,\", 100-\$13}"
done
cmd="echo -n \`date +%s\`, && mpstat -P ALL 1 1 | awk '${cmd}' && uptime | awk 'NR==1{printf \"%.2f,\", \$8}' && free | awk 'NR==2{printf \"%s,%s\n\", \$3, \$2}'"
# print headers
cpu_headers=""
for (( i=1; i<=$(nproc); i++ ))
do
cpu_headers="${cpu_headers}cpu_$i,"
done
echo "epoch,cpu_all,${cpu_headers}load,mem_used,mem_total"> $OUTPUT_FILE
# loop data
while true; do (eval $cmd) >> $OUTPUT_FILE; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment