Using RRD with Python: short introduction
RRDtool is a great facility which aims to replace MRTG and was written by Tobias Oetiker. RRDtool provides powerful features for collecting and visualizing various system metrics like network traffic, MySQL counters//http://godfatherhost.com/cloud/index.html or whatever you want. Itâs always good idea to know what is going on under the hood of your server. Managing servers we prefer to monitor its parameters. Here is a short introduction into using RRD with Python. We will use Fedora 15  here but all examples should work in any another distributions as well. If youâre not familiar with RRD  you might need to check its tutorial.
At the beginning we need to install RRD library for using with Python:
yum install rrdtool-python
Now we can create RRD database with all sources we need. Please note that here we use 30 minutes intervals, but you can use any you want. We use two parameters as data source: metric1 and metric2. Itâs type is GAUGE. The thing is RRD supports different types (actually, Data Source Type): GAUGE, DERIVE, ABSOLUTE, COMPUTE or COUNTER. Probably, the most popular are COUNTER and GAUGE. The first one should be used for something like SNMP network interface counters for input and output traffic which increase constantly. And when you use just some parameters like HDD or CPU temperature//http://godfatherhost.com/dedicated/index.htm you need to use GAUGE and RRD will gather data as is with no deltas. You can find other types description in RRD documentation.
03ret = rrdtool.create("example.rrd", "--step", "1800", "--start", '0',
04Â "DS:metric1:GAUGE:2000:U:U",
05Â "DS:metric2:GAUGE:2000:U:U",
06Â "RRA:AVERAGE:0.5:1:600",
07Â "RRA:AVERAGE:0.5:6:700",
08Â "RRA:AVERAGE:0.5:24:775",
09Â "RRA:AVERAGE:0.5:288:797",
12Â "RRA:MAX:0.5:24:775",
13Â "RRA:MAX:0.5:444:797")
Letâs consider all lines in details. First line include name of RRD database (âexample.rrdâ) and you can use here any path you want, step of parameters checking (30 minutes in our case), and the start point (0 or N means ânowâ).  âDSâ in line 4-5 means Data Source, these lines include two our metrics. â2000Ⲡmeans that RRD can wait for 2000 seconds to get new values until it considers them as unknown (thatâs is why we use 2000, which 200 seconds more of our 30 minutes interval).  Last two parameters â âU:Uâ â stand for min and max values of each metric (âunknownâ in our case). Lines 6-13 describe what types of gained values RRD should store in its database.  Itâs pretty self-describing (average and max values).  Mentioned values describe how many parameters RRD should keep. Considering it can be confusing I will omit explanation but note that these values were choosen to be compatible with MRTG (actually, itâs not quite true since we use 1800 seconds periods and not 5 minutes, so you might need to modify it (if you also donât use 5 minutes period) or keep like I did).
To update your RRD database use next example:
1from rrdtool import update as rrd_update
2ret = rrd_update('example.rrd', 'N:%s:%s' %(metric1, metric2));
âNâ also means ânowâ. It should be launched every 30 minutes by cron, for instance. After some time RRD is ready to graph our data for a day, a week and a month.
02for sched in ['daily' , 'weekly', 'monthly']:
04    if sched == 'weekly':
05        period = 'w'
06    elif sched == 'daily':
07        period = 'd'
08    elif sched == 'monthly':
09        period = 'm'
10    ret = rrdtool.graph( "/var/www/html/metrics-%s.png" %(sched), "--start","-1%s" %(period), "--vertical-label=Num",
11Â Â Â Â Â Â Â Â Â '--watermark=playground.in.supportex.net',
12Â Â Â Â Â Â Â Â Â "-w 800",
13Â Â Â Â Â Â Â Â Â "DEF:m1_num=example:metric1:AVERAGE",
14Â Â Â Â Â Â Â Â Â "DEF:m2_num=example.rrd:metric2:AVERAGE",
15Â Â Â Â Â Â Â Â Â "LINE1:m1_num#0000FF:metric1\\r",
16Â Â Â Â Â Â Â Â Â "LINE2:m2_num#00FF00:metric2\\r",
17Â Â Â Â Â Â Â Â Â "GPRINT:m1_num:AVERAGE:Avg m1\: %6.0lf ",
18Â Â Â Â Â Â Â Â Â "GPRINT:m1_num:MAX:Max m1\: %6.0lf \\r",
19Â Â Â Â Â Â Â Â Â "GPRINT:m2_num:AVERAGE:Avg m2\: %6.0lf ",
20Â Â Â Â Â Â Â Â Â "GPRINT:m2_num:MAX:Max m2\: %6.0lf \\r")
You should get something like:
Various visualized system metrics always help us to provide better services in server management. Of course, you could use munin or Ganglia. But in some cases it can not be acceptable or probably you donât want to write your own plugins. Â That is where RRD can help.