Script for monitoring memory usage with MRTG
With MRTG you can graph your network traffic, but you can also use it to gather other network information, such as monitoring your system’s swap and memory usage. MRTG is useful for more than just graphing network traffic. If you look at the MRTG Companion Sites page, you’ll see numerous links to different scripts and tools to put all kinds of information into an MRTG graph.
Here, we will look at monitoring the local system’s swap and memory usage over time. This assumes that MRTG is already configured or you have an understanding of how to configure MRTG .The first step is to create the script, which is very simplistic. Save it as /usr/local/bin/memstat.sh with the following contents:
#!/bin/sh/usr/bin/free -b | /bin/awk 'NR==2 {ramUsed = $3 }NR==4 {swapUsed = $3 }END { print swapUsed "n" ramUsed "n0n0" }'
The output, for a human, is not very interesting:
# /usr/local/bin/memstat.sh0154800947200
For MRTG, however, with the appropriate configuration, it becomes very interesting. Add the following snippet to /etc/mrtg.cfg. Ideally, MRTG should be running every five minutes for a good sampling.
Target[localmem]: `/usr/local/bin/memstat.sh`Title[localmem]: Mem and Swap Usage [surtr]PageTop[localmem]: <h1>Memory and Swap Usage [surtr]</h1>MaxBytes[localmem]: 100000000000ShortLegend[localmem]: BYLegend[localmem]: MemoryLegendI[localmem]: SwapLegendO[localmem]: MemLegend1[localmem]: SwapLegend2[localmem]: MemOptions[localmem]: gauge,growright,nopercentkMG[localmem]: k,M,G,T,P,XColours[localmem]: RED#bb0000,BLUE#1000ff,GREEN#006600,VIOLET#ff00ff
This code tells MRTG to execute the /usr/local/bin/memstat.sh script and take the numbers it provides as output as input for itself, which will then be used to create a typical MRTG graph of the data. This will then give you a sampling of memory and swap usage over time. MRTG will create graphs for yearly, monthly, weekly, and daily statistics.Using the same, you could perform the same data analysis for CPU usage, disk usage, e-mail statistics, Web traffic, and much more.(from www.builderau.com.au)

Leave a Reply