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/15 23:46] neil add max cpu count |
— (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 (%), CPU max, 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' | ||
- | CPU_COUNT=`cat /proc/cpuinfo | grep processor | wc -l` | ||
- | MAX_CPU=CPU_COUNT*100 | ||
- | while [ true ] | ||
- | do | ||
- | printf -v date '%(%Y-%m-%d %H:%M:%S)T' -1 | ||
- | CPU_USAGE=`ps -ax -h -o 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','$CPU_COUNT','$MEM_USAGE','$LOAD_AVG | ||
- | sleep 30; | ||
- | done | ||
- | </code> | ||
- | Example: | ||
- | <code> | ||
- | SERVER1,2020-04-13 22:59:59,3.86818,400,15929,2082,9545,273,4301,13258,0.27,0.31,0.24,703 | ||
- | SERVER1,2020-04-13 23:06:04,3.87079,400,15929,2127,9497,276,4304,13211,0.22,0.25,0.22,708 | ||
- | SERVER1,2020-04-13 23:06:22,3.87107,400,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 -xs /bin/save_load.sh` ]]; then | ||
- | /bin/save_load.sh >> /logs/stats.log & | ||
- | fi | ||
- | </code> |