| /trunk/rrd.py r8 | /trunk/rrd.py r9 | ||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
|---|---|---|---|
| 2 | # | 2 | # |
| 3 | # rrd.py | 3 | # rrd.py |
| 4 | # Simple RRDTool wrapper | 4 | # Simple RRDTool wrapper |
| 5 | # Copyright (c) 2008 Corey Goldberg (corey@goldb.org) | 5 | # Copyright (c) 2008 Corey Goldberg (corey@goldb.org) |
| 6 | # | 6 | # |
| 7 | # Download the Windows version of RRDTool from: | 7 | # Download the Windows version of RRDTool from: |
| 8 | # http://www.gknw.net/mirror/rrdtool/ | 8 | # http://www.gknw.net/mirror/rrdtool/ |
| 9 | # | 9 | # |
| 10 | # You may need these fonts if RRDTool throws an error when you graph: | 10 | # You may need these fonts if RRDTool throws an error when you graph: |
| 11 | # http://dejavu.sourceforge.net/wiki/index.php/Main_Page | 11 | # http://dejavu.sourceforge.net/wiki/index.php/Main_Page |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | import os | 14 | import os |
| 15 | import time | 15 | import time |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | class RRD(object): | 18 | class RRD(object): |
| 19 | def __init__(self, rrd_name, vertical_label='test'): | 19 | def __init__(self, rrd_name, vertical_label='test'): |
| 20 | self.rrd_name = rrd_name | 20 | self.rrd_name = rrd_name |
| 21 | self.vertical_label = vertical_label | 21 | self.vertical_label = vertical_label |
| 22 | 22 | ||
| 23 | |||
| 23 | def create_rrd(self, interval): | 24 | def create_rrd(self, interval): |
| 24 | interval = str(interval) | 25 | interval = str(interval) |
| 25 | interval_mins = float(interval) / 60 | 26 | interval_mins = float(interval) / 60 |
| 26 | heartbeat = str(int(interval) * 2) | 27 | heartbeat = str(int(interval) * 2) |
| 27 | ds_string = ' DS:test:GAUGE:%s:U:U' % heartbeat | 28 | ds_string = ' DS:test:GAUGE:%s:U:U' % heartbeat |
| 28 | cmd_create = ''.join(( | 29 | cmd_create = ''.join(( |
| 29 | 'rrdtool create ', self.rrd_name, ' --step ', interval, ds_string, | 30 | 'rrdtool create ', self.rrd_name, ' --step ', interval, ds_string, |
| 30 | ' RRA:AVERAGE:0.5:1:', str(int(4000 / interval_mins)), | 31 | ' RRA:AVERAGE:0.5:1:', str(int(4000 / interval_mins)), |
| 31 | ' RRA:AVERAGE:0.5:', str(int(30 / interval_mins)), ':800', | 32 | ' RRA:AVERAGE:0.5:', str(int(30 / interval_mins)), ':800', |
| 32 | ' RRA:AVERAGE:0.5:', str(int(120 / interval_mins)), ':800', | 33 | ' RRA:AVERAGE:0.5:', str(int(120 / interval_mins)), ':800', |
| 33 | ' RRA:AVERAGE:0.5:', str(int(1440 / interval_mins)), ':800', | 34 | ' RRA:AVERAGE:0.5:', str(int(1440 / interval_mins)), ':800', |
| 34 | )) | 35 | )) |
| 35 | cmd = os.popen4(cmd_create) | 36 | cmd = os.popen4(cmd_create) |
| 36 | cmd_output = cmd[1].read() | 37 | cmd_output = cmd[1].read() |
| 37 | for fd in cmd: fd.close() | 38 | for fd in cmd: fd.close() |
| 38 | if len(cmd_output) > 0: | 39 | if len(cmd_output) > 0: |
| 39 | raise RRDException, 'Unable to create RRD: ' + cmd_output | 40 | raise RRDException, 'Unable to create RRD: ' + cmd_output |
| 40 | 41 | ||
| 42 | |||
| 41 | def update(self, *values): | 43 | def update(self, *values): |
| 42 | values_args = ''.join([str(value) + ':' for value in values])[:-1] | 44 | values_args = ''.join([str(value) + ':' for value in values])[:-1] |
| 43 | cmd_update = 'rrdtool update %s N:%s' % (self.rrd_name, values_args) | 45 | cmd_update = 'rrdtool update %s N:%s' % (self.rrd_name, values_args) |
| 44 | cmd = os.popen4(cmd_update) | 46 | cmd = os.popen4(cmd_update) |
| 45 | cmd_output = cmd[1].read() | 47 | cmd_output = cmd[1].read() |
| 46 | for fd in cmd: fd.close() | 48 | for fd in cmd: fd.close() |
| 47 | if len(cmd_output) > 0: | 49 | if len(cmd_output) > 0: |
| 48 | raise RRDException, 'Unable to update RRD: ' + cmd_output | 50 | raise RRDException, 'Unable to update RRD: ' + cmd_output |
| 49 | 51 | ||
| 52 | |||
| 50 | def graph(self, mins): | 53 | def graph(self, mins): |
| 51 | start_time = 'now-%s' % (mins * 60) | 54 | start_time = 'now-%s' % (mins * 60) |
| 52 | output_filename = self.rrd_name + '.png' | 55 | output_filename = self.rrd_name + '.png' |
| 53 | end_time = 'now' | 56 | end_time = 'now' |
| 54 | ds_name = 'test' | 57 | ds_name = 'test' |
| 55 | width = '400' | 58 | width = '400' |
| 56 | height = '150' | 59 | height = '150' |
| 57 | base = '1000' | 60 | base = '1000' |
| 58 | cur_date = time.strftime('%m/%d/%Y %H\:%M\:%S', time.localtime()) | 61 | cur_date = time.strftime('%m/%d/%Y %H\:%M\:%S', time.localtime()) |
| 59 | cmd_graph = 'rrdtool graph ' + output_filename + \ | 62 | cmd_graph = 'rrdtool graph ' + output_filename + \ |
| 60 | ' DEF:' + ds_name + '=' + self.rrd_name + ':' + ds_name + ':AVERAGE' + \ | 63 | ' DEF:' + ds_name + '=' + self.rrd_name + ':' + ds_name + ':AVERAGE' + \ |
| 61 | ' AREA:' + ds_name + '#FF0000' + \ | 64 | ' AREA:' + ds_name + '#FF0000' + \ |
| 62 | ' VDEF:' + ds_name + 'last=' + ds_name + ',LAST' + \ | 65 | ' VDEF:' + ds_name + 'last=' + ds_name + ',LAST' + \ |
| 63 | ' COMMENT:"' + cur_date + '"' + \ | 66 | ' COMMENT:"' + cur_date + '"' + \ |
| 64 | ' --title="' + self.rrd_name +'"' + \ | 67 | ' --title="' + self.rrd_name +'"' + \ |
| 65 | ' --vertical-label="' + self.vertical_label + '"' \ | 68 | ' --vertical-label="' + self.vertical_label + '"' \ |
| 66 | ' --start=' + start_time + \ | 69 | ' --start=' + start_time + \ |
| 67 | ' --end=' + end_time + \ | 70 | ' --end=' + end_time + \ |
| 68 | ' --width=' + width + \ | 71 | ' --width=' + width + \ |
| 69 | ' --height=' + height + \ | 72 | ' --height=' + height + \ |
| 70 | ' --lower-limit="0"' | 73 | ' --lower-limit="0"' |
| 71 | cmd = os.popen4(cmd_graph) | 74 | cmd = os.popen4(cmd_graph) |
| 72 | for fd in cmd: fd.close() | 75 | for fd in cmd: fd.close() |
| 73 | 76 | ||
| 74 | 77 | ||
| 75 | class RRDException(Exception): pass | 78 | class RRDException(Exception): pass |