RRDTool graphic output for memory usage monitoring

This script below is an example of what I used to monitor via Nagios in the company I used to work for. I wrote it in order to get a graphic output of our servers’ memory usage. Used to help a lot. Still does I think.

In this script I used SNMP protocol [snmpwalk] — gather server’s data — and rrdtool to build the graphic.

#!/bin/bash

. /usr/lib/nagios/plugins/utils.sh || exit 3

HOST="1.1.1.1"

# Track dependencies
err=""
snmpwalk=$(which snmpwalk) || err="snmpwalk";
rrdtool=$(which rrdtool)   || err="rrdtool";
date=$(which date)         || err="date";
echo=$(which echo)         || err="echo";
bc=$(which bc)             || err="bc";

if [ ! -z "$err" ] ; then
        echo "Command not found: $err"
        exit 0
fi

# snmpwalk's output
TOTAL="2076876"
counter=0

while [ $counter -le 3 ] ; do

        # snmpwalk's return
        AVAILABLE=`snmpwalk -v2c -c public -OvQ $HOST UCD-SNMP-MIB::memAvailReal`

        if [ $? -eq 1 ] ; then
                let "counter = counter + 1"
                sleep 30;
                continue;
        else
                break
        fi

done

LIMIT="200000"

# Current timestamp
UPDATETIMESTAMP=$(date -d "`date +%F` `date +%R`" +%s)

rrdtool update /usr/lib/nagios/plugins/custom/rrds/serverx-memory.rrd "$UPDATETIMESTAMP":"$LIMIT":"$AVAILABLE" 1>/dev/null 2>&1

# Graphics generation
rrdtool graph /var/lib/nagios2/graphs/${HOST}-Memory-LastHour.png \
--title="Memory Usage (${HOST}) - Last hour" \
--end now \
--start end-1h \
DEF:maxline=/usr/lib/nagios/plugins/custom/rrds/serverx-memory.rrd:limit:AVERAGE \
DEF:conline=/usr/lib/nagios/plugins/custom/rrds/serverx-memory.rrd:current:AVERAGE \
LINE1:maxline#FF0000:"Memory Usage Limit" \
LINE1:conline#0000BB:"Available Memory Usage" 1>/dev/null 2>&1

rrdtool graph /var/lib/nagios2/graphs/${HOST}-Memory-LastDay.png \
--title="Memory Usage (${HOST}) - Last 24 hours" \
--end now \
--start end-24h \
DEF:maxline=/usr/lib/nagios/plugins/custom/rrds/serverx-memory.rrd:limit:AVERAGE \
DEF:conline=/usr/lib/nagios/plugins/custom/rrds/serverx-memory.rrd:current:AVERAGE \
LINE1:maxline#FF0000:"Memory Usage Limit" \
LINE1:conline#0000BB:"Available Memory Usage" 1>/dev/null 2>&1

# Nagios return
if [ $AVAILABLE -lt $LIMIT ]; then
        echo 1
else
        echo 0
fi

And the graphical output … [only an example since I don’t have an actual output for this script]

Example RRDTool output

Example RRDTool output

Deixe uma resposta

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s