From 3a7826b6f81dc58d4fc7805ae384030cbd068701 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Sep 2008 13:17:02 +0200 Subject: [PATCH] Implemented dynamic charts into the configuration system --- src/chartgenerator.php | 12 +++++ src/include/config.sample.php | 7 +++ src/include/constants_errors.php | 1 + src/include/db_template.txt | 18 ++++++++ src/include/db_update_v3.txt | 17 +++++++ src/include/functions_common.php | 7 ++- src/include/functions_config.php | 53 +++++++++++++++++++++ src/include/functions_db.php | 2 +- src/include/functions_installhelpers.php | 31 +++++++++++++ src/statistics.php | 59 ++++++++++++------------ src/templates/statistics.html | 33 ++++++------- 11 files changed, 189 insertions(+), 51 deletions(-) create mode 100644 src/include/db_update_v3.txt diff --git a/src/chartgenerator.php b/src/chartgenerator.php index 6cb29b5..a876958 100644 --- a/src/chartgenerator.php +++ b/src/chartgenerator.php @@ -96,6 +96,18 @@ if ( isset($_GET['maxrecords']) ) if ( $content['maxrecords'] < 2 || $content['maxrecords'] > 100 ) $content['maxrecords'] = 10; } +else + $content['maxrecords'] = 10; + +if ( isset($_GET['showpercent']) ) +{ + // read and verify value + $content['showpercent'] = intval($_GET['showpercent']); + if ( $content['showpercent'] >= 1 ) + $content['showpercent'] = 1; + else + $content['showpercent'] = 0; +} else $content['maxrecords'] = 10; // --- diff --git a/src/include/config.sample.php b/src/include/config.sample.php index f2a678f..9ad76f6 100644 --- a/src/include/config.sample.php +++ b/src/include/config.sample.php @@ -98,6 +98,13 @@ $CFG['Search'][] = array ( "DisplayName" => "All messages from last 31 days", "S // $CFG['Search'][] = array ( "DisplayName" => "", "SearchQuery" => "" ); // --- +// --- Predefined Charts! +$CFG['Charts'][] = array ( "DisplayName" => "Top Hosts", "chart_type" => CHART_BARS_HORIZONTAL, "chart_width" => 400, "chart_field" => SYSLOG_HOST, "maxrecords" => 10, "showpercent" => 0 ); +$CFG['Charts'][] = array ( "DisplayName" => "SyslogTags", "chart_type" => CHART_CAKE, "chart_width" => 400, "chart_field" => SYSLOG_SYSLOGTAG, "maxrecords" => 10, "showpercent" => 0 ); +$CFG['Charts'][] = array ( "DisplayName" => "Severities", "chart_type" => CHART_BARS_VERTICAL, "chart_width" => 400, "chart_field" => SYSLOG_SEVERITY, "maxrecords" => 10, "showpercent" => 1 ); +$CFG['Charts'][] = array ( "DisplayName" => "Date", "chart_type" => CHART_CAKE, "chart_width" => 400, "chart_field" => SYSLOG_DATE, "maxrecords" => 10, "showpercent" => 0 ); +// --- + // --- Source Options /* Example for DiskType Source: $CFG['Sources']['Source1']['ID'] = "Source1"; diff --git a/src/include/constants_errors.php b/src/include/constants_errors.php index 4cefa0a..c80bfe4 100644 --- a/src/include/constants_errors.php +++ b/src/include/constants_errors.php @@ -61,6 +61,7 @@ define('ERROR_DB_TABLENOTFOUND', 17); define('ERROR_DB_DBFIELDNOTFOUND', 19); define('ERROR_MSG_NOMATCH', 18); +define('ERROR_CHARTS_NOTCONFIGURED', 20); ?> diff --git a/src/include/db_template.txt b/src/include/db_template.txt index 2eb633d..b5a9ea3 100644 --- a/src/include/db_template.txt +++ b/src/include/db_template.txt @@ -109,3 +109,21 @@ CREATE TABLE IF NOT EXISTS `logcon_views` ( `groupid` int(11) default NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Stores custom defined user views.' AUTO_INCREMENT=1 ; + +-- +-- Table structure for table `logcon_charts` +-- + +DROP TABLE IF EXISTS `logcon_charts`; +CREATE TABLE IF NOT EXISTS `logcon_charts` ( + `ID` int(11) NOT NULL auto_increment, + `DisplayName` varchar(255) NOT NULL, + `chart_type` int(11) NOT NULL, + `chart_width` int(11) NOT NULL, + `chart_field` varchar(255) NOT NULL, + `maxrecords` int(11) NOT NULL, + `showpercent` tinyint(1) NOT NULL, + `userid` int(11) default NULL, + `groupid` int(11) default NULL, + PRIMARY KEY (`ID`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This table contains all configured charts' AUTO_INCREMENT=1 ; diff --git a/src/include/db_update_v3.txt b/src/include/db_update_v3.txt new file mode 100644 index 0000000..3c7ccac --- /dev/null +++ b/src/include/db_update_v3.txt @@ -0,0 +1,17 @@ +-- New Database Structure Updates +CREATE TABLE IF NOT EXISTS `logcon_charts` ( + `ID` int(11) NOT NULL auto_increment, + `DisplayName` varchar(255) NOT NULL, + `chart_type` int(11) NOT NULL, + `chart_width` int(11) NOT NULL, + `chart_field` varchar(255) NOT NULL, + `maxrecords` int(11) NOT NULL, + `showpercent` tinyint(1) NOT NULL, + `userid` int(11) default NULL, + `groupid` int(11) default NULL, + PRIMARY KEY (`ID`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This table contains all configured charts' AUTO_INCREMENT=1 ; + +-- Insert data + +-- Updated Data diff --git a/src/include/functions_common.php b/src/include/functions_common.php index 809654d..523c6f8 100644 --- a/src/include/functions_common.php +++ b/src/include/functions_common.php @@ -582,6 +582,9 @@ function InitConfigurationValues() // Load Configured Searches LoadSearchesFromDatabase(); + // Load Configured Charts + LoadChartsFromDatabase(); + // Load Configured Views LoadViewsFromDatabase(); @@ -1227,7 +1230,9 @@ function GetErrorMessage($errorCode) return $content['LN_ERROR_DB_INVALIDDBDRIVER']; case ERROR_DB_TABLENOTFOUND: return $content['LN_ERROR_DB_TABLENOTFOUND']; - + + case ERROR_CHARTS_NOTCONFIGURED: + return $content['LN_ERROR_CHARTS_NOTCONFIGURED']; default: return GetAndReplaceLangStr( $content['LN_ERROR_UNKNOWN'], $errorCode ); diff --git a/src/include/functions_config.php b/src/include/functions_config.php index fccf720..3d00a18 100644 --- a/src/include/functions_config.php +++ b/src/include/functions_config.php @@ -314,6 +314,7 @@ function InitPhpLogConConfigFile($bHandleMissing = true) define('DB_SOURCES', $tblPref . "sources"); define('DB_USERS', $tblPref . "users"); define('DB_VIEWS', $tblPref . "views"); + define('DB_CHARTS', $tblPref . "charts"); // Legacy support for old columns definition format! if ( isset($CFG['Columns']) && is_array($CFG['Columns']) ) @@ -394,6 +395,58 @@ function LoadSearchesFromDatabase() } } +/* +* Helper function to load configured Searches from the database +*/ +function LoadChartsFromDatabase() +{ + // Needed to make global + global $CFG, $content; + + // --- Create SQL Query + // Create Where for USERID + if ( isset($content['SESSION_LOGGEDIN']) && $content['SESSION_LOGGEDIN'] ) + $szWhereUser = " OR " . DB_CHARTS . ".userid = " . $content['SESSION_USERID'] . " "; + else + $szWhereUser = ""; + + if ( isset($content['SESSION_GROUPIDS']) ) + $szGroupWhere = " OR " . DB_CHARTS . ".groupid IN (" . $content['SESSION_GROUPIDS'] . ")"; + else + $szGroupWhere = ""; + $sqlquery = " SELECT " . + DB_CHARTS . ".ID, " . + DB_CHARTS . ".DisplayName, " . + DB_CHARTS . ".chart_type, " . + DB_CHARTS . ".chart_width, " . + DB_CHARTS . ".chart_field, " . + DB_CHARTS . ".maxrecords, " . + DB_CHARTS . ".showpercent, " . + DB_CHARTS . ".userid, " . + DB_CHARTS . ".groupid, " . + DB_USERS . ".username, " . + DB_GROUPS . ".groupname " . + " FROM " . DB_CHARTS . + " LEFT OUTER JOIN (" . DB_USERS . ") ON (" . DB_CHARTS . ".userid=" . DB_USERS . ".ID ) " . + " LEFT OUTER JOIN (" . DB_GROUPS . ") ON (" . DB_CHARTS . ".groupid=" . DB_GROUPS . ".ID ) " . + " WHERE (" . DB_CHARTS . ".userid IS NULL AND " . DB_CHARTS . ".groupid IS NULL) " . + $szWhereUser . + $szGroupWhere . + " ORDER BY " . DB_CHARTS . ".userid, " . DB_CHARTS . ".groupid, " . DB_CHARTS . ".DisplayName"; + // --- + + // Get Searches from DB now! + $result = DB_Query($sqlquery); + $myrows = DB_GetAllRows($result, true); + if ( isset($myrows ) && count($myrows) > 0 ) + { + // Overwrite Search Array with Database one + $CFG['Charts'] = $myrows; + $content['Charts'] = $myrows; + } +} + + function LoadViewsFromDatabase() { // Needed to make global diff --git a/src/include/functions_db.php b/src/include/functions_db.php index ea3839d..d9e0ce2 100644 --- a/src/include/functions_db.php +++ b/src/include/functions_db.php @@ -45,7 +45,7 @@ $errdesc = ""; $errno = 0; // --- Current Database Version, this is important for automated database Updates! -$content['database_internalversion'] = "2"; // Whenever incremented, a database upgrade is needed +$content['database_internalversion'] = "3"; // Whenever incremented, a database upgrade is needed $content['database_installedversion'] = "0"; // 0 is default which means Prior Versioning Database // --- diff --git a/src/include/functions_installhelpers.php b/src/include/functions_installhelpers.php index e0e5cfb..59768d2 100644 --- a/src/include/functions_installhelpers.php +++ b/src/include/functions_installhelpers.php @@ -87,6 +87,9 @@ function ConvertGeneralSettings() SaveGeneralSettingsIntoDB(); } +/* +* Convert Custom searches into DB +*/ function ConvertCustomSearches() { global $CFG, $content; @@ -102,6 +105,34 @@ function ConvertCustomSearches() } } +/* +* Convert Custom Charts into DB +*/ +function ConvertCustomCharts() +{ + global $CFG, $content; + + // Insert all searches into the DB! + foreach($CFG['Charts'] as $chartid => &$myChart) + { + // New Entry + $result = DB_Query("INSERT INTO " . DB_CHARTS . " (DisplayName, chart_type, chart_width, chart_field, maxrecords, showpercent) + VALUES ( + '" . PrepareValueForDB($myChart['DisplayName']) . "', + " . intval($mySearch['chart_type']) . ", + " . intval($mySearch['chart_width']) . ", + '" . PrepareValueForDB($mySearch['chart_field']) . "', + " . intval($mySearch['maxrecords']) . ", + " . intval($mySearch['showpercent']) . " + )"); + $myChart['DBID'] = DB_ReturnLastInsertID($result); + DB_FreeQuery($result); + } +} + +/* +* Convert Custom Views into DB +*/ function ConvertCustomViews() { global $CFG, $content; diff --git a/src/statistics.php b/src/statistics.php index 3de9ebc..522bbfc 100644 --- a/src/statistics.php +++ b/src/statistics.php @@ -53,43 +53,44 @@ InitFilterHelpers(); // Helpers for frontend filtering! // --- // --- BEGIN Custom Code -/*if ( isset($content['Sources'][$currentSourceID]) ) + +if ( isset($content['Charts']) ) { - // Obtain and get the Config Object - $stream_config = $content['Sources'][$currentSourceID]['ObjRef']; + // This will enable to Stats View + $content['statsenabled'] = true; - // Create LogStream Object - $stream = $stream_config->LogStreamFactory($stream_config); - $res = $stream->Open( $content['AllColumns'], true ); - if ( $res == SUCCESS ) + // PreProcess Charts Array for display! + $i = 0; // Help counter! + foreach ($content['Charts'] as &$myChart ) { - // This will enable to Stats View - $content['statsenabled'] = "true"; - - - } - else - { - // This will disable to Stats View and show an error message - $content['statsenabled'] = "false"; - - // Set error code - $content['error_code'] = $ret; - - if ( $ret == ERROR_FILE_NOT_FOUND ) - $content['detailederror'] = $content['LN_ERROR_FILE_NOT_FOUND']; - else if ( $ret == ERROR_FILE_NOT_READABLE ) - $content['detailederror'] = $content['LN_ERROR_FILE_NOT_READABLE']; - else - $content['detailederror'] = $content['LN_ERROR_UNKNOWN']; + // --- Set CSS Class + if ( $i % 2 == 0 ) + { + $myChart['cssclass'] = "line1"; + $myChart['rowbegin'] = ''; + $myChart['rowend'] = ''; + } + else + { + $myChart['cssclass'] = "line2"; + $myChart['rowbegin'] = ''; + $myChart['rowend'] = ''; + } + $i++; + // --- } - // Close file! - $stream->Close(); } -*/ +else +{ + // This will disable to Stats View and show an error message + $content['statsenabled'] = false; + // Set error code + $content['ISERROR'] = true; + $content['ERROR_MSG'] = GetErrorMessage(ERROR_CHARTS_NOTCONFIGURED); +} // --- // --- BEGIN CREATE TITLE diff --git a/src/templates/statistics.html b/src/templates/statistics.html index 1d955b3..67c6be0 100644 --- a/src/templates/statistics.html +++ b/src/templates/statistics.html @@ -12,32 +12,25 @@

- + +
- - - - +{rowend} +
{LN_MENU_STATISTICS}
-

- - - + +{rowbegin} +

+
{LN_STATS_GRAPH}: {LN_STATS_COUNTBYSOURCE}
+ +
{DisplayName}
+ +
-

-
-

- - - - -
{LN_STATS_GRAPH}: {LN_STATS_COUNTBYSYSLOGTAG}
- -

-
+ \ No newline at end of file