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".
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 ]
echo "Date MemTotal MemFree MemCached CPU-usage" > $FILE
echo "$(date) $MEMDATA $CPUDATA" >> $FILE
Steps to install/use:
- $ nano ./logsysinfo.sh
- paste the script, then do CTRL + X
- chmod the file to 755
- $ chmod 755 ./logsysinfo.sh
- $ sudo ./logsysinfo.sh
- tail ./sysinfo.log
- If the output of the last command was 2 lines (1st=header, 2nd=numbers), then all is good.
- Make sure the log is updated every 5 minutes:
- $ sudo crontab -e
- add the following line in the text editor which just opened, then close the editor:
- */5 * * * * /home/paulk/logsysinfo.sh
- Wait for max 5 minutes, then do $ tail ./sysinfo.log again
- 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!| Viewed 4128 times