Contributing to open source: a new feature for RRDtool

Vincent Bernat

Note (2026-07)

This ar­ti­cle was not com­pleted and stayed as a draft for more than 10 years. As I think it is still use­ful to­day, even in­com­plete, I am pub­lish­ing it as is.

With this post, I would like to start a se­ries around con­tri­bu­tion to open source. Each post will be an ex­am­ple of con­tri­bu­tion to a project. There is no uni­ver­sal recipe but the steps are usually:

  1. Spot a problem.
  2. Check it is still present in re­cent versions.
  3. Check it has not been al­ready reported.
  4. Fix the problem.
  5. Send the fix upstream.

Let’s ex­plore how to add a fea­ture to RRDtool to ig­nore miss­ing RRD files.

What is RRDtool?#

RRDtool stands for round-robin data­base tool. It stores time-se­ries data like CPU us­age or net­work band­width in a cir­cu­lar buf­fer to en­sure that the size of each RRD file re­mains con­stant over time. Once stored, data can be graphed to get some­thing like this:

Example of rrdtool graph output
Example of graph generated by RRDtool

The rrdtutorial man­ual page pro­vides a com­plete in­tro­duc­tion to RRDtool. To un­der­stand the re­main­der of this post, you need to have a ba­sic knowl­edge of how RRD files work. Let’s do a very quick in­tro­duction:

  • an RRD file de­fines a step in­ter­val, for ex­am­ple 30 seconds;
  • an RRD file also de­fines one or sev­eral data sources (DS);
  • a DS is com­posed of one pri­mary data point (PDP) ev­ery step in­ter­val; those PDP are not stored in the RRD files: an ex­ter­nal process will feed data points ev­ery step in­ter­val and RRDtool will in­ter­po­late them into PDP so that they ex­actly fit the step interval;
  • PDP are trans­formed into con­sol­i­dated data points (CDP) us­ing a con­sol­i­da­tion func­tion (CF), like the av­er­age function;
  • CDP are stored in a round-robin ar­chive (RRA) in­side the RRD file;
  • an RRA is de­fined by a CF, the num­ber of PDP to con­sol­i­date to get a CDP and the num­ber of CDP to keep; an RRD file can have sev­eral RRA.

To cre­ate a new RRD file, you can is­sue the fol­low­ing command:

rrdtool create sensor1.rrd         \
    --step 30                      \
    DS:temperature:GAUGE:100:0:U   \
    RRA:AVERAGE:0.5:1:100          \
    RRA:AVERAGE:0.5:10:100

The step in­ter­val is 30. There is one data source named temperature. We have two RRA. The first one will take one PDP and turn it into a CDP and will store 100 of them. It will con­tain less than one hour of data (30×1×100). The sec­ond one will take 10 PDP, com­pute the av­er­age to get a CDP and store 100 of them. It cov­ers a greater time span with less precision.

To feed the data source, you can use some script:

while true; do
   temp=$(get_sensor1_temperature)
   [ -z "$temp" ] || \
      rrdtool update sensor1.rrd N:$temp
   sleep 30
done

Sup­pose you have built three RRD files for three dif­fer­ent sen­sors and you want to graph them:

rrdtool graph temperatures.png             \
  --start="now-1h" --end="now"             \
  --title="Temperatures for X, Y and Z"    \
  --vertical-label="K"                     \
  --lower-limit=0                          \
  DEF:t1=sensor1.rrd:temperature:AVERAGE   \
  DEF:t2=sensor2.rrd:temperature:AVERAGE   \
  DEF:t3=sensor3.rrd:temperature:AVERAGE   \
  LINE1:t1#ff0000:"Sensor 1"               \
  LINE1:t2#00ff00:"Sensor 2"               \
  LINE1:t3#0000ff:"Sensor 3"

The DEF lines al­low you to name data sources to be used. RRDtool will se­lect the most ap­pro­pri­ate RRA to match the re­quested in­ter­val with the max­i­mum precision.

The problem#

While you can store sev­eral dif­fer­ent time-se­ries in an RRD file, it is com­mon prac­tice to use sev­eral RRD files to build a graph, like in the ex­am­ple above.

If you have many graphs to gen­er­ate for dif­fer­ent sys­tems, you can use a tool which comes with a tem­plat­ing sys­tem, like dr­raw. How­ever, if it is not smart enough to only in­clude def­i­ni­tions for RRD files that ac­tu­ally ex­ist,1 rrdtool graph will com­plain about the miss­ing RRD files and won’t out­put anything.

You could pro­vide an empty RRD file, but this takes disk space. It would be con­ve­nient to let RRDtool con­sider data points from a miss­ing RRD file as un­known val­ues. A quick search about a so­lu­tion wasn’t fruitful.

Reproduce the problem#

Be­fore try­ing to fix the prob­lem, we need to re­pro­duce it with sim­pler data. It is likely that you run into the prob­lem with a com­plex graph com­bin­ing sev­eral RRD. We need to find a small test case that ex­hibits the prob­lem and that we will be able to use un­til the res­o­lu­tion of the prob­lem. This test case also al­lows you to work out­side of your pro­duc­tion en­vi­ronment.

So, let’s build three sim­ple RRD files:

for i in $(seq 1 3); do
  rrdtool create sensor${i}.rrd        \
        --start=now-5h --step 30       \
        DS:temperature:GAUGE:100:U:U   \
        RRA:AVERAGE:0.5:1:300          \
        RRA:AVERAGE:0.5:10:100
  for t in $(seq 1 600); do
    t=$(( (601-$t) * -30))
    v=$(echo "c($t/5+$i)*$i*4 + 10" | bc -l)
    rrdtool update sensor${i}.rrd -- ${t}:${v}
  done
done

Let’s build the ap­pro­pri­ate graph:

rrdtool graph out.png                          \
      --start="now-2h" --end="now-1h"          \
      --title="Temperatures for X, Y and Z"    \
      --vertical-label="°C"                    \
      --lower-limit=-10                        \
      DEF:t1=sensor1.rrd:temperature:AVERAGE   \
      DEF:t2=sensor2.rrd:temperature:AVERAGE   \
      DEF:t3=sensor3.rrd:temperature:AVERAGE   \
      LINE1:t1#ff0000:"Sensor 1"               \
      LINE1:t2#00ff00:"Sensor 2"               \
      LINE1:t3#0000ff:"Sensor 3"

We get the fol­low­ing graph:

Output of rrdtool graph
Output of rrdtool graph with three data sources

Re­pro­duc­ing the prob­lem is then quite easy. We just sup­press one of the RRD files and the pre­vi­ous com­mand now says: ERROR: opening 'sensor2.​rrd': No such file or directory.

Reproduce the problem with the latest version#

The pre­vi­ous ex­per­i­ment was done with ver­sion 1.4.7. This is the lat­est sta­ble ver­sion. How­ever, maybe the prob­lem has been al­ready fixed in the de­vel­op­ment ver­sion. More­over, de­vel­op­ing a fix on the de­vel­op­ment branch is more ef­fec­tive than on a ran­dom sta­ble version.

De­vel­op­ment of RRDtool is now hosted on GitHub. Let’s grab the source tree and compile it:

$ git clone git@github.com:oetiker/rrdtool-1.x.git
Cloning into 'rrdtool-1.x'...
$ cd rrdtool-1.x
$ ./autogen.sh
[...]
$ ./configure CFLAGS="-O0 -g"
checking build system type... x86_64-unknown-linux-gnu
[...]
Config is DONE!
$ make
[...]

Once com­piled, we can try again our test case. We just need to re­place rrdtool with libtool execute src/​rrdtool. We get the ex­act same er­ror. A quick look at the up­dated man­ual page shows no new op­tion that could help here.

The fix#

The first step is to find where the er­ror happens:

$ git grep "ERROR: opening"
$ git grep "opening '"
src/rrd_cgi.c:            rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));
src/rrd_open.c:        rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));

The cor­re­spond­ing source code looks like this:

rrd_file_t *rrd_open(
    const char *const file_name,
    rrd_t *rrd,
    unsigned rdwr)
{
/* [...] */
    if ((rrd_simple_file->fd = open(file_name, flags, 0666)) < 0) {
        rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));
        goto out_free;
    }
/* [...] */
}

If the file doesn’t ex­ist, we could open a fake file. How­ever, there may be a bet­ter place. We need to find where rrd_​open() is called. In­stead of us­ing git grep, let’s use gdb and set a break­point when the er­ror happens:

$ libtool execute gdb --args   \
rrdtool-1.x/src/rrdtool        \
      graph out.png            \
      --start="now-2h" --end="now-1h"        \
      --title="Temperatures for X, Y and Z"  \
      --vertical-label="°C"                  \
      --lower-limit=-10                      \
      DEF:t1=sensor1.rrd:temperature:AVERAGE \
      DEF:t2=sensor2.rrd:temperature:AVERAGE \
      DEF:t3=sensor3.rrd:temperature:AVERAGE \
      LINE1:t1#ff0000:"Sensor 1"             \
      LINE1:t2#00ff00:"Sensor 2"             \
      LINE1:t3#0000ff:"Sensor 3"

gdb$ b rrd_open.c:196
No source file named rrd_open.c.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (rrd_open.c:196) pending.

gdb$ run
Starting program: /home/bernat/tmp/rrdtool-1.x/src/.libs/lt-rrdtool graph out.png --start=now-2h --end=now-1h --title=Temperatures\ for\ X,\ Y\ and\ Z --vertical-label=°C --lower-limit=-10 DEF:t1=sensor1.rrd:temperature:AVERAGE DEF:t2=sensor2.rrd:temperature:AVERAGE DEF:t3=sensor3.rrd:temperature:AVERAGE LINE1:t1\#ff0000:Sensor\ 1 LINE1:t2\#00ff00:Sensor\ 2 LINE1:t3\#0000ff:Sensor\ 3

Breakpoint 1, rrd_open (file_name=0x425e30 "sensor2.rrd", rrd=0x7fffffffaea0, rdwr=1) at rrd_open.c:196
196             rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));

gdb$ bt
#0  rrd_open (file_name=0x425e30 "sensor2.rrd", rrd=0x7fffffffaea0, rdwr=1) at rrd_open.c:196
#1  0x00007ffff7faa274 in rrd_fetch_fn (filename=0x425e30 "sensor2.rrd", cf_idx=CF_AVERAGE, start=0x4265b0, end=0x4265b8, step=0x7fffffffaff0, ds_cnt=0x4265e0, ds_namv=0x4265f0, data=0x4265f8) at rrd_fetch.c:249
#2  0x00007ffff7fafd4b in data_fetch (im=0x7fffffffb0c0) at rrd_graph.c:905
#3  0x00007ffff7fb9bc7 in graph_paint (im=0x7fffffffb0c0) at rrd_graph.c:3296
#4  0x00007ffff7fbdf67 in rrd_graph_v (argc=13, argv=0x7fffffffe5c0) at rrd_graph.c:4132
#5  0x00007ffff7fbd9b3 in rrd_graph (argc=13, argv=0x7fffffffe5c0, prdata=0x7fffffffe288, xsize=0x7fffffffe284, ysize=0x7fffffffe280, stream=0x0, ymin=0x7fffffffe278, ymax=0x7fffffffe270) at rrd_graph.c:4013
#6  0x0000000000403279 in HandleInputLine (argc=14, argv=0x7fffffffe5b8, out=0x7ffff7dd7880) at rrd_tool.c:740
#7  0x000000000040259a in main (argc=14, argv=0x7fffffffe5b8) at rrd_tool.c:523

So, we could mod­ify rrd_​fetch_​fn() in rrd_​fetch.c or data_​fetch() in rrd_​graph.c. Since we only want to mod­ify the be­hav­iour of rrdtool graph, let’s have a look at rrd_​graph.c:

/* If connecting was successfull, use the daemon to query the data.
 * If there is no connection, for example because no daemon address
 * was specified, (try to) use the local file directly. */
if (rrdc_is_connected (rrd_daemon))
{
    status = rrdc_fetch (im->gdes[i].rrd,
            cf_to_string (im->gdes[i].cf),
            &im->gdes[i].start,
            &im->gdes[i].end,
            &ft_step,
            &im->gdes[i].ds_cnt,
            &im->gdes[i].ds_namv,
            &im->gdes[i].data);
    if (status != 0)
        return (status);
}
else
{
    if ((rrd_fetch_fn(im->gdes[i].rrd,
                    im->gdes[i].cf,
                    &im->gdes[i].start,
                    &im->gdes[i].end,
                    &ft_step,
                    &im->gdes[i].ds_cnt,
                    &im->gdes[i].ds_namv,
                    &im->gdes[i].data)) == -1) {
        return -1;                      
    }               
}

There are two cases that should be handled:

  1. when the data is fetched from the RRD daemon and
  2. when the data is fetched di­rectly from an RRD file.

Let’s fo­cus on the sec­ond case, but for the sake of co­herency, the first case should be han­dled too. Be­fore do­ing any mod­i­fi­ca­tion, let’s switch to a ded­i­cated topic branch:

$ git checkout -b feature/missing-rrd

rrd_​fetch_​fn() is ex­pected to mod­ify the start, end, ft_​step to match what’s pos­si­ble with the re­quested RRD file. It is also ex­pected to give proper val­ues to ds_​cnt (num­ber of data sources), ds_​namv (names of data sources) and data (the CDP from the most ap­pro­pri­ate RRA). We can try a sim­ple fix: pro­vide an empty RRD with no data source:

if ((rrd_fetch_fn(...)) == -1) {
    rrd_clear_error();
    im->gdes[i].ds_cnt = im->gdes[i].start = im->gdes[i].end = 0;
    ft_step = 1; 
}

If we try this mod­i­fi­ca­tion, we get an er­ror at the end of the data_​fetch() func­tion: ERROR: No DS called 'temperature' in 'sensor2.​rrd. This is due to the fol­low­ing check:

/* lets see if the required data source is really there */
for (ii = 0; ii < (int) im->gdes[i].ds_cnt; ii++) {
    if (strcmp(im->gdes[i].ds_namv[ii], im->gdes[i].ds_nam) == 0) {
        im->gdes[i].ds = ii;
    }
}
if (im->gdes[i].ds == -1) {
    rrd_set_error("No DS called '%s' in '%s'",
                  im->gdes[i].ds_nam, im->gdes[i].rrd);
    return -1;
}

Re­mov­ing the last check seems to do the trick. There are two problems:

  1. Can the re­main­ing code han­dle a graph de­scrip­tion with ds set to -1 and ds_​cnt set to 0?
  2. We have re­moved per­fectly valid er­rors about miss­ing files and miss­ing data sources.

For the first prob­lem, the test case seems to run fine. In my opin­ion, you should not spend too much time check­ing that your patch doesn’t break any­thing: sub­mit it to up­stream, tell them that you have some doubts and, be­cause they have far more knowl­edge of the code­base than you, they should be able to tell.

Let’s tackle the sec­ond is­sue. We will add an op­tion to rrdtool graph al­low­ing one to pro­vide miss­ing RRD files. We need to find a free let­ter (like -Z) and a long name (like --missing-ds). The image_​desc_t struc­ture passed to data_​fetch() func­tion has an extra_​flags mem­ber. We de­fine a new flag, ALLOW_​MISSING_DS, that will be used when the user spec­i­fies our new option.

Adding this op­tion is not re­ally dif­fi­cult. Have a look at the fi­nal patch.2

Submitting the fix#

The last step is to for­ward the patch to the up­stream project. Even if your patch is in­com­plete or buggy, it is still worth sub­mit­ting it. There is no rea­son to be shy or ashamed: some­body may take the patch from here and en­hance it or sug­gest im­provements.

There are usu­ally two pos­si­bilities:

  1. Sub­mit the patch to the mail­ing-list of the project: use git send-email master for this pur­pose. This can be cum­ber­some be­cause you may have to sub­scribe to the mail­ing-list be­fore be­ing able to post.
  2. Sub­mit the patch through an is­sue tracker.

Di­rectly sub­mit­ting the patch to a pri­vate per­son is less likely to be use­ful since it won’t be avail­able to any­one else un­til the per­son acts upon it.

RRDtool is hosted on GitHub which comes with a sim­ple is­sue tracker. If you are not fa­mil­iar with GitHub, there is a con­ve­nient way to open a new is­sue and at­tach­ing a branch di­rectly to it. Fol­low those sim­ple steps:

  1. Fork the repos­i­tory. The repos­i­tory will be show in your ac­count with a link to the original.
  2. Com­mit the change into a branch of your copy of the repository.
  3. Push the changes to GitHub.
  4. Send a pull request.

If you have al­ready cloned the up­stream repos­i­tory and do not want to start again af­ter fork­ing it on GitHub, you can add your fork as a remote:

# Add your fork as a remote repository
git remote add github git@github.com:vincentbernat/rrdtool-1.x
# Rename the origin remote to something more sensible
git remote rename origin upstream
# Sync with upstream (optional)
git pull upstream master
git rebase master
# Push the topic branch to GitHub
git push feature/missing-rrd github

Note (2026-07)

I don’t re­mem­ber the ex­act rea­son I didn’t com­plete the ar­ti­cle, but I think I wanted to show the process was “easy.” Since the patch was ini­tially not ac­cepted, it didn’t make sense to end with a “fail­ure.” The patch was even­tu­ally merged a few months later. The fea­ture also gained at least one other user who opened an is­sue about it.