This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
load_logger [2020/04/13 22:42] neil oops, writing this from memory, that's not how bash while loops work |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Load logger ====== | ||
- | 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 | ||
- | |||
- | |||
- | <code bash save_load.sh> | ||
- | #!/bin/bash | ||
- | ID='SERVER1' | ||
- | while [ true ] | ||
- | do | ||
- | printf -v date '%(%Y-%m-%d %H:%M:%S)T' -1 | ||
- | CPU_USAGE=`grep 'cpu ' /proc/stat | awk '{print ($2+$4)*100/($2+$4+$5)}'` | ||
- | 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 | ||
- | </code> | ||
- | Example: | ||
- | <code> | ||
- | 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 | ||
- | </code> | ||
- | |||
- | I use a cronjob to check every 5 minutes to make sure it's always running: | ||
- | <code bash check_load.sh> | ||
- | #!/bin/bash | ||
- | if [[ ! `pidof -s save_load.sh` ]]; then | ||
- | /bin/save_load.sh >> /logs/stats.log | ||
- | fi | ||
- | </code> |