From c3f4addb0e4d57d7c6845c5a34c6494e5d264a13 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 4 Nov 2021 12:33:40 +0100 Subject: [PATCH] wip special days alerts --- pandora_console/extras/mr/51.sql | 3 +- .../pandoradb_migrate_6.0_to_7.0.mysql.sql | 3 +- .../include/class/CalendarManager.class.php | 11 ++++- pandora_console/include/functions_api.php | 38 ++++++++++++++-- pandora_console/pandoradb.sql | 3 +- pandora_server/lib/PandoraFMS/Core.pm | 15 ++++--- pandora_server/util/pandora_manage.pl | 43 ++++++++++++++++--- 7 files changed, 94 insertions(+), 22 deletions(-) diff --git a/pandora_console/extras/mr/51.sql b/pandora_console/extras/mr/51.sql index eecc97c4c8..d89d3807cd 100644 --- a/pandora_console/extras/mr/51.sql +++ b/pandora_console/extras/mr/51.sql @@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` ( `name` varchar(100) NOT NULL default '', `id_group` INT(10) NOT NULL DEFAULT 0, `description` text, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar'); diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index 2488289dbf..275cf5a2a9 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -4230,7 +4230,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` ( `name` varchar(100) NOT NULL default '', `id_group` INT(10) NOT NULL DEFAULT 0, `description` text, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar'); diff --git a/pandora_console/include/class/CalendarManager.class.php b/pandora_console/include/class/CalendarManager.class.php index 125c4c1c26..d62fa2e9b1 100644 --- a/pandora_console/include/class/CalendarManager.class.php +++ b/pandora_console/include/class/CalendarManager.class.php @@ -848,7 +848,7 @@ class CalendarManager if ($change === true && empty($search) === false) { $reason = \__( - 'Failed saving calendar: name exists', + 'Failed saving calendar: already exists', $config['dbconnection']->error ); } else { @@ -958,6 +958,7 @@ class CalendarManager 'method' => 'dataAlertTemplates', 'day_code' => $day_code, 'id_calendar' => $id_calendar, + 'id_group' => $id_group, ], 'no_sortable_columns' => [-1], 'order' => [ @@ -1014,8 +1015,14 @@ class CalendarManager $filter[] = "name LIKE '%".$filters['name']."%'"; } - $id_calendar = get_parameter('id_calendar', 0); + $id_calendar = (int) get_parameter('id_calendar', 0); + $id_group = (int) get_parameter('id_group', 0); $filter['special_day'] = $id_calendar; + + if ($id_group !== 0) { + $filter['id_group'] = $id_group; + } + $templates = alerts_get_alert_templates($filter); $count = alerts_get_alert_templates($filter, ['COUNT(*) AS total']); diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 162a48bc14..bfdc8c712e 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -14148,10 +14148,10 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3) } $special_day = $other['data'][0]; - $day_code = $other['data'][1]; + $same_day = $other['data'][1]; $description = $other['data'][2]; $idGroup = $other['data'][3]; - $id_calendar = $other['data'][4] || 1; + $calendar_name = (isset($other['data'][4]) === true) ? $other['data'][4] : 'Default'; if (!check_acl($config['id_user'], $idGroup, 'LM', true)) { returnError('forbidden', 'string'); @@ -14188,6 +14188,36 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3) } } + $weekdays = [ + 'monday' => 1, + 'tuesday' => 2, + 'wednesday' => 3, + 'thursday' => 4, + 'friday' => 5, + 'saturday' => 6, + 'sunday' => 7, + 'holiday' => 8, + ]; + + $day_code = (isset($weekdays[$same_day]) === true) ? $weekdays[$same_day] : 0; + + if ($day_code === 0) { + returnError('Special Day could not be created. Same day doesn\'t exists.'); + return; + } + + $id_calendar = db_get_value_sql( + sprintf( + 'SELECT id FROM talert_calendar WHERE name="%s"', + $calendar_name + ) + ); + + if ($id_calendar === false) { + returnError('Special Day could not be created. Calendar doesn\'t exists.'); + return; + } + try { $sd = new SpecialDay(); $sd->date($special_day); @@ -14197,9 +14227,9 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3) $sd->id_calendar($id_calendar); $sd->save(); if ($sd->save() === true) { - returnError('Special Day could not be created'); - } else { returnData('string', ['type' => 'string', 'data' => $sd->id()]); + } else { + returnError('Special Day could not be created'); } } catch (Exception $e) { returnData('string', ['type' => 'string', 'data' => $e]); diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index bdea73e00c..cc0bef115d 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -578,7 +578,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` ( `name` varchar(100) NOT NULL default '', `id_group` INT(10) NOT NULL DEFAULT 0, `description` text, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------------------------------- diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index c57df509ff..54d2b34a49 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -531,15 +531,16 @@ sub pandora_evaluate_alert ($$$$$$$;$$$$) { my $date = sprintf("%4d%02d%02d", $year + 1900, $mon + 1, $mday); # '0001' means every year. my $date_every_year = sprintf("0001%02d%02d", $mon + 1, $mday); - my $special_day = get_db_value ($dbh, 'SELECT same_day FROM talert_special_days WHERE (date = ? OR date = ?) AND (id_group = 0 OR id_group = ?) ORDER BY date DESC', $date, $date_every_year, $alert->{'id_group'}); - + my $special_day = get_db_value ($dbh, 'SELECT day_code FROM talert_special_days WHERE (date = ? OR date = ?) AND (id_group = 0 OR id_group = ?) AND (id_calendar = ?) ORDER BY date DESC', $date, $date_every_year, $alert->{'id_group'}, $alert->{'special_day'}); + if (!defined($special_day)) { - $special_day = ''; + $special_day = 0; } - - if ($special_day ne '') { - logger ($pa_config, $date . " is a special day for " . $alert->{'name'} . ". (as a " . $special_day . ")", 10); - return $status if ($alert->{$special_day} != 1); + + my @weeks = ( 'none', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'holiday'); + if ($special_day != 0) { + logger ($pa_config, $date . " is a special day for " . $alert->{'name'} . ". (as a " . $weeks[$special_day] . ")", 10); + return $status if (!defined($alert->{$weeks[$special_day]}) || $alert->{$weeks[$special_day]} == 0); } else { logger ($pa_config, $date . " is *NOT* a special day for " . $alert->{'name'}, 10); diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 2039eb6452..55c17aaacf 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -179,7 +179,7 @@ sub help_screen{ help_screen_line('--update_alert_template', " \n\t ", 'Update a field of an alert template'); help_screen_line('--validate_all_alerts', '', 'Validate all the alerts'); help_screen_line('--validate_alert', ' []', 'Validate alert given angent, module and alert'); - help_screen_line('--create_special_day', " ", 'Create special day'); + help_screen_line('--create_special_day', " ", 'Create special day'); help_screen_line('--delete_special_day', '', 'Delete special day'); help_screen_line('--update_special_day', " ", 'Update a field of a special day'); help_screen_line('--create_data_module_from_local_component', ' []', "Create a new data \n\t module from a local component"); @@ -956,6 +956,31 @@ sub pandora_get_special_day_id ($$) { return defined ($special_day_id) ? $special_day_id : -1; } + +########################################################################## +## SUB pandora_get_calendar_id(id) +## Return calendar id, given "calendar_name" +########################################################################## +sub pandora_get_calendar_id ($$) { + my ($dbh, $calendar_name) = @_; + + my $calendar_id = get_db_value ($dbh, "SELECT id FROM talert_calendar WHERE ${RDBMS_QUOTE}name${RDBMS_QUOTE} = ?", $calendar_name); + + return defined ($calendar_id) ? $calendar_id : -1; +} + +########################################################################## +## SUB pandora_get_same_day_id(id) +## Return same day id, given "same_day" +########################################################################## +sub pandora_get_same_day_id ($$) { + my ($dbh, $same_day) = @_; + + my $weeks = { 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6, 'sunday' => 7, 'holiday' => 8}; + + return defined ($weeks{$same_day}) ? $weeks{$same_day} : -1; +} + ########################################################################## ## Delete a special day. ########################################################################## @@ -6413,12 +6438,16 @@ sub pandora_get_network_component_id($$) { ############################################################################## sub cli_create_special_day() { - my ($special_day, $same_day, $description, $group_name) = @ARGV[2..5]; + my ($special_day, $calendar_name, $same_day, $description, $group_name) = @ARGV[2..5]; + my $calendar_name_exists = pandora_get_calendar_id ($dbh, $calendar_name); + my $same_day_exists = pandora_get_same_day_id ($dbh, $same_day); my $special_day_exists = pandora_get_special_day_id ($dbh, $special_day); non_exist_check($special_day_exists,'special day',$special_day); + non_exist_check($calendar_name_exists,'calendar name',$calendar_name); + non_exist_check($same_day_exists,'same day',$same_day); my $group_id = 0; - + # If group name is not defined, we assign group All (0) if(defined($group_name)) { $group_id = get_group_id($dbh, decode('UTF-8', $group_name)); @@ -6434,19 +6463,21 @@ sub cli_create_special_day() { help_screen (); exit 1; } - if ($same_day !~ /monday|tuesday|wednesday|thursday|friday|saturday|sunday/) { + + if ($same_day !~ /monday|tuesday|wednesday|thursday|friday|saturday|sunday|holiday/) { print_log "[ERROR] '$same_day' is invalid day.\n\n"; $param = '--create_special_day'; help_screen (); exit 1; } - + my %parameters; - + $parameters{"${RDBMS_QUOTE}date${RDBMS_QUOTE}"} = $special_day; $parameters{'same_day'} = $same_day; $parameters{'description'} = decode('UTF-8', $description); $parameters{'id_group'} = $group_id; + $parameters{'calendar_name'} = $calendar_name; pandora_create_special_day_from_hash ($conf, \%parameters, $dbh); }