User Tools

Site Tools


load_logger

This is an old revision of the document!


Load logger

A simple bash script to log the datetime and system load to a file every minute.

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`
#    echo -e $ID','$date','$CPU_USAGE','$CPU_COUNT','$MEM_USAGE','$LOAD_AVG','$NET_USAGE
    # to trim that last comma
    OUTPUT=$ID','$date','$CPU_USAGE','$MAX_CPU','$MEM_USAGE','$LOAD_AVG','$NET_USAGE
    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
load_logger.1587314574.txt.gz ยท Last modified: 2020/04/19 16:42 by neil