| 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 | |
| 152 | $graphrows = array(); $gC = 0; |
| 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'; |
| 161 | } |
| 162 | elseif ($timeframe == 'week') { |
| 163 | $duration = isset($extra['duration'])?$extra['duration']:8*86400; // 8 days |
| 164 | $slice = isset($extra['slice'])?$extra['slice']:1800; // 30 minutes |
| 165 | // vertical lines at week borders |
| 166 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')+1).' day').'#FF0000'; |
| 167 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')-6).' day').'#FF0000'; |
| 168 | } |
| 169 | elseif ($timeframe == 'month') { |
| 170 | $duration = isset($extra['duration'])?$extra['duration']:36*86400; // 36 days |
| 171 | $slice = isset($extra['slice'])?$extra['slice']:7200; // 2 hours |
| 172 | // vertical lines at month borders |
| 173 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01')).'#FF0000'; |
| 174 | $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01').' -1 month').'#FF0000'; |
| 175 | } |
| 176 | elseif ($timeframe == 'year') { |
| 177 | $duration = isset($extra['duration'])?$extra['duration']:396*86400; // 365+31 days |
| 178 | $slice = isset($extra['slice'])?$extra['slice']:86400; // 1 day |
| 179 | // vertical lines at month borders |
| 180 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01')).'#FF0000'; |
| 181 | $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01').' -1 year').'#FF0000'; |
| 182 | } |
| 183 | else { |
| 184 | $duration = isset($extra['duration'])?$extra['duration']:$this->rrd_step*500; // 500 steps |
| 185 | $slice = isset($extra['slice'])?$extra['slice']:$this->rrd_step; // whatever our step is |
| 186 | } |
| 187 | |
| 188 | if (isset($extra['rows']) && count($extra['rows'])) { |
| 189 | foreach ($extra['rows'] as $erow) { |
| 190 | if (isset($erow['name']) && strlen($erow['name'])) { |
| 191 | $grow = array(); |
| 192 | $grow['dType'] = isset($erow['dType'])?$erow['dType']:'DEF'; |
| 193 | $grow['name'] = $erow['name']; |
| 194 | $grow['dsname'] = isset($erow['dsname'])?$erow['dsname']:$erow['name']; |
| 195 | $grow['cf'] = isset($erow['cf'])?$erow['cf']:'AVERAGE'; |
| 196 | $grow['gType'] = isset($erow['gType'])?$erow['gType']:'LINE1'; |
| 197 | $grow['color'] = isset($erow['color'])?$erow['color']:$gColors[$gC++]; |
| 198 | if ($gC >= count($gColors)) { $gC = 0; } |
| 199 | if (isset($erow['legend'])) { |
| 200 | $grow['legend'] = $erow['legend']; |
| 201 | if (!isset($extra['show_legend'])) { $extra['show_legend'] = true; } |
| 202 | } |
| 203 | if (isset($erow['stack'])) { $grow['stack'] = ($erow['stack'] == true); } |
| 204 | $graphrows[] = $grow; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | else { |
| 209 | foreach ($this->rrd_fields as $ds) { |
| 210 | $grow = array(); |
| 211 | $grow['dType'] = 'DEF'; |
| 212 | $grow['name'] = $ds['name']; |
| 213 | $grow['dsname'] = $ds['name']; |
| 214 | $grow['cf'] = 'AVERAGE'; |
| 215 | $grow['gType'] = ($ds['name']=='ds0')?'AREA':'LINE1'; |
| 216 | $grow['color'] = $gColors[$gC++]; if ($gC >= count($gColors)) { $gC = 0; } |
| 217 | $graphrows[] = $grow; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $gOpts = ' --start '.($this->last_update()-$duration).' --end '.$this->last_update().' --step '.$slice; |
| 222 | if (isset($extra['label_top'])) { $gOpts .= ' --title '.$extra['label_top']; } |
| 223 | if (isset($extra['label_y'])) { $gOpts .= ' --vertical-label '.$extra['label_y']; } |
| 224 | if (isset($extra['width'])) { $gOpts .= ' --width '.$extra['width']; } |
| 225 | if (isset($extra['height'])) { $gOpts .= ' --height '.$extra['height']; |
| 226 | if (($extra['height'] <= 32) && isset($extra['thumb']) && ($extra['thumb'])) { $gOpts .= ' --only-graph'; } |
| 227 | } |
| 228 | if (!isset($extra['show_legend']) || (!$extra['show_legend'])) { $gOpts .= ' --no-legend'; } |
| 229 | if (isset($extra['min_y'])) { $gOpts .= ' --lower-limit '.$extra['min_y']; } |
| 230 | if (isset($extra['max_y'])) { $gOpts .= ' --upper-limit '.$extra['max_y']; } |
| 231 | if (isset($extra['fix_scale_y']) && $extra['fix_scale_y']) { $gOpts .= ' --rigid'; } |
| 232 | if (isset($extra['grid_x'])) { $gOpts .= ' --x-grid '.$extra['grid_x']; } |
| 233 | if (isset($extra['grid_y'])) { $gOpts .= ' --y-grid '.$extra['grid_y']; } |
| 234 | if (isset($extra['units_exponent'])) { $gOpts .= ' --units-exponent '.$extra['units_exponent']; } |
| 235 | if (isset($extra['units_length'])) { $gOpts .= ' --units-length '.$extra['units_length']; } |
| 236 | if (!isset($extra['force_recreate']) || (!$extra['force_recreate'])) { $gOpts .= ' --lazy'; } |
| 237 | if (isset($extra['force_color']) && is_array($extra['force_color'])) { |
| 238 | foreach ($extra['force_color'] as $ctag=>$cval) { $gOpts .= ' --color '.$ctag.$cval; } |
| 239 | } |
| 240 | if (isset($extra['force_font']) && is_array($extra['force_font'])) { |
| 241 | foreach ($extra['force_font'] as $ctag=>$cval) { $gOpts .= ' --font '.$ctag.$cval; } |
| 242 | } |
| 243 | if (isset($extra['units_binary']) && $extra['units_binary']) { $gOpts .= ' --base 1024'; } |
| 244 | |
| 245 | foreach ($graphrows as $grow) { |
| 246 | if (isset($grow['dType']) && strlen($grow['dType'])) { |
| 247 | $gDefs .= ' '.$grow['dType'].':'.$grow['name'].'='; |
| 248 | $gDefs .= ($grow['dType']=='DEF')?$this->rrd_file.':'.$grow['dsname'].':'.$grow['cf']:$grow['rpn_expr']; |
| 249 | } |
| 250 | if (isset($grow['gType']) && strlen($grow['gType'])) { |
| 251 | // XXX: current rrdtools version doesn't support STACK flag, only STACK type |
| 252 | if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; } |
| 253 | $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color']; |
| 254 | if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); } |
| 255 | // XXX: when STACK flag is supported, remove above STACK if-command and uncomment the one below |
| 256 | //if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | $graph_cmd = 'rrdtool graph '.$fname.$gOpts.$gDefs.$gGraphs.$addSpecial; |
| 261 | $return = `$graph_cmd 2>&1`; |
| 262 | |
| 263 | $retval = 0; |
| 264 | if (strpos($return, 'ERROR') !== false) { |
| 265 | trigger_error('rrd graph error: '.$return, E_USER_WARNING); |
| 266 | $retval = 1; |
| 267 | } |
| 268 | return $retval; |
| 269 | } |
| 270 | |
| 271 | function simple_html($page_extras = null, $graph_extras = null) { |
| 272 | // create a simple (MRTG-like) HTML page and return it in a string |
| 273 | $basename = str_replace('.rrd', '', $this->rrd_file); |
| 274 | $out = '<html><head><title>'.$basename.' - RRD statistics</title></head><body>'; |
| 275 | |
| 276 | $out .= '<p>Last Update: '.date('Y-m-d H:i:s', $this->last_update()).'</p>'; |
| 277 | |
| 278 | if (in_array($this->status, array('ok','readonly'))) { |
| 279 | foreach (array('day','week','month','year') as $tframe) { |
| 280 | $this->graph(null, $tframe, $graph_extras); |
| 281 | $out .= '<p><img src="'.$basename.'-'.$tframe.'.png"></p>'; |
| 282 | } |
| 283 | } |
| 284 | else { |
| 285 | $out .= 'RRD error: status is "'.$this->status.'"'; |
| 286 | } |
| 287 | |
| 288 | $ou .= '</body></html>'; |
| 289 | return $out; |
| 290 | } |
| 291 | |
| 292 | function last_update() { |
| 293 | // fetch time of last update in this RRD file |
| 294 | static $last_update; |
| 295 | if (!isset($last_update) && in_array($this->status, array('ok','readonly'))) { |
| 296 | $last_cmd = 'rrdtool last '.$this->rrd_file; |
| 297 | $return = trim(`$last_cmd 2>&1`); |
| 298 | $last_update = is_numeric($return)?$return:null; |
| 299 | } |
| 300 | return isset($last_update)?$last_update:null; |
| 301 | } |
| 302 | |
| 303 | function text_quote($text) { return '"'.str_replace('"', '\"', str_replace(':', '\:', $text)).'"'; } |
| 304 | } |
| 305 | ?> |