0

Hey guys (and off course Ladys) ,

i have this little script which should show me some nice rrd graphs. But i seems like i cant find a way to bring it to work to show me some stats. This is my Script:

# Function: Simple ping plotter for rrd
import rrdtool,tempfile,commands,time,sys
from model import hosts
sys.path.append('/home/dirk/devel/python/stattool/stattool/lib')
import nurrd
from nurrd import RRDplot

class rrdPing(RRDplot):

    def __init__(self):
        self.DAY = 86400
        self.YEAR = 365 * self.DAY
        self.rrdfile = 'hostname.rrd'
        self.interval = 300
        self.probes = 5
        self.rrdList = []


    def create_rrd(self, interval):
        ret = rrdtool.create("%s" % self.rrdfile, "--step", "%s" % self.interval,
                             "DS:packets:COUNTER:600:U:U",
                             "RRA:AVERAGE:0.5:1:288",
                             "RRA:AVERAGE:0.5:1:336")


    def getHosts(self, userID):
        myHosts = hosts.query.filter_by(uid=userID).all()
        return myHosts.pop(0)

    def _doPing(self,host):
        for x in xrange(0, self.probes):
            ans,unans = commands.getstatusoutput("ping -c 3 -w 6 %s| grep rtt| awk -F '/' '{ print $5 }'" % host)
            print x
            self.probes -=1
            self.rrdList.append(unans)
        return self.rrdList 

    def plotRRD(self):
        self.create_rrd(self.interval)
        times = self._doPing(self.getHosts(3))
        for x in xrange(0,len(times)):
            loc = times.pop(0)
            rrdtool.update(self.rrdfile, '%d:%d' % (int(time.time()), int(float(loc))))
            print  '%d:%d' % (int(time.time()), int(float(loc)))
            time.sleep(5)
        self.graph(60)

    def graph(self, mins):
        ret = rrdtool.graph( "%s.png" % self.rrdfile, "--start", "-1", "--end" , "+1","--step","300",
                             "--vertical-label=Bytes/s",
                             "DEF:inoctets=%s:packets:AVERAGE" % self.rrdfile ,
                             "AREA:inoctets#7113D6:In traffic",
                             "CDEF:inbits=inoctets,8,*",
                             "COMMENT:\\n",
                             "GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf \\r",
                             "COMMENT:  ",
                             "GPRINT:inbits:MAX:Max In traffic\: %6.2lf")



if __name__ == "__main__":

    ping = rrdPing()
    ping.plotRRD()
    info = rrdtool.info('hostname.rrd')
    print info['last_update']

Could somebody please give me some advice or some tips how to solve this? (Sorry code is a litte mess)

Thanks in advance

Kind regards,

Dirk

Dirk
  • 451
  • 7
  • 21
  • When you say 'it doesnt work' what do you mean? Does the script fail to run, or does it run but not generate a graph, or it generates a graph but without the content you expect? A little more detail would help. An initial glance shows that you seem to only be giving the RRD one data sample; you need at least 2, separated by 300s, to get a point in the RRA to be graphed else you'll get an empty graph. – Steve Shipway Apr 14 '14 at 04:06

1 Answers1

0

Several issues.

Firstly, you appear to only be collecting a single data sample and storing it before you try to generate a graph. You will need at least two samples, separated by about 300s, before you can get a single Primary Data Point and therefore something to graph.

Secondly, you do not post nay information as to what data you are actually storing. Are you sure your rrdPing function is returning valid data to store? You are not testing the error status of the write either.

Thirdly, the data you are collecting seems to be ping times or similar, which is a GAUGE type value. Yet, your RRD DS definition uses a COUNTER type and your graph call is treating it as network traffic data. A COUNTER type assumes increasing values and converts to a rate of change, so if you give it ping RTT data you'll get either unknowns or zeroes stored, which will not show up on a graph.

Fourthly, your call to RRDGraph is specifying a start of -1 and and end of +1. From 1 second in the past to 1 second in the future? Since your step is 300s this is an odd graph. Maybe you should have--end 'now-1' --start 'end-1day' or similar?

You should make your code test the return values for any error messages produced by the RRDTool library -- this is good practice anyway. When testing, print out the values you are updating with to be sure you are giving valid values. With RRDTool, you should collect several data samples at the step interval and store them before expecting to see a line on the graph. Also, make sure you are using the correct data type, GAUGE or COUNTER.

Steve Shipway
  • 3,754
  • 3
  • 22
  • 39