User Tools

Site Tools


projects:load_logger

Retired project. This had it's uses for some of my more esoteric systems (that only had busybox for example) but I've sinced moved to an infrastructure that all supports collectd.

Load logger

A simple bash script to log the datetime and system load to a file every minute. I wanted something that didn't rely on having any other programs installed (though it does rely on the /proc/ filesystem and some basic utilities like free, ps, awk, bc).

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, network usage (one column per interface in the format [interface name, received bytes, transmitted bytes])

save_load.sh
#!/bin/bash
ID='SERVER1'
CPU_COUNT=`cat /proc/cpuinfo | grep processor | wc -l`
MAX_CPU=$(($CPU_COUNT * 100))
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`
NET_USAGE=`awk -v ORS="," 'NR>2{print $1,$2,$9}' /proc/net/dev`
OUTPUT=$ID','$date','$CPU_USAGE','$MAX_CPU','$MEM_USAGE','$LOAD_AVG','$NET_USAGE
# to trim that last awkward comma
echo $OUTPUT | awk '{gsub(/,$/,""); print $0}'

Example output

SERVER1,2020-04-19 17:42:13,17.1,400,15929,3401,6050,545,6477,11664,0.28,0.24,0.26,798,eno1: 0 0,lo: 43147702 0,pan1: 0 0,wlp2s0: 29433068499 0
SERVER1,2020-04-19 17:42:41,17.1,400,15929,3404,6042,551,6483,11656,0.17,0.21,0.25,795,eno1: 0 0,lo: 43148718 0,pan1: 0 0,wlp2s0: 29433079099 0

You can log this to a file using cron:

* * * * * /home/seven/bin/save_load.sh >> /home/seven/data/load.log

Central logging

Alternatively, you can send this information to a central location. I wrote a quick script to save this to a database then used CURL to POST it to the server. I swapped the echo line from the script above with something like this:

curl --request POST "https://myserver/" --data-urlencode "data=$OUTPUT"
projects/load_logger.txt · Last modified: 2020/11/18 06:17 by admin