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

python rrdtool try catch statement

P: 20
Hi

I have a Python script that updates an rrd file with data it pulls from a database. Here is a snippet of the Python code
Expand|Select|Wrap|Line Numbers
  1. for i in range(numrowed):
  2.                 row = curred.fetchone()
  3.                 time_stamp = row[0]
  4.                 rx_max = row[1]
  5.  
  6.                 ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
  7.  
  8.                 if ret:
  9.                        print rrdtool.error()
  10.  
  11.           i = i + 1
My problem is that the script crashes anytime there is a duplicate time stamp because rrdtool allows only one value per timestamp. Thus i get this error

Traceback (most recent call last):
File "rrdfile_update.py", line 40, in <module>
ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
rrdtool.error: ds_file: illegal attempt to update using time 1363358441 when last update time is 1363358441 (minimum one second step)

I will appreciate very much if you can help me with a Python try catch statement to help skip this error and continue to update the rrd file with the next timestamp and value so the script does not stop.

Help very much appreciated. Thanks
Mar 18 '13 #1

✓ answered by bvdet

Try/except blocks can catch any error by doing this:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except Exception, e:
  4.     print e
Generally it's better to catch specific errors such as your by checking for it:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except rrdtool.error, e:
  4.     print e
You can also issue a pass statement instead of printing the error

Share this Question
Share on Google+
3 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Try/except blocks can catch any error by doing this:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except Exception, e:
  4.     print e
Generally it's better to catch specific errors such as your by checking for it:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     ...code that may raise an exception...
  3. except rrdtool.error, e:
  4.     print e
You can also issue a pass statement instead of printing the error
Mar 18 '13 #2

P: 20
hi bvdet

Thanks a lot for your answer it worked and this is my final code
hope it helps others too
Expand|Select|Wrap|Line Numbers
  1. for i in range(numrowed):
  2.                 row = curred.fetchone()
  3.                 time_stamp = row[0]
  4.                 rx_max = row[1]
  5.                 try:
  6.                     ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
  7.                 except rrdtool.error, e:
  8.                     print e
  9.           i = i + 1
  10.  
  11.  
  12.  
Mar 18 '13 #3

Expert 100+
P: 626
Assuming you want to use the first time stamp only, construct a set to contain time sets already processed and check against it.
Expand|Select|Wrap|Line Numbers
  1. all_times = set()
  2. for i in range(numrowed):
  3.     row = curred.fetchone()
  4.     time_stamp = row[0]
  5.     rx_max = row[1]
  6.  
  7.     if time_stamp not in all_times:
  8.         ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max))
  9.         all_times.add(time_stamp) 
Mar 18 '13 #4

Post your reply

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