Home
Blog
Tech How To
Jobs

Fast HowTo Create RRD

RRD stores its information in a flat file, so lets initialize it. In this example we are monitoring temperature. You'll need to modify one line.

DS:temp:GAUGE:600:0:60 

Change temp to whatever you're measuring. Also GAUGE is for absolute measurements like cpu, free-mem, or temperature. Use COUNTER for continuous metrics like secs since epoch or bits.

rrdtool create /home/data/rrd/disktemp.rrd --start `date +"%s"` \
DS:temp:GAUGE:600:0:60 \
--step 300 \
RRA:AVERAGE:0.5:1:2000 \
RRA:AVERAGE:0.5:6:2000 \
RRA:AVERAGE:0.5:24:2000 \
RRA:AVERAGE:0.5:288:2000 \
RRA:MAX:0.5:1:2000 \
RRA:MAX:0.5:6:2000 \
RRA:MAX:0.5:24:2000 \
RRA:MAX:0.5:288:2000 \
RRA:MIN:0.5:1:2000 \
RRA:MIN:0.5:6:2000 \
RRA:MIN:0.5:24:2000 \
RRA:MIN:0.5:288:2000


The 2000 at the end of almost every line is the number of samples we are storing. The second to last number (1,6,24,288) is the number of intervales summarized in the sample. MIN, MAX, and AVERAGE with an interval of 1 are redundant.

Now all we need to do is update the RRD database and create our graphs. Here is a shell script to do just that. The last 3 commands update the rrd and create two graphs, daily and weekly. HDTemp is a little program that slurps out the internal temperature of your hard drive.

#!/bin/bash

loop=6
now=$(/bin/date +%s)
let yesterday=${now}-86400
let lastweek=${now}-86400*7
lastTemp=$(/bin/cat /var/tmp/last_disk_temp.txt)

#
# loop around fetching the temp
# try a few times to get the right number
#
while [ "$loop" -gt "0" ]
do
  let loop=$loop-1

  # deamon on 7634 is hdtemp and it closes itself
  diskTemp=$(/usr/bin/telnet 127.0.0.1 7634 \
                | /bin/grep "|" \
                | /usr/bin/cut -d"|" -f4)

  # sometimes nothing comes back from hdtemp
  # if the temperature is good exit
  if [ "$diskTemp" -gt 5 \
       -a $(/bin/echo $diskTemp | /bin/grep [0-9] | /usr/bin/wc -c) -gt 0 ]
  then
    break
  fi

  # a little backoff
  sleep 1
done

# if there still isn't a good temp use the last one
if [ ! $(/bin/echo $diskTemp | /bin/grep [0-9] | /usr/bin/wc -c) -gt 0 ]
then
 diskTemp=$lastTemp
fi


# update the rrd database
/usr/bin/rrdtool update /home/data/rrd/disktemp.rrd ${now}:${diskTemp}


# make the graph
/usr/bin/rrdtool graph /var/www/disktemp.png --start $yesterday --end $now --vertical-label celsius DEF:mytemp=/home/data/rrd/disktemp.r rd:temp:AVERAGE LINE2:mytemp#FF0000
/usr/bin/rrdtool graph /var/www/weeklydisktemp.png --start $lastweek --end $now --vertical-label celsius DEF:mytemp=/home/data/rrd/diskt emp.rrd:temp:AVERAGE LINE2:mytemp#FF0000

Now create an HTML page and include the new graphs.