[rrd-users] Creating one graph from multiply sources

Emily Chouinard cousin17 at msu.edu
Thu Jun 12 20:21:05 CEST 2008


Its the newbie again,
  I promise one day I will get a hang on this RRDtool stuff, but please 
hang with in the meantime. So I have been working with the python-rrd 
binding and I have data being generated from CPU usage, from the 
/proc/stat file,  and now I want to graph data from the usr, sys and 
idle columns to make a comparison between the three. Problem is I don't 
know how to graph 3 sources together. I've included the 2 file I've been 
working with, one is the python binding from Corey Goldberg which only 
takes in one source and the second is my code for accessing the 
information from /proc/stat. Any help would be greatly appreciated. 
Thanks everyone!!

rrd.py (Python rrd binding)
#!/usr/bin/env python
#  rrd.py
#  Simple RRDTool wrapper
#  Copyright (c) 2008 Corey Goldberg (corey at goldb.org)
import os
import time

class RRD(object):
    def __init__(self, rrd_name, vertical_label='test'):    
        self.rrd_name = rrd_name
        self.vertical_label = vertical_label

    def create_rrd(self, interval): 
        interval = str(interval)
        interval_mins = float(interval) / 60 
        heartbeat = str(int(interval) * 2)
        ds_string = ' DS:test:COUNTER:%s:U:U' % heartbeat
        cmd_create = ''.join((
            'rrdtool create ', self.rrd_name, ' --step ', interval, 
ds_string,
            ' RRA:AVERAGE:0.5:1:', str(int(4000 / interval_mins)),
            ' RRA:AVERAGE:0.5:', str(int(30 / interval_mins)), ':800',
            ' RRA:AVERAGE:0.5:', str(int(120 / interval_mins)), ':800',
            ' RRA:AVERAGE:0.5:', str(int(1440 / interval_mins)), ':800',
            ))
        cmd = os.popen4(cmd_create)
        cmd_output = cmd[1].read()
        for fd in cmd: fd.close()
        if len(cmd_output) > 0:
            raise RRDException, 'Unable to create RRD: ' + cmd_output

   def update(self, *values):  
        values_args = ''.join([str(value) + ':' for value in values])[:-1]
        cmd_update = 'rrdtool update %s N:%s' % (self.rrd_name, values_args)
        cmd = os.popen4(cmd_update)
        cmd_output = cmd[1].read()
        for fd in cmd: fd.close()
        if len(cmd_output) > 0:
            raise RRDException, 'Unable to update RRD: ' + cmd_output

     def graph(self, mins):      
        start_time = 'now-%s' % (mins * 60) 
        output_filename = self.rrd_name + '.png'
        end_time = 'now'
        ds_name = 'test'
        width = '400'
        height = '150'
        base = '1000'
        cur_date = time.strftime('%m/%d/%Y %H\:%M\:%S', 
time.localtime())      
        cmd_graph = 'rrdtool graph ' + output_filename + \
            ' DEF:' + ds_name + '=' + self.rrd_name + ':' + ds_name + 
':AVERAGE' + \
        ' AREA:' + ds_name + '#FF00FF' + \
        ' VDEF:' + ds_name + 'last=' + ds_name + ',LAST' + \
        ' COMMENT:"' + cur_date + '"' + \
            ' --title="' + self.rrd_name +'"' + \
            ' --vertical-label="' + self.vertical_label + '"' \
            ' --start=' + start_time + \
            ' --end=' + end_time + \
            ' --width=' + width + \
            ' --height=' + height
           
        cmd = os.popen4(cmd_graph)
        for fd in cmd: fd.close()
         
class RRDException(Exception): pass

and my very own file real_time.py
#!/bin/env python
# RRDtool /proc/stat/ feeder and grapher
# Emily Chouinard 6/11

import rrd
import time

interval = 5
rrd_file='test.rrd'

my_rrd=rrd.RRD(rrd_file,'Change in CPU User Usuage')

while True:
    infile= open('/proc/stat', 'r')
    #for x in range(0,1): #Reads first line only
    line = infile.readline()
    if line.startswith('cpu'): #Check to make sure in 1st line
    info = line.split()    #Returns a list of the words in the string
    usr = info[1]          # These 3 index the specific CPU usages
    sys= info[3]
    idle= info[4]
    my_rrd.update(usr,sys, idle)
    my_rrd.graph(5)
    print  usr sys idle
    time.sleep(10)



More information about the rrd-users mailing list