[rrd-users] rrd python help needed.

Jeremiah Jester jeremiah.jester at panasonic.aero
Fri Dec 26 23:29:24 CET 2008


Seasons greeting,

I'm hoping someone on here can help me understand rrdtool a little
better. I have a python script that is interacting with the rrdtool
module via import.

What I am trying to do is to collect data from /dev/proc for eth0
device , grab new stats about traffic every 2 seconds, and assign those
values to a list and attempting to generate the plotted points
accordingly.

However, it appears that I'm not passing the values in properly as It
errors out if I add more than one value. I'm essentially passing a list
of values to this line of code: 

'LINE1:%s#CC9999:Net1' % a,

If i change the value from the list to just one number then it just
plots a straight line. 

Can anyone point me in the right direction so that the chart will plot
the varying data i've collected? Script is attached and inline.

Thanks,
JJ

===

#!/usr/bin/python
import rrdtool, time, random, sys, ConfigParser

def get_proc_net_dev(interface_dict):
        for line in open('/proc/net/dev','r'):
                if ':' not in line: continue

                for interface in interface_dict:
                        if interface not in line: continue
                        line = line.strip().split(':')[1]
                        x = line.split()
                        rx_kbytes = int(x[0])/1024
                        rx_packets = int(x[1])
                        tx_kbytes = int(x[8])/1024
                        tx_packets = int(x[9])
                        interface_dict[interface] = (rx_kbytes,
rx_packets, tx_kbytes, tx_packets)


def create_graph():

	stime = int(time.time()) - 3 * 86400 
	dpoints = 10
	etime = stime + (dpoints * 300)
	fname = 'test.rrd'
	gfname = 'test.png'
 
	#CREATE GRAPH 1ST

	rrdtool.create('test.rrd' ,
	         '--start' , str(stime),
		 #### can sepcify end time below
		 # '--end' , str(endtime)
		 #add DS for another line element
        	 'DS:speed:COUNTER:600:U:U',
		 'DS:average:GAUGE:600:U:U',
	         'DS:min:COUNTER:600:U:U',
		 'RRA:AVERAGE:0.5:1:576',
        	 'RRA:AVERAGE:0.5:6:336'
	)
 
	ctime = stime
	cmiles = 0

	#GET & PARSE UPDATED DATA
	my_array=parse(fname,ctime)

	#GRAB VALUES FROM PARSE	
	a=my_array[0]
	b=my_array[1]
	c=my_array[2]
	
	print a
	print b
	print c


	#LASTLY, GRAPH THE UPDATED DATA
	rrdtool.graph(gfname ,
	        '--start' , str(etime - (24 * 3600)) ,
	        '--end' , str(etime) ,
	        '--vertical-label' , 'CPU Load' ,
       		'--imgformat' , 'PNG' ,
        	'--title' , 'System Load Average' ,
        	'--lower-limit' , '0' ,
        	
		'DEF:myspeed=%s:speed:AVERAGE' % fname ,

		'CDEF:mph=myspeed,3600,*' ,
        	'VDEF:msmax=mph,MAXIMUM' ,
        	'VDEF:msavg=mph,AVERAGE' ,
        	'VDEF:msmin=mph,MINIMUM' ,
        	
		#lines
		'LINE1:%s#CC9999:Net1' % a,
        	'LINE2:%s#FF0000:Net2' % b,
		'LINE3:%s#FFDDDD:Net3' % c,

        	#r'GPRINT:msmax:Max\: %6.1lf Load' ,
        	#r'GPRINT:msavg:Avg\: %6.1lf Load' ,
        	#r'GPRINT:msmin:Min\: %6.1lf Load\l' ,
	) 

def parse(fname, ctime):

        interface_dict = {'eth0':None}
        get_proc_net_dev(interface_dict)
        old_interface_dict = interface_dict

	i=0

	#declare lists
	stat0_list=[]
	stat1_list=[]
	stat2_list=[]

	while i<=5:
		i+=1
                get_proc_net_dev(interface_dict)
	           
		print interface_dict
		
		#update graph with new numbers
		# loop through interface_dict items
	    	# {wlan: (123, 123, 123)}
	
	    	for x,y in interface_dict.items():
			net_device=x
			for x in y:
				stat0=y[0]
				stat1=y[1]
				stat2=y[2]

		#add each iter item to list
		stat0_list.append(stat0)
		stat1_list.append(stat1)
		stat2_list.append(stat2)

		
		print "STAT0:",stat0		
		print "STAT1:",stat1	
		print "STAT2:",stat2	

		ctime=ctime
		ctime+=300		

		print "CTIME:",ctime

		rrdtool.update(fname, '%s:%s:%s:%s' % (ctime,stat0,stat1,stat2))

	    	time.sleep(2)

	return stat0_list, stat1_list, stat2_list

create_graph()




Disclaimer: The information contained in this transmission, including any 
attachments, may contain confidential information of Panasonic Avionics
Corporation.  This transmission is intended only for the use of the 
addressee(s) listed above.  Unauthorized review, dissemination or other use 
of the information contained in this transmission is strictly prohibited. 
If you have received this transmission in error or have reason to believe 
you are not authorized to receive it, please notify the sender by return 
email and promptly delete the transmission.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: script3.py
Type: text/x-python
Size: 3215 bytes
Desc: not available
Url : http://lists.oetiker.ch/pipermail/rrd-users/attachments/20081226/adc24cb4/attachment.py 


More information about the rrd-users mailing list