Thursday, January 23, 2025

logging load average and memory usage on Linux servers

Just redirecting the output of top to a text file, like top >> logtop, we get lot of unprintable characters due to its refresh nature.

uptime gives load average.

top can do various things, like batch mode, -b


Probably more efficient to just log timestamps, free and uptime.

So, put in a cron job to run every 15 minutes,
*/15 * * * * /home/path/to/logtopscript.sh

$ more logtopscript.sh
#!/usr/bin/bash
echo $(date '+%Y-%m-%d:%H:%M:%S') uptime: $(uptime) free: $(free)  >> /home/path/to/toplog.txt

This can be imported into spreadsheets like LibreOffice Calc using <SPACE> as the delimiter, but has problems with uptime's output not being exactly the same length - sometimes
up 1 hour
sometimes
up 6 days,  8:33
etc. It has issues for 3-4 data points every day, which need to be manually finagled. Probably a regex to select only the load average can solve that.

No comments:

Post a Comment