My favorites | Sign in
Project Logo
          
Changes to /trunk/rrd.py
r9 vs. r11   Edit
  Compare: vs.   Format:
Revision r11
Go to: 
Project members, sign in to write a code review
/trunk/rrd.py   r9 /trunk/rrd.py   r11
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
24 def create_rrd(self, interval): 24 def create_rrd(self, interval):
25 interval = str(interval) 25 interval = str(interval)
26 interval_mins = float(interval) / 60 26 interval_mins = float(interval) / 60
27 heartbeat = str(int(interval) * 2) 27 heartbeat = str(int(interval) * 2)
28 ds_string = ' DS:test:GAUGE:%s:U:U' % heartbeat 28 ds_string = ' DS:test:GAUGE:%s:U:U' % heartbeat
29 cmd_create = ''.join(( 29 cmd_create = ''.join((
30 'rrdtool create ', self.rrd_name, ' --step ', interval, ds_string, 30 'rrdtool create ', self.rrd_name, ' --step ', interval, ds_string,
31 ' RRA:AVERAGE:0.5:1:', str(int(4000 / interval_mins)), 31 ' RRA:AVERAGE:0.5:1:', str(int(4000 / interval_mins)),
32 ' RRA:AVERAGE:0.5:', str(int(30 / interval_mins)), ':800', 32 ' RRA:AVERAGE:0.5:', str(int(30 / interval_mins)), ':800',
33 ' RRA:AVERAGE:0.5:', str(int(120 / interval_mins)), ':800', 33 ' RRA:AVERAGE:0.5:', str(int(120 / interval_mins)), ':800',
34 ' RRA:AVERAGE:0.5:', str(int(1440 / interval_mins)), ':800', 34 ' RRA:AVERAGE:0.5:', str(int(1440 / interval_mins)), ':800',
35 )) 35 ))
36 cmd = os.popen4(cmd_create) 36 cmd = os.popen4(cmd_create)
37 cmd_output = cmd[1].read() 37 cmd_output = cmd[1].read()
38 for fd in cmd: fd.close() 38 for fd in cmd: fd.close()
39 if len(cmd_output) > 0: 39 if len(cmd_output) > 0:
40 raise RRDException, 'Unable to create RRD: ' + cmd_output 40 raise RRDException, 'Unable to create RRD: ' + cmd_output
41 41
42 42
43 def update(self, *values): 43 def update(self, *values):
44 values_args = ''.join([str(value) + ':' for value in values])[:-1] 44 values_args = ''.join([str(value) + ':' for value in values])[:-1]
45 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)
46 cmd = os.popen4(cmd_update) 46 cmd = os.popen4(cmd_update)
47 cmd_output = cmd[1].read() 47 cmd_output = cmd[1].read()
48 for fd in cmd: fd.close() 48 for fd in cmd: fd.close()
49 if len(cmd_output) > 0: 49 if len(cmd_output) > 0:
50 raise RRDException, 'Unable to update RRD: ' + cmd_output 50 raise RRDException, 'Unable to update RRD: ' + cmd_output
51 51
52 52
53 def graph(self, mins): 53 def graph(self, mins):
54 start_time = 'now-%s' % (mins * 60) 54 start_time = 'now-%s' % (mins * 60)
55 output_filename = self.rrd_name + '.png' 55 output_filename = self.rrd_name + '.png'
56 end_time = 'now' 56 end_time = 'now'
57 ds_name = 'test' 57 ds_name = 'test'
58 width = '400' 58 width = '400'
59 height = '150' 59 height = '150'
60 base = '1000' 60 base = '1000'
61 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())
62 cmd_graph = 'rrdtool graph ' + output_filename + \ 62 cmd_graph = 'rrdtool graph ' + output_filename + \
63 ' DEF:' + ds_name + '=' + self.rrd_name + ':' + ds_name + ':AVERAGE' + \ 63 ' DEF:' + ds_name + '=' + self.rrd_name + ':' + ds_name + ':AVERAGE' + \
64 ' AREA:' + ds_name + '#FF0000' + \ 64 ' AREA:' + ds_name + '#FF0000' + \
65 ' VDEF:' + ds_name + 'last=' + ds_name + ',LAST' + \ 65 ' VDEF:' + ds_name + 'last=' + ds_name + ',LAST' + \
66 ' VDEF:' + ds_name + 'avg=' + ds_name + ',AVERAGE' + \
66 ' COMMENT:"' + cur_date + '"' + \ 67 ' COMMENT:"' + cur_date + '"' + \
68 ' GPRINT:' + ds_name + 'avg:" average=%6.2lf%S"' + \
67 ' --title="' + self.rrd_name +'"' + \ 69 ' --title="' + self.rrd_name +'"' + \
68 ' --vertical-label="' + self.vertical_label + '"' \ 70 ' --vertical-label="' + self.vertical_label + '"' \
69 ' --start=' + start_time + \ 71 ' --start=' + start_time + \
70 ' --end=' + end_time + \ 72 ' --end=' + end_time + \
71 ' --width=' + width + \ 73 ' --width=' + width + \
72 ' --height=' + height + \ 74 ' --height=' + height + \
73 ' --lower-limit="0"' 75 ' --lower-limit="0"'
74 cmd = os.popen4(cmd_graph) 76 cmd = os.popen4(cmd_graph)
75 for fd in cmd: fd.close() 77 for fd in cmd: fd.close()
76 78
77 79
78 class RRDException(Exception): pass 80 class RRDException(Exception): pass
Hosted by Google Code