fetching values from rrd file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaf3773
    New Member
    • Jan 2012
    • 20

    fetching values from rrd file

    Hi
    I am trying to cycle through values i fetch from an rrd file with a python script and this is the code i have
    Code:
    output = rrdtool.fetch("mutime.rrd", 'AVERAGE')
    trmaxTuples = output[2]
    n  = len(trmaxTuples)
    for n in trmaxTuples:
        if (trmaxTuples > 100):
            print n
        else:
            print "0"
    Here is the result i get
    (43.0,)
    (46.0,)
    (69.0,)
    (41.0,)
    (51.0,)
    (62.0,)
    (55.0,)

    But i am trying to extract the values one at a time out of the brackets and use them in making a comparison.

    I will appreciate your help very much.

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You assign len(trmaxTuples ) to "n", but you reassign "n" to each element of trmaxTuples in the for loop. Do you want to print "n" if it is greater than 100? The following prints "n" if greater than 50.
    Code:
    trmaxTuples = ((43.0,),(46.0,),(69.0,),(41.0,),(51.0,),(62.0,),(55.0,))
    
    for item in trmaxTuples:
        n = item[0]
        if n > 50:
            print n
        else:
            print "x"
    The output:
    Code:
    >>> x
    x
    69.0
    x
    51.0
    62.0
    55.0
    >>>

    Comment

    • kaf3773
      New Member
      • Jan 2012
      • 20

      #3
      Smashing!! Thanks a lot bvdet this worked.

      Comment

      • kaf3773
        New Member
        • Jan 2012
        • 20

        #4
        I am trying to extend this code for an rrd file with multiple datasources.

        When i run this code
        Code:
        trmaxTuples = rrdtool.fetch("mutime.rrd", 'AVERAGE', '-s -2hr')
        for item in trmaxTuples:
                n = item[1]
                if n > 50:
                    print n
                else:
                    print "x"
        i get the results below
        Code:
        -bash-3.2$ /opt/csw/bin/python alarming.py 
        1425417900
        second
        (9637507.0800000001, 10465039.640000001, 98183.733333333337, 84958.386666666673)
        -bash-3.2$
        When i run rrdtool fetch from the command line here is what i get

        Code:
        -bash-3.2$ /opt/csw/bin/rrdtool fetch mutime.rrd AVERAGE -s -2hr
                               first            second            third            fourth
        
        1425410700: 9.7907263600e+06 1.0574282213e+07 9.9081866667e+04 8.6084040000e+04
        1425411000: 9.6375070800e+06 1.0465039640e+07 9.8183733333e+04 8.4958386667e+04
        1425411300: 9.5396297067e+06 1.0361895200e+07 9.7132146667e+04 8.4087973333e+04
        1425411600: 9.4494586067e+06 1.0304070603e+07 9.6382913333e+04 8.3760376667e+04
        1425411900: 9.4662621467e+06 1.0230560960e+07 9.6236346667e+04 8.3033670000e+04
        1425412200: 9.3551098567e+06 1.0105500320e+07 9.4713120000e+04 8.2404246667e+04
        I would appreciate if you can help me cycle through the values of first and i am assuming it will be the same way to cycle through the values of second, third and fourth which are the datasources in the rrd file.

        Thanks

        Comment

        • kaf3773
          New Member
          • Jan 2012
          • 20

          #5
          After a number of hours at this here is the code i came up with that works for me. improvements to this will be very much appreciated.
          Code:
          trmaxTuples = rrdtool.fetch("mutime.rrd", 'AVERAGE', '-s -2hr')
          n = trmaxTuples[2]
          p = 0
          for p in range(len(n)):
              s = n[p]
              print s
              first = s[0]
              print first
              second = s[1]
              print second
              third = s[2]
              print third
              fourth = s[3]
              print fourth
          p = p + 1

          Comment

          Working...