By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,821 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,821 IT Pros & Developers. It's quick & easy.

fetching values from rrd file

P: 20
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
Expand|Select|Wrap|Line Numbers
  1. output = rrdtool.fetch("mutime.rrd", 'AVERAGE')
  2. trmaxTuples = output[2]
  3. n  = len(trmaxTuples)
  4. for n in trmaxTuples:
  5.     if (trmaxTuples > 100):
  6.         print n
  7.     else:
  8.         print "0"
  9.  
  10.  
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
Apr 16 '13 #1

✓ answered by bvdet

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.
Expand|Select|Wrap|Line Numbers
  1. trmaxTuples = ((43.0,),(46.0,),(69.0,),(41.0,),(51.0,),(62.0,),(55.0,))
  2.  
  3. for item in trmaxTuples:
  4.     n = item[0]
  5.     if n > 50:
  6.         print n
  7.     else:
  8.         print "x"
The output:
Expand|Select|Wrap|Line Numbers
  1. >>> x
  2. x
  3. 69.0
  4. x
  5. 51.0
  6. 62.0
  7. 55.0
  8. >>> 

Share this Question
Share on Google+
4 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. trmaxTuples = ((43.0,),(46.0,),(69.0,),(41.0,),(51.0,),(62.0,),(55.0,))
  2.  
  3. for item in trmaxTuples:
  4.     n = item[0]
  5.     if n > 50:
  6.         print n
  7.     else:
  8.         print "x"
The output:
Expand|Select|Wrap|Line Numbers
  1. >>> x
  2. x
  3. 69.0
  4. x
  5. 51.0
  6. 62.0
  7. 55.0
  8. >>> 
Apr 16 '13 #2

P: 20
Smashing!! Thanks a lot bvdet this worked.
Apr 16 '13 #3

P: 20
I am trying to extend this code for an rrd file with multiple datasources.

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

Expand|Select|Wrap|Line Numbers
  1. -bash-3.2$ /opt/csw/bin/rrdtool fetch mutime.rrd AVERAGE -s -2hr
  2.                        first            second            third            fourth
  3.  
  4. 1425410700: 9.7907263600e+06 1.0574282213e+07 9.9081866667e+04 8.6084040000e+04
  5. 1425411000: 9.6375070800e+06 1.0465039640e+07 9.8183733333e+04 8.4958386667e+04
  6. 1425411300: 9.5396297067e+06 1.0361895200e+07 9.7132146667e+04 8.4087973333e+04
  7. 1425411600: 9.4494586067e+06 1.0304070603e+07 9.6382913333e+04 8.3760376667e+04
  8. 1425411900: 9.4662621467e+06 1.0230560960e+07 9.6236346667e+04 8.3033670000e+04
  9. 1425412200: 9.3551098567e+06 1.0105500320e+07 9.4713120000e+04 8.2404246667e+04
  10.  
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
Mar 3 '15 #4

P: 20
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.
Expand|Select|Wrap|Line Numbers
  1. trmaxTuples = rrdtool.fetch("mutime.rrd", 'AVERAGE', '-s -2hr')
  2. n = trmaxTuples[2]
  3. p = 0
  4. for p in range(len(n)):
  5.     s = n[p]
  6.     print s
  7.     first = s[0]
  8.     print first
  9.     second = s[1]
  10.     print second
  11.     third = s[2]
  12.     print third
  13.     fourth = s[3]
  14.     print fourth
  15. p = p + 1
  16.  
  17.  
Mar 4 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.