Location: PHPKode > projects > Php-Logging-System > phplogsys-1.1.4/includes/syslog-nggraphbyday.php
<?php
require_once(BASE_PATH . '/jpgraph/jpgraph.php');
require_once(BASE_PATH . '/jpgraph/jpgraph_line.php');
require_once(BASE_PATH . '/jpgraph/jpgraph_bar.php');
require_once(BASE_PATH . '/jpgraph/jpgraph_log.php');
$inputValError = array();
require_once(BASE_PATH . '/parse_syslog-ng.php');

$facilityarray = array();
$priorityarray = array('debug', 'info', 'notice', 'warning', 'err', 'crit', 'alert', 'emerg');
//------------------------------------------------------------------------
// Use cache?
//------------------------------------------------------------------------
if(USE_CACHE && table_exists(CACHETABLENAME, $dbLink)) {
	// Get the timestamp for the last update.
	$sql = 'SELECT updatetime FROM ' . CACHETABLENAME . ' LIMIT 1';
	$queryresult = perform_query($sql, $dbLink);
	$row = fetch_array($queryresult);
	$cacheUpdate = $row['updatetime'];

	// Print info about the cache.
	echo '<table class="searchform">';
	echo '<tr class="lighter"><td>';
	echo '<b>USING CACHE TO POPULATE HOST AND FACILITY FIELDS, </b>';
	echo '<br />Cache last updated on ', $cacheUpdate, ', ';
	echo '</td></tr></table>';

	// Get the FACILITY list
	$sql = 'SELECT DISTINCT value FROM '.CACHETABLENAME.' WHERE type=\'FACILITY\'';
	$queryresult = perform_query($sql, $dbLink);
	while ($line = fetch_array($queryresult)) {
		array_push($facilityarray, $line['value']);
	}
	sort($facilityarray);
}
else {
//------------------------------------------------------------------------
// If no cache then get possible values for facility and host from table.
//------------------------------------------------------------------------
	$sql = 'SELECT DISTINCT facility FROM '.$table;
	$queryresult = perform_query($sql, $dbLink);
	while ($line = fetch_array($queryresult)) {
		array_push($facilityarray, $line['facility']);
	}
	sort($facilityarray);
}

//------------------------------------------------------------------------
// Create the complete SQL statement
//------------------------------------------------------------------------
if($table) {
	$srcTable = $table;
}else {
	$srcTable = DEFAULTSYSLOGTABLE;
}

$resultarray = array();

if($blnByFacility){
	$query = 'SELECT count(*) AS totallogs, facility, YEAR(datetime) AS datetimeyear, DAYOFYEAR(datetime) AS datetimeday FROM '.$srcTable.' ';
	if($where) $query = $query.'WHERE '.$where;
	$query = $query.'GROUP BY facility, datetimeyear, datetimeday ORDER BY datetimeyear, datetimeday';
	$results = perform_query($query, $dbLink);
	$cols = $facilityarray;
	while ($row = fetch_array($results)) $resultarray[$row['datetimeyear']][$row['datetimeday']][$row['facility']] = $row['totallogs'];
	$safequerystring = preg_replace(array('/pageId=Daily\+Log\+Graph/', '/excludefacility=[^&]*/', '/facility(\[\])?=[^&]*/', '/&&*/'), array('pageId=Search+Syslog-ng', '', '', '&'), $_SERVER['QUERY_STRING']);
}else{
	$query = 'SELECT count(*) AS totallogs, priority, YEAR(datetime) AS datetimeyear, DAYOFYEAR(datetime) AS datetimeday FROM '.$srcTable.' ';
	if($where) $query = $query.'WHERE '.$where;
	$query = $query.'GROUP BY priority, datetimeyear, datetimeday ORDER BY datetimeyear, datetimeday';
	$results = perform_query($query, $dbLink);
	$cols = $priorityarray;
	while ($row = fetch_array($results)) $resultarray[$row['datetimeyear']][$row['datetimeday']][$row['priority']] = $row['totallogs'];
	$safequerystring = preg_replace(array('/pageId=Daily\+Log\+Graph/', '/excludepriority=[^&]*/', '/priority(\[\])?=[^&]*/', '/&&*/'), array('pageId=Search+Syslog-ng', '', '', '&'), $_SERVER['QUERY_STRING']);
}

//------------------------------------------------------------------------
// Execute the query
// The FOUND_ROWS function returns the value from the SQL_CALC_FOUND_ROWS
// count.
//------------------------------------------------------------------------
//========================================================================
// END: BUILD AND EXECUTE SQL STATEMENT
// AND BUILD PARAMETER LIST FOR HTML GETS
//========================================================================

//========================================================================
// BEGIN: PREPARE RESULT ARRAY
//========================================================================
//------------------------------------------------------------------------
// Collapse consecutive identical messages into one line
//------------------------------------------------------------------------
foreach(array_keys($resultarray) as $year){
	foreach(array_keys($resultarray[$year]) as $day){
		$resultarray[$year][$day]['date'] = date('m/d/Y', strtotime('12/31/' . ($year - 1) . ' + ' . $day . ' days'));
	}
}
$arrdates = array();
foreach(array_keys($resultarray) as $year){
	foreach(array_keys($resultarray[$year]) as $day){
		$arrdates[] = $resultarray[$year][$day]['date'];
	}
}

//========================================================================
// END: PREPARE RESULT ARRAY
//========================================================================

$colsarray = array();
foreach(array_keys($resultarray) as $year){
	foreach(array_keys($resultarray[$year]) as $day){
		foreach($cols as $col){
			if(!isset($resultarray[$year][$day][$col])){
				$colsarray[$col][] = 0;
			}else{
				$colsarray[$col][] = $resultarray[$year][$day][$col];
			}
		}
	}
}

//========================================================================
// BEGIN: BUILDING THE HTML PAGE
//========================================================================
// Print result sub-header
require_once(BASE_PATH . '/syslog-ng_result_subheader.php');
?>
<table align='center'>
<tr><td>
<?php

$graph = new Graph(1024, 768,'auto');
//$graph->SetScale('textlog');
$graph->SetScale($scale);
$graph->SetMarginColor('white');
$graph->img->SetMargin(40,90,20,80);

//$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#A0A0A0','#909090');
$graph->xgrid->Show();

if(count($colsarray[$cols[0]]) == 1){
	$graph->xaxis->SetTickLabels($cols);
}else{
	$graph->xaxis->SetTickLabels($arrdates);
}
$graph->xaxis->SetLabelAngle(90);

if($blnByFacility){
	$graph->title->Set('Facility Entries Per Day');
	//$graph->xaxis->title->Set('Date');
	//$graph->yaxis->title->Set('Log Entries');
}else{
	$graph->title->Set('Priority Entries Per Day');
	//$graph->xaxis->title->Set('Date');
	//$graph->yaxis->title->Set('Log Entries');
}

$lines = array();
$colors = array('#00FFFF','#808080','#000080','#C0C0C0',
	'#000000','#008000','#808000','#008080',
	'#0000FF','#00FF00','#800080','#FFFFFF',
	'#FF00FF','#800000','#FF0000','#FFFF00');
$cindex = 0;
if(count($colsarray[$cols[0]]) == 1){
	$data = array();
	$targ = array();
	$alts = array();
	foreach($cols as $col){
		$data[] = $colsarray[$col][0];
		if(!$blnByFacility){
			$targ[] = $_SERVER['PHP_SELF'] . '?' . $safequerystring . '&excludepriority=0&priority%5B%5D=' . $col;
			$alts[] = 'View the ' . $col . ' priority logs';
		}else{
			$targ[] = $_SERVER['PHP_SELF'] . '?' . $safequerystring . '&excludefacility=0&facility%5B%5D=' . $col;
			$alts[] = 'View the ' . $col . ' facility logs';
		}
	}
	$bars = new BarPlot($data);
	if(!$blnByFacility){
		$bars->SetFillColor(array('#ffffff','#dfffdf','#a0ffa0','#ffff00','#ff8f00','#ffa0a0','#ff0000','#ff00ff'));
	}else{
		$bars->SetFillColor($colors);
	}
	$bars->SetCSIMTargets($targ,$alts);
	$graph->Add($bars);
}else{
	$types = array(MARK_SQUARE, MARK_UTRIANGLE, MARK_DTRIANGLE, MARK_DIAMOND, MARK_FILLEDCIRCLE);
	$safequerystring = preg_replace(array('/date2?=[^&]*/', '/time=[^&]*/', '/time2=[^&]*/'), array('', 'time=00%3A00%3A00', 'time2=23%3A59%3A59'), $safequerystring);
	foreach($cols as $col){
		$targ = array();
		$alts = array();
		foreach(array_keys($resultarray) as $year){
			foreach(array_keys($resultarray[$year]) as $day){
				$date = date('Y-m-d', strtotime($resultarray[$year][$day]['date']));
				if(!$blnByFacility){
					$targ[] = $_SERVER['PHP_SELF'] . '?' . $safequerystring . '&excludepriority=0&priority%5B%5D=' . $col . '&date=' . $date . '&date2=' . $date;
					$alts[] = 'View the ' . $col . ' priority logs for ' . $resultarray[$year][$day]['date'];
				}else{
					$targ[] = $_SERVER['PHP_SELF'] . '?' . $safequerystring . '&excludefacility=0&facility%5B%5D=' . $col . '&date=' . $date . '&date2=' . $date;
					$alts[] = 'View the ' . $col . ' facility logs for ' . $resultarray[$year][$day]['date'];
				}
			}
		}
		$lines[$col] = new LinePlot($colsarray[$col]);
		$lines[$col]->mark->SetType($types[$cindex % count($types)]);
		$lines[$col]->SetCSIMTargets($targ,$alts);
		$lines[$col]->SetWeight(2);
		$lines[$col]->SetLegend(strtoupper($col));
		if(!$blnByFacility){
			if($col == 'debug') {
				$lines[$col]->SetColor('#ffffff');
				$lines[$col]->mark->SetFillColor('#ffffff');
			}elseif($col == 'info') {
				$lines[$col]->SetColor('#dfffdf');
				$lines[$col]->mark->SetFillColor('#dfffdf');
			}elseif ($col == 'notice') {
				$lines[$col]->SetColor('#a0ffa0');
				$lines[$col]->mark->SetFillColor('#a0ffa0');
			}elseif ($col == 'warning') {
				$lines[$col]->SetColor('#ffff00');
				$lines[$col]->mark->SetFillColor('#ffff00');
			}elseif ($col == 'err') {
				$lines[$col]->SetColor('#ff8f00');
				$lines[$col]->mark->SetFillColor('#ff8f00');
			}elseif ($col == 'crit') {
				$lines[$col]->SetColor('#ffa0a0');
				$lines[$col]->mark->SetFillColor('#ffa0a0');
			}elseif ($col == 'alert') {
				$lines[$col]->SetColor('#ff0000');
				$lines[$col]->mark->SetFillColor('#ff0000');
			}elseif ($col == 'emerg') {
				$lines[$col]->SetColor('#ff00ff');
				$lines[$col]->mark->SetFillColor('#ff00ff');
			}
		}else{
			$lines[$col]->SetColor($colors[$cindex % count($colors)]);
			$lines[$col]->mark->SetFillColor($colors[$cindex % count($colors)]);
		}
		$cindex++;
		$graph->Add($lines[$col]);
	}
}

$graph->legend->Pos(0.01,0.5,'right','center');

$ih = $graph->Stroke(_IMG_HANDLER);
ob_start();
$graph->img->Stream();
$img = base64_encode(ob_get_contents());
ob_end_clean();

$imgMap = $graph->GetHTMLImageMap('imgmap');
// die('?offset='.$offset. 'PPP' .$ParamsGET);
echo $imgMap, '<TD ALIGN="center"><img src="data:image/gif;base64,', $img, '" ismap usemap="#imgmap" border="0"></TD></TR>';
//========================================================================
// END: BUILDING THE HTML PAGE
//========================================================================
?>
Return current item: Php-Logging-System