Added support to use a defaultfilter in charts.

You can add custom filters to your charts, for example if you want to create charts for single servers or hosts.
This commit is contained in:
Andre Lorbach 2010-10-26 17:50:54 +02:00
parent 7315258c57
commit 85f70328da
9 changed files with 99 additions and 7 deletions

View File

@ -96,6 +96,7 @@ if ( isset($_GET['op']) )
$content['maxrecords'] = 5;
$content['showpercent'] = 0;
$content['CHECKED_ISSHOWPERCENT'] = "";
$content['chart_defaultfilter'] = "";
// Chart Field
$content['chart_field'] = SYSLOG_HOST;
CreateChartFields($content['chart_field']);
@ -149,6 +150,7 @@ if ( isset($_GET['op']) )
$content['CHECKED_ISCHARTENABLED'] = "";
$content['chart_width'] = $myChart['chart_width'];
$content['maxrecords'] = $myChart['maxrecords'];
$content['chart_defaultfilter'] = $myChart['chart_defaultfilter'];
$content['showpercent'] = $myChart['showpercent'];
if ( $myChart['showpercent'] == 1 )
$content['CHECKED_ISSHOWPERCENT'] = "checked";
@ -261,6 +263,7 @@ if ( isset($_POST['op']) )
if ( isset($_POST['chart_field']) ) { $content['chart_field'] = DB_RemoveBadChars($_POST['chart_field']); }
if ( isset($_POST['maxrecords']) ) { $content['maxrecords'] = intval(DB_RemoveBadChars($_POST['maxrecords'])); }
if ( isset($_POST['showpercent']) ) { $content['showpercent'] = intval(DB_RemoveBadChars($_POST['showpercent'])); } else {$content['showpercent'] = 0; }
if ( isset($_POST['chart_defaultfilter']) ) { $content['chart_defaultfilter'] = DB_RemoveBadChars($_POST['chart_defaultfilter']); }
// User & Group handeled specially
if ( isset ($_POST['isuseronly']) )
@ -310,12 +313,13 @@ if ( isset($_POST['op']) )
if ( $_POST['op'] == "addnewchart" )
{
// Add custom search now!
$sqlquery = "INSERT INTO " . DB_CHARTS . " (DisplayName, chart_enabled, chart_type, chart_width, chart_field, maxrecords, showpercent, userid, groupid)
$sqlquery = "INSERT INTO " . DB_CHARTS . " (DisplayName, chart_enabled, chart_type, chart_width, chart_field, chart_defaultfilter, maxrecords, showpercent, userid, groupid)
VALUES ('" . $content['Name'] . "',
" . $content['chart_enabled'] . ",
" . $content['chart_type'] . ",
" . $content['chart_width'] . ",
'" . $content['chart_field'] . "',
'" . $content['chart_defaultfilter'] . "',
" . $content['maxrecords'] . ",
" . $content['showpercent'] . ",
" . $content['userid'] . ",
@ -345,6 +349,7 @@ if ( isset($_POST['op']) )
chart_type = " . $content['chart_type'] . ",
chart_width = " . $content['chart_width'] . ",
chart_field = '" . $content['chart_field'] . "',
chart_defaultfilter = '" . $content['chart_defaultfilter'] . "',
maxrecords = " . $content['maxrecords'] . ",
showpercent = " . $content['showpercent'] . ",
userid = " . $content['userid'] . ",
@ -439,6 +444,12 @@ if ( !isset($_POST['op']) && !isset($_GET['op']) )
$myChart['ChartEnabledImage'] = $content["MENU_SELECTION_DISABLED"];
// ---
// --- Set enabled or disabled state
if ( strlen($myChart['chart_defaultfilter']) > 0 )
$myChart['chart_defaultfilter_urldecoded'] = urlencode($myChart['chart_defaultfilter']);
else
$myChart['chart_defaultfilter_urldecoded'] = "";
// ---
// --- Set CSS Class
if ( $i % 2 == 0 )

View File

@ -114,6 +114,11 @@ if ( isset($_GET['showpercent']) )
}
else
$content['showpercent'] = 0;
if ( isset($_GET['defaultfilter']) )
$content['chart_defaultfilter'] = $_GET['defaultfilter'];
else
$content['chart_defaultfilter'] = "";
// ---
// --- BEGIN CREATE TITLE
@ -132,10 +137,37 @@ if ( !$content['error_occured'] )
// Create LogStream Object
$stream = $stream_config->LogStreamFactory($stream_config);
$stream->SetFilter($content['chart_defaultfilter']);
// Set Columns we want to open!
$content['ChartColumns'][] = SYSLOG_UID;
$content['ChartColumns'][] = $content['chart_field'];
// Append fields from defaultfilter as well !
if ( strlen($content['chart_defaultfilter']) > 0 )
{
$tmpFilters = explode(" ", $content['chart_defaultfilter']);
foreach($tmpFilters as $myFilter)
{
if ( strlen(trim($myFilter)) <= 0 )
continue;
if ( ($pos = strpos($myFilter, ":")) !== false
&&
($pos > 0 && substr($myFilter, $pos-1,1) != '\\') /* Only if character before is no backslash! */ )
{
// Split key and value
$tmpArray = explode(":", $myFilter, 2);
// Check if keyfield is valid and add to our Columns array if available
$newField = $stream->ReturnFilterKeyBySearchField($tmpArray[FILTER_TMP_KEY]);
if ( $newField )
$content['ChartColumns'][] = $newField;
}
}
}
// Open Data Connection!
$res = $stream->Open( $content['ChartColumns'], true );
if ( $res == SUCCESS )
{

View File

@ -541,6 +541,23 @@ abstract class LogStream {
return $this->_filters;
}
/**
* Helper function to find a fieldkey by using the SearchField
*/
public function ReturnFilterKeyBySearchField($szSearchField)
{
global $fields;
foreach ($fields as $myField)
{
if ( $myField['SearchField'] == $szSearchField )
return $myField['FieldID'];
}
return FALSE;
}
/*
* --- PIRVATE HELPERS!
@ -1016,7 +1033,7 @@ abstract class LogStream {
}
/*
* Helpre function needed in ParseFilters
* Helper function needed in SetFilterIncludeMode
*/
private function SetFilterIncludeMode(&$szValue)
{

View File

@ -885,11 +885,20 @@ class LogStreamDB extends LogStream {
$myDBQueryFieldName = "DATE( " . $myDBFieldName . ") AS " . $mySelectFieldName ;
}
// Create SQL Where Clause!
if ( $this->_SQLwhereClause == "" )
{
$res = $this->CreateSQLWhereClause();
if ( $res != SUCCESS )
return $res;
}
// Create SQL String now!
$szSql = "SELECT " .
$myDBQueryFieldName . ", " .
"count(" . $myDBFieldName . ") as TotalCount " .
" FROM " . $this->_logStreamConfigObj->DBTableName .
$this->_SQLwhereClause .
" GROUP BY " . $mySelectFieldName .
" ORDER BY TotalCount DESC" .
" LIMIT " . $nRecordLimit;

View File

@ -1045,11 +1045,20 @@ class LogStreamPDO extends LogStream {
}
}
// Create SQL Where Clause!
if ( $this->_SQLwhereClause == "" )
{
$res = $this->CreateSQLWhereClause();
if ( $res != SUCCESS )
return $res;
}
// Create SQL String now!
$szSql = "SELECT " .
$myDBQueryFieldName . ", " .
"count(" . $myDBFieldName . ") as totalcount " .
" FROM " . $this->_logStreamConfigObj->DBTableName .
$this->_SQLwhereClause .
" GROUP BY " . $mySelectFieldName .
" ORDER BY totalcount DESC";
// Append LIMIT in this case!

View File

@ -0,0 +1,7 @@
-- New Database Structure Updates
ALTER TABLE `logcon_charts` ADD `chart_defaultfilter` VARCHAR( 1024 ) NOT NULL AFTER `chart_field` ;
ALTER TABLE `logcon_sources` ADD `defaultfilter` VARCHAR( 1024 ) NOT NULL AFTER `DBRecordsPerQuery` ;
-- Insert data
-- Updated Data

View File

@ -823,6 +823,8 @@ function LoadChartsFromDatabase()
if ( $content['database_installedversion'] < 3 )
return;
// Add new fields depending on DB Version!
// --- Create SQL Query
// Create Where for USERID
if ( isset($content['SESSION_LOGGEDIN']) && $content['SESSION_LOGGEDIN'] )
@ -841,6 +843,7 @@ function LoadChartsFromDatabase()
DB_CHARTS . ".chart_type, " .
DB_CHARTS . ".chart_width, " .
DB_CHARTS . ".chart_field, " .
DB_CHARTS . ".chart_defaultfilter, " .
DB_CHARTS . ".maxrecords, " .
DB_CHARTS . ".showpercent, " .
DB_CHARTS . ".userid, " .

View File

@ -45,7 +45,7 @@ $errdesc = "";
$errno = 0;
// --- Current Database Version, this is important for automated database Updates!
$content['database_internalversion'] = "9"; // Whenever incremented, a database upgrade is needed
$content['database_internalversion'] = "10"; // Whenever incremented, a database upgrade is needed
$content['database_installedversion'] = "0"; // 0 is default which means Prior Versioning Database
// ---

View File

@ -54,7 +54,7 @@
&nbsp;<img src="{MENU_EDIT_DISABLED}" width="16" title="{LN_GEN_DISABLED}">
&nbsp;<img src="{MENU_DELETE_DISABLED}" width="16" title="{LN_GEN_DISABLED}">
<!-- ENDIF ActionsAllowed!="true" -->
&nbsp;<a href="{BASEPATH}chartgenerator.php?type={chart_type}&byfield={chart_field}&width={chart_width}&maxrecords={maxrecords}&showpercent={showpercent}" target="_blank"><img src="{MENU_CHART_PREVIEW}" width="16" title="{LN_CHARTS_PREVIEW}"></a>
&nbsp;<a href="{BASEPATH}chartgenerator.php?type={chart_type}&byfield={chart_field}&width={chart_width}&maxrecords={maxrecords}&showpercent={showpercent}&defaultfilter={chart_defaultfilter_urldecoded}" target="_blank"><img src="{MENU_CHART_PREVIEW}" width="16" title="{LN_CHARTS_PREVIEW}"></a>
</td>
</tr>
@ -112,6 +112,10 @@
<td align="left" class="cellmenu2" nowrap><b>{LN_CHART_SHOWPERCENT}</b></td>
<td align="left" class="line1"><input type="checkbox" name="showpercent" value="1" {CHECKED_ISSHOWPERCENT}></td>
</tr>
<tr>
<td align="left" class="cellmenu2_naked" width="150" valign="top" nowrap><b>{LN_CHARTS_FILTERSTRING}</b></td>
<td align="left" class="line1" width="100%"><font class="ErrorMsg">{LN_CHARTS_FILTERSTRING_HELP}</font></br><input type="text" name="chart_defaultfilter" size="100" maxlength="1024" value="{chart_defaultfilter}"></td>
</tr>
<tr>
<td align="left" class="cellmenu2"><b>{LN_GEN_USERONLY}</b></td>