mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-27 07:44:35 +02:00
2009-12-18 Ramon Novoa <rnovoa@artica.es>
* DEBIAN/control: Updated version. * util/pandora_db.pl: Added history database support. Re-wrote some parts. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2215 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
b7e1aa3a99
commit
c56519daea
@ -1,3 +1,10 @@
|
|||||||
|
2009-12-18 Ramon Novoa <rnovoa@artica.es>
|
||||||
|
|
||||||
|
* DEBIAN/control: Updated version.
|
||||||
|
|
||||||
|
* util/pandora_db.pl: Added history database support. Re-wrote some
|
||||||
|
parts.
|
||||||
|
|
||||||
2009-12-17 Miguel de Dios <miguel.dedios@artica.es>
|
2009-12-17 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
|
|
||||||
* DEBIAN/postinst: change the code to insert as service the server and
|
* DEBIAN/postinst: change the code to insert as service the server and
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package: PandoraFMS-Server
|
package: PandoraFMS-Server
|
||||||
Version: 3.0.0.RC3
|
Version: 3.0.0
|
||||||
Architecture: all
|
Architecture: all
|
||||||
Priority: optional
|
Priority: optional
|
||||||
Section: admin
|
Section: admin
|
||||||
|
@ -26,69 +26,58 @@ use PandoraFMS::DB;
|
|||||||
use POSIX qw(strftime);
|
use POSIX qw(strftime);
|
||||||
|
|
||||||
# version: define current version
|
# version: define current version
|
||||||
my $version = "3.0-dev PS091214";
|
my $version = "3.0 PS091214";
|
||||||
|
|
||||||
# Setup variables
|
# Pandora server configuration
|
||||||
my $dirname="";
|
my %conf;
|
||||||
my $dbname = 'pandora';
|
|
||||||
my $dbhost ='';
|
|
||||||
my $dbuser ='';
|
|
||||||
my $verbosity =0;
|
|
||||||
my $onlypurge = 0;
|
|
||||||
|
|
||||||
my $dbpass='';
|
|
||||||
my $server_threshold='';
|
|
||||||
my $log_file="";
|
|
||||||
my $pandora_path="";
|
|
||||||
my $config_days_compact;
|
|
||||||
my $config_days_purge;
|
|
||||||
my $config_step_compact;# Step compact variable defines "how-fine" is thecompact algorithm. 1 Hour its very fine, 24 hours is bad value
|
|
||||||
|
|
||||||
# FLUSH in each IO
|
# FLUSH in each IO
|
||||||
$| = 0;
|
$| = 0;
|
||||||
|
|
||||||
pandora_init();
|
# Init
|
||||||
|
pandora_init(\%conf);
|
||||||
|
|
||||||
# Read config file for Global variables
|
# Read config file
|
||||||
pandora_loadconfig ($pandora_path);
|
pandora_load_config (\%conf);
|
||||||
|
|
||||||
# Begin pandora_server
|
# Load enterprise module
|
||||||
pandoradb_main();
|
if (enterprise_load (\%conf) == 0) {
|
||||||
|
print " [*] Pandora FMS Enterprise module not available.\n\n";
|
||||||
|
} else {
|
||||||
|
print " [*] Pandora FMS Enterprise module loaded.\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Connect to the DB
|
||||||
|
my $dbh = db_connect ('mysql', $conf{'dbname'}, $conf{'dbhost'}, '3306', $conf{'dbuser'}, $conf{'dbpass'});
|
||||||
|
my $history_dbh = ($conf{'_history_db_enabled'} eq '1') ? db_connect ('mysql', $conf{'_history_db_name'},
|
||||||
|
$conf{'_history_db_host'}, '3306', $conf{'_history_db_user'}, $conf{'_history_db_pass'}) : undef;
|
||||||
|
|
||||||
|
# Main
|
||||||
|
pandoradb_main(\%conf, $dbh, $history_dbh);
|
||||||
|
|
||||||
|
# Cleanup and exit
|
||||||
|
db_disconnect ($history_dbh) if defined ($history_dbh);
|
||||||
|
db_disconnect ($dbh);
|
||||||
|
exit;
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
# Delete old data from the database.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
###############################################################################
|
sub pandora_purgedb ($$) {
|
||||||
###############################################################################
|
my ($conf, $dbh) = @_;
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
## SUB pandora_purgedb (days, dbname, dbuser, dbpass, dbhost)
|
|
||||||
###############################################################################
|
|
||||||
sub pandora_purgedb {
|
|
||||||
|
|
||||||
# 1) Obtain last value for date limit
|
# 1) Obtain last value for date limit
|
||||||
# 2) Delete all elements below date limit
|
# 2) Delete all elements below date limit
|
||||||
# 3) Insert last value in date_limit position
|
# 3) Insert last value in date_limit position
|
||||||
|
|
||||||
my $days = $_[0];
|
# Calculate limit for deletion, today - $conf->{'_days_purge'}
|
||||||
my $dbname = $_[1];
|
|
||||||
my $dbuser = $_[2];
|
|
||||||
my $dbpass = $_[3];
|
|
||||||
my $dbhost = $_[4];
|
|
||||||
my @query;
|
|
||||||
my $counter;
|
|
||||||
my $buffer; my $buffer2; my $buffer3;
|
|
||||||
my $err; # error code in datecalc function
|
|
||||||
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306",$dbuser, $dbpass,{RaiseError => 1, AutoCommit => 1 });
|
|
||||||
|
|
||||||
# Calculate limit for deletion, today - $days
|
|
||||||
my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime());
|
my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime());
|
||||||
|
|
||||||
my $ulimit_access_timestamp = time() - 86400;
|
my $ulimit_access_timestamp = time() - 86400;
|
||||||
my $ulimit_timestamp = time() - (86400 * $days);
|
my $ulimit_timestamp = time() - (86400 * $conf->{'_days_purge'});
|
||||||
my $limit_timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($ulimit_timestamp));
|
my $limit_timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($ulimit_timestamp));
|
||||||
|
|
||||||
print "[PURGE] Deleting old event data (More than $config_days_purge days)... \n";
|
print "[PURGE] Deleting old event data (More than " . $conf->{'_days_purge'} . " days)... \n";
|
||||||
$dbh->do("DELETE FROM tevento WHERE utimestamp < '$ulimit_timestamp'");
|
$dbh->do("DELETE FROM tevento WHERE utimestamp < '$ulimit_timestamp'");
|
||||||
|
|
||||||
print "[PURGE] Deleting old data... \n";
|
print "[PURGE] Deleting old data... \n";
|
||||||
@ -118,94 +107,55 @@ sub pandora_purgedb {
|
|||||||
|
|
||||||
print "[PURGE] Deleting old access data (More than 24hr) \n";
|
print "[PURGE] Deleting old access data (More than 24hr) \n";
|
||||||
$dbh->do("DELETE FROM tagent_access WHERE utimestamp < '$ulimit_access_timestamp'");
|
$dbh->do("DELETE FROM tagent_access WHERE utimestamp < '$ulimit_access_timestamp'");
|
||||||
|
|
||||||
$dbh->disconnect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## SUB pandora_compactdb (days, dbname, dbuser, dbpass, dbhost)
|
# Compact agent data.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
sub pandora_compactdb {
|
sub pandora_compactdb ($$) {
|
||||||
my $days = $_[0];
|
my ($conf, $dbh) = @_;
|
||||||
my $dbname = $_[1];
|
|
||||||
my $dbuser = $_[2];
|
|
||||||
my $dbpass = $_[3];
|
|
||||||
my $dbhost = $_[4];
|
|
||||||
|
|
||||||
my %count_hash;
|
my %count_hash;
|
||||||
my %id_agent_hash;
|
my %id_agent_hash;
|
||||||
my %value_hash;
|
my %value_hash;
|
||||||
my $err;
|
|
||||||
|
|
||||||
if ($days == 0){
|
return if ($conf->{'_days_purge'} == 0 || $conf->{'_step_compact'} < 1);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Connect to the database
|
|
||||||
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306",$dbuser, $dbpass,
|
|
||||||
{ RaiseError => 1, AutoCommit => 1 });
|
|
||||||
|
|
||||||
if ($config_step_compact < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Compact interval length in seconds
|
# Compact interval length in seconds
|
||||||
# $config_step_compact varies between 1 (36 samples/day) and
|
# $conf->{'_step_compact'} varies between 1 (36 samples/day) and
|
||||||
# 20 (1.8 samples/day)
|
# 20 (1.8 samples/day)
|
||||||
my $step = $config_step_compact * 2400;
|
my $step = $conf->{'_step_compact'} * 2400;
|
||||||
|
|
||||||
# The oldest timestamp will be the lower limit
|
# The oldest timestamp will be the lower limit
|
||||||
my $query = "SELECT min(utimestamp) as min FROM tagente_datos";
|
my $limit_utime = get_db_value ($dbh, 'SELECT min(utimestamp) as min FROM tagente_datos');
|
||||||
my $query_st = $dbh->prepare($query);
|
return unless (defined ($limit_utime) && $limit_utime > 0);
|
||||||
$query_st->execute();
|
|
||||||
if ($query_st->rows == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $data = $query_st->fetchrow_hashref();
|
|
||||||
my $limit_utime = $data->{'min'};
|
|
||||||
$query_st->finish();
|
|
||||||
|
|
||||||
if ($limit_utime < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Calculate the start date
|
# Calculate the start date
|
||||||
my $start_utime = time() - $days * 24 * 60 * 60;
|
my $start_utime = time() - $conf->{'_days_purge'} * 24 * 60 * 60;
|
||||||
my $start_date = strftime ("%Y-%m-%d %H:%M:%S", localtime($start_utime));
|
|
||||||
my $stop_date;
|
|
||||||
my $stop_utime;
|
my $stop_utime;
|
||||||
|
|
||||||
print "[COMPACT] Compacting data until $start_date\n";
|
print "[COMPACT] Compacting data until " . strftime ("%Y-%m-%d %H:%M:%S", localtime($start_utime)) ."\n";
|
||||||
|
|
||||||
# Prepare the query to retrieve data from an interval
|
# Prepare the query to retrieve data from an interval
|
||||||
$query = "SELECT * FROM tagente_datos WHERE utimestamp < ? AND
|
|
||||||
utimestamp >= ?";
|
|
||||||
$query_st = $dbh->prepare($query);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
# Calculate the stop date for the interval
|
# Calculate the stop date for the interval
|
||||||
$stop_utime = $start_utime - $step;
|
$stop_utime = $start_utime - $step;
|
||||||
|
|
||||||
# Out of limits
|
# Out of limits
|
||||||
if ($start_utime < $limit_utime) {
|
return if ($start_utime < $limit_utime);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$query_st->execute($start_utime, $stop_utime);
|
my @data = get_db_rows ($dbh, 'SELECT * FROM tagente_datos WHERE utimestamp < ? AND utimestamp >= ?', $start_utime, $stop_utime);
|
||||||
$query_st->rows;
|
|
||||||
if ($query_st->rows == 0) {
|
|
||||||
# No data, move to the next interval
|
# No data, move to the next interval
|
||||||
|
if ($#data == 0) {
|
||||||
$start_utime = $stop_utime;
|
$start_utime = $stop_utime;
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get interval data
|
# Get interval data
|
||||||
while ($data = $query_st->fetchrow_hashref()) {
|
foreach my $data (@data) {
|
||||||
my $id_module = $data->{'id_agente_modulo'};
|
my $id_module = $data->{'id_agente_modulo'};
|
||||||
|
|
||||||
|
|
||||||
if (! defined($value_hash{$id_module})) {
|
if (! defined($value_hash{$id_module})) {
|
||||||
$value_hash{$id_module} = 0;
|
$value_hash{$id_module} = 0;
|
||||||
$count_hash{$id_module} = 0;
|
$count_hash{$id_module} = 0;
|
||||||
@ -220,18 +170,12 @@ sub pandora_compactdb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Delete interval from the database
|
# Delete interval from the database
|
||||||
$dbh->do("DELETE FROM tagente_datos WHERE utimestamp < $start_utime
|
db_do ($dbh, 'DELETE FROM tagente_datos WHERE utimestamp < ? AND utimestamp >= ?', $start_utime, $stop_utime);
|
||||||
AND utimestamp >= $stop_utime");
|
|
||||||
|
|
||||||
# Insert interval average value
|
# Insert interval average value
|
||||||
foreach my $key (keys(%value_hash)) {
|
foreach my $key (keys(%value_hash)) {
|
||||||
$value_hash{$key} /= $count_hash{$key};
|
$value_hash{$key} /= $count_hash{$key};
|
||||||
$stop_date = strftime ("%Y-%m-%d %H:%M:%S", localtime());
|
db_do ($dbh, 'INSERT INTO tagente_datos (id_agente_modulo, datos, utimestamp) VALUES (?, ?, ?)', $key, $value_hash{$key}, $stop_utime);
|
||||||
$dbh->do("INSERT INTO tagente_datos (id_agente_modulo,
|
|
||||||
datos, utimestamp) VALUES
|
|
||||||
($key, $value_hash{$key} ,
|
|
||||||
$stop_utime)");
|
|
||||||
|
|
||||||
delete($value_hash{$key});
|
delete($value_hash{$key});
|
||||||
delete($count_hash{$key});
|
delete($count_hash{$key});
|
||||||
}
|
}
|
||||||
@ -239,294 +183,154 @@ sub pandora_compactdb {
|
|||||||
# Move to the next interval
|
# Move to the next interval
|
||||||
$start_utime = $stop_utime;
|
$start_utime = $stop_utime;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query_st->finish();
|
|
||||||
$dbh->disconnect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# SUB pandora_init ()
|
# Check command line parameters.
|
||||||
# Makes the initial parameter parsing, initializing and error checking
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
sub pandora_init ($) {
|
||||||
|
my $conf = shift;
|
||||||
|
|
||||||
sub pandora_init {
|
print "\nPandora FMS DB Tool $version Copyright (c) 2004-2008 Artica ST\n";
|
||||||
print "\nPandora FMS DB Tool $version Copyright (c) 2004-2009 Artica ST\n";
|
|
||||||
print "This program is Free Software, licensed under the terms of GPL License v2\n";
|
print "This program is Free Software, licensed under the terms of GPL License v2\n";
|
||||||
print "You can download latest versions and documentation at http://www.pandorafms.org\n";
|
print "You can download latest versions and documentation at http://www.pandorafms.org\n\n";
|
||||||
|
|
||||||
# Load config file from command line
|
# Load config file from command line
|
||||||
if ($#ARGV == -1 ){
|
help_screen () if ($#ARGV < 0);
|
||||||
print "\n[ERROR] I Need at least one parameter: Complete path to pandora_server.conf file\n";
|
|
||||||
help_screen();
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
# If there are not valid parameters
|
# If there are not valid parameters
|
||||||
my $parametro;
|
foreach my $param (@ARGV) {
|
||||||
my $ltotal=$#ARGV; my $ax;
|
|
||||||
for ($ax=0;$ax<=$ltotal;$ax++){
|
# help!
|
||||||
$parametro = $ARGV[$ax];
|
help_screen () if ($param =~ m/--*h\w*\z/i );
|
||||||
if ($parametro =~ m/-h\z/i ) { help_screen(); }
|
if ($param =~ m/-p\z/i) {
|
||||||
elsif ($parametro =~ m/-help\z/i ) { help_screen(); }
|
$conf->{'_onlypurge'} = 1;
|
||||||
elsif ($parametro =~ m/--help\z/i ) { help_screen(); }
|
} else {
|
||||||
elsif ($parametro =~ m/-v\z/i) { $verbosity=5; }
|
$conf->{'_pandora_path'} = $param;
|
||||||
elsif ($parametro =~ m/-d\z/i) { $verbosity=10; }
|
|
||||||
elsif ($parametro =~ m/-d\z/i) { $verbosity=0; }
|
|
||||||
elsif ($parametro =~ m/-p\z/i) { $onlypurge=1; }
|
|
||||||
else { ($pandora_path = $parametro); }
|
|
||||||
}
|
}
|
||||||
if ($pandora_path eq ""){
|
|
||||||
print "\n[ERROR] I Need complete path to pandora_server.conf file \n\n";
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
help_screen () if ($conf->{'_pandora_path'} eq '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# Read external configuration file
|
# Read external configuration file.
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
sub pandora_load_config ($) {
|
||||||
|
my $conf = shift;
|
||||||
|
|
||||||
sub pandora_loadconfig {
|
# Read conf file
|
||||||
my $archivo_cfg = @_[0];
|
open (CFG, '< ' . $conf->{'_pandora_path'}) or die ("[ERROR] Could not open configuration file: $!\n");
|
||||||
my $buffer_line;
|
while (my $line = <CFG>){
|
||||||
my @command_line;
|
next unless ($line =~ m/([\w-_\.]+)\s([0-9\w-_\.\/\?\&\=\)\(\_\-\\*\@\#\%\$\~\"\']+)/);
|
||||||
# Check for file
|
$conf->{$1} = $2;
|
||||||
if ( ! -e $archivo_cfg ) {
|
|
||||||
printf "\n[ERROR] Cannot open configuration file. Please specify a valid one in command line \n";
|
|
||||||
exit 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Collect items from config file and put in an array
|
|
||||||
open (CFG, "< $archivo_cfg");
|
|
||||||
while (<CFG>){
|
|
||||||
$buffer_line = $_;
|
|
||||||
if ($buffer_line =~ m/([\w-_\.]+)\s([0-9\w-_\.\/\?\&\=\)\(\_\-\\*\@\#\%\$\~\"\']+)/){
|
|
||||||
push @command_line,$1;
|
|
||||||
push @command_line,$2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
close (CFG);
|
close (CFG);
|
||||||
# Process this array with commandline like options
|
|
||||||
# Process input parameters
|
|
||||||
my @args = @command_line;
|
|
||||||
my $parametro;
|
|
||||||
my $ltotal=$#args; my $ax;
|
|
||||||
|
|
||||||
# Has read setup file ok ?
|
# Check conf tokens
|
||||||
if ( $ltotal == 0 ) {
|
foreach my $param ('dbuser', 'dbpass', 'dbname', 'dbhost', 'log_file') {
|
||||||
print "\n[ERROR] No valid setup tokens readed in $archivo_cfg ";
|
die ("[ERROR] Bad config values. Make sure " . $conf->{'_pandora_path'} . " is a valid config file.\n\n") unless defined ($conf->{$param});
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ($ax=0;$ax<=$ltotal;$ax++){
|
# Read additional tokens from the DB
|
||||||
$parametro = $args[$ax];
|
my $dbh = db_connect ('mysql', $conf->{'dbname'}, $conf->{'dbhost'}, '3306', $conf->{'dbuser'}, $conf->{'dbpass'});
|
||||||
if ($parametro =~ m/dirname\z/) { $dirname = $args[$ax+1]; $ax++; }
|
$conf->{'_days_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'days_purge'");
|
||||||
elsif ($parametro =~ m/dbuser\z/) { $dbuser = $args[$ax+1]; $ax++; }
|
$conf->{'_days_compact'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'days_compact'");
|
||||||
elsif ($parametro =~ m/dbpass\z/) { $dbpass = $args[$ax+1]; $ax++; }
|
$conf->{'_step_compact'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'step_compact'");
|
||||||
elsif ($parametro =~ m/dbname\z/) { $dbname = $args[$ax+1]; $ax++; }
|
$conf->{'_step_compact'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'step_compact'");
|
||||||
elsif ($parametro =~ m/dbhost\z/) { $dbhost = $args[$ax+1]; $ax++; }
|
$conf->{'_history_db_enabled'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_enabled'");
|
||||||
elsif ($parametro =~ m/log_file\z/) { $log_file = $args[$ax+1]; $ax++; }
|
$conf->{'_history_db_host'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_host'");
|
||||||
elsif ($parametro =~ m/server_threshold\z/) { $server_threshold = $args[$ax+1]; $ax++; }
|
$conf->{'_history_db_port'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_port'");
|
||||||
}
|
$conf->{'_history_db_name'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_name'");
|
||||||
|
$conf->{'_history_db_user'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_user'");
|
||||||
|
$conf->{'_history_db_pass'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_pass'");
|
||||||
|
$conf->{'_history_db_days'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_days'");
|
||||||
|
$conf->{'_history_db_step'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_step'");
|
||||||
|
$conf->{'_history_db_delay'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_delay'");
|
||||||
|
db_disconnect ($dbh);
|
||||||
|
|
||||||
# Check for valid token token values
|
printf "Pandora DB now initialized and running (PURGE=" . $conf->{'_days_purge'} . " days, COMPACT=$conf->{'_days_compact'} days, STEP=" . $conf->{'_step_compact'} . ") ... \n\n";
|
||||||
if (( $dbuser eq "" ) || ( $log_file eq "" ) || ( $dbhost eq "") || ($dbpass eq "" ) ) {
|
|
||||||
print "\n[ERROR] Bad Config values. Be sure that $archivo_cfg is a valid setup file";
|
|
||||||
print "\n\n";
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Open database to get days_purge days_compact values
|
|
||||||
my $query; my $query_ready; my @data; my $rows_selected;
|
|
||||||
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306",$dbuser, $dbpass, {RaiseError => 1, AutoCommit => 1 });
|
|
||||||
$query = "select * from tconfig where token = 'days_purge'";
|
|
||||||
$query_ready = $dbh->prepare($query);
|
|
||||||
$query_ready ->execute();
|
|
||||||
$rows_selected = $query_ready->rows;
|
|
||||||
if ($query_ready->rows > 0) {
|
|
||||||
@data = $query_ready->fetchrow_array();
|
|
||||||
$config_days_purge = $data[2]; # value
|
|
||||||
} else {
|
|
||||||
print "\n[ERROR] I cannot find in database a config item (DAYS_PURGE)\n";
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
$query_ready->finish();
|
|
||||||
|
|
||||||
$query = "select * from tconfig where token = 'days_compact'";
|
|
||||||
$query_ready = $dbh->prepare($query);
|
|
||||||
$query_ready ->execute();
|
|
||||||
$rows_selected = $query_ready->rows;
|
|
||||||
if ($query_ready->rows > 0) {
|
|
||||||
@data = $query_ready->fetchrow_array();
|
|
||||||
$config_days_compact = $data[2]; # value
|
|
||||||
} else {
|
|
||||||
print "[ERROR] I cannot find in database a config item (DAYS_COMPACT)\n";
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
$query_ready->finish();
|
|
||||||
|
|
||||||
$query = "select * from tconfig where token = 'step_compact'";
|
|
||||||
$query_ready = $dbh->prepare($query);
|
|
||||||
$query_ready ->execute();
|
|
||||||
$rows_selected = $query_ready->rows;
|
|
||||||
if ($query_ready->rows > 0) {
|
|
||||||
@data = $query_ready->fetchrow_array();
|
|
||||||
$config_step_compact = $data[2]; # value
|
|
||||||
} else {
|
|
||||||
print "[ERROR] I cannot find in database a config item (CONFIG_STEP_COMPACT)\n";
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$query_ready->finish();
|
|
||||||
$dbh->disconnect;
|
|
||||||
|
|
||||||
printf "Pandora DB now initialized and running (PURGE=$config_days_purge days, COMPACT=$config_days_compact days, STEP=$config_step_compact) ... \n\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## SUB pandora_checkdb_consistency (dbname, dbuser, dbpass, dbhost)
|
# Check database consistency.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
sub pandora_checkdb_consistency {
|
sub pandora_checkdb_consistency {
|
||||||
|
my $dbh = shift;
|
||||||
|
|
||||||
# 1. Check for modules that do not have tagente_estado but have tagente_module
|
# 1. Check for modules that do not have tagente_estado but have tagente_module
|
||||||
|
|
||||||
my $dbname = $_[0];
|
print "[CHECKDB] Deleting non-init data... \n";
|
||||||
my $dbuser = $_[1];
|
my @modules = get_db_rows ($dbh, 'SELECT id_agente_modulo FROM tagente_estado WHERE utimestamp = 0');
|
||||||
my $dbpass = $_[2];
|
foreach my $module (@modules) {
|
||||||
my $dbhost = $_[3];
|
my $id_agente_modulo = $module->{'id_agente_modulo'};
|
||||||
|
|
||||||
my @query;
|
|
||||||
my $counter;
|
|
||||||
my $err; # error code in datecalc function
|
|
||||||
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306",$dbuser, $dbpass,{RaiseError => 1, AutoCommit => 1 });
|
|
||||||
|
|
||||||
print "\n[CHECKDB] Deleting non-init data... \n";
|
|
||||||
my $query4 = "SELECT * FROM tagente_estado WHERE utimestamp = 0";
|
|
||||||
my $prep4 = $dbh->prepare($query4);
|
|
||||||
$prep4 ->execute;
|
|
||||||
my @datarow4;
|
|
||||||
if ($prep4->rows != 0) {
|
|
||||||
# for each record in tagente_modulo
|
|
||||||
while (@datarow4 = $prep4->fetchrow_array()) {
|
|
||||||
my $id_agente_modulo = $datarow4[1];
|
|
||||||
|
|
||||||
# Skip policy modules
|
# Skip policy modules
|
||||||
next if (is_policy_module ($dbh, $id_agente_modulo));
|
next if (is_policy_module ($dbh, $id_agente_modulo));
|
||||||
|
|
||||||
# Delete the module
|
# Delete the module
|
||||||
my $query0 = "DELETE FROM tagente_modulo WHERE disabled = 0 AND id_agente_modulo = $id_agente_modulo";
|
db_do ($dbh, 'DELETE FROM tagente_modulo WHERE disabled = 0 AND id_agente_modulo = ?', $id_agente_modulo);;
|
||||||
my $prep0 = $dbh->prepare($query0);
|
|
||||||
$prep0 ->execute;
|
|
||||||
$prep0->finish();
|
|
||||||
|
|
||||||
# Delete any alerts associated to the module
|
# Delete any alerts associated to the module
|
||||||
$query0 = "DELETE FROM talert_template_modules WHERE id_agent_module = $id_agente_modulo";
|
db_do ($dbh, 'DELETE FROM talert_template_modules WHERE id_agent_module = ?', $id_agente_modulo);
|
||||||
$prep0 = $dbh->prepare($query0);
|
|
||||||
$prep0 ->execute;
|
|
||||||
$prep0->finish();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
$prep4->finish();
|
|
||||||
|
|
||||||
print "\n[CHECKDB] Checking database consistency (Missing status)... \n";
|
print "[CHECKDB] Checking database consistency (Missing status)... \n";
|
||||||
|
|
||||||
|
@modules = get_db_rows ($dbh, 'SELECT * FROM tagente_modulo');
|
||||||
|
foreach my $module (@modules) {
|
||||||
|
my $id_agente_modulo = $module->{'id_agente_modulo'};
|
||||||
|
my $id_agente = $module->{'id_agente'};
|
||||||
|
|
||||||
my $query1 = "SELECT * FROM tagente_modulo";
|
|
||||||
my $prep1 = $dbh->prepare($query1);
|
|
||||||
$prep1 ->execute;
|
|
||||||
my @datarow1;
|
|
||||||
if ($prep1->rows != 0) {
|
|
||||||
# for each record in tagente_modulo
|
|
||||||
while (@datarow1 = $prep1->fetchrow_array()) {
|
|
||||||
my $id_agente_modulo = $datarow1[0];
|
|
||||||
# check if exist in tagente_estado and create if not
|
# check if exist in tagente_estado and create if not
|
||||||
my $query2 = "SELECT * FROM tagente_estado WHERE id_agente_modulo = $id_agente_modulo";
|
my $count = get_db_value ($dbh, 'SELECT COUNT(*) FROM tagente_estado WHERE id_agente_modulo = ?', $id_agente_modulo);
|
||||||
my $prep2 = $dbh->prepare($query2);
|
next if (defined ($count) && $count > 0);
|
||||||
$prep2->execute;
|
|
||||||
# If have 0 items, we need to re-create tagente_estado record
|
|
||||||
if ($prep2->rows == 0) {
|
|
||||||
my $id_agente = $datarow1[1];
|
|
||||||
my $query3 = "INSERT INTO tagente_estado (id_agente_modulo, datos, timestamp, estado, id_agente, last_try, utimestamp, current_interval, running_by, last_execution_try) VALUE ($id_agente_modulo, 0, '0000-00-00 00:00:00', 1, $id_agente, '0000-00-00 00:00:00', 0, 0, 0, 0)";
|
|
||||||
print "[CHECKDB] Inserting module $id_agente_modulo in state table \n";
|
|
||||||
my $prep3 = $dbh->prepare($query3);
|
|
||||||
$prep3->execute;
|
|
||||||
$prep3->finish();
|
|
||||||
}
|
|
||||||
$prep2->finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$prep1->finish();
|
|
||||||
|
|
||||||
print "\n[CHECKDB] Checking database consistency (Missing module)... \n";
|
db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, datos, timestamp, estado, id_agente, last_try, utimestamp, current_interval, running_by, last_execution_try) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente_modulo, 0, '0000-00-00 00:00:00', 1, $id_agente, '0000-00-00 00:00:00', 0, 0, 0, 0);
|
||||||
|
print "[CHECKDB] Inserting module $id_agente_modulo in state table \n";
|
||||||
|
}
|
||||||
|
|
||||||
|
print "[CHECKDB] Checking database consistency (Missing module)... \n";
|
||||||
# 2. Check for modules in tagente_estado that do not have tagente_modulo, if there is any, delete it
|
# 2. Check for modules in tagente_estado that do not have tagente_modulo, if there is any, delete it
|
||||||
|
|
||||||
my $query1 = "SELECT * FROM tagente_estado";
|
@modules = get_db_rows ($dbh, 'SELECT * FROM tagente_estado');
|
||||||
my $prep1 = $dbh->prepare($query1);
|
foreach my $module (@modules) {
|
||||||
$prep1 ->execute;
|
my $id_agente_modulo = $module->{'id_agente_modulo'};
|
||||||
my @datarow1;
|
|
||||||
if ($prep1->rows != 0) {
|
|
||||||
# for each record in tagente_modulo
|
|
||||||
while (@datarow1 = $prep1->fetchrow_array()) {
|
|
||||||
my $id_agente_modulo = $datarow1[1];
|
|
||||||
# check if exist in tagente_estado and create if not
|
|
||||||
my $query2 = "SELECT * FROM tagente_modulo WHERE id_agente_modulo = $id_agente_modulo";
|
|
||||||
my $prep2 = $dbh->prepare($query2);
|
|
||||||
$prep2->execute;
|
|
||||||
# If have 0 items, we need to create tagente_estado record
|
|
||||||
if ($prep2->rows == 0) {
|
|
||||||
my $id_agente = $datarow1[1];
|
|
||||||
my $query3 = "DELETE FROM tagente_estado WHERE id_agente_modulo = $id_agente_modulo";
|
|
||||||
print "[CHECKDB] Deleting non-existing module $id_agente_modulo in state table \n";
|
|
||||||
my $prep3 = $dbh->prepare($query3);
|
|
||||||
$prep3->execute;
|
|
||||||
$prep3->finish();
|
|
||||||
}
|
|
||||||
$prep2->finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$prep1->finish();
|
|
||||||
|
|
||||||
|
# check if exist in tagente_estado and create if not
|
||||||
|
my $count = get_db_value ($dbh, 'SELECT COUNT(*) FROM tagente_modulo WHERE id_agente_modulo = ?', $id_agente_modulo);
|
||||||
|
next if (defined ($count) && $count > 0);
|
||||||
|
|
||||||
|
db_do ($dbh, 'DELETE FROM tagente_estado WHERE id_agente_modulo = ?', $id_agente_modulo);
|
||||||
|
print "[CHECKDB] Deleting non-existing module $id_agente_modulo in state table \n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Returns undef if the module is not a policy module.
|
# Returns undef if the given module is not a policy module.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
sub is_policy_module ($$) {
|
sub is_policy_module ($$) {
|
||||||
my ($dbh, $module_id) = @_;
|
my ($dbh, $module_id) = @_;
|
||||||
my ($agent_id, $module_name, $policy_id) = (undef, undef, undef);
|
|
||||||
|
eval {
|
||||||
|
my $count = get_db_value ($dbh, 'SELECT COUNT(*) FROM tpolicies');
|
||||||
|
};
|
||||||
|
|
||||||
|
# Not running Pandora FMS Enterprise
|
||||||
|
return undef if ($@);
|
||||||
|
|
||||||
# Get agent id
|
# Get agent id
|
||||||
my $sth = $dbh->prepare('SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo = ?');
|
my $agent_id = get_db_value ($dbh, 'SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo = ?', $module_id);
|
||||||
$sth->execute ($module_id);
|
return undef unless defined ($agent_id);
|
||||||
while (my @row = $sth->fetchrow_array()) {
|
|
||||||
$agent_id = $row[0];
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
$sth->finish();
|
|
||||||
return unless defined ($agent_id);
|
|
||||||
|
|
||||||
# Get module name
|
# Get module name
|
||||||
$sth = $dbh->prepare('SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?');
|
my $module_name = get_db_value ($dbh, 'SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?', $module_id);
|
||||||
$sth->execute ($module_id);
|
return undef unless defined ($module_name);
|
||||||
while (my @row = $sth->fetchrow_array()) {
|
|
||||||
$module_name = $row[0];
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
$sth->finish();
|
|
||||||
return unless defined ($module_name);
|
|
||||||
|
|
||||||
# Search policies
|
# Search policies
|
||||||
$sth = $dbh->prepare('SELECT t3.id FROM tpolicy_agents AS t1
|
my $policy_id = get_db_value ($dbh, 'SELECT t3.id FROM tpolicy_agents AS t1 INNER JOIN tpolicy_modules AS t2 ON t1.id_policy = t2.id_policy
|
||||||
INNER JOIN tpolicy_modules AS t2 ON t1.id_policy = t2.id_policy
|
INNER JOIN tpolicies AS t3 ON t1.id_policy = t3.id WHERE t1.id_agent = ? AND t2.name LIKE ?', $agent_id, $module_name);
|
||||||
INNER JOIN tpolicies AS t3 ON t1.id_policy = t3.id
|
|
||||||
WHERE t1.id_agent = ? AND t2.name LIKE ?');
|
|
||||||
$sth->execute ($agent_id, $module_name);
|
|
||||||
while (my @row = $sth->fetchrow_array()) {
|
|
||||||
$policy_id = $row[0];
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
$sth->finish();
|
|
||||||
|
|
||||||
# Not a policy module
|
# Not a policy module
|
||||||
return undef unless defined ($policy_id);
|
return undef unless defined ($policy_id);
|
||||||
@ -535,36 +339,40 @@ sub is_policy_module ($$) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# SUB ERRORhelp_screen()
|
# Print a help screen and exit.
|
||||||
# Show a help screen an exits
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
sub help_screen{
|
sub help_screen{
|
||||||
printf "\nSyntax: \n pandora_db.pl fullpathname_to_pandora_server.conf \n\n";
|
print "\n\nUsage: $0 <path to pandora_server.conf> [options]\n\n";
|
||||||
print " -d Debug output (very verbose) \n";
|
print "\n\tAvailable options:\n\t\t-d Debug output (very verbose).\n";
|
||||||
print " -v Verbose output \n";
|
print "\t\t-v Verbose output.\n";
|
||||||
print " -q Quiet output \n";
|
print "\t\t-q Quiet output.\n";
|
||||||
print " -p Only purge and consistency check, skip compact \n\n";
|
print "\t\t-p Only purge and consistency check, skip compact.\n\n";
|
||||||
print "If you get the Undefined subroutine &main::UnixDate error, ";
|
|
||||||
print "verify that you have the Date::Manip Perl module. ";
|
|
||||||
print "If not, install it using cpan Date::Manip \n\n";
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Program main begin
|
# Main
|
||||||
#
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
sub pandoradb_main {
|
sub pandoradb_main ($$$) {
|
||||||
|
my ($conf, $dbh, $history_dbh) = @_;
|
||||||
|
|
||||||
print "Starting at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n";
|
print "Starting at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n";
|
||||||
pandora_purgedb ($config_days_purge, $dbname, $dbuser, $dbpass, $dbhost);
|
|
||||||
pandora_checkdb_consistency ($dbname, $dbuser, $dbpass, $dbhost);
|
|
||||||
|
|
||||||
if ($onlypurge == 0){
|
# Move old data to the history DB
|
||||||
pandora_compactdb ($config_days_compact, $dbname, $dbuser, $dbpass, $dbhost);
|
if (defined ($history_dbh)) {
|
||||||
|
undef ($history_dbh) unless defined (enterprise_hook ('pandora_historydb', [$dbh, $history_dbh, $conf->{'_history_db_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Purge
|
||||||
|
pandora_purgedb ($conf, $dbh);
|
||||||
|
|
||||||
|
# Consistency check
|
||||||
|
pandora_checkdb_consistency ($dbh);
|
||||||
|
|
||||||
|
# Compact
|
||||||
|
if ($conf->{'_onlypurge'} == 0) {
|
||||||
|
pandora_compactdb ($conf, defined ($history_dbh) ? $history_dbh : $dbh);
|
||||||
|
}
|
||||||
|
|
||||||
print "Ending at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n";
|
print "Ending at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n";
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user