From 91194155b09abcdaa9b7fdf3caf7a38595cd56f4 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 19 Sep 2022 11:15:22 +0200 Subject: [PATCH 1/3] implemented trap history --- .../snmpconsole/snmp_trap_generator.php | 1 + pandora_console/include/functions_config.php | 28 +++++++++++++++++++ pandora_server/util/pandora_db.pl | 11 ++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pandora_console/godmode/snmpconsole/snmp_trap_generator.php b/pandora_console/godmode/snmpconsole/snmp_trap_generator.php index 72578868e7..e03cfa9bb9 100755 --- a/pandora_console/godmode/snmpconsole/snmp_trap_generator.php +++ b/pandora_console/godmode/snmpconsole/snmp_trap_generator.php @@ -76,6 +76,7 @@ if ($generate_trap) { ); } +$table = new stdClass(); $traps_generator = '
'; $table->width = '100%'; $table->class = 'databox filters'; diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index fbe12e27f8..65bb579f09 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1554,6 +1554,10 @@ function config_update_config() $error_update[] = __('Enable history event'); } + if (config_update_value('history_trap_enabled', get_parameter('history_trap_enabled'), true) === false) { + $error_update[] = __('Enable history trap'); + } + if (config_update_value('history_db_user', get_parameter('history_db_user'), true) === false) { $error_update[] = __('Database user'); } @@ -1578,6 +1582,14 @@ function config_update_config() $error_update[] = __('Event Days'); } + $history_trap_days = get_parameter('history_trap_days'); + if (is_numeric($history_trap_days) === false + || $history_trap_days <= 0 + || config_update_value('history_trap_days', $history_trap_days) === false + ) { + $error_update[] = __('Trap Days'); + } + $history_db_step = get_parameter('history_db_step'); if (!is_numeric($history_db_step) || $history_db_step <= 0 @@ -1642,6 +1654,14 @@ function config_update_config() $error_update[] = __('Historical database events purge'); } + if ($dbm->setConfigToken( + 'trap_purge', + get_parameter('history_dbh_traps_purge') + ) !== true + ) { + $error_update[] = __('Historical database traps purge'); + } + if ($dbm->setConfigToken( 'string_purge', get_parameter('history_dbh_string_purge') @@ -2476,6 +2496,10 @@ function config_process_config() config_update_value('history_event_enabled', false); } + if (!isset($config['history_trap_enabled'])) { + config_update_value('history_trap_enabled', false); + } + if (!isset($config['history_db_host'])) { config_update_value('history_db_host', ''); } @@ -2504,6 +2528,10 @@ function config_process_config() config_update_value('history_event_days', 90); } + if (!isset($config['history_trap_days'])) { + config_update_value('history_trap_days', 90); + } + if (!isset($config['history_db_step'])) { config_update_value('history_db_step', 0); } diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 2c498d6d0f..b92d027dab 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -638,6 +638,7 @@ sub pandora_load_config_pdb ($) { $conf->{'_step_compact'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'step_compact'"); $conf->{'_history_db_enabled'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_enabled'"); $conf->{'_history_event_enabled'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_event_enabled'"); + $conf->{'_history_trap_enabled'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_trap_enabled'"); $conf->{'_history_db_host'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_db_host'"); $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'"); @@ -645,6 +646,7 @@ sub pandora_load_config_pdb ($) { $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_event_days'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_event_days'"); + $conf->{'_history_trap_days'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'history_trap_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'"); $conf->{'_days_delete_unknown'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'days_delete_unknown'"); @@ -1128,9 +1130,12 @@ sub pandoradb_main ($$$;$) { # Move old data to the history DB 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'}])); - if (defined($conf{'_history_event_enabled'}) && $conf->{'_history_event_enabled'} ne "" && $conf->{'_history_event_enabled'} == 1) { - undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_event', [$dbh, $history_dbh, $conf->{'_history_event_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); + #undef ($history_dbh) unless defined (enterprise_hook ('pandora_historydb', [$dbh, $history_dbh, $conf->{'_history_db_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); + #if (defined($conf{'_history_event_enabled'}) && $conf->{'_history_event_enabled'} ne "" && $conf->{'_history_event_enabled'} == 1) { + # undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_event', [$dbh, $history_dbh, $conf->{'_history_event_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); + #} + if (defined($conf{'_history_trap_enabled'}) && $conf->{'_history_trap_enabled'} ne "" && $conf->{'_history_trap_enabled'} == 1) { + undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_trap', [$dbh, $history_dbh, $conf->{'_history_trap_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); } } From 230494ffca2ff43a09fa49409d3a04e5f651b6f2 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 20 Sep 2022 16:03:13 +0200 Subject: [PATCH 2/3] traps history --- pandora_server/util/pandora_db.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index b92d027dab..d1f7192d27 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -1130,10 +1130,10 @@ sub pandoradb_main ($$$;$) { # Move old data to the history DB 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'}])); - #if (defined($conf{'_history_event_enabled'}) && $conf->{'_history_event_enabled'} ne "" && $conf->{'_history_event_enabled'} == 1) { - # undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_event', [$dbh, $history_dbh, $conf->{'_history_event_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); - #} + undef ($history_dbh) unless defined (enterprise_hook ('pandora_historydb', [$dbh, $history_dbh, $conf->{'_history_db_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); + if (defined($conf{'_history_event_enabled'}) && $conf->{'_history_event_enabled'} ne "" && $conf->{'_history_event_enabled'} == 1) { + undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_event', [$dbh, $history_dbh, $conf->{'_history_event_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); + } if (defined($conf{'_history_trap_enabled'}) && $conf->{'_history_trap_enabled'} ne "" && $conf->{'_history_trap_enabled'} == 1) { undef ($history_dbh) unless defined (enterprise_hook ('pandora_history_trap', [$dbh, $history_dbh, $conf->{'_history_trap_days'}, $conf->{'_history_db_step'}, $conf->{'_history_db_delay'}])); } From 9343796ae19f16c1c4ff418b286cac6cc7936e18 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 20 Sep 2022 17:26:29 +0200 Subject: [PATCH 3/3] traps history --- pandora_console/include/functions_config.php | 16 +++++++++++++-- pandora_server/util/pandora_db.pl | 21 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 65bb579f09..ab69e70e71 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1590,6 +1590,14 @@ function config_update_config() $error_update[] = __('Trap Days'); } + $trap_history_purge = get_parameter('history_traps_days_purge'); + if (is_numeric($trap_history_purge) === false + || $trap_history_purge <= 0 + || config_update_value('trap_history_purge', $trap_history_purge) === false + ) { + $error_update[] = __('Trap history purge'); + } + $history_db_step = get_parameter('history_db_step'); if (!is_numeric($history_db_step) || $history_db_step <= 0 @@ -1655,8 +1663,8 @@ function config_update_config() } if ($dbm->setConfigToken( - 'trap_purge', - get_parameter('history_dbh_traps_purge') + 'trap_history_purge', + get_parameter('history_traps_days_purge') ) !== true ) { $error_update[] = __('Historical database traps purge'); @@ -2532,6 +2540,10 @@ function config_process_config() config_update_value('history_trap_days', 90); } + if (!isset($config['trap_history_purge'])) { + config_update_value('trap_history_purge', 180); + } + if (!isset($config['history_db_step'])) { config_update_value('history_db_step', 0); } diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index d1f7192d27..21d4a431ac 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -628,6 +628,7 @@ sub pandora_load_config_pdb ($) { $conf->{'_event_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'event_purge'"); $conf->{'_trap_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'trap_purge'"); + $conf->{'_trap_history_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'trap_purge'"); $conf->{'_audit_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'audit_purge'"); $conf->{'_string_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'string_purge'"); $conf->{'_gis_purge'} = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'gis_purge'"); @@ -1104,6 +1105,26 @@ sub pandoradb_history ($$) { log_message ('', "\n"); } + # Delete old traps. + if ($conf->{'_trap_history_purge'} > 0) { + log_message ('PURGE', "Deleting traps older than " . $conf->{'_trap_history_purge'} . " days from ttrap (history).", ''); + + my $trap_limit = strftime ("%Y-%m-%d %H:%M:%S", localtime(time() - 86400 * $conf->{'_trap_history_purge'})); + + my $traps_to_delete = get_db_value ($dbh, "SELECT COUNT(*) FROM ttrap WHERE timestamp < ?", $trap_limit); + while($traps_to_delete > 0) { + db_delete_limit($dbh, 'ttrap', "timestamp < ?", $BIG_OPERATION_STEP, $trap_limit); + $traps_to_delete = $traps_to_delete - $BIG_OPERATION_STEP; + + # Mark the progress. + log_message ('', "."); + + # Do not overload the MySQL server. + usleep (10000); + } + log_message ('', "\n"); + } + # Update tconfig with last time of database maintance time (now) db_do ($dbh, "DELETE FROM tconfig WHERE token = 'db_maintance'"); db_do ($dbh, "INSERT INTO tconfig (token, value) VALUES ('db_maintance', '".time()."')");