python rrdtool try catch statement

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

    python rrdtool try catch statement

    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
    Code:
    for i in range(numrowed):
                    row = curred.fetchone()
                    time_stamp = row[0]
                    rx_max = row[1]
                    
                    ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
                   
                    if ret:
                           print rrdtool.error()
                
              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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

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

    Comment

    • kaf3773
      New Member
      • Jan 2012
      • 20

      #3
      hi bvdet

      Thanks a lot for your answer it worked and this is my final code
      hope it helps others too
      Code:
      for i in range(numrowed):
                      row = curred.fetchone()
                      time_stamp = row[0]
                      rx_max = row[1]
                      try:
                          ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max));
                      except rrdtool.error, e:
                          print e
                i = i + 1

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Assuming you want to use the first time stamp only, construct a set to contain time sets already processed and check against it.
        Code:
        all_times = set()
        for i in range(numrowed):
            row = curred.fetchone()
            time_stamp = row[0]
            rx_max = row[1]
         
            if time_stamp not in all_times:
                ret = rrdtool.update(ds_file.rrd,'%s:%s' %(time_stamp,rx_max))
                all_times.add(time_stamp)

        Comment

        Working...