<?php
/*
 * This script runs by cron.. updates the graph images..
 */
require_once('config.php');
require_once(
'jpgraph/jpgraph.php');
require_once(
'jpgraph/jpgraph_bar.php');
require_once(
'jpgraph/jpgraph_line.php');
require_once(
'jpgraph/jpgraph_regstat.php');

$dblink mysql_connect('10.200.200.3',$dbuser,$dbpass);
mysql_select_db('cable_watch',$dblink);

/* stats */

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%M %Y"), avg(status) '.
  
'from pings group by DATE_FORMAT(FROM_UNIXTIME(date),"%M %Y") order by date desc';
$res mysql_query($query);

$out_month[] = '<table border=1 cellspacing=0 cellpadding=4>';
$out_month[] = ' <tr><td colspan=2 bgcolor=#EEEEEE><center><font size=4>Monthly Averages (Last 9)</td></tr>';

$i=0;
while(
$row mysql_fetch_row($res)) { 
    
$i++;
    if(
$i 9) { 
    break;
    }
    
    
$out_month[] = ' <tr>';
    
$out_month[] = '  <td>'.$row[0].'</td>';
    
$out_month[] = '  <td>'.($row[1] * 100).'%</td>';
    
$out_month[] = ' </tr>';
    
    
$bigtotal += $row[1];
    
$bigcount++;
}

$out_month[] = '</table>';

$query 'select date from pings order by date asc limit 0,1';
$res mysql_query($query);
list(
$first) = mysql_fetch_row($res);

$query 'select avg(status) from pings';
$res mysql_query($query);
list(
$total) = mysql_fetch_row($res);

$duration time() - $first/* time test has been running */

$uptime = ($duration $total);
$downtime = ($duration $uptime);

/* average lag in seconds - guesstimate*/
$avglag .3;

$query 'select count(*) from pings';
$res mysql_query($query);
list(
$pingcount) = mysql_fetch_row($res);

$out_test[] = '<table  border=1 cellspacing=0 cellpadding=4>';
$out_test[] = ' <tr><td colspan=2 bgcolor=#EEEEEE><center><font size=4>Overall Statistics</td></tr>';
$out_test[] = ' <tr>';
$out_test[] = '  <td>Log Start Date:</td>';
$out_test[] = '  <td>'.date('r',$first).'</td>';
$out_test[] = ' </tr>';
$out_test[] = ' <tr>';
$out_test[] = '  <td>Log Duration:</td>';
$out_test[] = '  <td>'.round(($duration 86400),2).' days ('.number_format($pingcount).' pings)</td>';
$out_test[] = ' </tr>';
$out_test[] = ' <tr>';
$out_test[] = '  <td>Approximate Uptime:</td>';
$out_test[] = '  <td>'.round(($uptime 86400),2).' days</td>';
$out_test[] = ' </tr>';
$out_test[] = ' <tr>';
$out_test[] = '  <td>Approximate Downtime:</td>';
$out_test[] = '  <td>'.round(($downtime 86400),2).' days</td>';
$out_test[] = ' </tr>';

$out_test[] = ' <tr>';
$out_test[] = '  <td>Overall Uptime Percentage:</td>';
$out_test[] = '  <td>'.round(($total 100),2).'%</td>';
$out_test[] = ' </tr>';
$out_test[] = '</table>';

$out[] = '<table>';
$out[] = ' <tr>';
$out[] = '  <td valign=top>'.join("\n",$out_test).'</td>';
$out[] = '  <td valign=top>'.join("\n",$out_month).'</td>';
$out[] = ' </tr>';
$out[] = '</table>';

$fd fopen($statsfile,'w+');
fwrite($fd,join("\n",$out));
fclose($fd);

/* graphs */

$tdate['hour of day'] = '%H';
$tdate['day of week'] = '%w';
$tdate['day of month'] = '%d';

/* $tdate['week of year'] = '%V';
 $tdate['month of year'] = '%M'; */

foreach(array_keys($tdate) as $graphname) {
    
$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "'.$tdate[$graphname].'") as timeframe, '.
      
'avg(status) as availability from pings '.
      
'group by DATE_FORMAT(FROM_UNIXTIME(date), "'.$tdate[$graphname].'") ';
    
$res mysql_query($query);
    
    while(
$row mysql_fetch_assoc($res)) {
    
$allplots[$graphname][$row['timeframe']] = round($row['availability'] * 100,2);
    }
}

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%m/%y") as timeframe, '.
  
'avg(status) as availability from pings '.
  
'group by DATE_FORMAT(FROM_UNIXTIME(date), "%m/%y") '.
  
'order by date';
$res mysql_query($query);

while(
$row mysql_fetch_assoc($res)) {
    
$allplots['previous months'][$row['timeframe']] = round($row['availability'] * 100,2);
}

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%m/%d") as timeframe, '.
  
'avg(status) as availability from pings '.
  
'where date > '.(time() - (86400 90)).' '.
  
'group by DATE_FORMAT(FROM_UNIXTIME(date), "%U/%y") '.
  
'order by date';
$res mysql_query($query);

while(
$row mysql_fetch_assoc($res)) {
    
$allplots['last 90 weeks'][$row['timeframe']] = round($row['availability'] * 100,2);
}

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%m/%d") as timeframe, '.
  
'avg(status) as availability from pings '.
  
'where date > '.(time() - (86400 90)).' '.
  
'group by DATE_FORMAT(FROM_UNIXTIME(date), "%m/%d/%y") '.
  
'order by date';
$res mysql_query($query);

while(
$row mysql_fetch_assoc($res)) {
    
$allplots['last 90 days'][$row['timeframe']] = round($row['availability'] * 100,2);
}

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%m/%d %H") as timeframe, '.
  
'avg(status) as availability from pings '.
  
'where date > '.(time() - (86400 3)).' '.
  
'group by DATE_FORMAT(FROM_UNIXTIME(date), "%m/%d/%y:%H") '.
  
'order by date';
$res mysql_query($query);

while(
$row mysql_fetch_assoc($res)) {
    
$allplots['last 72 hours'][$row['timeframe']] = round($row['availability'] * 100,2);
}

$query 'select DATE_FORMAT(FROM_UNIXTIME(date), "%H:%i") as timeframe, '.
  
'avg(status) as availability from pings '.
  
'where date > '.(time() - (60 90)).' '.
  
'group by DATE_FORMAT(FROM_UNIXTIME(date), "%H:%i") '.
  
'order by date';
$res mysql_query($query);

while(
$row mysql_fetch_assoc($res)) {
    
$allplots['last 90 minutes'][$row['timeframe']] = round($row['availability'] * 100,2);
}


foreach(
$allplots as $graphname => $plots) { 
    
$xdata array_keys($plots);
    
$ydata array_values($plots);
    
    
/*
    $spline = new Spline ($xdata,$ydata);
    list($spline_datax,$spline_datay) =  $spline->Get(50);
    */
    
    
$graph = new Graph(980,400,"auto");
    
$graph->SetScale('textlin');

    
$graph->SetMargin(55,20,20,80);
    
$tilt 0;

    
$graph->SetMarginColor('lightgray');

    
$graph->SetShadow();
    
    
$graph->title->Set('Availability average by '.$graphname);
    
    
$graph->title->SetFont(FF_VERA,FS_NORMAL,14);

    
$graph->title->SetColor("#000000");
    
$graph->yscale = new LinearScale(0,100);
    
$graph->yaxis->scale->SetGrace(8);
    
$graph->xaxis->scale->SetGrace(8);
    
$graph->xaxis->SetFont(FF_VERAMONO,FS_NORMAL,7);
    
$graph->yaxis->SetFont(FF_VERAMONO,FS_NORMAL,7);
    
$graph->yscale->ticks->SupressZeroLabel(false);
    
$graph->xaxis->SetTickLabels($xdata);

    
/* Rotate as needed */
    
$graph->xaxis->SetLabelAngle(90 $tilt);
    
    
/* A nice margin */
    
$graph->xaxis->SetLabelMargin(10);

    
/* x titles and fonts */
    
$graph->xaxis->title->Set('Time Frame');
    
$graph->xaxis->title->SetFont(FF_VERA,FS_NORMAL);
    
$graph->xaxis->SetTitleMargin(44);
    
$graph->yaxis->title->Set('Percent Availability');
    
$graph->yaxis->title->SetFont(FF_VERA,FS_NORMAL);
    
$graph->yaxis->SetTitleMargin(33);    

/*    $bplot_y2 = new LinePlot($spline_datay);
    $bplot_y2->SetColor("black"); */
    
    
$bplot_y1 = new LinePlot($ydata);
//    $bplot_y1->SetWidth(0.6);
    
$bplot_y1->SetFillGradient("#444488","#EEEEEE",100);
    
$bplot_y1->SetColor("#000000");
//    $bplot_y1->value->SetFont(FF_VERA,FS_BOLD,8);
//    $bplot_y1->value->SetAngle(0);
//    $bplot_y1->value->Show();

    
$graph->Add($bplot_y1);
//    $graph->Add($bplot_y2);
    
$graph->img->SetImgFormat('jpeg');
    
$graph->Stroke('/var/www/localhost/htdocs/cablewatch/images/cable_'.str_replace(' ','_',$graphname).'.jpg');
}

mysql_close($dblink);