December 7, 2024

rrd – snmp

rrd is a database that works like a 5 gallon bucket with a hose coming out the bottom: You put stuff in until it overflows, and it keeps a steady 5 gallons of “information” in it at all times and just rotates through. It’s just a normal file on your server but it’s specially dedicated to this rotating process. It’s typically used for graphing some small history of a given dataset and then dumping the old stuff you no longer care about. When you combine it with snmp, it can monitor stuff on your network and feed it to a graphing thing so you can keep a historical graph of trends on your network.

Create a basic graph from data

First, you have to create an rrd database to use. We start with a generic one that creates test.rrd:

vi createdatabase.sh
  #!/bin/bash
 
  rrdtool create test.rrd i\
        --step=1 --start=now-1000s \
        DS:ds1:GAUGE:1:U:U \
        RRA:AVERAGE:0.5:1:1000
chmod 755 createdatabase.sh
./createdatabase.sh

Okay, now you have a database that steps at 1 second, and graphs the last 1000 seconds.

Now you have to fill it with stuff, so write a script that does that like:

vi filldatabase.sh
  #!/bin/bash
 
  START=$(expr $(date "+%s") - 1000)
  COUNT=1000
  for (( i = 0; i < ${COUNT}; i++ )); do
    VALUE=$(echo "scale=3; s($i/10) * 100" | bc -l)
    rrdtool update test.rrd ${START}:${VALUE}
    START=$(expr ${START} + 1)
  done
chmod 755 filldatabase.sh
./filldatabase.sh

Now you have a database with stuff in it, so you have to create a graphic .png of your data set that you can display on webpage like:

vi creategraph.sh
  #!/bin/bash
 
  rrdtool graph graph.png \
    --start now-1000s --end now \
    DEF:ds1a=test.rrd:ds1:AVERAGE \
    LINE1:ds1a#FF0000:"Wavy line"
chmod 755 creategraph.sh
./creategraph.sh

Now you should have a .png file called graph.png, which you can move to a website and see in a browser.

Now you have to update your data to keep it fresh, so you need another script to do that with some kind of regularity. I want to query snmp data off network devices.

Here are a few links:

https://wiki.alpinelinux.org/wiki/Setting_up_traffic_monitoring_using_rrdtool_(and_snmp)