From a487fa011eab91505bcda8917d8dc8931de70f76 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Thu, 30 Apr 2015 13:46:36 +0200 Subject: [PATCH] Execute all previously run actions when an alert recovers. --- pandora_server/lib/PandoraFMS/Core.pm | 61 +++++++++++++++++--------- pandora_server/lib/PandoraFMS/Tools.pm | 6 +++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 62812da7f0..604a4e8686 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -647,15 +647,26 @@ sub pandora_execute_alert ($$$$$$$$;$) { # Simple alert if (defined ($alert->{'id_template_module'})) { - @actions = get_db_rows ($dbh, 'SELECT *, talert_template_module_actions.id AS id_alert_template_module_actions - FROM talert_template_module_actions, talert_actions, talert_commands - WHERE talert_template_module_actions.id_alert_action = talert_actions.id - AND talert_actions.id_alert_command = talert_commands.id - AND talert_template_module_actions.id_alert_template_module = ? - AND ((fires_min = 0 AND fires_max = 0) - OR (fires_min <= fires_max AND ? >= fires_min AND ? <= fires_max) - OR (fires_min > fires_max AND ? >= fires_min))', - $alert->{'id_template_module'}, $alert->{'times_fired'}, $alert->{'times_fired'}, $alert->{'times_fired'}); + if ($alert_mode == RECOVERED_ALERT) { + @actions = get_db_rows ($dbh, 'SELECT *, talert_template_module_actions.id AS id_alert_template_module_actions + FROM talert_template_module_actions, talert_actions, talert_commands + WHERE talert_template_module_actions.id_alert_action = talert_actions.id + AND talert_actions.id_alert_command = talert_commands.id + AND talert_template_module_actions.id_alert_template_module = ? + AND ((fires_min = 0 AND fires_max = 0) + OR ? >= fires_min)', + $alert->{'id_template_module'}, $alert->{'times_fired'}); + } else { + @actions = get_db_rows ($dbh, 'SELECT *, talert_template_module_actions.id AS id_alert_template_module_actions + FROM talert_template_module_actions, talert_actions, talert_commands + WHERE talert_template_module_actions.id_alert_action = talert_actions.id + AND talert_actions.id_alert_command = talert_commands.id + AND talert_template_module_actions.id_alert_template_module = ? + AND ((fires_min = 0 AND fires_max = 0) + OR (fires_min <= fires_max AND ? >= fires_min AND ? <= fires_max) + OR (fires_min > fires_max AND ? >= fires_min))', + $alert->{'id_template_module'}, $alert->{'times_fired'}, $alert->{'times_fired'}, $alert->{'times_fired'}); + } # Get default action if ($#actions < 0) { @@ -667,15 +678,25 @@ sub pandora_execute_alert ($$$$$$$$;$) { } # Event alert else { - @actions = get_db_rows ($dbh, 'SELECT * FROM tevent_alert_action, talert_actions, talert_commands - WHERE tevent_alert_action.id_alert_action = talert_actions.id - AND talert_actions.id_alert_command = talert_commands.id - AND tevent_alert_action.id_event_alert = ? - AND ((fires_min = 0 AND fires_max = 0) - OR (fires_min <= fires_max AND ? >= fires_min AND ? <= fires_max) - OR (fires_min > fires_max AND ? >= fires_min))', - $alert->{'id'}, $alert->{'times_fired'}, $alert->{'times_fired'}, $alert->{'times_fired'}); - + if ($alert_mode == RECOVERED_ALERT) { + @actions = get_db_rows ($dbh, 'SELECT * FROM tevent_alert_action, talert_actions, talert_commands + WHERE tevent_alert_action.id_alert_action = talert_actions.id + AND talert_actions.id_alert_command = talert_commands.id + AND tevent_alert_action.id_event_alert = ? + AND ((fires_min = 0 AND fires_max = 0) + OR ? >= fires_min)', + $alert->{'id'}, $alert->{'times_fired'}); + } else { + @actions = get_db_rows ($dbh, 'SELECT * FROM tevent_alert_action, talert_actions, talert_commands + WHERE tevent_alert_action.id_alert_action = talert_actions.id + AND talert_actions.id_alert_command = talert_commands.id + AND tevent_alert_action.id_event_alert = ? + AND ((fires_min = 0 AND fires_max = 0) + OR (fires_min <= fires_max AND ? >= fires_min AND ? <= fires_max) + OR (fires_min > fires_max AND ? >= fires_min))', + $alert->{'id'}, $alert->{'times_fired'}, $alert->{'times_fired'}, $alert->{'times_fired'}); + } + # Get default action if ($#actions < 0) { @actions = get_db_rows ($dbh, 'SELECT * FROM talert_actions, talert_commands @@ -733,7 +754,7 @@ sub pandora_execute_alert ($$$$$$$$;$) { if ($event_generated == 0) { #If we've spotted an alert recovered, we set the new event's severity to 2 (NORMAL), otherwise the original value is maintained. - my ($text, $event, $severity) = ($alert_mode == 0) ? ('recovered', 'alert_recovered', 2) : ('fired', 'alert_fired', $alert->{'priority'}); + my ($text, $event, $severity) = ($alert_mode == RECOVERED_ALERT) ? ('recovered', 'alert_recovered', 2) : ('fired', 'alert_fired', $alert->{'priority'}); pandora_event ($pa_config, "Alert $text (" . safe_output($alert->{'name'}) . ") " . (defined ($module) ? 'assigned to ('. safe_output($module->{'nombre'}) . ")" : ""), (defined ($agent) ? $agent->{'id_grupo'} : 0), (defined ($agent) ? $agent->{'id_agente'} : 0), $severity, (defined ($alert->{'id_template_module'}) ? $alert->{'id_template_module'} : 0), @@ -785,7 +806,7 @@ sub pandora_execute_action ($$$$$$$$$;$) { } # Recovery fields, thanks to Kato Atsushi - if ($alert_mode == 0) { + if ($alert_mode == RECOVERED_ALERT) { # Field 1 is a special case where [RECOVER] prefix is not added even when it is defined $field1 = $alert->{'field1_recovery'} ? $alert->{'field1_recovery'} : $field1; $field1 = $action->{'field1_recovery'} ? $action->{'field1_recovery'} : $field1; diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 265374fb6a..c31e2751e4 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -58,6 +58,8 @@ our @EXPORT = qw( SNMPSERVER METACONSOLE_LICENSE $DEVNULL + RECOVERED_ALERT + FIRED_ALERT cron_get_closest_in_range cron_next_execution cron_next_execution_date @@ -110,6 +112,10 @@ use constant SNMPSERVER => 12; # Value for a metaconsole license type use constant METACONSOLE_LICENSE => 0x01; +# Alert modes +use constant RECOVERED_ALERT => 0; +use constant FIRED_ALERT => 1; + # /dev/null our $DEVNULL = ($^O eq 'MSWin32') ? '/Nul' : '/dev/null';