b7878f4d |
1 | <?php |
2 | // ************ RRD status class ************** |
3 | class rrdstat { |
4 | |
2a386f5a |
5 | var $rrd_file = null; |
b7878f4d |
6 | |
a039a75c |
7 | var $config_raw = null; |
8 | var $config_graph = null; |
9 | var $config_page = null; |
10 | |
b7878f4d |
11 | var $rrd_fields = array(); |
12 | var $rra_base = array(); |
13 | var $rrd_step = 300; |
14 | var $rra_add_max = true; |
15 | |
16 | var $status = 'unused'; |
17 | |
ebe03325 |
18 | function rrdstat($rrdconfig, $conf_id = null) { |
b7878f4d |
19 | // ***** init RRD stat module ***** |
ebe03325 |
20 | $this->set_def($rrdconfig, $conf_id); |
b7878f4d |
21 | |
2a386f5a |
22 | if (!is_null($this->rrd_file)) { |
23 | if (!is_writeable($this->rrd_file)) { |
24 | if (!file_exists($this->rrd_file)) { |
25 | if (touch($this->rrd_file)) { $this->create(); } |
26 | else { trigger_error('RRD file can not be created', E_USER_WARNING); } |
27 | } |
28 | else { |
29 | if (is_readable($this->rrd_file)) { $this->status = 'readonly'; } |
30 | else { trigger_error('RRD file is not readable', E_USER_WARNING); } |
31 | } |
b7878f4d |
32 | } |
33 | else { |
2a386f5a |
34 | $this->status = 'ok'; |
b7878f4d |
35 | } |
36 | } |
b7878f4d |
37 | } |
38 | |
ebe03325 |
39 | function set_def($rrdconfig, $conf_id = null) { |
40 | if (is_array($rrdconfig)) { |
b7878f4d |
41 | // we have an array in the format we like to have |
ebe03325 |
42 | $complete_conf =& $rrdconfig; |
b7878f4d |
43 | } |
44 | else { |
45 | // we have something else (XML data?), try to generate the iinfo aray from it |
ebe03325 |
46 | $complete_conf =& $rrdconfig; |
47 | } |
48 | |
49 | if (!is_null($conf_id)) { |
50 | $iinfo = isset($complete_conf[$conf_id])?$complete_conf[$conf_id]:array(); |
e1727e7f |
51 | if (isset($complete_conf['*'])) { |
52 | $iinfo = (array)$iinfo + (array)$complete_conf['*']; |
53 | if (isset($complete_conf['*']['graph'])) { $iinfo['graph'] = (array)$iinfo['graph'] + (array)$complete_conf['*']['graph']; } |
54 | if (isset($complete_conf['*']['page'])) { $iinfo['page'] = (array)$iinfo['page'] + (array)$complete_conf['*']['page']; } |
55 | } |
ebe03325 |
56 | } |
57 | else { |
58 | $iinfo = $complete_conf; |
b7878f4d |
59 | } |
60 | |
61 | if (!isset($iinfo['file'])) { return false; } |
62 | |
63 | $this->rrd_file = $iinfo['file']; |
64 | |
65 | // fields (data sources, DS) |
66 | // name - DS name |
67 | // type - one of COUNTER, GAUGE, DERIVE, ABSOLUTE |
68 | // heartbeat - if no sample recieved for that time, store UNKNOWN |
69 | // min - U (unconstrained) or minimum value |
70 | // max - U (unconstrained) or maximum value |
71 | // update - this string will be fed into eval() for updating this field |
72 | if (isset($iinfo['fields']) && is_array($iinfo['fields'])) { |
73 | $this->rrd_fields = $iinfo['fields']; |
74 | } |
75 | else { |
76 | $this->rrd_fields[] = array('name' => 'ds0', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U'); |
77 | $this->rrd_fields[] = array('name' => 'ds1', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U'); |
78 | } |
79 | |
80 | |
81 | // MRTG-style RRD "database", see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/tut/rrdtutorial.en.html |
82 | // |
83 | // archives (RRAs): |
84 | // 600 samples of 5 minutes (2 days and 2 hours) |
85 | // 700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days) |
86 | // 775 samples of 2 hours (above + 50 days) |
87 | // 797 samples of 1 day (above + 732 days, rounded up to 797) |
88 | |
89 | $this->rrd_step = isset($iinfo['rrd_step'])?$iinfo['rrd_step']:300; |
90 | |
91 | if (isset($iinfo['rra_base']) && is_array($iinfo['rra_base'])) { |
92 | $this->rra_base = $iinfo['rra_base']; |
93 | } |
94 | else { |
95 | $this->rra_base[] = array('step' => 1, 'rows' => 600); |
96 | $this->rra_base[] = array('step' => 6, 'rows' => 700); |
97 | $this->rra_base[] = array('step' => 24, 'rows' => 775); |
98 | $this->rra_base[] = array('step' => 288, 'rows' => 797); |
99 | } |
100 | |
101 | $this->rra_add_max = isset($iinfo['rra_add_max'])?$iinfo['rra_add_max']:true; |
a039a75c |
102 | |
103 | if (isset($iinfo['graph'])) { $this->config_graph = $iinfo['graph']; } |
104 | if (isset($iinfo['page'])) { $this->config_page = $iinfo['page']; } |
105 | $this->config_raw = $iinfo; |
b7878f4d |
106 | } |
107 | |
108 | function create() { |
109 | // create RRD file |
110 | |
111 | // compose create command |
112 | $create_cmd = 'rrdtool create '.$this->rrd_file.' --step '.$this->rrd_step; |
113 | foreach ($this->rrd_fields as $ds) { |
114 | if (!isset($ds['type'])) { $ds['type'] = 'COUNTER'; } |
115 | if (!isset($ds['heartbeat'])) { $ds['heartbeat'] = 2*$this->rrd_step; } |
116 | if (!isset($ds['min'])) { $ds['min'] = 'U'; } |
117 | if (!isset($ds['max'])) { $ds['max'] = 'U'; } |
118 | $create_cmd .= ' DS:'.$ds['name'].':'.$ds['type'].':'.$ds['heartbeat'].':'.$ds['min'].':'.$ds['max']; |
119 | } |
120 | foreach ($this->rra_base as $rra) { |
121 | if (!isset($rra['cf'])) { $rra['cf'] = 'AVERAGE'; } |
122 | if (!isset($rra['xff'])) { $rra['xff'] = 0.5; } |
123 | if (!isset($rra['step'])) { $rra['step'] = 1; } |
124 | if (!isset($rra['rows'])) { $rra['rows'] = 600; } |
125 | $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows']; |
126 | } |
127 | if ($this->rra_add_max) { |
128 | foreach ($this->rra_base as $rra) { |
129 | if (!isset($rra['cf'])) { |
130 | // only rows that have no CF set will be looked at here |
131 | $rra['cf'] = 'MAX'; |
132 | if (!isset($rra['xff'])) { $rra['xff'] = 0.5; } |
133 | if (!isset($rra['step'])) { $rra['step'] = 1; } |
134 | if (!isset($rra['rows'])) { $rra['rows'] = 600; } |
135 | $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows']; |
136 | } |
137 | } |
138 | } |
139 | $output = array(); $return_var = null; |
140 | exec($create_cmd, $output, $return_var); |
141 | if ($return_var) { trigger_error('rrd create returned with value '.$return_var, E_USER_WARNING); } |
142 | else { $this->status = 'ok'; } |
143 | } |
144 | |
145 | function update($upArray = null) { |
146 | // feed new data into RRD |
147 | if ($this->status != 'ok') { trigger_error('Cannot update non-writeable file', E_USER_WARNING); return 1; } |
148 | $upvals = array(); |
de093632 |
149 | if (isset($this->config_raw['update'])) { |
150 | $evalcode = $this->config_raw['update']; |
151 | if (!is_null($evalcode)) { |
152 | ob_start(); |
153 | eval($evalcode); |
154 | $upvals = explode("\n", ob_get_contents()); |
155 | ob_end_clean(); |
156 | } |
157 | } |
158 | else { |
159 | foreach ($this->rrd_fields as $ds) { |
160 | if (is_array($upArray) && isset($upArray[$ds['name']])) { $val = $upArray[$ds['name']]; } |
161 | elseif (isset($ds['update'])) { |
162 | $val = null; $evalcode = null; |
163 | if (substr($ds['update'], 0, 4) == 'val:') { |
164 | $evalcode = 'print(trim('.substr($ds['update'], 4).'));'; |
c5db3bd5 |
165 | } |
de093632 |
166 | elseif (substr($ds['update'], 0, 8) == 'snmp-if:') { |
167 | $snmphost = 'localhost'; $snmpcomm = 'public'; |
168 | list($nix, $ifname, $valtype) = explode(':', $ds['update'], 3); |
169 | $iflist = explode("\n", `snmpwalk -v2c -c $snmpcomm $snmphost interfaces.ifTable.ifEntry.ifDescr`); |
170 | $ifnr = null; |
171 | foreach ($iflist as $ifdesc) { |
172 | if (preg_match('/ifDescr\.(\d+) = STRING: '.$ifname.'/', $ifdesc, $regs)) { $ifnr = $regs[1]; } |
173 | } |
174 | $oid = null; |
175 | if ($valtype == 'in') { $oid = '1.3.6.1.2.1.2.2.1.10.'.$ifnr; } |
176 | elseif ($valtype == 'out') { $oid = '1.3.6.1.2.1.2.2.1.16.'.$ifnr; } |
177 | if (!is_null($ifnr) && !is_null($oid)) { |
178 | $evalcode = 'print(trim(substr(strrchr(`snmpget -v2c -c '.$snmpcomm.' '.$snmphost.' '.$oid.'`,":"),1)));'; |
179 | } |
180 | } |
181 | else { $evalcode = $ds['update']; } |
182 | if (!is_null($evalcode)) { |
183 | ob_start(); |
184 | eval($evalcode); |
185 | $val = ob_get_contents(); |
186 | ob_end_clean(); |
c5db3bd5 |
187 | } |
188 | } |
de093632 |
189 | else { $val = null; } |
190 | $upvals[] = is_null($val)?'U':$val; |
c5db3bd5 |
191 | } |
b7878f4d |
192 | } |
193 | $update_cmd = 'rrdtool update '.$this->rrd_file.' N:'.implode(':', $upvals); |
194 | $output = array(); $return_var = null; |
195 | exec($update_cmd, $output, $return_var); |
196 | if ($return_var) { trigger_error('rrd update returned with value '.$return_var, E_USER_WARNING); } |
197 | return ($return_var == 0); |
198 | } |
199 | |
a039a75c |
200 | function fetch($cf = 'AVERAGE', $resolution = null, $start = null, $end = null) { |
201 | // fetch data from a RRD |
202 | if (!in_array($this->status, array('ok','readonly'))) { trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false; } |
203 | |
204 | if (!in_array($cf, array('AVERAGE','MIN','MAX','LAST'))) { $cf = 'AVERAGE'; } |
205 | if (!is_numeric($resolution)) { $resolution = $this->rrd_step; } |
206 | if (!is_numeric($end)) { $end = $this->last_update(); } |
207 | elseif ($end < 0) { $end += $this->last_update(); } |
208 | $end = intval($end/$resolution)*$resolution; |
209 | if (!is_numeric($start)) { $start = $end; } |
210 | elseif ($start < 0) { $start += $end; } |
211 | $start = intval($start/$resolution)*$resolution; |
212 | |
213 | $fetch_cmd = 'rrdtool fetch '.$this->rrd_file.' '.$cf.' --resolution '.$resolution.' --start '.$start.' --end '.$end; |
214 | $return = `$fetch_cmd 2>&1`; |
215 | |
216 | if (strpos($return, 'ERROR') !== false) { |
217 | trigger_error('rrd fetch error: '.$return, E_USER_WARNING); |
218 | $fresult = false; |
219 | } |
220 | else { |
221 | $fresult = array(); |
222 | $rows = explode("\n", $return); |
223 | $fields = preg_split('/\s+/', array_shift($rows)); |
224 | if (array_shift($fields) == 'timestamp') { |
225 | $fresult[0] = $fields; |
226 | foreach ($rows as $row) { |
227 | if (strlen(trim($row))) { |
228 | $rvals = preg_split('/\s+/', $row); |
229 | $rtime = array_shift($rvals); |
230 | $rv_array = array(); |
231 | foreach ($rvals as $key=>$rval) { |
232 | $rv_array[$fields[$key]] = ($rval=='nan')?null:floatval($rval); |
233 | } |
234 | $fresult[$rtime] = $rv_array; |
235 | } |
236 | } |
237 | } |
238 | } |
239 | return $fresult; |
240 | } |
b7878f4d |
241 | |
2c30ff69 |
242 | function graph($timeframe = 'day', $sub = null, $extra = null) { |
a039a75c |
243 | // create a RRD graph |
244 | static $gColors; |
b7878f4d |
245 | if (!isset($gColors)) { |
246 | $gColors = array('#00CC00','#0000FF','#000000','#FF0000','#00FF00','#FFFF00','#FF00FF','#00FFFF','#808080','#800000','#008000','#000080','#808000','#800080','#008080','#C0C0C0'); |
247 | } |
248 | |
a039a75c |
249 | if (!in_array($this->status, array('ok','readonly'))) { trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false; } |
250 | |
251 | // assemble configuration |
e1727e7f |
252 | $gconf = (array)$extra; |
2c30ff69 |
253 | if (!is_null($sub) && is_array($this->config_raw['graph.'.$sub])) { |
e1727e7f |
254 | $gconf = $gconf + $this->config_raw['graph.'.$sub]; |
a039a75c |
255 | } |
e1727e7f |
256 | $gconf = $gconf + (array)$this->config_graph; |
a039a75c |
257 | |
258 | if (isset($gconf['format']) && ($gconf['format'] == 'SVG')) { |
259 | $format = $gconf['format']; $fmt_ext = '.svg'; |
260 | } |
261 | elseif (isset($gconf['format']) && ($gconf['format'] == 'EPS')) { |
262 | $format = $gconf['format']; $fmt_ext = '.eps'; |
263 | } |
264 | elseif (isset($gconf['format']) && ($gconf['format'] == 'PDF')) { |
265 | $format = $gconf['format']; $fmt_ext = '.pdf'; |
266 | } |
267 | else { |
268 | $format = 'PNG'; $fmt_ext = '.png'; |
269 | } |
270 | |
271 | if (isset($gconf['filename'])) { $fname = $gconf['filename']; } |
2c30ff69 |
272 | else { $fname = str_replace('.rrd', (is_null($sub)?'':'-%s').'-%t%f', $this->rrd_file); } |
273 | $fname = str_replace('%s', strval($sub), $fname); |
a039a75c |
274 | $fname = str_replace('%t', $timeframe, $fname); |
275 | $fname = str_replace('%f', $fmt_ext, $fname); |
276 | if (substr($fname, -strlen($fmt_ext)) != $fmt_ext) { $fname .= $fmt_ext; } |
ebe03325 |
277 | if (isset($gconf['path'])) { $fname = $gconf['path'].'/'.$fname; } |
278 | $fname = str_replace('//', '/', $fname); |
b7878f4d |
279 | |
b7878f4d |
280 | $graphrows = array(); $gC = 0; |
4ba56977 |
281 | $gDefs = ''; $gGraphs = ''; $addSpecial = ''; |
282 | |
283 | if ($timeframe == 'day') { |
a039a75c |
284 | $duration = isset($gconf['duration'])?$gconf['duration']:30*3600; // 30 hours |
285 | $slice = isset($gconf['slice'])?$gconf['slice']:300; // 5 minutes |
4ba56977 |
286 | // vertical lines at day borders |
287 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d')).'#FF0000'; |
288 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' -1 day').'#FF0000'; |
a039a75c |
289 | if (!isset($gconf['grid_x'])) { $gconf['grid_x'] = 'HOUR:1:HOUR:6:HOUR:2:0:%-H'; } |
4ba56977 |
290 | } |
291 | elseif ($timeframe == 'week') { |
a039a75c |
292 | $duration = isset($gconf['duration'])?$gconf['duration']:8*86400; // 8 days |
293 | $slice = isset($gconf['slice'])?$gconf['slice']:1800; // 30 minutes |
4ba56977 |
294 | // vertical lines at week borders |
295 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')+1).' day').'#FF0000'; |
296 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')-6).' day').'#FF0000'; |
297 | } |
298 | elseif ($timeframe == 'month') { |
a039a75c |
299 | $duration = isset($gconf['duration'])?$gconf['duration']:36*86400; // 36 days |
300 | $slice = isset($gconf['slice'])?$gconf['slice']:7200; // 2 hours |
4ba56977 |
301 | // vertical lines at month borders |
302 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01')).'#FF0000'; |
303 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01').' -1 month').'#FF0000'; |
304 | } |
305 | elseif ($timeframe == 'year') { |
a039a75c |
306 | $duration = isset($gconf['duration'])?$gconf['duration']:396*86400; // 365+31 days |
307 | $slice = isset($gconf['slice'])?$gconf['slice']:86400; // 1 day |
4ba56977 |
308 | // vertical lines at month borders |
309 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01')).'#FF0000'; |
310 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01').' -1 year').'#FF0000'; |
311 | } |
312 | else { |
a039a75c |
313 | $duration = isset($gconf['duration'])?$gconf['duration']:$this->rrd_step*500; // 500 steps |
314 | $slice = isset($gconf['slice'])?$gconf['slice']:$this->rrd_step; // whatever our step is |
4ba56977 |
315 | } |
316 | |
a039a75c |
317 | if (isset($gconf['rows']) && count($gconf['rows'])) { |
318 | foreach ($gconf['rows'] as $erow) { |
b7878f4d |
319 | if (isset($erow['name']) && strlen($erow['name'])) { |
a039a75c |
320 | if (!isset($erow['scale']) && isset($gconf['scale'])) { $erow['scale'] = $gconf['scale']; } |
b7878f4d |
321 | $grow = array(); |
322 | $grow['dType'] = isset($erow['dType'])?$erow['dType']:'DEF'; |
5f42eda6 |
323 | $grow['name'] = $erow['name'].(isset($erow['scale'])?'_tmp':''); |
16cc643c |
324 | if ($grow['dType'] == 'DEF') { |
325 | $grow['dsname'] = isset($erow['dsname'])?$erow['dsname']:$erow['name']; |
326 | $grow['cf'] = isset($erow['cf'])?$erow['cf']:'AVERAGE'; |
327 | } |
328 | else { |
329 | $grow['rpn_expr'] = isset($erow['rpn_expr'])?$erow['rpn_expr']:'0'; |
330 | } |
5f42eda6 |
331 | if (isset($erow['scale'])) { |
332 | $graphrows[] = $grow; |
333 | $grow = array(); |
334 | $grow['dType'] = 'CDEF'; |
335 | $grow['name'] = $erow['name']; |
336 | $grow['rpn_expr'] = $erow['name'].'_tmp,'.$erow['scale'].',*'; |
337 | } |
b7878f4d |
338 | $grow['gType'] = isset($erow['gType'])?$erow['gType']:'LINE1'; |
339 | $grow['color'] = isset($erow['color'])?$erow['color']:$gColors[$gC++]; |
340 | if ($gC >= count($gColors)) { $gC = 0; } |
4ba56977 |
341 | if (isset($erow['legend'])) { |
342 | $grow['legend'] = $erow['legend']; |
a039a75c |
343 | if (!isset($gconf['show_legend'])) { $gconf['show_legend'] = true; } |
4ba56977 |
344 | } |
b7878f4d |
345 | if (isset($erow['stack'])) { $grow['stack'] = ($erow['stack'] == true); } |
346 | $graphrows[] = $grow; |
347 | } |
348 | } |
349 | } |
350 | else { |
e920ca68 |
351 | foreach ($this->rrd_fields as $key=>$ds) { |
b7878f4d |
352 | $grow = array(); |
353 | $grow['dType'] = 'DEF'; |
a039a75c |
354 | $grow['name'] = $ds['name'].(isset($gconf['scale'])?'_tmp':''); |
b7878f4d |
355 | $grow['dsname'] = $ds['name']; |
356 | $grow['cf'] = 'AVERAGE'; |
a039a75c |
357 | if (isset($gconf['scale'])) { |
5f42eda6 |
358 | $graphrows[] = $grow; |
359 | $grow = array(); |
360 | $grow['dType'] = 'CDEF'; |
361 | $grow['name'] = $ds['name']; |
a039a75c |
362 | $grow['rpn_expr'] = $ds['name'].'_tmp,'.$gconf['scale'].',*'; |
5f42eda6 |
363 | } |
e920ca68 |
364 | $grow['gType'] = ((count($this->rrd_fields)==2) && ($key==0))?'AREA':'LINE1'; |
b7878f4d |
365 | $grow['color'] = $gColors[$gC++]; if ($gC >= count($gColors)) { $gC = 0; } |
366 | $graphrows[] = $grow; |
367 | } |
368 | } |
369 | |
16cc643c |
370 | if (isset($gconf['special']) && count($gconf['special'])) { |
371 | foreach ($gconf['special'] as $crow) { |
372 | $srow = array(); |
373 | $srow['sType'] = isset($crow['sType'])?$crow['sType']:'COMMENT'; |
374 | if ($grow['sType'] != 'COMMENT') { |
375 | // XXX: use line below and remove cf var once we have rrdtol 1.2 |
376 | // $srow['name'] = $crow['name'].(isset($crow['cf'])?'_'.$crow['cf']:''); |
377 | $srow['name'] = $crow['name']; |
378 | $srow['cf'] = isset($crow['cf'])?$crow['cf']:'AVERAGE'; |
379 | if (isset($crow['cf'])) { |
380 | // XXX: use line below once we have rrdtol 1.2 |
381 | // $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'].'_'.$crow['cf'], 'rpn_expr'=>$srow['name'].','.$crow['cf']); |
382 | } |
383 | elseif (isset($crow['rpn_expr'])) { |
384 | // XXX: does only work with rrdtool 1.2 |
385 | $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'], 'rpn_expr'=>$crow['rpn_expr']); |
386 | } |
387 | } |
388 | $srow['text'] = isset($crow['text'])?$crow['text']:''; |
389 | $specialrows[] = $srow; |
390 | } |
391 | } |
392 | else { |
393 | foreach ($graphrows as $grow) { |
394 | if (isset($grow['gType']) && strlen($grow['gType'])) { |
395 | $textprefix = isset($grow['legend'])?$grow['legend']:$grow['name']; |
396 | // XXX: use lines below once we have rrdtol 1.2 |
397 | // $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_last', 'rpn_expr'=>$grow['name'].',LAST'); |
398 | // $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_last', 'text'=>'%3.2lf%s'); |
399 | $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'MAX', 'text'=>$textprefix.'|Maximum|%.2lf%s'); |
400 | $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'AVERAGE', 'text'=>$textprefix.'|Average|%.2lf%s'); |
401 | $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'LAST', 'text'=>$textprefix.'|Current|%.2lf%s'); |
402 | } |
403 | } |
404 | } |
405 | |
a039a75c |
406 | $endtime = isset($gconf['time_end'])?$gconf['time_end']:(is_numeric($this->last_update())?$this->last_update():time()); |
407 | $gOpts = ' --start '.($endtime-$duration).' --end '.$endtime.' --step '.$slice; |
408 | if (isset($gconf['label_top'])) { $gOpts .= ' --title '.$this->text_quote($gconf['label_top']); } |
409 | if (isset($gconf['label_y'])) { $gOpts .= ' --vertical-label '.$this->text_quote($gconf['label_y']); } |
410 | if (isset($gconf['width'])) { $gOpts .= ' --width '.$gconf['width']; } |
411 | if (isset($gconf['height'])) { $gOpts .= ' --height '.$gconf['height']; |
412 | if (($gconf['height'] <= 32) && isset($gconf['thumb']) && ($gconf['thumb'])) { $gOpts .= ' --only-graph'; } |
4ba56977 |
413 | } |
a039a75c |
414 | if (!isset($gconf['show_legend']) || (!$gconf['show_legend'])) { $gOpts .= ' --no-legend'; } |
415 | if (isset($gconf['min_y'])) { $gOpts .= ' --lower-limit '.$gconf['min_y']; } |
416 | if (isset($gconf['max_y'])) { $gOpts .= ' --upper-limit '.$gconf['max_y']; } |
417 | if (isset($gconf['fix_scale_y']) && $gconf['fix_scale_y']) { $gOpts .= ' --rigid'; } |
418 | if (isset($gconf['grid_x'])) { $gOpts .= ' --x-grid '.$gconf['grid_x']; } |
419 | if (isset($gconf['grid_y'])) { $gOpts .= ' --y-grid '.$gconf['grid_y']; } |
420 | if (isset($gconf['units_exponent'])) { $gOpts .= ' --units-exponent '.$gconf['units_exponent']; } |
421 | if (isset($gconf['units_length'])) { $gOpts .= ' --units-length '.$gconf['units_length']; } |
422 | if (!isset($gconf['force_recreate']) || (!$gconf['force_recreate'])) { $gOpts .= ' --lazy'; } |
423 | if (isset($gconf['force_color']) && is_array($gconf['force_color'])) { |
424 | foreach ($gconf['force_color'] as $ctag=>$cval) { $gOpts .= ' --color '.$ctag.$cval; } |
4ba56977 |
425 | } |
a039a75c |
426 | if (isset($gconf['force_font']) && is_array($gconf['force_font'])) { |
427 | foreach ($gconf['force_font'] as $ctag=>$cval) { $gOpts .= ' --font '.$ctag.$cval; } |
4ba56977 |
428 | } |
a039a75c |
429 | if (isset($gconf['units_binary']) && $gconf['units_binary']) { $gOpts .= ' --base 1024'; } |
4ba56977 |
430 | |
b7878f4d |
431 | foreach ($graphrows as $grow) { |
4ba56977 |
432 | if (isset($grow['dType']) && strlen($grow['dType'])) { |
433 | $gDefs .= ' '.$grow['dType'].':'.$grow['name'].'='; |
434 | $gDefs .= ($grow['dType']=='DEF')?$this->rrd_file.':'.$grow['dsname'].':'.$grow['cf']:$grow['rpn_expr']; |
435 | } |
436 | if (isset($grow['gType']) && strlen($grow['gType'])) { |
16cc643c |
437 | // XXX: change from STACK type to STACK flag once we have rrdtool 1.2 |
4ba56977 |
438 | if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; } |
439 | $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color']; |
440 | if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); } |
16cc643c |
441 | // XXX: remove above STACK if-command and uncomment the one below once we have rrdtool 1.2 |
4ba56977 |
442 | //if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; } |
443 | } |
b7878f4d |
444 | } |
445 | |
16cc643c |
446 | foreach ($specialrows as $srow) { |
447 | $addSpecial .= ' '.$srow['sType']; |
448 | // XXX: eliminate cf once we have rrdtool 1.2 |
449 | // $addSpecial .= ($grow['sType']!='COMMENT')?':'.$grow['name']:''); |
450 | $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name'].':'.$srow['cf']:''); |
451 | $addSpecial .= ':'.$this->text_quote($srow['text']); |
452 | } |
453 | |
5f42eda6 |
454 | $graph_cmd = 'rrdtool graph '.str_replace('*', '\*', $fname.$gOpts.$gDefs.$gGraphs.$addSpecial); |
b7878f4d |
455 | $return = `$graph_cmd 2>&1`; |
456 | |
b7878f4d |
457 | if (strpos($return, 'ERROR') !== false) { |
458 | trigger_error('rrd graph error: '.$return, E_USER_WARNING); |
16cc643c |
459 | $return = $graph_cmd."\n\n".$return; |
b7878f4d |
460 | } |
ebe03325 |
461 | $return = 'file:'.$fname."\n".$return; |
a039a75c |
462 | return $return; |
b7878f4d |
463 | } |
464 | |
2c30ff69 |
465 | function simple_html($sub = null, $page_extras = null, $graph_extras = null) { |
b7878f4d |
466 | // create a simple (MRTG-like) HTML page and return it in a string |
467 | $basename = str_replace('.rrd', '', $this->rrd_file); |
a039a75c |
468 | |
469 | // assemble configuration |
e1727e7f |
470 | $pconf = (array)$page_extras; |
2c30ff69 |
471 | if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) { |
e1727e7f |
472 | $pconf = $pconf + $this->config_raw['page.'.$sub]; |
a039a75c |
473 | } |
e1727e7f |
474 | $pconf = $pconf + (array)$this->config_page; |
a039a75c |
475 | |
2c30ff69 |
476 | $ptitle = isset($pconf['title_page'])?$pconf['title_page']:$basename.' - RRD statistics'; |
16cc643c |
477 | $gtitle = array(); |
2c30ff69 |
478 | $gtitle['day'] = isset($pconf['title_day'])?$pconf['title_day']:'Day overview (scaling 5 minutes)'; |
479 | $gtitle['week'] = isset($pconf['title_week'])?$pconf['title_week']:'Week overview (scaling 30 minutes)'; |
480 | $gtitle['month'] = isset($pconf['title_month'])?$pconf['title_month']:'Month overview (scaling 2 hours)'; |
481 | $gtitle['year'] = isset($pconf['title_year'])?$pconf['title_year']:'Year overview (scaling 1 day)'; |
b7878f4d |
482 | |
16cc643c |
483 | $out = '<html><head>'; |
484 | $out .= '<title>'.$ptitle.'</title>'; |
485 | $out .= '<style>'; |
486 | if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; } |
487 | else { |
488 | $out .= 'h1 { font-weight: bold; font-size: 1.5em; }'; |
489 | $out .= 'h2 { font-weight: bold; font-size: 1em; }'; |
490 | $out .= '.gdata, .gvar, .ginfo { font-size: 0.75em; margin: 0.5em 0; }'; |
491 | $out .= 'table.gdata { border: 1px solid gray; border-collapse: collapse; }'; |
492 | $out .= 'table.gdata td, table.gdata th { border: 1px solid gray; padding: 0.1em 0.2em; }'; |
c5db3bd5 |
493 | $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }'; |
16cc643c |
494 | } |
495 | if (isset($pconf['style'])) { $out .= $pconf['style']; } |
496 | $out .= '</style>'; |
497 | $out .= '</head>'; |
498 | $out .= '<body>'; |
499 | |
500 | $out .= '<h1>'.$ptitle.'</h1>'; |
2c30ff69 |
501 | if (!isset($pconf['show_update']) || $pconf['show_update']) { |
502 | $out .= '<p class="last_up">Last Update: '.date('Y-m-d H:i:s', $this->last_update()).'</p>'; |
503 | } |
4ba56977 |
504 | |
b7878f4d |
505 | if (in_array($this->status, array('ok','readonly'))) { |
2c30ff69 |
506 | $g_sub = isset($pconf['graph_sub'])?$pconf['graph_sub']:null; |
b7878f4d |
507 | foreach (array('day','week','month','year') as $tframe) { |
2c30ff69 |
508 | $ret = $this->graph($tframe, $g_sub, $graph_extras); |
16cc643c |
509 | if (strpos($ret, "\n\n") !== false) { $graph_cmd = substr($ret, 0, strpos($ret, "\n\n")); $ret = substr($ret, strpos($ret, "\n\n")+2); } |
510 | else { $graph_cmd = null; } |
511 | $grout = explode("\n",$ret); |
ebe03325 |
512 | $gfilename = null; |
16cc643c |
513 | $gmeta = array(); |
514 | foreach ($grout as $gline) { |
ebe03325 |
515 | if (preg_match('/^file:(.+)$/', $gline, $regs)) { |
516 | $gfilename = $regs[1]; |
517 | } |
518 | elseif (preg_match('/^(\d+)x(\d+)$/', $gline, $regs)) { |
16cc643c |
519 | $gmeta['width'] = $regs[1]; $gmeta['height'] = $regs[2]; |
520 | } |
521 | elseif (preg_match('/^([^\|]+)\|([^|]+)\|([^\|]*)$/', $gline, $regs)) { |
522 | $gmeta['data'][$regs[1]][$regs[2]] = $regs[3]; |
523 | } |
524 | elseif (preg_match('/^([^\|]+)\|([^\|]*)$/', $gline, $regs)) { |
525 | $gmeta['var'][$regs[1]] = $regs[2]; |
526 | } |
527 | elseif (strlen(trim($gline))) { |
528 | $gmeta['info'][] = $gline; |
529 | } |
530 | } |
253ead9f |
531 | if (is_null($gfilename)) { $gfilename = $basename.(!is_null($g_sub)?'-'.$g_sub:'').'-'.$tframe.'.png'; } |
532 | if (isset($pconf['graph_url'])) { |
533 | $gURL = $pconf['graph_url']; |
534 | $fname = str_replace('%f', basename($gfilename), $gURL); |
535 | $fname = str_replace('%p', $gfilename, $gURL); |
536 | if (substr($gURL, -1) == '/') { $gURL .= $gfilename; } |
537 | } |
538 | else { |
539 | $gURL = $gfilename; |
540 | } |
16cc643c |
541 | $out .= '<div class="'.$tframe.'">'; |
b25db622 |
542 | // $out .= '<p>'.nl2br($ret).'</p>'; |
16cc643c |
543 | $out .= '<h2>'.$gtitle[$tframe].'</h2>'; |
253ead9f |
544 | $out .= '<img src="'.$gURL.'"'; |
2c30ff69 |
545 | $out .= ' alt="'.$basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"'; |
16cc643c |
546 | $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;">'; |
547 | if (isset($gmeta['data']) && count($gmeta['data'])) { |
548 | $out .= '<table class="gdata">'; |
549 | foreach ($gmeta['data'] as $field=>$gdata) { |
550 | $out .= '<tr><th>'.$field.'</th>'; |
551 | foreach ($gdata as $gkey=>$gval) { |
552 | $out .= '<td><span class="gkey">'.$gkey.': </span>'.$gval.'</td>'; |
553 | } |
554 | $out .= '</tr>'; |
555 | } |
556 | $out .= '</table>'; |
557 | } |
558 | if (isset($gmeta['var']) && count($gmeta['var'])) { |
559 | foreach ($gmeta['var'] as $gkey=>$gval) { |
560 | $out .= '<p class="gvar"><span class="gkey">'.$gkey.': </span>'.$gval.'</p>'; |
561 | } |
562 | } |
563 | if (isset($gmeta['info']) && count($gmeta['info'])) { |
564 | foreach ($gmeta['info'] as $gval) { |
565 | $out .= '<p class="ginfo">'.$gval.'</p>'; |
566 | } |
567 | } |
b7878f4d |
568 | } |
569 | } |
570 | else { |
571 | $out .= 'RRD error: status is "'.$this->status.'"'; |
572 | } |
16cc643c |
573 | $out .= '</div>'; |
b7878f4d |
574 | |
c5db3bd5 |
575 | $out .= '<p class="footer">'; |
576 | $out .= 'Statistics created by <a href="http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/">RRDtool</a>'; |
577 | $out .= ' using a library created by <a href="http://www.kairo.at/">KaiRo.at</a>.'; |
578 | $out .= '</p>'; |
579 | |
16cc643c |
580 | $out .= '</body></html>'; |
b7878f4d |
581 | return $out; |
582 | } |
583 | |
4ba56977 |
584 | function last_update() { |
585 | // fetch time of last update in this RRD file |
586 | static $last_update; |
587 | if (!isset($last_update) && in_array($this->status, array('ok','readonly'))) { |
588 | $last_cmd = 'rrdtool last '.$this->rrd_file; |
589 | $return = trim(`$last_cmd 2>&1`); |
590 | $last_update = is_numeric($return)?$return:null; |
591 | } |
592 | return isset($last_update)?$last_update:null; |
593 | } |
594 | |
b7878f4d |
595 | function text_quote($text) { return '"'.str_replace('"', '\"', str_replace(':', '\:', $text)).'"'; } |
596 | } |
597 | ?> |