Logging memory and cpu usage in Ubuntu

I recently installed a cluster of Ubuntu VMs for a client's web application. Now it was time for logging and monitoring, but that turned out to be not so simple at all. I couldn't find a solid Linux script which would log CPU and memory usage, so decided to write it myself.

The script extracts data from the files /proc/meminfo and /proc/loadavg, and saves them in sysinfo.log as a tab-delimited text file. The columns are:

Date   MemTotal   MemFree   MemCached   CPU-usage (=last minute avg.)

MemTotal will always remain the same, but is usefull for calculating relative available memory. MemFree + MemCached == available memory; don't be misled by the column name "MemFree".

#!/bin/bash

FILE=/home/paulk/sysinfo.log
MEMDATA=`sudo cat /proc/meminfo | egrep "^(MemTotal|MemFree|Cached)" | sed 's/[^0-9]\+//g' | tr '\n' '\t'`
CPUDATA=`sudo cat /proc/loadavg | sed 's/ .\+//'`

if [ ! -f $FILE ]
then
echo "Date MemTotal MemFree MemCached CPU-usage" > $FILE
fi
echo "$(date) $MEMDATA $CPUDATA" >> $FILE

Steps to install/use:

  1. sysinfo console output example

    Save this script as 'logsysinfo.sh' (for example in your home directory)
    1. $ nano ./logsysinfo.sh
    2. paste the script, then do CTRL + X
  2. chmod the file to 755
    1. $ chmod 755 ./logsysinfo.sh
  3. test-run:
    1. $ sudo ./logsysinfo.sh
    2. tail ./sysinfo.log
  4. If the output of the last command was 2 lines (1st=header, 2nd=numbers), then all is good.
  5. Make sure the log is updated every 5 minutes:
    1. $ sudo crontab -e
    2. add the following line in the text editor which just opened, then close the editor:
      1. */5 * * * * /home/paulk/logsysinfo.sh
    3. Wait for max 5 minutes, then do $ tail ./sysinfo.log again
    4. If there is more output then before, all seems to be working :)

Personally, I now use this log file for input of a daily monitoring script: check min-max available memory (= MemFree + MemCached), cpu spikes, etc. I hope it helps you out as well!

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 982 times
  1. Bart

    #1 by Bart - April 23, 2014 at 9:30 AM

    Hi Paul,

    This is a usefull script, i will try this out on one of our Debian servers and see if it works there as well. Never read your blog because it's cf related, but now it's getting interesting :)

    Cheers,

    Bart
(will not be published)
Leave this field empty

draw-trap