- Timestamp:
- 06/08/05 17:45:15 (18 years ago)
- Location:
- trunk/web/addons/toga
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/web/addons/toga/libtoga.php
r129 r130 87 87 global $metrics, $hosts_up; 88 88 89 90 class TarchDbase { 91 92 var $ip, $dbase; 93 94 function TarchDbase( $ip = null, $dbase = 'toga' ) { 95 $this->ip = $ip; 96 $this->dbase = $dbase; 97 } 98 99 function connect() { 100 101 if( $this->ip == null and $this->dbase == 'toga' ) 102 $this->conn = pg_connect( "dbase=".$this->dbase ); 103 else 104 $this->conn = pg_connect( "host=".$this->ip." dbase=".$this->dbase ); 105 } 106 } 107 108 class TarchRrd { 109 var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics; 110 111 function TarchRrd( $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) { 112 $this->rrdbin = $rrdbin; 113 $this->rrdvalues = array(); 114 $this->tarchdir = $tarchdir; 115 $this->tempdir = '/tmp/toga-web-temp'; 116 $this->metrics = array(); 117 } 118 119 function doCmd( $command ) { 120 121 printf( "command = %s\n", $command ); 122 $pipe = popen( $command . ' 2>&1', 'r' ); 123 124 if (!$pipe) { 125 print "pipe failed."; 126 return ""; 127 } 128 129 $output = ''; 130 while(!feof($pipe)) 131 $output .= fread($pipe, 1024); 132 133 pclose($pipe); 134 135 $output = explode( "\n", $output ); 136 print_r( $output ); 137 return $output; 138 } 139 140 function dirList( $dir ) { 141 142 $dirlist = array(); 143 144 if ($handle = opendir( $dir )) { 145 while (false !== ($file = readdir($handle))) { 146 if ($file != "." && $file != "..") { 147 $dirlist[] = $file; 148 } 149 } 150 closedir($handle); 151 } 152 153 return $dirlist; 154 } 155 156 function getTimePeriods( $start, $end ) { 157 158 $times = array(); 159 $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname ); 160 //print_r( $dirlist ); 161 162 $first = 0; 163 $last = 9999999999999; 164 165 foreach( $dirlist as $dir ) { 166 167 //printf("dir = %s\n", $dir ); 168 169 if( $dir > $first and $dir <= $start ) 170 $first = $dir; 171 if( $dir < $last and $dir >= $end ) 172 $last = $dir; 173 } 174 175 //if( $first != 0 and !array_key_exists( $first, $times ) ) 176 // $times[] = $first; 177 178 foreach( $dirlist as $dir ) { 179 180 if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) 181 $times[] = $dir; 182 } 183 184 //if( $last != 9999999999 and !array_key_exists( $last, $times ) ) 185 // $times[] = $last; 186 187 sort( $times ); 188 189 //print_r( $times ); 190 191 return $times; 192 } 193 194 function getIntervalStep( $file ) { 195 196 $ret = $this->doCmd( $this->rrdbin .' info '. $file ); 197 198 foreach( $ret as $r ) { 199 200 $fields = explode( ' = ', $r ); 201 202 if( $fields[0] == 'step' ) 203 return $fields[1]; 204 } 205 206 return null; 207 } 208 209 function makeJobRrds( $clustername, $hostname, $descr, $start, $end) { 210 $this->clustername = $clustername; 211 $this->hostname = $hostname; 212 213 $myvalues = array(); 214 215 $times = $this->getTimePeriods( $start, $end ); 216 217 if( count( $times ) > 0 ) { 218 219 $time_size = count( $times ); 220 $curtime = 1; 221 $this->metrics = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $times[0] ); 222 //print_r( $this->metrics ); 223 224 $intv = $this->getIntervalStep( '"'.$this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $times[0]. '/'.$this->metrics[0].'"' ); 225 foreach( $this->metrics as $metric ) { 226 $newfile = $this->tempdir .'/toga.temp-'. $descr .'-'.$metric; 227 228 foreach( $times as $timep ) { 229 230 $r_start = null; 231 $r_end = null; 232 233 if( $curtime == 1 ) 234 $r_start = $start; 235 236 if( $curtime == $time_size ) 237 $r_end = $end; 238 239 $file = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $timep .'/'. $metric; 240 241 $r_values = $this->getValues( $file, $r_start, $r_end ); 242 //print_r($r_values); 243 244 $myvalues = $myvalues + $r_values; 245 246 $curtime++; 247 } 248 //printf( "----myvalues----\n" ); 249 //print_r($myvalues); 250 //printf( "----myvalues----\n" ); 251 252 $heartbeat = intval( 8 * $intv ); 253 $ret = $this->doCmd( $this->rrdbin .' create "'.$newfile.'" --step '. $intv .' --start '. $start .' DS:sum:GAUGE:'.$heartbeat.':U:U RRA:AVERAGE:0.5:1:'. count( $myvalues ) ); 254 255 $update_args = array(); 256 $arglist_nr = 0; 257 258 ksort( $myvalues ); 259 260 foreach( $myvalues as $mytime=>$myvalue ) { 261 $myupdateval = ' '.trim($mytime).':'.trim($myvalue); 262 263 if( !isset($update_args[$arglist_nr]) ) 264 $update_args[$arglist_nr] = ''; 265 266 if( intval( strlen($update_args[$arglist_nr]) + strlen($myupdateval) ) > 50000 ) 267 $arglist_nr++; 268 269 $update_args[$arglist_nr] .= $myupdateval; 270 } 271 272 //printf( "grootte args = %s\n", strlen( $update_args ) ); 273 274 $ret = $this->doCmd( $this->rrdbin .' update "'. $newfile . '" blaaa' ); 275 foreach( $update_args as $update_arg ) 276 $ret = $this->doCmd( $this->rrdbin .' update "'. $newfile . '"'.$update_arg ); 277 278 return; 279 } 280 } 281 } 282 283 function getValues( $file, $start = null, $end = null ) { 284 285 $rrdargs = 'AVERAGE -r 15'; 286 287 if( $start ) 288 $rrdargs .= ' -s '. $start; 289 if( $end ) 290 $rrdargs .= ' -e '. $end; 291 292 $values = $this->doCmd( $this->rrdbin .' fetch "'.$file.'" '. $rrdargs ); 293 294 //print_r( $values ); 295 $arvalues = array(); 296 297 foreach( $values as $value ) { 298 //printf( "value = %s\n", $value ); 299 300 $fields = explode( ':', $value ); 301 302 if( count( $fields ) == 2 ) { 303 304 $timestamp = trim($fields[0]); 305 $keepval = 1; 306 307 if( $start ) { 308 309 if( intval($timestamp) >= intval($start) ) 310 $keepval = 1; 311 else 312 $keepval = 0; 313 } else if( $stop ) { 314 315 if( intval($timestamp) <= intval($stop) ) 316 $keepval = 1; 317 else 318 $keepval = 0; 319 } 320 321 $value = $fields[1]; 322 //printf("timestamp = %s, value = %s\n", $timestamp, $value ); 323 324 if( $keepval ) 325 $arvalues[$timestamp] = $value; 326 } 327 } 328 //printf( "----arvalues----\n" ); 329 //print_r( $arvalues); 330 //printf( "----arvalues----\n" ); 331 332 ksort( $arvalues ); 333 //printf( "----arsortvalues----\n" ); 334 //print_r( $arvalues); 335 //printf( "----arsortvalues----\n" ); 336 337 return $arvalues; 338 } 339 340 function graph( $descr ) { 341 //$command = $this->rrdbin . " graph - --start $start --end $end ". 342 "--width $width --height $height $upper_limit $lower_limit ". 343 "--title '$title' $vertical_label $extras $background ". $series; 344 345 //$graph = $this->doCmd( $command ); 346 347 //return $graph; 348 } 349 } 89 350 90 351 class DataSource {
Note: See TracChangeset
for help on using the changeset viewer.