[rrd-users] rrd and python -> HELP!!!

Lorenz Breu breul at student.ethz.ch
Tue Apr 24 21:19:52 CEST 2007


hi folks

i seriously hope somebody actually reads this...

anywho...

i am using rrdtool to collect data for a semester thesis at the eth, and 
i have decided to use python to collect the data...my god, there is no 
useful documentation on the module anywhere out there...
i hacked together the following code that is supposed to query 3 
gameservers to retrieve the number of players on the server and write it 
into the rrd file, then graph the data to have it displayed on a simple 
webpage...
a) i know that rrdtool works because the files are created correctly and 
the statements at the end to print the last update keep increasing in 
steps of 10 seconds as expected.

b) i know that the number of players is correctly retrieved because when 
i have the updateString printed i get things like 'N:0:4:0:4'.

c) changing the titles and line colors of the graphs works fine

d) the graphs always show a straight line at '27 k' as seen on 
http://82.130.102.202

can somebody tell me where the problem could be??? i am really stuck :(

regards,
lorenz

--------------CODE-----------------

import rrdtool
import SRCDS as rcon
from optparse import OptionParser
import os
import string
import time


class MyServer:
   def __init__(self,server_port=27015,pwd='mypasswd'):
       self.port=server_port
       self.rcon_pwd=pwd

basedir=''
rrdfile=''
portlist=''
htdocs='/var/www/'
host='82.130.102.202'
create_file=False
verbose=False

def countPlayers(list):
   result = []
   for i in list:
       if i['time_on']>0:
           result.append(i)
   return len(result)

def listPlayers(list):
   result=[]
   for i in list:
       if i['time_on']>0:
           result.append(i)
   return result

def createRRD(file):
   rrdtool.create(file,'--step', 
'10','DS:27015:GAUGE:20:0:14','DS:27016:GAUGE:20:0:14','DS:27017:GAUGE:20:0:14','DS:total:GAUGE:20:0:42','RRA:AVERAGE:0.5:1:259200') 




def parseInput():
   global basedir
   global rrdfile
   global portlist      global gfxfiles
   global create_file
   global verbose
   parser=OptionParser()
   parser.set_defaults(basedir='/home/srcds/logs/rrd/')
   parser.set_defaults(ports='27015,27016,27017')
   parser.set_defaults(file='srcds.rrd')
   parser.set_defaults(create=False)
   parser.set_defaults(verb=False)
   parser.add_option('-v',action='store_true',dest='verb')
   parser.add_option('-d',action='store',type='string',dest='basedir')
   parser.add_option('-p',action='store',type='string',dest='ports')
   parser.add_option('-f',action='store',type='string',dest='file')
   parser.add_option('-c',action='store_true',dest='create')
   (options, args) = parser.parse_args()
   basedir=options.basedir
   rrdfile=basedir+options.file
   portlist=options.ports.split(',')
   create_file=options.create
   verbose=options.verb
 
def makeServerList():
   result=[]
   for p in portlist:
       s=MyServer(server_port=int(p))
       result.append(s)
   return result

def makeUpdates(servers):
   global host
   updateString='N'
   total=0
   for s in servers:
       if verbose:
           print 'connecting to ' + host + 'on port %i with password %s' 
% (s.port, s.rcon_pwd)
       rconServer=rcon.SRCDS(host,port=s.port,rconpass=s.rcon_pwd)
       i=countPlayers(rconServer.players())
       tempString=':%i' % (i)
       updateString=updateString+tempString
       total=total+i
   tempString=':%i' % (total)
   updateString=updateString+tempString
   if verbose:
       print "Updating "+rrdfile+"..."
       print updateString
   rrdtool.update(rrdfile,updateString)
   if verbose:
       print "done"
 



parseInput()

if create_file:
   print 'Creating RRD file in '+basedir
   createRRD(rrdfile)
else:
   serverlist=makeServerList()
   
gfxfiles=[htdocs+'images/out_eth1.png',htdocs+'images/out_eth2.png',htdocs+'images/out_eth3.png'] 

   while True:
       makeUpdates(serverlist)
       if verbose:
           print 'graphing from '+rrdfile
       
#rrdtool.graph(gfxfiles[0],'--end','now','--start','end-43200s','--title','Players 
on 
ETH1','DEF:27015='+rrdfile+':27015:AVERAGE','LINE2:27015#FF0000:\'ETH1\'')
       
rrdtool.graph(gfxfiles[1],'--end','now','--start','end-6000s','--title','Players 
on 
ETH2','DEF:27016='+rrdfile+':27016:AVERAGE','LINE2:27016#FFFF00:\'27016\'')
       
#rrdtool.graph(gfxfiles[2],'--end','now','--start','end-43200s','--title','Players 
on 
ETH3','DEF:27017='+rrdfile+':27017:AVERAGE','LINE2:27017#FF0000:\'ETH3\'')
       info=rrdtool.info(rrdfile)
       if verbose:
           print info['last_update']
       time.sleep(10-(time.time()%10))





More information about the rrd-users mailing list