Merge branch 'ent-I-do-now-like-procedures' into 'develop'

Allow both behaviours in alert time slots

See merge request artica/pandorafms!4711
This commit is contained in:
Daniel Rodriguez 2022-02-24 09:55:40 +00:00
commit 3f7067fe48
6 changed files with 158 additions and 158 deletions

View File

@ -14,14 +14,6 @@ ALTER TABLE `talert_execution_queue`
ALTER TABLE `talert_templates` ADD COLUMN `schedule` TEXT; ALTER TABLE `talert_templates` ADD COLUMN `schedule` TEXT;
ALTER TABLE `tevent_alert` ADD COLUMN `schedule` TEXT; ALTER TABLE `tevent_alert` ADD COLUMN `schedule` TEXT;
SOURCE procedures/alertTemplates.sql;
CALL `migrateRanges`();
DROP PROCEDURE `migrateRanges`;
SOURCE procedures/alertEvents.sql;
CALL `migrateEventRanges`();
DROP PROCEDURE `migrateEventRanges`;
ALTER TABLE `tautoconfig` ADD COLUMN `disabled` TINYINT DEFAULT 0; ALTER TABLE `tautoconfig` ADD COLUMN `disabled` TINYINT DEFAULT 0;
COMMIT; COMMIT;

View File

@ -1,40 +0,0 @@
CREATE PROCEDURE migrateEventRanges()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE i INT;
DECLARE r TEXT;
DECLARE cur1 CURSOR FOR SELECT `id` FROM `tevent_alert`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
UPDATE `tevent_alert` SET `schedule` = '{"monday":_1_,"tuesday":_2_,"wednesday":_3_,"thursday":_4_,"friday":_5_,"saturday":_6_,"sunday":_7_}';
UPDATE `tevent_alert` SET `time_from` = "00:00:00", `time_to` = "00:00:00" WHERE `time_from` = `time_to`;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO i;
IF done THEN
LEAVE read_loop;
END IF;
SELECT concat('[{"start":"', `time_from`, '","end":"', `time_to`, '"}]') into r FROM `tevent_alert` WHERE `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_1_", r) WHERE `monday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_2_", r) WHERE `tuesday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_3_", r) WHERE `wednesday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_4_", r) WHERE `thursday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_5_", r) WHERE `friday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_6_", r) WHERE `saturday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_7_", r) WHERE `sunday` > 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_1_", '""') WHERE `monday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_2_", '""') WHERE `tuesday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_3_", '""') WHERE `wednesday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_4_", '""') WHERE `thursday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_5_", '""') WHERE `friday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_6_", '""') WHERE `saturday` = 0 AND `id` = i;
UPDATE `tevent_alert` SET `schedule` = REPLACE(`schedule`, "_7_", '""') WHERE `sunday` = 0 AND `id` = i;
END LOOP;
CLOSE cur1;
END ;

View File

@ -1,40 +0,0 @@
CREATE PROCEDURE migrateRanges()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE i INT;
DECLARE r TEXT;
DECLARE cur1 CURSOR FOR SELECT `id` FROM `talert_templates`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
UPDATE `talert_templates` SET `schedule` = '{"monday":_1_,"tuesday":_2_,"wednesday":_3_,"thursday":_4_,"friday":_5_,"saturday":_6_,"sunday":_7_}';
UPDATE `talert_templates` SET `time_from` = "00:00:00", `time_to` = "00:00:00" WHERE `time_from` = `time_to`;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO i;
IF done THEN
LEAVE read_loop;
END IF;
SELECT concat('[{"start":"', `time_from`, '","end":"', `time_to`, '"}]') into r FROM `talert_templates` WHERE `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_1_", r) WHERE `monday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_2_", r) WHERE `tuesday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_3_", r) WHERE `wednesday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_4_", r) WHERE `thursday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_5_", r) WHERE `friday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_6_", r) WHERE `saturday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_7_", r) WHERE `sunday` > 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_1_", '""') WHERE `monday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_2_", '""') WHERE `tuesday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_3_", '""') WHERE `wednesday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_4_", '""') WHERE `thursday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_5_", '""') WHERE `friday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_6_", '""') WHERE `saturday` = 0 AND `id` = i;
UPDATE `talert_templates` SET `schedule` = REPLACE(`schedule`, "_7_", '""') WHERE `sunday` = 0 AND `id` = i;
END LOOP;
CLOSE cur1;
END ;

View File

@ -61,50 +61,7 @@ if (defined('LAST_STEP') === false) {
} }
// Default events calendar. // Default events calendar.
$default_events_calendar = [ $default_events_calendar = default_events_calendar($id, 'talert_templates');
'monday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'tuesday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'wednesday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'thursday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'friday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'saturday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'sunday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
];
if ($duplicate_template === true) { if ($duplicate_template === true) {
$source_id = (int) get_parameter('source_id'); $source_id = (int) get_parameter('source_id');
@ -370,7 +327,12 @@ function update_template($step)
$result = alerts_update_alert_template($id, $values); $result = alerts_update_alert_template($id, $values);
} else if ($step == 2) { } else if ($step == 2) {
$schedule = get_parameter('schedule'); $schedule = io_safe_output(get_parameter('schedule', []));
json_decode($schedule, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}
$special_day = (int) get_parameter('special_day'); $special_day = (int) get_parameter('special_day');
$threshold = (int) get_parameter('threshold'); $threshold = (int) get_parameter('threshold');
$max_alerts = (int) get_parameter('max_alerts'); $max_alerts = (int) get_parameter('max_alerts');
@ -389,12 +351,7 @@ function update_template($step)
} }
$values = [ $values = [
'schedule' => json_encode( 'schedule' => $schedule,
json_decode(
io_safe_output($schedule),
true
)
),
'special_day' => $special_day, 'special_day' => $special_day,
'time_threshold' => $threshold, 'time_threshold' => $threshold,
'id_alert_action' => $default_action, 'id_alert_action' => $default_action,
@ -485,7 +442,6 @@ $type = '';
$value = ''; $value = '';
$max = ''; $max = '';
$min = ''; $min = '';
$schedule = json_encode( $schedule = json_encode(
$default_events_calendar $default_events_calendar
); );
@ -614,7 +570,9 @@ if ($id && ! $create_template) {
$min = $template['min_value']; $min = $template['min_value'];
$matches = $template['matches_value']; $matches = $template['matches_value'];
$schedule = $template['schedule']; $schedule = json_encode(
$default_events_calendar
);
$special_day = (int) $template['special_day']; $special_day = (int) $template['special_day'];
$max_alerts = $template['max_alerts']; $max_alerts = $template['max_alerts'];
$min_alerts = $template['min_alerts']; $min_alerts = $template['min_alerts'];

View File

@ -3395,3 +3395,120 @@ function alerts_get_templates_name_array($array_ids)
return $result; return $result;
} }
/**
* Default values events calendar templates.
*
* @param integer $id ID.
* @param string $table Name table.
*
* @return array Data Events.
*/
function default_events_calendar($id, $table)
{
$result = [
'monday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'tuesday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'wednesday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'thursday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'friday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'saturday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
'sunday' => [
[
'start' => '00:00:00',
'end' => '00:00:00',
],
],
];
$days = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
];
// Check Exists.
if (empty($id) === false) {
$sql_default_alert = sprintf(
'SELECT `id`,
`name`,
`time_from`,
`time_to`,
`monday`,
`tuesday`,
`wednesday`,
`thursday`,
`friday`,
`saturday`,
`sunday`,
`schedule`
FROM %s
WHERE id = %d',
$table,
$id
);
$r = db_get_row_sql($sql_default_alert);
if ($r != false) {
// Check Exist schedule.
if (empty($r['schedule']) === false) {
$result = json_decode(io_safe_output($r['schedule']), true);
} else {
// Compatibility mode old.
$result = [];
foreach ($days as $day) {
if ((int) $r[$day] === 1) {
$start = $r['time_from'];
$to = $r['time_to'];
if ($r['time_from'] === $r['time_to']) {
$start = '00:00:00';
$to = '00:00:00';
}
$result[$day][0] = [
'start' => $start,
'end' => $to,
];
}
}
}
}
}
return $result;
}

View File

@ -552,30 +552,43 @@ sub pandora_evaluate_alert ($$$$$$$;$$$$) {
} }
my $schedule = PandoraFMS::Tools::p_decode_json($pa_config, $alert->{'schedule'}); my $schedule = PandoraFMS::Tools::p_decode_json($pa_config, $alert->{'schedule'});
if (defined($schedule)) {
# New behaviour.
return $status unless defined($schedule) && ref $schedule eq "HASH";
return $status unless defined($schedule) && ref $schedule eq "HASH"; return $status unless defined($schedule->{$DayNames[$wday]});
return $status unless defined($schedule->{$DayNames[$wday]}); return $status unless ref($schedule->{$DayNames[$wday]}) eq "ARRAY";
return $status unless ref($schedule->{$DayNames[$wday]}) eq "ARRAY"; my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec);
my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec); #
# Check time slots
#
my $inSlot = 0;
foreach my $timeBlock (@{$schedule->{$DayNames[$wday]}}) {
if ($timeBlock->{'start'} eq $timeBlock->{'end'}) {
# All day.
$inSlot = 1;
} elsif ($timeBlock->{'start'} le $time && $timeBlock->{'end'} ge $time) {
# In range.
$inSlot = 1;
}
}
# return $status if $inSlot eq 0;
# Check time slots } else {
# # Old behaviour.
my $inSlot = 0; # Check time slot
foreach my $timeBlock (@{$schedule->{$DayNames[$wday]}}) { my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec);
if ($timeBlock->{'start'} eq $timeBlock->{'end'}) { if (($alert->{'time_from'} ne $alert->{'time_to'})) {
# All day. if ($alert->{'time_from'} lt $alert->{'time_to'}) {
$inSlot = 1; return $status if (($time le $alert->{'time_from'}) || ($time ge $alert->{'time_to'}));
} elsif ($timeBlock->{'start'} le $time && $timeBlock->{'end'} ge $time) { } else {
# In range. return $status if (($time le $alert->{'time_from'}) && ($time ge $alert->{'time_to'}));
$inSlot = 1; }
} }
} }
return $status if $inSlot eq 0;
# Check time threshold # Check time threshold
my $limit_utimestamp = $alert->{'last_reference'} + $alert->{'time_threshold'}; my $limit_utimestamp = $alert->{'last_reference'} + $alert->{'time_threshold'};