diff --git a/src/classes/logstreamdb.class.php b/src/classes/logstreamdb.class.php index c32cc90..59a4e85 100644 --- a/src/classes/logstreamdb.class.php +++ b/src/classes/logstreamdb.class.php @@ -664,8 +664,8 @@ class LogStreamDB extends LogStream { // Get affected rows and return! $rowcount = mysql_affected_rows(); - // Free query now - mysql_free_result ($myQuery); + // Free result not needed here! + //mysql_free_result ($myQuery); } } diff --git a/src/cron/maintenance.bat b/src/cron/maintenance.bat new file mode 100644 index 0000000..dfd820d --- /dev/null +++ b/src/cron/maintenance.bat @@ -0,0 +1,2 @@ + +C:\php\php.exe -q -a .\maintenance.php cleardata 2 olderthan 86400 diff --git a/src/cron/maintenance.php b/src/cron/maintenance.php new file mode 100644 index 0000000..44b0287 --- /dev/null +++ b/src/cron/maintenance.php @@ -0,0 +1,224 @@ + This file is meant for command line maintenance + * + * All directives are explained within this file + * + * Copyright (C) 2008 Adiscon GmbH. + * + * This file is part of phpLogCon. + * + * PhpLogCon is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PhpLogCon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with phpLogCon. If not, see . + * + * A copy of the GPL can be found in the file "COPYING" in this + * distribution + ********************************************************************* +*/ + +// *** Default includes and procedures *** // +define('IN_PHPLOGCON', true); +$gl_root_path = './../'; + +// Now include necessary include files! +include($gl_root_path . 'include/functions_common.php'); + +// Set commandline mode for the script +define('IN_PHPLOGCON_COMMANDLINE', true); +$content['IN_PHPLOGCON_COMMANDLINE'] = true; +InitPhpLogCon(); +InitSourceConfigs(); + +// Init admin langauge file now! +IncludeLanguageFile( $gl_root_path . '/lang/' . $LANG . '/admin.php' ); +// *** *** // + +// --- Helper functions +/* +* Cleans data in desired logstream +*/ +function CleanData($optParam1, $optParam2, $optParam3, $optParam4) +{ + global $content, $gl_root_path; + + // Get Source reference! + $mySource = $content['Sources'][ $content['SOURCEID'] ]; + + // Check Source Type + if ( $mySource['SourceType'] == SOURCE_DB || $mySource['SourceType'] == SOURCE_PDO ) + { + // Include LogStream facility + include($gl_root_path . 'classes/logstream.class.php'); + + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "CleanData", GetAndReplaceLangStr($content["LN_CMD_CLEANINGDATAFOR"], $mySource['Name']) ); + + // Create LogStream Object + $stream = $mySource['ObjRef']->LogStreamFactory($mySource['ObjRef']); + $res = $stream->Verify(); + if ( $res == SUCCESS ) + { + // Gather Database Stats + $content['ROWCOUNT'] = $stream->GetLogStreamTotalRowCount(); + if ( isset($content['ROWCOUNT']) ) + { + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "CleanData", GetAndReplaceLangStr($content["LN_CMD_ROWSFOUND"], $content['ROWCOUNT'], $mySource['Name']) ); + + if ( $optParam1 != NULL ) + { + if ( $optParam1 == "all" ) + { + $timestamp = 0; + } + else if ( $optParam1 == "olderthan" && $optParam2 != NULL ) + { + // Take current time and subtract Seconds + $nSecondsSubtract = intval($optParam2); + $timestamp = time() - $nSecondsSubtract; + } + else if ( $optParam1 == "date" && $optParam2 != NULL && $optParam3 != NULL && $optParam4 != NULL ) + { + // Generate Timestamp + $timestamp = mktime( 0, 0, 0, intval($optParam2), intval($optParam3), intval($optParam4) ); + } + else + // Print error and die! + DieWithErrorMsg( $content["LN_CMD_WRONGSUBOPORMISSING"] ); + + // Continue with delete only if $timestamp is set! + if ( isset($timestamp) ) + { + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "CleanData", GetAndReplaceLangStr($content["LN_CMD_DELETINGOLDERTHEN"], date("Y-m-d", $timestamp) ) ); + + // Now perform the data cleanup! + $content['affectedrows'] = $stream->CleanupLogdataByDate($timestamp); + + if ( isset($content['affectedrows']) ) + { + //Debug Output + PrintHTMLDebugInfo( DEBUG_INFO, "CleanData", GetAndReplaceLangStr($content["LN_CMD_DELETEDROWS"], $content['affectedrows']) ); + } + else + // Print error and die! + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_CMD_FAILEDTOCLEANDATA"], $mySource['Name']) ); + } + } + else + // Print error and die! + DieWithErrorMsg( $content["LN_CMD_SUBPARAM1MISSING"] ); + } + else + // Print error and die! + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_CMD_COULDNOTGETROWCOUNT"], $mySource['Name']) ); + } + else + { + // Print error and die! + $szErroMsg = GetAndReplaceLangStr($content["LN_SOURCES_ERROR_WITHINSOURCE"],$mySource['Name'], GetErrorMessage($res)); + if ( isset($extraErrorDescription) ) + $szErroMsg .= "\r\n\r\n" . GetAndReplaceLangStr( $content['LN_SOURCES_ERROR_EXTRAMSG'], $extraErrorDescription); + DieWithErrorMsg( $szErroMsg ); + } + } + else + // Print error and die! + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_SOURCES_ERROR_NOCLEARSUPPORT"], $content['SOURCEID']) ); +} +// --- + + +// --- BEGIN Custom Code + //Additional Includes + include($gl_root_path . 'include/functions_debugoutput.php'); + + // Run into Commandline part now! + /* Only run if we are in command line mode + * + * Possible Operation Types: + * cleandata = If you want to clear data from a logstream source, you can use the operation type. + * Be carefull using this option, any deletion process cannot be undone! + * Sample 1: Delete all data in the logstream with id 2 + * php maintenance.php cleardata 2 all + * Sample 2: Delete all data older then 60 seconds in the logstream with id 2 + * php maintenance.php cleardata 2 olderthan 60 + * Sample 3: Delete all data before 2008-11-18 in the logstream with id 2 + * php maintenance.php cleardata 2 date 11 18 2008 + * + */ + + // Init DebugHeader + PrintDebugInfoHeader(); + + // --- Now read command line args! + // Operation argv + if ( isset($_SERVER["argv"][1]) ) + $operation = $_SERVER["argv"][1]; + else + DieWithErrorMsg( $content["LN_CMD_NOOP"] ); + + // SourceID argv + if ( isset($_SERVER["argv"][2]) ) + { + // Set to SourceID property! + $content['SOURCEID'] = intval( $_SERVER["argv"][2] ); + + // Check if exists + if ( !isset($content['Sources'][ $content['SOURCEID'] ]) ) + DieWithErrorMsg( GetAndReplaceLangStr($content["LN_CMD_LOGSTREAMNOTFOUND"], $content['SOURCEID']) ); + } + else + DieWithErrorMsg( $content["LN_CMD_NOLOGSTREAM"] ); + + + // First Optional Parameter + if ( isset($_SERVER["argv"][3]) ) + $optparam1 = $_SERVER["argv"][3]; + else + $optparam1 = NULL; + + // Second Optional Parameter + if ( isset($_SERVER["argv"][4]) ) + $optparam2 = $_SERVER["argv"][4]; + else + $optparam2 = NULL; + + // Third Optional Parameter + if ( isset($_SERVER["argv"][5]) ) + $optParam3 = $_SERVER["argv"][5]; + else + $optParam3 = NULL; + + // fourth Optional Parameter + if ( isset($_SERVER["argv"][6]) ) + $optParam4 = $_SERVER["argv"][6]; + else + $optParam4 = NULL; + // --- + + // --- Operation Handling now + if ( $operation == "cleandata" ) + { + // Run Parser only + CleanData($optparam1, $optparam2, $optParam3, $optParam4); + } + // --- +// --- + +?> \ No newline at end of file diff --git a/src/cron/maintenance.sh b/src/cron/maintenance.sh new file mode 100644 index 0000000..d068067 --- /dev/null +++ b/src/cron/maintenance.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cd /var/www/phplogcon/cron/ +php ./maintenance.php cleardata 2 olderthan 86400 diff --git a/src/include/functions_common.php b/src/include/functions_common.php index d3e9750..f4a444d 100644 --- a/src/include/functions_common.php +++ b/src/include/functions_common.php @@ -484,10 +484,15 @@ function InitPhpDebugMode() function CheckAndSetRunMode() { global $content, $RUNMODE; + // Set to command line mode if argv is set! - if ( !isset($_SERVER["GATEWAY_INTERFACE"]) ) + if ( !isset($_SERVER["SERVER_SOFTWARE"]) ) $RUNMODE = RUNMODE_COMMANDLINE; + // Check if we require the command line mode! + if ( defined("IN_PHPLOGCON_COMMANDLINE") && $RUNMODE != RUNMODE_COMMANDLINE ) + DieWithErrorMsg( "This PHP Script cannot be run within the webserver process, it designed to run over command line." ); + // Obtain max_execution_time $content['MaxExecutionTime'] = ini_get("max_execution_time"); @@ -722,7 +727,10 @@ function InitConfigurationValues() if ( GetConfigSetting("UserDBLoginRequired", false) ) { // Redirect USER if not on loginpage or installpage! - if ( !defined("IS_NOLOGINPAGE") && !defined("IN_PHPLOGCON_INSTALL") ) + if ( !defined("IS_NOLOGINPAGE") && + !defined("IN_PHPLOGCON_INSTALL") && + !defined("IN_PHPLOGCON_COMMANDLINE") + ) RedirectToUserLogin(); } else if ( defined('IS_ADMINPAGE') ) @@ -898,37 +906,62 @@ function CheckUrlOrIP($ip) function DieWithErrorMsg( $szerrmsg ) { - global $gl_root_path, $content; - echo - "phpLogCon :: Critical Error occured" . - "

" . - "". - "" . - "" . - "
" . - "

Critical Error occured

" . - "
Errordetails:
" . - $szerrmsg . - "

" . - ""; + global $RUNMODE, $content, $gl_root_path; + if ( $RUNMODE == RUNMODE_COMMANDLINE ) + { + print("\n\n\tCritical Error occured\t-\tErrordetails:\n"); + print("\t" . $szerrmsg . "\n\n"); + print("\tTerminating now!\n"); + } + else if ( $RUNMODE == RUNMODE_WEBSERVER ) + { + print( + "phpLogCon :: Critical Error occured" . + "

" . + "". + "" . + "" . + "
" . + "

Critical Error occured

" . + "
Errordetails:
" . + $szerrmsg . + "

" . + "" + ); + } + + // Abort further execution exit; } function DieWithFriendlyErrorMsg( $szerrmsg ) { - global $gl_root_path, $content; - echo - "phpLogCon :: Error occured" . - "

" . - "". - "" . - "" . - "
" . - "

Error occured

" . - "
Errordetails:
" . - $szerrmsg . - "

" . - ""; + global $RUNMODE, $content, $gl_root_path; + if ( $RUNMODE == RUNMODE_COMMANDLINE ) + { + print("\n\n\t\tError occured\n"); + print("\t\tErrordetails:\t" . $szerrmsg . "\n"); + print("\t\tTerminating now!\n"); + } + else if ( $RUNMODE == RUNMODE_WEBSERVER ) + { + + print( + "phpLogCon :: Error occured" . + "

" . + "". + "" . + "" . + "
" . + "

Error occured

" . + "
Errordetails:
" . + $szerrmsg . + "

" . + "" + ); + } + + // Abort further execution exit; } @@ -1316,8 +1349,7 @@ function StartPHPSession() if ( $RUNMODE == RUNMODE_WEBSERVER ) { // This will start the session - if (session_id() == "") - session_start(); + @session_start(); if ( !isset($_SESSION['SESSION_STARTED']) ) $_SESSION['SESSION_STARTED'] = "true"; diff --git a/src/lang/en/admin.php b/src/lang/en/admin.php index 70699d4..919c82d 100644 --- a/src/lang/en/admin.php +++ b/src/lang/en/admin.php @@ -344,5 +344,18 @@ $content['LN_PARSERS_ERROR_HASBEENADDED'] = "All required settings ('%2' custom $content['LN_PARSERS_ERROR_NOFIELDS'] = "The Message Parser '%1' does not have any custom fields to add."; $content['LN_PARSERS_'] = ""; +// Command Line stuff +$content['LN_CMD_NOOP'] = "Operation parameter is missing"; +$content['LN_CMD_NOLOGSTREAM'] = "The logstream source parameter is missing"; +$content['LN_CMD_LOGSTREAMNOTFOUND'] = "Logstream Source with ID '%1' could not be found in the Database!"; +$content['LN_CMD_COULDNOTGETROWCOUNT'] = "Could not obtain rowcount from logstream source '%1'"; +$content['LN_CMD_SUBPARAM1MISSING'] = "Subparameter 1 is missing, it should be set to 'all', 'since' or 'date'. For more details see the documentation."; +$content['LN_CMD_WRONGSUBOPORMISSING'] = "Either the sub-operation is wrong, or another parameter is missing"; +$content['LN_CMD_FAILEDTOCLEANDATA'] = "Failed to cleandata for the logstream '%1'."; +$content['LN_CMD_CLEANINGDATAFOR'] = "Cleaning data for logstream source '%1'."; +$content['LN_CMD_ROWSFOUND'] = "Successfully connected and found '%1' rows in the logstream source."; +$content['LN_CMD_DELETINGOLDERTHEN'] = "Performing deletion of data entries older then '%1'."; +$content['LN_CMD_DELETEDROWS'] = "Successfully Deleted '%1' rows in the logstream source.'"; +$content['LN_CMD_'] = ""; ?> \ No newline at end of file