Added support for logstream maintenance by using php over commandline

This commit is contained in:
Andre Lorbach 2008-11-18 17:05:00 +01:00
parent 7afe7b52cd
commit 5ddc6f1374
6 changed files with 307 additions and 32 deletions

View File

@ -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);
}
}

2
src/cron/maintenance.bat Normal file
View File

@ -0,0 +1,2 @@
C:\php\php.exe -q -a .\maintenance.php cleardata 2 olderthan 86400

224
src/cron/maintenance.php Normal file
View File

@ -0,0 +1,224 @@
<?php
/*
*********************************************************************
* phpLogCon - http://www.phplogcon.org
* -----------------------------------------------------------------
* Sources Admin File
*
* -> 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 <http://www.gnu.org/licenses/>.
*
* 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);
}
// ---
// ---
?>

4
src/cron/maintenance.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
cd /var/www/phplogcon/cron/
php ./maintenance.php cleardata 2 olderthan 86400

View File

@ -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,8 +906,16 @@ function CheckUrlOrIP($ip)
function DieWithErrorMsg( $szerrmsg )
{
global $gl_root_path, $content;
echo
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(
"<html><title>phpLogCon :: Critical Error occured</title><head>" .
"<link rel=\"stylesheet\" href=\"" . $gl_root_path . "themes/default/main.css\" type=\"text/css\"></head><body><br><br>" .
"<table width=\"600\" align=\"center\" class=\"with_border_alternate ErrorMsg\" cellpadding=\"2\"><tr>".
@ -910,14 +926,27 @@ function DieWithErrorMsg( $szerrmsg )
"<td class=\"tableBackground\" align=\"left\"><br>" .
$szerrmsg .
"<br><br></td></tr></table>" .
"</body></html>";
"</body></html>"
);
}
// Abort further execution
exit;
}
function DieWithFriendlyErrorMsg( $szerrmsg )
{
global $gl_root_path, $content;
echo
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(
"<html><title>phpLogCon :: Error occured</title><head>" .
"<link rel=\"stylesheet\" href=\"" . $gl_root_path . "themes/default/main.css\" type=\"text/css\"></head><body><br><br>" .
"<table width=\"600\" align=\"center\" class=\"with_border_alternate ErrorMsg\" cellpadding=\"2\"><tr>".
@ -928,7 +957,11 @@ function DieWithFriendlyErrorMsg( $szerrmsg )
"<td class=\"tableBackground\" align=\"left\"><br>" .
$szerrmsg .
"<br><br></td></tr></table>" .
"</body></html>";
"</body></html>"
);
}
// 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";

View File

@ -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_'] = "";
?>