<?php
include ("plotter/jpgraph/src/jpgraph.php");
include ("plotter/jpgraph/src/jpgraph_log.php");
include ("plotter/jpgraph/src/jpgraph_line.php");
include ("plotter/jpgraph/src/jpgraph_pie.php");
include ("plotter/jpgraph/src/jpgraph_bar.php");
include ("plotter/jpgraph/src/jpgraph_pie3d.php");
//plot a graph with daily granularity
Class GranDayPlotter{
private $starttime;
private $finishtime;
//determine active window monitoring
private $startwindow;
private $finishwindow;
private $table;
private $dba;
//the line graph to plot the actual data
private $graph;
//the pie graph to plot availability
private $pieGraph;
private $data;
private $availdata;
private $interval;
private $legend;
private $redvalue;
private $greenvalue;
private $map;
//average (graphtype==avg) or full rough data (graphtype==full)
//or undefined/null if you want button average or detail disabled
private $graphtype;
private $caller; //actual class that invoke plotter, to determine href for image maps
private $post; //caller post
private $title;
private $graphAvail;
//statistic
private $maxstat;
private $minstat;
private $averagestat;
private $lateststat;
//return the downtime/totalcheckpoint
private $availability;
public function __construct(){
// Setup the graph
$this->dba = DBAccess::getInstance();
//defaul is line graph
$this->graphAvail = false;
$this->interval = 1;
}
public function setTitle($title){
$this->title = $title;
}
public function getData(){
if($this->graphAvail){
return $this->availdata;
}
else{
return $this->data;
}
}
public function setLegend($legend){
$this->legend = $legend;
}
//boolean value. determine wether graph should be displayed as bar availability or line detail line
public function setGraphAvail($graphavail){
$this->graphAvail = $graphavail;
}
public function setCaller($caller,$post){
$this->caller = $caller;
$this->post = $post;
}
public function setRedLine($red){
$this->redvalue = $red;
}
public function setGreenLine($green){
$this->greenvalue = $green;
}
//set the graph graphtype. avg for average and full for full rough data
//null or undefined if you want average and detail button disabled
public function setGraphType($graphtype){
$this->graphtype = $graphtype;
}
//please invoke after setGraph
public function getAvailability(){
return $this->availability;
}
public function setRangeTime($st,$ft){
$this->starttime = $st;
$this->finishtime = $ft;
}
public function parseTime(){
//parse from start time to finish time into small daily chunk
$stts = strtotime($this->starttime);
$ftts = strtotime($this->finishtime);
$diff = ($ftts - $stts)/(3600*24);
for($i=0;$i<$diff;$i++){
$message .= 'tick: '.date('Y-m-d',$stts+($i*3600*24)).'<br>';
}
}
public function setGraph($table,$st,$ft){
$this->starttime = $st;
$this->finishtime = $ft;
$this->table = $table;
//table determine the monitoring id
//plotter self aware
$obj = '';
if($table[0]=='p'){
$obj = new Ping();
$obj->selectByID($table);
$this->setLegend('Ping');
}
else
if($table[0]=='s'){
$obj = new ServiceMon();
$obj->selectByID($table);
$objservdef = new ServicesDef();
$objservdef->selectByID($obj->getService());
$this->setLegend('Service '.$objservdef->getName());
}else{
die('currently not support for monitoring with priority id '.$table);
}
$this->redvalue = $obj->getRedLine();
$this->greenvalue = $obj->getGreenLine();
$this->startwindow = $obj->getStartTime();
$this->finishwindow = $obj->getFinishTime();
//setting up the graph
$posttime = array();
$datay = array();
$redline = array();
$greenine = array();
$this->data = array();
$this->availdata = array();
$availability = array();
$peak = 0;
//magic number here :)
$valley = 10000;
//time range
$stts = strtotime($this->starttime);
$ftts = strtotime($this->finishtime);
$tmpobj = new StdClass();
$diff = ($ftts - $stts)/(3600*24);
$result = array();
for($i=0;$i<$diff;$i++){
$offset = $stts+($i*24*3600);
$q_start_time = " AND posttime >= \"".date('Y-m-d',$offset)." 00:00:00\"";
$q_finish_time = " AND posttime <= \"".date('Y-m-d',$offset)." 23:59:59\"";
$sql = "SELECT posttime,value FROM ".$this->table." WHERE value>=0 ".$q_start_time." ".$q_finish_time;
$subresult = $this->dba->query($sql);
$total = 0;
$count = 0;
//windowtime parse here. can not be inserted in sql
for($j=0;$j<sizeof($subresult);$j++){
$tmpdate = date('G:i:s',strtotime($subresult[$j]->posttime));
if((strtotime($tmpdate)>strtotime($this->startwindow))&&(strtotime($tmpdate)<strtotime($this->finishwindow))){
//outside the window time we just ignore the value
$total+=$subresult[$j]->value;
$count++;
}
}
$average = null;
if($count>0){
$average=$total/$count;
}
$tmpobj->itemid = date('Y-m-d',$offset);
$tmpobj->posttime = date('d',$offset);
$tmpobj->value = $average;
$datay[] = $average;
$posttime[] = $tmpobj->posttime;
$redline[] = $this->redvalue;
$greenline[] = $this->greenvalue;
$result[] = $tmpobj;
if($peak < $average){
$peak = $average;
}
if($valley > $average){
$valley = $average;
}
}
$peak += ($peak-$valley)*0.15;
$valley -= ($peak-$valley)*0.05;
//if negatif pick zero as valley
if($valley<0){
$valley=0;
}
// Setup the graph
$this->graph = new Graph(510,300);
$this->graph->SetMarginColor('white');
$this->graph->SetFrame(false);
$this->graph->SetMargin(30,50,30,30);
if(sizeof($result)<2){
die('Data not big enought to be plotted. Please wait while system is collecting the data');
}
$this->graph->SetScale("textlin",$valley,$peak);
$p1 = new LinePlot($datay);
$p1->SetColor("navy");
$p1->SetLegend($this->legend);
$this->graph->Add($p1);
if(isset($this->redvalue)){
// Create red line
$p3 = new LinePlot($redline);
$p3->SetColor("red");
$this->graph->Add($p3);
}
if(isset($this->greenvalue)){
$p2 = new LinePlot($greenline);
$p2->SetColor("yellow");
$this->graph->Add($p2);
}
$this->graph->xaxis->SetTickLabels($posttime);
$this->graph->xaxis->SetTextTickInterval(1);
$this->graph->title->Set($this->title);
}
//similar with getrecent
public function getLatestStat(){
return $this->lateststat;
}
public function getMaxStat(){
return $this->maxstat;
}
public function getMinStat(){
return $this->minstat;
}
public function getAverageStat(){
return $this->averagestat;
}
public function getImageMaps(){
return '<MAP NAME="MyMap">'.$this->map.'</MAP>';
}
public function attachLine(){
//set graph
$file = rand(2,20000).'plotline.png';
$this->graph->Stroke('/www/htdocs/ajaxmonyet/tmp/'.$file);
return $file;
}
}
?>