The Usual Tech Ramblings

Adding a New Datasource to an RRD Database

After upgrading several servers with a newer instance of nsclient++, we discovered Memory graphs were broken because of new performance metrics included in the check memory results. Well broken is subjective, the new performance data included the percentage free (as before), with the addition of the actual physical value (ie 1GB). This addition was missing from the RRDs when they were originally created, so when pnp4Nagios was updating the XML file it uses, it said it had 2 data sets, but the RRD only had one. Here’s how I fixed the issue.

A few searches showed that there wasn’t a built in way to add a new data source, but it was possible by doing a ‘rrdtool dump’, then editing the XML output, and then doing an ‘rrdtool restore’ to load it back in again. A few enterprising people write scripts to do the XML editing for you, but it all still seemed a little kludgy. That’s when I stumbled across RRD::Simple, a Perl module that makes working with RRD files rather, well, simple. It has a handy function called “add_source”, which does exactly that. So for my memory charts I was having issues with, I just had to add a new data source that matched the name from the pnp4Nagios XML file. Fortunately it doesn’t use complex names, just an incrementing ID, so the new data source name was ‘2’. The add_source code looked like this:

use strict;
use warnings;
use RRD::Simple();
my $rrd_file = "./Memory.rrd";
my $rrd = RRD::Simple->new();
$rrd->add_source($rrd_file, '2' => 'GAUGE');

Nagios Memory Graph This takes my Memory.rrd file, and adds the ‘2’ data source as a gauge type. The great thing about this is it retains the information from data source 1. The outcome of the change is in the screen shot to the right. As you can see from the graph, the top graph still shows the percentage of used memory, and the bottom graph shows how much is physically available. You’ll notice a difference in the amount of data being shown on the graph, which represents the fact that the RRD::Simple used no values for the historic data matching up to the current timestamp in the RRD database.

Once I tested on one host (after making a backup of the RRD file), I repeated on all the other hosts too.

Have you got another method to handle adding data sources to an rrd database?