Skip to content

Instantly share code, notes, and snippets.

@kadin2048
Created October 14, 2021 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadin2048/b4a1e64a2f8e5623aeb8307282d23752 to your computer and use it in GitHub Desktop.
Save kadin2048/b4a1e64a2f8e5623aeb8307282d23752 to your computer and use it in GitHub Desktop.
Just some notes on an old SNMP monitor-and-graph project

Some Notes on SNMP Dashboarding

My SNMP "dashboard" consisted of three parts: one, a set of Round Robin Databases created with rrdtool; two, a set of shell scripts run frequently by cron to add new data to the databases; three, a set of less-frequently run scripts to generate the graph PNGs displayed on a web page.

Round Robin Databases

The empty Round Robin Databases need to be initialized first, before you can add data to them.

Tutorial here: https://oss.oetiker.ch/rrdtool/tut/rrd-beginners.en.html

The most important consideration is the --step value, because it needs to match the frequency of the data-retrieval and update interval (how often the update script will run). I used 300s, or 5 minutes.

To work properly with my router's SNMP output, I had to use data type COUNTER. HEARTBEAT was set to 600, I believe.

Updating Data

The following script was run every 5 minutes and pulled down the latest "octets in" and "octets out" from physical interface 8 (the uplink port) on an HP router with IP address 192.168.1.1:

#!/bin/bash
# Update RRD file containing total WAN traffic stats through router
# This should be run every 5 minutes by cron

RRDFILE=/path/to/routerwanio.rrd

IN=`snmpget -v2c -c public 192.168.1.1 ifInOctets.8 | cut -d" " -f4`
OUT=`snmpget -v2c -c public 192.168.1.1 ifOutOctets.8 | cut -d" " -f4`

rrdupdate $RRDFILE N:$IN:$OUT

Generating Graphs

The following script was run periodically to generate the graphics for the web page:

#!/bin/bash
# This script produces graphs for a web page using an RRD database as a source file.

OUTDIR="/var/www/bwgraphs"
INFILE="/path/to/routerwanio.rrd"
HEIGHT="200"

# Six-hour graph
rrdtool graph $OUTDIR/6hr.png -h $HEIGHT --start "N-6hr" \
DEF:input=$INFILE:input:AVERAGE \
DEF:output=$INFILE:output:AVERAGE \
LINE1:input#FF0000:Downstream \
LINE1:output#0000FF:Upstream \
'GPRINT:input:AVERAGE:Ave. Down %2.0lf B/s' \
'GPRINT:output:AVERAGE:Ave. Up %2.0lf B/s \j'

# One-day graph
rrdtool graph $OUTDIR/1dy.png -h $HEIGHT --start "N-24hr" \
DEF:input=$INFILE:input:AVERAGE \
DEF:output=$INFILE:output:AVERAGE \
LINE1:input#FF0000:Downstream \
LINE1:output#0000FF:Upstream \
'GPRINT:input:AVERAGE:Ave. Down %2.0lf B/s' \
'GPRINT:output:AVERAGE:Ave. Up %2.0lf B/s \j'

# One-month graph
rrdtool graph $OUTDIR/1mo.png -h $HEIGHT --start "N-1months" \
DEF:input=$INFILE:input:AVERAGE \
DEF:output=$INFILE:output:AVERAGE \
LINE1:input#FF0000:Downstream \
LINE1:output#0000FF:Upstream \
'GPRINT:input:AVERAGE:Ave. Down %2.0lf B/s' \
'GPRINT:output:AVERAGE:Ave. Up %2.0lf B/s \j'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment