author | Tero Marttila <terom@fixme.fi> |
Wed, 03 Nov 2010 00:51:06 +0200 | |
changeset 28 | 89a4d9879171 |
parent 17 | a1608b66be45 |
child 32 | 47e977c23ba2 |
permissions | -rw-r--r-- |
5 | 1 |
import rrd |
2 |
import time |
|
3 |
||
4 |
""" |
|
5 |
RRDTool graph output |
|
6 |
""" |
|
7 |
||
8 |
def timestamp () : |
|
9 |
return time.strftime("%Y/%m/%d %H:%M:%S %Z") |
|
10 |
||
11 |
def common_opts () : |
|
12 |
""" |
|
13 |
Common options for all views |
|
14 |
""" |
|
15 |
||
16 |
return dict( |
|
17 |
# output |
|
18 |
imgformat = "PNG", |
|
19 |
# lazy = True, |
|
20 |
||
21 |
color = [ |
|
22 |
# disable border |
|
23 |
# border = 0, |
|
24 |
"SHADEA#ffffff00", |
|
25 |
"SHADEB#ffffff00", |
|
26 |
||
27 |
# keep background transparent |
|
28 |
"BACK#ffffff00", |
|
29 |
"SHADEB#ffffff00", |
|
30 |
], |
|
31 |
||
32 |
# labels |
|
33 |
vertical_label = "bits/s", |
|
34 |
units = "si", |
|
35 |
||
36 |
# use logarithmic scaling |
|
37 |
logarithmic = True, |
|
38 |
||
39 |
# smooth out lines |
|
40 |
slope_mode = True, |
|
41 |
) |
|
42 |
||
43 |
def overview_opts () : |
|
44 |
""" |
|
45 |
Common options for the overview graph |
|
46 |
""" |
|
47 |
||
48 |
return dict( |
|
49 |
width = 600, |
|
50 |
height = 50, |
|
51 |
), [ |
|
7
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
52 |
"CDEF:all=in,out,+", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
53 |
|
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
54 |
"VDEF:max=all,MAXIMUM", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
55 |
"VDEF:avg=all,AVERAGE", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
56 |
"VDEF:min=all,MINIMUM", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
57 |
|
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
58 |
"LINE1:in#0000FF:In", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
59 |
"LINE1:out#00CC00:Out", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
60 |
|
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
61 |
"GPRINT:max:%6.2lf %Sbps max", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
62 |
"GPRINT:avg:%6.2lf %Sbps avg", |
804243b0bcb9
add sum max/avg/min to overview graphs
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
63 |
"GPRINT:min:%6.2lf %Sbps min\\l", |
5 | 64 |
] |
65 |
||
66 |
def detail_opts () : |
|
67 |
""" |
|
68 |
Common options for the detail graph |
|
69 |
""" |
|
70 |
||
71 |
return dict( |
|
72 |
# dimensions |
|
73 |
width = 600, |
|
74 |
height = 200, |
|
75 |
), [ |
|
76 |
# values |
|
77 |
'VDEF:in_max=in,MAXIMUM', |
|
78 |
'VDEF:in_avg=in,AVERAGE', |
|
79 |
'VDEF:in_min=in,MINIMUM', |
|
28
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
80 |
'VDEF:in_cur=in,LAST', |
5 | 81 |
'VDEF:out_max=out,MAXIMUM', |
82 |
'VDEF:out_avg=out,AVERAGE', |
|
83 |
'VDEF:out_min=out,MINIMUM', |
|
28
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
84 |
'VDEF:out_cur=out,LAST', |
5 | 85 |
|
86 |
# legend/graph |
|
87 |
"COMMENT:%4s" % "", |
|
88 |
"COMMENT:%11s" % "Maximum", |
|
89 |
"COMMENT:%11s" % "Average", |
|
28
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
90 |
"COMMENT:%11s" % "Minimum", |
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
91 |
"COMMENT:%11s\\l" % "Current", |
5 | 92 |
|
93 |
"LINE1:in#0000FF:%4s" % "In", |
|
94 |
'GPRINT:in_max:%6.2lf %Sbps', |
|
95 |
'GPRINT:in_avg:%6.2lf %Sbps', |
|
28
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
96 |
'GPRINT:in_min:%6.2lf %Sbps', |
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
97 |
'GPRINT:in_cur:%6.2lf %Sbps\\l', |
5 | 98 |
|
99 |
"LINE1:out#00CC00:%4s" % "Out", |
|
100 |
'GPRINT:out_max:%6.2lf %Sbps', |
|
101 |
'GPRINT:out_avg:%6.2lf %Sbps', |
|
28
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
102 |
'GPRINT:out_min:%6.2lf %Sbps', |
89a4d9879171
Add 'Current' column to detail view
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
103 |
'GPRINT:out_cur:%6.2lf %Sbps\\l', |
5 | 104 |
|
105 |
# mark |
|
106 |
"COMMENT:Generated %s\\r" % timestamp().replace(':', '\\:'), |
|
107 |
] |
|
108 |
||
109 |
STYLE_DEFS = { |
|
110 |
'overview': overview_opts, |
|
111 |
'detail': detail_opts, |
|
112 |
} |
|
113 |
||
114 |
def daily_opts (title) : |
|
115 |
""" |
|
116 |
Common options for the 'daily' view |
|
117 |
""" |
|
118 |
||
119 |
return dict( |
|
120 |
# labels |
|
121 |
x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", |
|
122 |
||
123 |
# general info |
|
124 |
title = "Daily %s" % (title, ), |
|
125 |
||
126 |
# interval |
|
127 |
start = "-24h", |
|
128 |
) |
|
129 |
||
130 |
def weekly_opts (title) : |
|
131 |
return dict( |
|
132 |
# labels |
|
133 |
# x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", |
|
134 |
||
135 |
# general info |
|
136 |
title = "Weekly %s" % (title, ), |
|
137 |
||
138 |
# interval |
|
139 |
start = "-7d", |
|
140 |
) |
|
141 |
||
142 |
def yearly_opts (title) : |
|
143 |
return dict( |
|
144 |
# labels |
|
145 |
# x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", |
|
146 |
||
147 |
# general info |
|
148 |
title = "Yearly %s" % (title, ), |
|
149 |
||
150 |
# interval |
|
151 |
start = "-1y", |
|
152 |
) |
|
153 |
||
154 |
||
155 |
INTERVAL_DEFS = { |
|
156 |
'daily': daily_opts, |
|
157 |
'weekly': weekly_opts, |
|
158 |
'yearly': yearly_opts, |
|
159 |
} |
|
160 |
||
161 |
def mrtg_data (rrd_path) : |
|
162 |
""" |
|
163 |
Data sources for network in/out from an MRTG rrd |
|
164 |
""" |
|
165 |
||
166 |
return [ |
|
167 |
# data sources, bytes/s |
|
168 |
r'DEF:in0=%s:ds0:AVERAGE' % rrd_path, |
|
169 |
r'DEF:out0=%s:ds1:AVERAGE' % rrd_path, |
|
170 |
||
171 |
# data, bits/s |
|
172 |
'CDEF:in=in0,8,*', |
|
173 |
'CDEF:out=out0,8,*', |
|
174 |
||
175 |
] |
|
176 |
||
17
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
177 |
def collectd_data (rrd_path) : |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
178 |
""" |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
179 |
Data sources for if_octets from a collectd rrd |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
180 |
""" |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
181 |
|
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
182 |
return [ |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
183 |
# data sources, bytes/s |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
184 |
r'DEF:in0=%s:rx:AVERAGE' % rrd_path, |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
185 |
r'DEF:out0=%s:tx:AVERAGE' % rrd_path, |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
186 |
|
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
187 |
# data, bits/s |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
188 |
'CDEF:in=in0,8,*', |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
189 |
'CDEF:out=out0,8,*', |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
190 |
|
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
191 |
] |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
192 |
|
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
193 |
def _graph (style, interval, title, data_func, rrd_path, out_path) : |
5 | 194 |
style_opts, style_vars = STYLE_DEFS[style]() |
195 |
interval_opts = INTERVAL_DEFS[interval](title) |
|
196 |
||
197 |
opts = rrd.merge_opts(common_opts(), style_opts, interval_opts) |
|
17
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
198 |
data = data_func(rrd_path) + style_vars |
5 | 199 |
|
200 |
return rrd.graph(out_path, *data, **opts) |
|
17
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
201 |
|
5 | 202 |
|
17
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
203 |
def mrtg (style, interval, title, rrd_path, out_path) : |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
204 |
return _graph(style, interval, title, mrtg_data, rrd_path, out_path) |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
205 |
|
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
206 |
def collectd_ifoctets (style, interval, title, rrd_path, out_path) : |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
207 |
return _graph(style, interval, title, collectd_data, rrd_path, out_path) |
a1608b66be45
add support for collectd_ifoctets in addition to mrtg
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
208 |