This is an old revision of the document!
A simple bash script to log the datetime and system load to a file every 30 seconds.
It saves data in the following format: Server ID, Date/Time, CPU usage (%), Memory Total (Mb), Memory Used (Mb), Memory Free (Mb), Memory Shared (Mb), Memory Buffered/Cached (Mb), Memory Available (Mb), 1 minute load average, 5 minute load average, 15 minute load average, process count
#!/bin/bash ID='SERVER1' while [ true ] do printf -v date '%(%Y-%m-%d %H:%M:%S)T' -1 CPU_USAGE=`ps -heo pcpu | paste "-sd+" | bc` MEM_USAGE=`free -m | grep 'Mem:' | awk -v OFS="," '{print ($2,$3,$4,$5,$6,$7)}'` LOAD_AVG=`awk -v OFS="," '{split($4,arr,"/")} {print $1,$2,$3,arr[2]}' /proc/loadavg` echo -e $ID','$date','$CPU_USAGE','$MEM_USAGE','$LOAD_AVG sleep 30; done
Example:
SERVER1,2020-04-13 22:59:59,3.86818,15929,2082,9545,273,4301,13258,0.27,0.31,0.24,703 SERVER1,2020-04-13 23:06:04,3.87079,15929,2127,9497,276,4304,13211,0.22,0.25,0.22,708 SERVER1,2020-04-13 23:06:22,3.87107,15929,2127,9494,278,4307,13208,0.36,0.28,0.24,710
I use a cronjob to check every 5 minutes to make sure it's always running:
#!/bin/bash if [[ ! `pidof -s /bin/save_load.sh` ]]; then /bin/save_load.sh >> /logs/stats.log & fi