b7878f4d |
1 | <?php |
2 | // ************ RRD status class ************** |
3 | class rrdstat { |
4 | |
5 | var $rrd_file = 'sample.rrd'; |
6 | |
7 | var $rrd_fields = array(); |
8 | var $rra_base = array(); |
9 | var $rrd_step = 300; |
10 | var $rra_add_max = true; |
11 | |
12 | var $status = 'unused'; |
13 | |
14 | function rrdstat($init_info = null) { |
15 | // ***** init RRD stat module ***** |
16 | $this->set_def($init_info); |
17 | |
18 | if (!is_writeable($this->rrd_file)) { |
19 | if (!file_exists($this->rrd_file)) { |
20 | if (touch($this->rrd_file)) { $this->create(); } |
21 | else { trigger_error('RRD file can not be created', E_USER_WARNING); } |
22 | } |
23 | else { |
24 | if (is_readable($this->rrd_file)) { $this->status = 'readonly'; } |
25 | else { trigger_error('RRD file is not readable', E_USER_WARNING); } |
26 | } |
27 | } |
28 | else { |
29 | $this->status = 'ok'; |
30 | } |
31 | } |
32 | |
33 | function set_def($init_info = null) { |
34 | if (is_array($init_info) && isset($init_info['file'])) { |
35 | // we have an array in the format we like to have |
36 | $iinfo =& $init_info; |
37 | } |
38 | else { |
39 | // we have something else (XML data?), try to generate the iinfo aray from it |
40 | $iinfo =& $init_info; |
41 | } |
42 | |
43 | if (!isset($iinfo['file'])) { return false; } |
44 | |
45 | $this->rrd_file = $iinfo['file']; |
46 | |
47 | // fields (data sources, DS) |
48 | // name - DS name |
49 | // type - one of COUNTER, GAUGE, DERIVE, ABSOLUTE |
50 | // heartbeat - if no sample recieved for that time, store UNKNOWN |
51 | // min - U (unconstrained) or minimum value |
52 | // max - U (unconstrained) or maximum value |
53 | // update - this string will be fed into eval() for updating this field |
54 | if (isset($iinfo['fields']) && is_array($iinfo['fields'])) { |
55 | $this->rrd_fields = $iinfo['fields']; |
56 | } |
57 | else { |
58 | $this->rrd_fields[] = array('name' => 'ds0', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U'); |
59 | $this->rrd_fields[] = array('name' => 'ds1', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U'); |
60 | } |
61 | |
62 | |
63 | // MRTG-style RRD "database", see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/tut/rrdtutorial.en.html |
64 | // |
65 | // archives (RRAs): |
66 | // 600 samples of 5 minutes (2 days and 2 hours) |
67 | // 700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days) |
68 | // 775 samples of 2 hours (above + 50 days) |
69 | // 797 samples of 1 day (above + 732 days, rounded up to 797) |
70 | |
71 | $this->rrd_step = isset($iinfo['rrd_step'])?$iinfo['rrd_step']:300; |
72 | |
73 | if (isset($iinfo['rra_base']) && is_array($iinfo['rra_base'])) { |
74 | $this->rra_base = $iinfo['rra_base']; |
75 | } |
76 | else { |
77 | $this->rra_base[] = array('step' => 1, 'rows' => 600); |
78 | $this->rra_base[] = array('step' => 6, 'rows' => 700); |
79 | $this->rra_base[] = array('step' => 24, 'rows' => 775); |
80 | $this->rra_base[] = array('step' => 288, 'rows' => 797); |
81 | } |
82 | |
83 | $this->rra_add_max = isset($iinfo['rra_add_max'])?$iinfo['rra_add_max']:true; |
84 | } |
85 | |
86 | function create() { |
87 | // create RRD file |
88 | |
89 | // compose create command |
90 | $create_cmd = 'rrdtool create '.$this->rrd_file.' --step '.$this->rrd_step; |
91 | foreach ($this->rrd_fields as $ds) { |
92 | if (!isset($ds['type'])) { $ds['type'] = 'COUNTER'; } |
93 | if (!isset($ds['heartbeat'])) { $ds['heartbeat'] = 2*$this->rrd_step; } |
94 | if (!isset($ds['min'])) { $ds['min'] = 'U'; } |
95 | if (!isset($ds['max'])) { $ds['max'] = 'U'; } |
96 | $create_cmd .= ' DS:'.$ds['name'].':'.$ds['type'].':'.$ds['heartbeat'].':'.$ds['min'].':'.$ds['max']; |
97 | } |
98 | foreach ($this->rra_base as $rra) { |
99 | if (!isset($rra['cf'])) { $rra['cf'] = 'AVERAGE'; } |
100 | if (!isset($rra['xff'])) { $rra['xff'] = 0.5; } |
101 | if (!isset($rra['step'])) { $rra['step'] = 1; } |
102 | if (!isset($rra['rows'])) { $rra['rows'] = 600; } |
103 | $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows']; |
104 | } |
105 | if ($this->rra_add_max) { |
106 | foreach ($this->rra_base as $rra) { |
107 | if (!isset($rra['cf'])) { |
108 | // only rows that have no CF set will be looked at here |
109 | $rra['cf'] = 'MAX'; |
110 | if (!isset($rra['xff'])) { $rra['xff'] = 0.5; } |
111 | if (!isset($rra['step'])) { $rra['step'] = 1; } |
112 | if (!isset($rra['rows'])) { $rra['rows'] = 600; } |
113 | $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows']; |
114 | } |
115 | } |
116 | } |
117 | $output = array(); $return_var = null; |
118 | exec($create_cmd, $output, $return_var); |
119 | if ($return_var) { trigger_error('rrd create returned with value '.$return_var, E_USER_WARNING); } |
120 | else { $this->status = 'ok'; } |
121 | } |
122 | |
123 | function update($upArray = null) { |
124 | // feed new data into RRD |
125 | if ($this->status != 'ok') { trigger_error('Cannot update non-writeable file', E_USER_WARNING); return 1; } |
126 | $upvals = array(); |
127 | foreach($this->rrd_fields as $ds) { |
128 | if (is_array($upArray) && isset($upArray[$ds['name']])) { $val = $upArray[$ds['name']]; } |
129 | elseif (isset($ds['update'])) { $val = eval($ds['update']); } |
130 | else { $val = null; } |
131 | $upvals[] = $val; |
132 | } |
133 | $update_cmd = 'rrdtool update '.$this->rrd_file.' N:'.implode(':', $upvals); |
134 | $output = array(); $return_var = null; |
135 | exec($update_cmd, $output, $return_var); |
136 | if ($return_var) { trigger_error('rrd update returned with value '.$return_var, E_USER_WARNING); } |
137 | return ($return_var == 0); |
138 | } |
139 | |
140 | function graph($filename, $timeframe, $extra = null) { |
141 | // create an RRD graph |
142 | static $gColors; |
143 | |
144 | if (!isset($gColors)) { |
145 | $gColors = array('#00CC00','#0000FF','#000000','#FF0000','#00FF00','#FFFF00','#FF00FF','#00FFFF','#808080','#800000','#008000','#000080','#808000','#800080','#008080','#C0C0C0'); |
146 | } |
147 | |
148 | if (is_null($filename) || !strlen($filename)) { $filename = str_replace('.rrd', '-%tf.png', $this->rrd_file); } |
149 | $fname = str_replace('%tf', $timeframe, $filename); |
150 | if (substr($fname, -4) != '.png') { $fname .= '.png'; } |
151 | |
b7878f4d |
152 | $graphrows = array(); $gC = 0; |
4ba56977 |
153 | $gDefs = ''; $gGraphs = ''; $addSpecial = ''; |
154 | |
155 | if ($timeframe == 'day') { |
156 | $duration = isset($extra['duration'])?$extra['duration']:30*3600; // 30 hours |
157 | $slice = isset($extra['slice'])?$extra['slice']:300; // 5 minutes |
158 | // vertical lines at day borders |
159 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d')).'#FF0000'; |
160 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' -1 day').'#FF0000'; |
5f42eda6 |
161 | if (!isset($extra['grid_x'])) { $extra['grid_x'] = 'HOUR:1:HOUR:6:HOUR:2:0:%-H'; } |
4ba56977 |
162 | } |
163 | elseif ($timeframe == 'week') { |
164 | $duration = isset($extra['duration'])?$extra['duration']:8*86400; // 8 days |
165 | $slice = isset($extra['slice'])?$extra['slice']:1800; // 30 minutes |
166 | // vertical lines at week borders |
167 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')+1).' day').'#FF0000'; |
168 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')-6).' day').'#FF0000'; |
169 | } |
170 | elseif ($timeframe == 'month') { |
171 | $duration = isset($extra['duration'])?$extra['duration']:36*86400; // 36 days |
172 | $slice = isset($extra['slice'])?$extra['slice']:7200; // 2 hours |
173 | // vertical lines at month borders |
174 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01')).'#FF0000'; |
175 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01').' -1 month').'#FF0000'; |
176 | } |
177 | elseif ($timeframe == 'year') { |
178 | $duration = isset($extra['duration'])?$extra['duration']:396*86400; // 365+31 days |
179 | $slice = isset($extra['slice'])?$extra['slice']:86400; // 1 day |
180 | // vertical lines at month borders |
181 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01')).'#FF0000'; |
182 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01').' -1 year').'#FF0000'; |
183 | } |
184 | else { |
185 | $duration = isset($extra['duration'])?$extra['duration']:$this->rrd_step*500; // 500 steps |
186 | $slice = isset($extra['slice'])?$extra['slice']:$this->rrd_step; // whatever our step is |
187 | } |
188 | |
b7878f4d |
189 | if (isset($extra['rows']) && count($extra['rows'])) { |
190 | foreach ($extra['rows'] as $erow) { |
191 | if (isset($erow['name']) && strlen($erow['name'])) { |
5f42eda6 |
192 | if (!isset($erow['scale']) && isset($extra['scale'])) { $erow['scale'] = $extra['scale']; } |
b7878f4d |
193 | $grow = array(); |
194 | $grow['dType'] = isset($erow['dType'])?$erow['dType']:'DEF'; |
5f42eda6 |
195 | $grow['name'] = $erow['name'].(isset($erow['scale'])?'_tmp':''); |
b7878f4d |
196 | $grow['dsname'] = isset($erow['dsname'])?$erow['dsname']:$erow['name']; |
197 | $grow['cf'] = isset($erow['cf'])?$erow['cf']:'AVERAGE'; |
5f42eda6 |
198 | if (isset($erow['rpn_expr'])) { $grow['rpn_expr'] = $erow['rpn_expr']; } |
199 | if (isset($erow['scale'])) { |
200 | $graphrows[] = $grow; |
201 | $grow = array(); |
202 | $grow['dType'] = 'CDEF'; |
203 | $grow['name'] = $erow['name']; |
204 | $grow['rpn_expr'] = $erow['name'].'_tmp,'.$erow['scale'].',*'; |
205 | } |
b7878f4d |
206 | $grow['gType'] = isset($erow['gType'])?$erow['gType']:'LINE1'; |
207 | $grow['color'] = isset($erow['color'])?$erow['color']:$gColors[$gC++]; |
208 | if ($gC >= count($gColors)) { $gC = 0; } |
4ba56977 |
209 | if (isset($erow['legend'])) { |
210 | $grow['legend'] = $erow['legend']; |
211 | if (!isset($extra['show_legend'])) { $extra['show_legend'] = true; } |
212 | } |
b7878f4d |
213 | if (isset($erow['stack'])) { $grow['stack'] = ($erow['stack'] == true); } |
214 | $graphrows[] = $grow; |
215 | } |
216 | } |
217 | } |
218 | else { |
219 | foreach ($this->rrd_fields as $ds) { |
220 | $grow = array(); |
221 | $grow['dType'] = 'DEF'; |
5f42eda6 |
222 | $grow['name'] = $ds['name'].(isset($extra['scale'])?'_tmp':''); |
b7878f4d |
223 | $grow['dsname'] = $ds['name']; |
224 | $grow['cf'] = 'AVERAGE'; |
5f42eda6 |
225 | if (isset($extra['scale'])) { |
226 | $graphrows[] = $grow; |
227 | $grow = array(); |
228 | $grow['dType'] = 'CDEF'; |
229 | $grow['name'] = $ds['name']; |
230 | $grow['rpn_expr'] = $ds['name'].'_tmp,'.$extra['scale'].',*'; |
231 | } |
b7878f4d |
232 | $grow['gType'] = ($ds['name']=='ds0')?'AREA':'LINE1'; |
233 | $grow['color'] = $gColors[$gC++]; if ($gC >= count($gColors)) { $gC = 0; } |
234 | $graphrows[] = $grow; |
235 | } |
236 | } |
237 | |
4ba56977 |
238 | $gOpts = ' --start '.($this->last_update()-$duration).' --end '.$this->last_update().' --step '.$slice; |
5f42eda6 |
239 | if (isset($extra['label_top'])) { $gOpts .= ' --title '.$this->text_quote($extra['label_top']); } |
240 | if (isset($extra['label_y'])) { $gOpts .= ' --vertical-label '.$this->text_quote($extra['label_y']); } |
4ba56977 |
241 | if (isset($extra['width'])) { $gOpts .= ' --width '.$extra['width']; } |
242 | if (isset($extra['height'])) { $gOpts .= ' --height '.$extra['height']; |
243 | if (($extra['height'] <= 32) && isset($extra['thumb']) && ($extra['thumb'])) { $gOpts .= ' --only-graph'; } |
244 | } |
245 | if (!isset($extra['show_legend']) || (!$extra['show_legend'])) { $gOpts .= ' --no-legend'; } |
246 | if (isset($extra['min_y'])) { $gOpts .= ' --lower-limit '.$extra['min_y']; } |
247 | if (isset($extra['max_y'])) { $gOpts .= ' --upper-limit '.$extra['max_y']; } |
248 | if (isset($extra['fix_scale_y']) && $extra['fix_scale_y']) { $gOpts .= ' --rigid'; } |
249 | if (isset($extra['grid_x'])) { $gOpts .= ' --x-grid '.$extra['grid_x']; } |
250 | if (isset($extra['grid_y'])) { $gOpts .= ' --y-grid '.$extra['grid_y']; } |
251 | if (isset($extra['units_exponent'])) { $gOpts .= ' --units-exponent '.$extra['units_exponent']; } |
252 | if (isset($extra['units_length'])) { $gOpts .= ' --units-length '.$extra['units_length']; } |
253 | if (!isset($extra['force_recreate']) || (!$extra['force_recreate'])) { $gOpts .= ' --lazy'; } |
254 | if (isset($extra['force_color']) && is_array($extra['force_color'])) { |
255 | foreach ($extra['force_color'] as $ctag=>$cval) { $gOpts .= ' --color '.$ctag.$cval; } |
256 | } |
257 | if (isset($extra['force_font']) && is_array($extra['force_font'])) { |
258 | foreach ($extra['force_font'] as $ctag=>$cval) { $gOpts .= ' --font '.$ctag.$cval; } |
259 | } |
260 | if (isset($extra['units_binary']) && $extra['units_binary']) { $gOpts .= ' --base 1024'; } |
261 | |
b7878f4d |
262 | foreach ($graphrows as $grow) { |
4ba56977 |
263 | if (isset($grow['dType']) && strlen($grow['dType'])) { |
264 | $gDefs .= ' '.$grow['dType'].':'.$grow['name'].'='; |
265 | $gDefs .= ($grow['dType']=='DEF')?$this->rrd_file.':'.$grow['dsname'].':'.$grow['cf']:$grow['rpn_expr']; |
266 | } |
267 | if (isset($grow['gType']) && strlen($grow['gType'])) { |
268 | // XXX: current rrdtools version doesn't support STACK flag, only STACK type |
269 | if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; } |
270 | $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color']; |
271 | if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); } |
272 | // XXX: when STACK flag is supported, remove above STACK if-command and uncomment the one below |
273 | //if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; } |
274 | } |
b7878f4d |
275 | } |
276 | |
5f42eda6 |
277 | $graph_cmd = 'rrdtool graph '.str_replace('*', '\*', $fname.$gOpts.$gDefs.$gGraphs.$addSpecial); |
b7878f4d |
278 | $return = `$graph_cmd 2>&1`; |
279 | |
280 | $retval = 0; |
281 | if (strpos($return, 'ERROR') !== false) { |
282 | trigger_error('rrd graph error: '.$return, E_USER_WARNING); |
283 | $retval = 1; |
284 | } |
285 | return $retval; |
286 | } |
287 | |
288 | function simple_html($page_extras = null, $graph_extras = null) { |
289 | // create a simple (MRTG-like) HTML page and return it in a string |
290 | $basename = str_replace('.rrd', '', $this->rrd_file); |
291 | $out = '<html><head><title>'.$basename.' - RRD statistics</title></head><body>'; |
292 | |
4ba56977 |
293 | $out .= '<p>Last Update: '.date('Y-m-d H:i:s', $this->last_update()).'</p>'; |
294 | |
b7878f4d |
295 | if (in_array($this->status, array('ok','readonly'))) { |
296 | foreach (array('day','week','month','year') as $tframe) { |
297 | $this->graph(null, $tframe, $graph_extras); |
298 | $out .= '<p><img src="'.$basename.'-'.$tframe.'.png"></p>'; |
299 | } |
300 | } |
301 | else { |
302 | $out .= 'RRD error: status is "'.$this->status.'"'; |
303 | } |
304 | |
305 | $ou .= '</body></html>'; |
306 | return $out; |
307 | } |
308 | |
4ba56977 |
309 | function last_update() { |
310 | // fetch time of last update in this RRD file |
311 | static $last_update; |
312 | if (!isset($last_update) && in_array($this->status, array('ok','readonly'))) { |
313 | $last_cmd = 'rrdtool last '.$this->rrd_file; |
314 | $return = trim(`$last_cmd 2>&1`); |
315 | $last_update = is_numeric($return)?$return:null; |
316 | } |
317 | return isset($last_update)?$last_update:null; |
318 | } |
319 | |
b7878f4d |
320 | function text_quote($text) { return '"'.str_replace('"', '\"', str_replace(':', '\:', $text)).'"'; } |
321 | } |
322 | ?> |