"); return str_replace ( $search, "_", $string); } /* function to create new RRDs */ function createRRD($path, $name, $interval, $count = 2000, $type = "GAUGE", $begin = "\"now - 3month\"") { //////////////////////////////////////////////////// // path: the path where to put the rrd file (eg. can be sharded using the metric name) // name, or the number of the metric // interval: time (in seconds) between samples // count : number of samples (or number of lines in csv file) // type: DERIVE or GAUGE (default GAUGE) // begin: when data begins // // the rrd will contain ONLY the single points, no aggregation. this is less space consuming and more specific // for performance tests // I subtract some seconds from the begin to permit update of the very first datapoint $begin -= $interval; print "Creating rrd ${path}/${name}.rrd\n"; $hb = $interval*5; //heartbeat if(!file_exists($path)) mkdir($path, null, true); $cmd="rrdtool create ".$path."/${name}.rrd -s ".$interval." \ -b $begin DS:value:$type:$hb:0:U \ RRA:LAST:0.5:1:$count"; //RRA:AVERAGE:0.5:1:$count \ //RRA:MIN:0.5:1:$count \ //RRA:MAX:0.5:1:$count \ //print $cmd."\n"; if (!file_exists($path."/${name}.rrd")) return passthru($cmd); } $basepath = realpath(dirname($_SERVER["argv"][0])); // counter to indentify the line number $lnum=0; // walk the entire file while ($line = fgets($fh)) { $lnum++; if ( $lnum == 1 ) { ///////////////////////////////// // inside this if // we are processing the headers!! // get all different headers $headers = split (',', trim($line)); //array_shift($headers); while ( list ($key, $header) = each ($headers)) { // format: \\SERVER\OBJECT(instance)\metric $arrpath = split ('\\\\', trim($header,'"')); //shift twice to strip 2 empty strings array_shift($arrpath); array_shift($arrpath); // arrpath[0]: SERVERNAME // arrpath[1]: OBJECT(INSTANCE) // arrpath[2]: COUNTER $path[$key]=$arrpath; } //endif first line } else { ///////////////////////////////// // inside the else I'm processing the REAL data // get all different values. // datapoints[0] will contain the date/time $datapoints = split (',', trim($line)); if ( $lnum == 2 ) { // the second line contain the BEGIN datetime // then I can use this date to start creating rrd files $begindate = strtotime(trim($datapoints[0],'"')); } while (list ($key, $value ) = each ($datapoints)) { $value = trim ($value,'"'); if ( $key == 0 ) { $date = strtotime($value); continue; } if ( $lnum == 2 ) { $actualPath[$key] = $basepath.'/'.friendlyName($path[$key][0]).'/'.friendlyName($path[$key][1]); $name[$key] = friendlyName($path[$key][2]); $precmd[$key] = 'rrdtool update '.$actualPath[$key].'/'.$name[$key].'.rrd '; $cmd[$key] = 'rrdtool update '.$actualPath[$key].'/'.$name[$key].'.rrd '; $i[$key]=0; } if ( $lnum == 3 ) { $interval = strtotime(trim($datapoints[0],'"'))-$begindate; if ( $update != "FALSE" ) createRRD ( $actualPath[$key], $name[$key], $interval , $linecount, "GAUGE", $begindate); } // rrdUpdate $cmd[$key].=" ".$date.":".$value; if ($i[$key] >= 100) { // when I reach 100 values per commandline I force // the update: next loop will reinitialize a new commandline. if ( $update != "FALSE" ) { passthru($cmd[$key]); print ("."); } //print($cmd[$key]."\n"); $cmd[$key]=$precmd[$key]; $i[$key]=0; } $i[$key]++; //end while all datapoints in one line } //endif data lines } print "-"; //end while all lines } fclose($fh); $enddate = strtotime(trim($datapoints[0],'"')); // here I'll have: // $path[] with [0]=server, [1]=object(instance), [2]=counter // $actualpath[] with friendly dirnames // $name[] with friendly filenames // $interval // $begindate // $enddate if ( $linecount > 1000 ) { $width = 1000; } else { $width = $linecount; } $total = count($path); for ($i=0; $i<$total; $i++) { print $i; if ( $update != "FALSE" ) { passthru($cmd[$i]); print ("."); } //print $path[$i][1]; //print $path[$i][2]; // add escape to colon (:) to prevent rrdgraph error $descr = str_replace(':', '\\:', $path[$i][1]."\\".$path[$i][2]); $cmdstr = 'rrdtool graph %1$s/%2$s.png --start "%3$s" --end "%4$s" --width %7$s DEF:ds0=%1$s/%2$s.rrd:value:LAST:step=%6$s LINE1:ds0#0000FF:"%5$s" VDEF:ds0max=ds0,MAXIMUM VDEF:ds0avg=ds0,AVERAGE VDEF:ds0min=ds0,MINIMUM COMMENT:" " COMMENT:" Maximum " GPRINT:ds0max:"%%6.2lf" COMMENT:" Average " GPRINT:ds0avg:"%%6.2lf" COMMENT:" Minimum " GPRINT:ds0min:"%%6.2lf"'; $cmdu = sprintf ($cmdstr, $actualPath[$i], $name[$i], $begindate, $enddate, $descr, $interval, $width); print ("Generating Graph: ".$actualPath[$i].'/'.$name[$i].".png\n"); print $cmdu."\n"; passthru($cmdu); } ?>