wip special days alerts

This commit is contained in:
Daniel Barbero Martin 2021-11-04 12:33:40 +01:00
parent 93aad130cb
commit c3f4addb0e
7 changed files with 94 additions and 22 deletions

View File

@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` (
`name` varchar(100) NOT NULL default '', `name` varchar(100) NOT NULL default '',
`id_group` INT(10) NOT NULL DEFAULT 0, `id_group` INT(10) NOT NULL DEFAULT 0,
`description` text, `description` text,
PRIMARY KEY (`id`) PRIMARY KEY (`id`),
UNIQUE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar'); INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar');

View File

@ -4230,7 +4230,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` (
`name` varchar(100) NOT NULL default '', `name` varchar(100) NOT NULL default '',
`id_group` INT(10) NOT NULL DEFAULT 0, `id_group` INT(10) NOT NULL DEFAULT 0,
`description` text, `description` text,
PRIMARY KEY (`id`) PRIMARY KEY (`id`),
UNIQUE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar'); INSERT IGNORE INTO `talert_calendar` VALUES (1, 'Default', 0, 'Default calendar');

View File

@ -848,7 +848,7 @@ class CalendarManager
if ($change === true && empty($search) === false) { if ($change === true && empty($search) === false) {
$reason = \__( $reason = \__(
'Failed saving calendar: name exists', 'Failed saving calendar: already exists',
$config['dbconnection']->error $config['dbconnection']->error
); );
} else { } else {
@ -958,6 +958,7 @@ class CalendarManager
'method' => 'dataAlertTemplates', 'method' => 'dataAlertTemplates',
'day_code' => $day_code, 'day_code' => $day_code,
'id_calendar' => $id_calendar, 'id_calendar' => $id_calendar,
'id_group' => $id_group,
], ],
'no_sortable_columns' => [-1], 'no_sortable_columns' => [-1],
'order' => [ 'order' => [
@ -1014,8 +1015,14 @@ class CalendarManager
$filter[] = "name LIKE '%".$filters['name']."%'"; $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; $filter['special_day'] = $id_calendar;
if ($id_group !== 0) {
$filter['id_group'] = $id_group;
}
$templates = alerts_get_alert_templates($filter); $templates = alerts_get_alert_templates($filter);
$count = alerts_get_alert_templates($filter, ['COUNT(*) AS total']); $count = alerts_get_alert_templates($filter, ['COUNT(*) AS total']);

View File

@ -14148,10 +14148,10 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3)
} }
$special_day = $other['data'][0]; $special_day = $other['data'][0];
$day_code = $other['data'][1]; $same_day = $other['data'][1];
$description = $other['data'][2]; $description = $other['data'][2];
$idGroup = $other['data'][3]; $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)) { if (!check_acl($config['id_user'], $idGroup, 'LM', true)) {
returnError('forbidden', 'string'); 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 { try {
$sd = new SpecialDay(); $sd = new SpecialDay();
$sd->date($special_day); $sd->date($special_day);
@ -14197,9 +14227,9 @@ function api_set_create_special_day($thrash1, $thrash2, $other, $thrash3)
$sd->id_calendar($id_calendar); $sd->id_calendar($id_calendar);
$sd->save(); $sd->save();
if ($sd->save() === true) { if ($sd->save() === true) {
returnError('Special Day could not be created');
} else {
returnData('string', ['type' => 'string', 'data' => $sd->id()]); returnData('string', ['type' => 'string', 'data' => $sd->id()]);
} else {
returnError('Special Day could not be created');
} }
} catch (Exception $e) { } catch (Exception $e) {
returnData('string', ['type' => 'string', 'data' => $e]); returnData('string', ['type' => 'string', 'data' => $e]);

View File

@ -578,7 +578,8 @@ CREATE TABLE IF NOT EXISTS `talert_calendar` (
`name` varchar(100) NOT NULL default '', `name` varchar(100) NOT NULL default '',
`id_group` INT(10) NOT NULL DEFAULT 0, `id_group` INT(10) NOT NULL DEFAULT 0,
`description` text, `description` text,
PRIMARY KEY (`id`) PRIMARY KEY (`id`),
UNIQUE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------------------------------- -- -----------------------------------------------------

View File

@ -531,15 +531,16 @@ sub pandora_evaluate_alert ($$$$$$$;$$$$) {
my $date = sprintf("%4d%02d%02d", $year + 1900, $mon + 1, $mday); my $date = sprintf("%4d%02d%02d", $year + 1900, $mon + 1, $mday);
# '0001' means every year. # '0001' means every year.
my $date_every_year = sprintf("0001%02d%02d", $mon + 1, $mday); 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)) { if (!defined($special_day)) {
$special_day = ''; $special_day = 0;
} }
if ($special_day ne '') { my @weeks = ( 'none', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'holiday');
logger ($pa_config, $date . " is a special day for " . $alert->{'name'} . ". (as a " . $special_day . ")", 10); if ($special_day != 0) {
return $status if ($alert->{$special_day} != 1); 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 { else {
logger ($pa_config, $date . " is *NOT* a special day for " . $alert->{'name'}, 10); logger ($pa_config, $date . " is *NOT* a special day for " . $alert->{'name'}, 10);

View File

@ -179,7 +179,7 @@ sub help_screen{
help_screen_line('--update_alert_template', "<template_name> <field_to_change> \n\t <new_value>", 'Update a field of an alert template'); help_screen_line('--update_alert_template', "<template_name> <field_to_change> \n\t <new_value>", 'Update a field of an alert template');
help_screen_line('--validate_all_alerts', '', 'Validate all the alerts'); help_screen_line('--validate_all_alerts', '', 'Validate all the alerts');
help_screen_line('--validate_alert', '<template_name> <agent_id> <module_id> [<use_alias>]', 'Validate alert given angent, module and alert'); help_screen_line('--validate_alert', '<template_name> <agent_id> <module_id> [<use_alias>]', 'Validate alert given angent, module and alert');
help_screen_line('--create_special_day', "<special_day> <same_day> <description> <group>", 'Create special day'); help_screen_line('--create_special_day', "<special_day> <calendar_name> <same_day> <description> <group>", 'Create special day');
help_screen_line('--delete_special_day', '<special_day>', 'Delete special day'); help_screen_line('--delete_special_day', '<special_day>', 'Delete special day');
help_screen_line('--update_special_day', "<special_day> <field_to_change> <new_value>", 'Update a field of a special day'); help_screen_line('--update_special_day', "<special_day> <field_to_change> <new_value>", 'Update a field of a special day');
help_screen_line('--create_data_module_from_local_component', '<agent_name> <component_name> [<use_alias>]', "Create a new data \n\t module from a local component"); help_screen_line('--create_data_module_from_local_component', '<agent_name> <component_name> [<use_alias>]', "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; 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. ## Delete a special day.
########################################################################## ##########################################################################
@ -6413,12 +6438,16 @@ sub pandora_get_network_component_id($$) {
############################################################################## ##############################################################################
sub cli_create_special_day() { 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); 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($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; my $group_id = 0;
# If group name is not defined, we assign group All (0) # If group name is not defined, we assign group All (0)
if(defined($group_name)) { if(defined($group_name)) {
$group_id = get_group_id($dbh, decode('UTF-8', $group_name)); $group_id = get_group_id($dbh, decode('UTF-8', $group_name));
@ -6434,19 +6463,21 @@ sub cli_create_special_day() {
help_screen (); help_screen ();
exit 1; 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"; print_log "[ERROR] '$same_day' is invalid day.\n\n";
$param = '--create_special_day'; $param = '--create_special_day';
help_screen (); help_screen ();
exit 1; exit 1;
} }
my %parameters; my %parameters;
$parameters{"${RDBMS_QUOTE}date${RDBMS_QUOTE}"} = $special_day; $parameters{"${RDBMS_QUOTE}date${RDBMS_QUOTE}"} = $special_day;
$parameters{'same_day'} = $same_day; $parameters{'same_day'} = $same_day;
$parameters{'description'} = decode('UTF-8', $description); $parameters{'description'} = decode('UTF-8', $description);
$parameters{'id_group'} = $group_id; $parameters{'id_group'} = $group_id;
$parameters{'calendar_name'} = $calendar_name;
pandora_create_special_day_from_hash ($conf, \%parameters, $dbh); pandora_create_special_day_from_hash ($conf, \%parameters, $dbh);
} }