From 05ce178854f40abbfc4691b56d2bf07e2b3b6d38 Mon Sep 17 00:00:00 2001 From: Fermin Date: Fri, 29 Mar 2019 11:51:51 +0100 Subject: [PATCH 1/8] Fixed week day cron in server Former-commit-id: cfbd3601904c85fa1112eaf0f2310edb53f9afe8 --- pandora_server/lib/PandoraFMS/Tools.pm | 82 +++++++++++--------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index b086ab1fbd..af1059ebd4 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -1359,45 +1359,22 @@ sub cron_next_execution { } # Get day of the week and month from cron config - my ($mday, $wday) = (split (/\s/, $cron))[2, 4]; + my ($wday) = (split (/\s/, $cron))[4]; # Get current time and day of the week my $cur_time = time(); my $cur_wday = (localtime ($cur_time))[6]; - # Any day of the week - if ($wday eq '*') { - my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); - return $nex_time - time(); - } - # A range? - else { - $wday = cron_get_closest_in_range ($cur_wday, $wday); + my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); + + # Check the day + while (!cron_check_interval($wday, (localtime ($nex_time))[6])) { + # If it does not acomplish the day of the week, go to the next day. + $nex_time += 86400; + $nex_time = cron_next_execution_date ($cron, $nex_time, 0); } - # A specific day of the week - my $count = 0; - my $nex_time = $cur_time; - do { - $nex_time = cron_next_execution_date ($cron, $nex_time, $interval); - my $nex_time_wd = $nex_time; - my ($nex_mon, $nex_wday) = (localtime ($nex_time_wd))[4, 6]; - my $nex_mon_wd; - do { - # Check the day of the week - if ($nex_wday == $wday) { - return $nex_time_wd - time(); - } - - # Move to the next day of the month - $nex_time_wd += 86400; - ($nex_mon_wd, $nex_wday) = (localtime ($nex_time_wd))[4, 6]; - } while ($mday eq '*' && $nex_mon_wd == $nex_mon); - $count++; - } while ($count < 60); - - # Something went wrong, default to 5 minutes - return $interval; + return $nex_time - time(); } ############################################################################### # Get the number of seconds left to the next execution of the given cron entry. @@ -1409,6 +1386,30 @@ sub cron_check_syntax ($) { return ($cron =~ m/^(\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+$/); } ############################################################################### +# Check if a value is inside an interval. +############################################################################### +sub cron_check_interval { + my ($elem_cron, $elem_curr_time) = @_; + + # Return 1 if wildcard. + return 1 if ($elem_cron eq "*"); + + my ($down, $up) = cron_get_interval($elem_cron); + # Check if it is not a range + if (!defined($up)) { + return ($down == $elem_curr_time) ? 1 : 0; + } + + # Check if it is on the range + if ($down < $up) { + return 0 if ($elem_curr_time < $down || $elem_curr_time > $up); + } else { + return 0 if ($elem_curr_time > $down || $elem_curr_time < $up); + } + + return 1; +} +############################################################################### # Get the next execution date for the given cron entry in seconds since epoch. ############################################################################### sub cron_next_execution_date { @@ -1560,20 +1561,9 @@ sub cron_is_in_cron { #If there is no elements means that is in cron return 1 unless (defined($elem_cron) || defined($elem_curr_time)); - # Go to last element if current is a wild card - if ($elem_cron ne '*') { - my ($down, $up) = cron_get_interval($elem_cron); - # Check if there is no a range - return 0 if (!defined($up) && ($down != $elem_curr_time)); - # Check if there is on the range - if (defined($up)) { - if ($down < $up) { - return 0 if ($elem_curr_time < $down || $elem_curr_time > $up); - } else { - return 0 if ($elem_curr_time > $down || $elem_curr_time < $up); - } - } - } + # Check the element interval + return 0 unless (cron_check_interval($elem_cron, $elem_curr_time)); + return cron_is_in_cron(\@deref_elems_cron, \@deref_elems_curr_time); } ############################################################################### From 4fd3aa3a51e90c27bf08908f93dc84bb00723866 Mon Sep 17 00:00:00 2001 From: Fermin Date: Fri, 29 Mar 2019 15:06:26 +0100 Subject: [PATCH 2/8] Avoid to execute non cron complianced modules on creation Former-commit-id: edfb13175ca00bc5aee99609c0eec30e06d5936e --- pandora_console/include/functions_cron.php | 16 +++++++++------- pandora_console/include/functions_modules.php | 3 ++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pandora_console/include/functions_cron.php b/pandora_console/include/functions_cron.php index e56a3d4a52..2823bfc4c6 100644 --- a/pandora_console/include/functions_cron.php +++ b/pandora_console/include/functions_cron.php @@ -20,15 +20,17 @@ function cron_update_module_interval($module_id, $cron) { // Check for a valid cron. if (!cron_check_syntax($cron)) { - return; + return false; } - if ($cron == '* * * * *') { - $module_interval = db_get_value_filter( - 'module_interval', - 'tagente_modulo', - ['id_agente_modulo' => $module_id] - ); + $module_interval = db_get_value( + 'module_interval', + 'tagente_modulo', + 'id_agente_modulo', + $module_id + ); + + if ($cron === '* * * * *') { return db_process_sql( 'UPDATE tagente_estado SET current_interval = '.$module_interval.' WHERE id_agente_modulo = '.(int) $module_id ); diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index ed19c6d2c1..18c89e11c5 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -663,10 +663,11 @@ function modules_create_agent_module( 'estado' => $status, 'known_status' => $status, 'id_agente' => (int) $id_agent, - 'utimestamp' => 0, + 'utimestamp' => (time() - (int) $values['interval']), 'status_changes' => 0, 'last_status' => $status, 'last_known_status' => $status, + 'current_interval' => (int) $values['interval'], ] ); From c5a49916cb3eebe5fa095e4dd123a1274b212af4 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 4 Apr 2019 15:04:23 +0200 Subject: [PATCH 3/8] Fixed cron day of the week Former-commit-id: eb6b7974d7304c430068375f19ef60db1a2bb442 --- pandora_console/include/functions_cron.php | 137 ++++++++++----------- 1 file changed, 64 insertions(+), 73 deletions(-) diff --git a/pandora_console/include/functions_cron.php b/pandora_console/include/functions_cron.php index d76419986e..c4adfe21d9 100644 --- a/pandora_console/include/functions_cron.php +++ b/pandora_console/include/functions_cron.php @@ -48,10 +48,6 @@ function cron_next_execution($cron, $module_interval, $module_id) { // Get day of the week and month from cron config. $cron_array = explode(' ', $cron); - $minute = $cron_array[0]; - $hour = $cron_array[1]; - $mday = $cron_array[2]; - $month = $cron_array[3]; $wday = $cron_array[4]; // Get last execution time. @@ -62,51 +58,18 @@ function cron_next_execution($cron, $module_interval, $module_id) $module_id ); $cur_time = ($last_execution !== false) ? $last_execution : time(); + $nex_time = cron_next_execution_date($cron, $cur_time, $module_interval); + $nex_wday = (int) date('w', $nex_time); - // Any day of the way. - if ($wday == '*') { - $nex_time = cron_next_execution_date( - $cron, - $cur_time, - $module_interval - ); - return ($nex_time - $cur_time); + // Check day of the way. + while (!cron_check_interval($nex_wday, $wday)) { + // If it does not acomplish the day of the week, go to the next day. + $nex_time += SECONDS_1DAY; + $nex_time = cron_next_execution_date($cron, $nex_time, 0); + $nex_wday = (int) date('w', $nex_time); } - // A specific day of the week. - $count = 0; - $nex_time = $cur_time; - do { - $nex_time = cron_next_execution_date( - $cron, - $nex_time, - $module_interval - ); - $nex_time_wd = $nex_time; - - $array_nex = explode(' ', date('m w', $nex_time_wd)); - $nex_mon = $array_nex[0]; - $nex_wday = $array_nex[1]; - - do { - // Check the day of the week. - if ($nex_wday == $wday) { - return ($nex_time_wd - $cur_time); - } - - // Move to the next day of the month. - $nex_time_wd += SECONDS_1DAY; - - $array_nex_w = explode(' ', date('m w', $nex_time_wd)); - $nex_mon_wd = $array_nex_w[0]; - $nex_wday = $array_nex_w[1]; - } while ($mday == '*' && $nex_mon_wd == $nex_mon); - - $count++; - } while ($count < SECONDS_1MINUTE); - - // Something went wrong, default to 5 minutes. - return SECONDS_5MINUTES; + return ($nex_time - $cur_time); } @@ -129,8 +92,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) } // Update minutes. - $min_s = cron_get_interval($cron_array[0]); - $nex_time_array[0] = ($min_s['down'] == '*') ? 0 : $min_s['down']; + $nex_time_array[0] = cron_get_next_time_element($cron_array[0]); $nex_time = cron_valid_date($nex_time_array); if ($nex_time >= $cur_time) { @@ -168,8 +130,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) } // Update the hour if fails. - $hour_s = cron_get_interval($cron_array[1]); - $nex_time_array[1] = ($hour_s['down'] == '*') ? 0 : $hour_s['down']; + $nex_time_array[1] = cron_get_next_time_element($cron_array[1]); // When an overflow is passed check the hour update again. $nex_time = cron_valid_date($nex_time_array); @@ -201,8 +162,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) } // Update the day if fails. - $mday_s = cron_get_interval($cron_array[2]); - $nex_time_array[2] = ($mday_s['down'] == '*') ? 1 : $mday_s['down']; + $nex_time_array[2] = cron_get_next_time_element($cron_array[2]); // When an overflow is passed check the hour update in the next execution. $nex_time = cron_valid_date($nex_time_array); @@ -228,8 +188,7 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) } // Update the month if fails. - $mon_s = cron_get_interval($cron_array[3]); - $nex_time_array[3] = ($mon_s['down'] == '*') ? 1 : $mon_s['down']; + $nex_time_array[3] = cron_get_next_time_element($cron_array[3]); // When an overflow is passed check the hour update in the next execution. $nex_time = cron_valid_date($nex_time_array); @@ -247,6 +206,14 @@ function cron_next_execution_date($cron, $cur_time=false, $module_interval=300) } +function cron_get_next_time_element($cron_array_elem) +{ + $interval = cron_get_interval($cron_array_elem); + $value = ($interval['down'] == '*' || ($interval['up'] !== false && $interval['down'] > $interval['up'] )) ? 0 : $interval['down']; + return $value; +} + + // Get an array with the cron interval. function cron_get_interval($element) { @@ -277,31 +244,55 @@ function cron_is_in_cron($elems_cron, $elems_curr_time) } // Go to last element if current is a wild card. - if ($elem_cron != '*') { - $elem_s = cron_get_interval($elem_cron); - // Check if there is no a range - if (($elem_s['up'] === false) && ($elem_s['down'] != $elem_curr_time)) { - return false; - } - - // Check if there is on the range. - if ($elem_s['up'] !== false) { - if ($elem_s['down'] < $elem_s['up']) { - if ($elem_curr_time < $elem_s['down'] || $elem_curr_time > $elem_s['up']) { - return false; - } - } else { - if ($elem_curr_time > $elem_s['down'] || $elem_curr_time < $elem_s['up']) { - return false; - } - } - } + if (cron_check_interval($elem_curr_time, $elem_cron) === false) { + return false; } return cron_is_in_cron($elems_cron, $elems_curr_time); } +/** + * Check if an element is inside the cron interval or not. + * + * @param integer $elem_curr_time Integer that represents the time to check. + * @param string $elem_cron Cron interval (splitted by hypen) + * or cron single value (a number). + * + * @return boolean True if is in interval. + */ +function cron_check_interval($elem_curr_time, $elem_cron) +{ + // Go to last element if current is a wild card. + if ($elem_cron === '*') { + return true; + } + + $elem_s = cron_get_interval($elem_cron); + // Check if there is no a range. + if (($elem_s['up'] === false) && ($elem_s['down'] != $elem_curr_time)) { + return false; + } + + // Check if there is on the range. + if ($elem_s['up'] !== false && (int) $elem_s['up'] === (int) $elem_curr_time) { + return true; + } + + if ($elem_s['down'] < $elem_s['up']) { + if ($elem_curr_time < $elem_s['down'] || $elem_curr_time > $elem_s['up']) { + return false; + } + } else { + if ($elem_curr_time > $elem_s['down'] || $elem_curr_time < $elem_s['up']) { + return false; + } + } + + return true; +} + + function cron_valid_date($da) { $st = sprintf( From 43b296c49cab8f90ea6b3ae74d35a0c3a4985a8d Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 4 Apr 2019 15:30:30 +0200 Subject: [PATCH 4/8] Added docs to cron functions Former-commit-id: 7f20d7abe989adcfc841e0ce20afbe007b660038 --- pandora_console/include/functions_cron.php | 108 +++++++++++++++++---- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/pandora_console/include/functions_cron.php b/pandora_console/include/functions_cron.php index c4adfe21d9..a5e1b2dcf7 100644 --- a/pandora_console/include/functions_cron.php +++ b/pandora_console/include/functions_cron.php @@ -1,21 +1,36 @@ Date: Thu, 4 Apr 2019 17:47:40 +0200 Subject: [PATCH 5/8] Fixed inverses intervals on server Former-commit-id: 20f9e139bb3f40700e3081e4ce9ec134b70786a6 --- pandora_server/lib/PandoraFMS/Tools.pm | 31 +++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index af1059ebd4..90fe9dc6c1 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -1446,8 +1446,7 @@ sub cron_next_execution_date { my @nex_time_array = @curr_time_array; # Update minutes - my ($min_down, undef) = cron_get_interval ($min); - $nex_time_array[0] = ($min_down eq '*') ? 0 : $min_down; + $nex_time_array[0] = cron_get_next_time_element($min); $nex_time = cron_valid_date(@nex_time_array, $cur_year); if ($nex_time >= $cur_time) { @@ -1480,8 +1479,7 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the hour if fails - my ($hour_down, undef) = cron_get_interval ($hour); - $nex_time_array[1] = ($hour_down eq '*') ? 0 : $hour_down; + $nex_time_array[1] = cron_get_next_time_element($hour); # When an overflow is passed check the hour update again $nex_time = cron_valid_date(@nex_time_array, $cur_year); @@ -1508,8 +1506,7 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the day if fails - my ($mday_down, undef) = cron_get_interval ($mday); - $nex_time_array[2] = ($mday_down eq '*') ? 1 : $mday_down; + $nex_time_array[2] = cron_get_next_time_element($mday, 1); # When an overflow is passed check the hour update in the next execution $nex_time = cron_valid_date(@nex_time_array, $cur_year); @@ -1531,8 +1528,7 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the month if fails - my ($mon_down, undef) = cron_get_interval ($mon); - $nex_time_array[3] = ($mon_down eq '*') ? 0 : $mon_down; + $nex_time_array[3] = cron_get_next_time_element($mon); # When an overflow is passed check the hour update in the next execution $nex_time = cron_valid_date(@nex_time_array, $cur_year); @@ -1566,6 +1562,25 @@ sub cron_is_in_cron { return cron_is_in_cron(\@deref_elems_cron, \@deref_elems_curr_time); } +################################################################################ +#Get the next tentative time for a cron value or interval in case of overflow. +#Floor data is the minimum localtime data for a position. Ex: +#Ex: +# * should returns floor data. +# 5 should returns 5. +# 10-55 should returns 10. +# 55-10 should retunrs floor data. +################################################################################ +sub cron_get_next_time_element { + # Default floor data is 0 + my ($curr_element, $floor_data) = @_; + $floor_data = 0 unless defined($floor_data); + + my ($elem_down, $elem_up) = cron_get_interval ($curr_element); + return ($elem_down eq '*' || (defined($elem_up) && $elem_down > $elem_up)) + ? $floor_data + : $elem_down; +} ############################################################################### # Returns the interval of a cron element. If there is not a range, # returns an array with the first element in the first place of array From e50fb4c5856c5d76155253f022d8b7c58fddad8e Mon Sep 17 00:00:00 2001 From: fermin831 Date: Fri, 5 Apr 2019 12:00:43 +0200 Subject: [PATCH 6/8] Fixed cron day of the week on linux agents Former-commit-id: 2e920e1a3387d0a42dcd67eec1fbd0e0a08d998d --- pandora_agents/unix/pandora_agent | 121 +++++++++++++++--------------- 1 file changed, 62 insertions(+), 59 deletions(-) diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index 7f5e835073..1a047590d8 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -2101,45 +2101,23 @@ sub cron_next_execution { } # Get day of the week and month from cron config - my ($mday, $wday) = (split (/\s/, $cron))[2, 4]; + my ($wday) = (split (/\s/, $cron))[4]; # Get current time and day of the week my $cur_time = time(); my $cur_wday = (localtime ($cur_time))[6]; - # Any day of the week - if ($wday eq '*') { - my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); - return $nex_time - time(); - } - # A range? - else { - $wday = cron_get_closest_in_range ($cur_wday, $wday); + my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); + + # Check the day + print localtime($nex_time)->strftime('%c') . " - OUTSIDE\n"; + while (!cron_check_interval($wday, (localtime ($nex_time))[6])) { + # If it does not acomplish the day of the week, go to the next day. + $nex_time += 86400; + $nex_time = cron_next_execution_date ($cron, $nex_time, 0); } - # A specific day of the week - my $count = 0; - my $nex_time = $cur_time; - do { - $nex_time = cron_next_execution_date ($cron, $nex_time, $interval); - my $nex_time_wd = $nex_time; - my ($nex_mon, $nex_wday) = (localtime ($nex_time_wd))[4, 6]; - my $nex_mon_wd; - do { - # Check the day of the week - if ($nex_wday == $wday) { - return $nex_time_wd - time(); - } - - # Move to the next day of the month - $nex_time_wd += 86400; - ($nex_mon_wd, $nex_wday) = (localtime ($nex_time_wd))[4, 6]; - } while ($mday eq '*' && $nex_mon_wd == $nex_mon); - $count++; - } while ($count < 60); - - # Something went wrong, default to 5 minutes - return $interval; + return $nex_time - time(); } ############################################################################### @@ -2151,7 +2129,30 @@ sub cron_check_syntax ($) { return 0 if !defined ($cron); return ($cron =~ m/^(\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+ (\d|\*|-)+$/); } +############################################################################### +# Check if a value is inside an interval. +############################################################################### +sub cron_check_interval { + my ($elem_cron, $elem_curr_time) = @_; + # Return 1 if wildcard. + return 1 if ($elem_cron eq "*"); + + my ($down, $up) = cron_get_interval($elem_cron); + # Check if it is not a range + if (!defined($up)) { + return ($down == $elem_curr_time) ? 1 : 0; + } + + # Check if it is on the range + if ($down < $up) { + return 0 if ($elem_curr_time < $down || $elem_curr_time > $up); + } else { + return 0 if ($elem_curr_time > $down || $elem_curr_time < $up); + } + + return 1; +} ############################################################################### # Get the next execution date for the given cron entry in seconds since epoch. ############################################################################### @@ -2189,8 +2190,7 @@ sub cron_next_execution_date { my @nex_time_array = @curr_time_array; # Update minutes - my ($min_down, undef) = cron_get_interval ($min); - $nex_time_array[0] = ($min_down eq '*') ? 0 : $min_down; + $nex_time_array[0] = cron_get_next_time_element($min); $nex_time = cron_valid_date(@nex_time_array, $cur_year); if ($nex_time >= $cur_time) { @@ -2224,8 +2224,7 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the hour if fails - my ($hour_down, undef) = cron_get_interval ($hour); - $nex_time_array[1] = ($hour_down eq '*') ? 0 : $hour_down; + $nex_time_array[1] = cron_get_next_time_element($hour); # When an overflow is passed check the hour update again $nex_time = cron_valid_date(@nex_time_array, $cur_year); @@ -2253,10 +2252,9 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the day if fails - my ($mday_down, undef) = cron_get_interval ($mday); - $nex_time_array[2] = ($mday_down eq '*') ? 1 : $mday_down; + $nex_time_array[2] = cron_get_next_time_element($mday, 1); - # When an overflow is passed check the day update in the next execution + # When an overflow is passed check the hour update in the next execution $nex_time = cron_valid_date(@nex_time_array, $cur_year); if ($nex_time >= $cur_time) { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); @@ -2276,8 +2274,7 @@ sub cron_next_execution_date { return $nex_time if cron_is_in_cron(\@cron_array, \@nex_time_array); #Update the month if fails - my ($mon_down, undef) = cron_get_interval ($mon); - $nex_time_array[3] = ($mon_down eq '*') ? 0 : $mon_down; + $nex_time_array[3] = cron_get_next_time_element($mon); # When an overflow is passed check the month update in the next execution $nex_time = cron_valid_date(@nex_time_array, $cur_year); @@ -2308,23 +2305,30 @@ sub cron_is_in_cron { #If there is no elements means that is in cron return 1 unless (defined($elem_cron) || defined($elem_curr_time)); - # Go to last element if current is a wild card - if ($elem_cron ne '*') { - my ($down, $up) = cron_get_interval($elem_cron); - # Check if there is no a range - return 0 if (!defined($up) && ($down != $elem_curr_time)); - # Check if there is on the range - if (defined($up)) { - if ($down < $up) { - return 0 if ($elem_curr_time < $down || $elem_curr_time > $up); - } else { - return 0 if ($elem_curr_time > $down || $elem_curr_time < $up); - } - } - } + # Check the element interval + return 0 unless (cron_check_interval($elem_cron, $elem_curr_time)); + return cron_is_in_cron(\@deref_elems_cron, \@deref_elems_curr_time); } +################################################################################ +#Get the next tentative time for a cron value or interval in case of overflow. +#Floor data is the minimum localtime data for a position. Ex: +#Ex: +# * should returns floor data. +# 5 should returns 5. +# 10-55 should returns 10. +# 55-10 should retunrs floor data. +################################################################################ +sub cron_get_next_time_element { + # Default floor data is 0 + my ($curr_element, $floor_data) = @_; + $floor_data = 0 unless defined($floor_data); + my ($elem_down, $elem_up) = cron_get_interval ($curr_element); + return ($elem_down eq '*' || (defined($elem_up) && $elem_down > $elem_up)) + ? $floor_data + : $elem_down; +} ############################################################################### # Returns the interval of a cron element. If there is not a range, # returns an array with the first element in the first place of array @@ -2416,12 +2420,11 @@ sub check_module_cron { return 1 unless ($is_first); # Check if current timestamp is a valid cron date - my $next_execution = cron_next_execution_date( + my $next_execution = cron_next_execution( $module->{'cron'}, - $now - $interval, - $interval + 0 ); - return 1 if ($next_execution == $now); + return 1 if (time() + $next_execution == $now); return 0; } From 873525726ebe79523fced2dae56d4544b9e799b9 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Fri, 5 Apr 2019 12:32:43 +0200 Subject: [PATCH 7/8] Avoid infinite loops when the day of the week in cron is out of limits Former-commit-id: 6c0a87a29818219364fb9c08c44b48cb79dd74a8 --- pandora_agents/unix/pandora_agent | 7 ++++++- pandora_console/include/functions_cron.php | 5 +++++ pandora_server/lib/PandoraFMS/Tools.pm | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index 1a047590d8..e6e2132f6a 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -2102,6 +2102,12 @@ sub cron_next_execution { # Get day of the week and month from cron config my ($wday) = (split (/\s/, $cron))[4]; + # Check the wday values to avoid infinite loop + my ($wday_down, $wday_up) = cron_get_interval($wday); + if ($wday_down ne "*" && ($wday_down > 6 || (defined($wday_up) && $wday_up > 6))) { + log_message('setup', "Invalid cron configuration $cron. Day of the week is out of limits."); + $wday = "*"; + } # Get current time and day of the week my $cur_time = time(); @@ -2110,7 +2116,6 @@ sub cron_next_execution { my $nex_time = cron_next_execution_date ($cron, $cur_time, $interval); # Check the day - print localtime($nex_time)->strftime('%c') . " - OUTSIDE\n"; while (!cron_check_interval($wday, (localtime ($nex_time))[6])) { # If it does not acomplish the day of the week, go to the next day. $nex_time += 86400; diff --git a/pandora_console/include/functions_cron.php b/pandora_console/include/functions_cron.php index a5e1b2dcf7..6a79096175 100644 --- a/pandora_console/include/functions_cron.php +++ b/pandora_console/include/functions_cron.php @@ -83,6 +83,11 @@ function cron_next_execution($cron, $module_interval, $module_id) $cur_time = ($last_execution !== false) ? $last_execution : time(); $nex_time = cron_next_execution_date($cron, $cur_time, $module_interval); $nex_wday = (int) date('w', $nex_time); + // Check the wday values to avoid infinite loop. + $wday_int = cron_get_interval($wday); + if ($wday_int['down'] !== '*' && ($wday_int['down'] > 6 || ($wday_int['up'] !== false && $wday_int['up'] > 6))) { + $wday = '*'; + } // Check day of the way. while (!cron_check_interval($nex_wday, $wday)) { diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 90fe9dc6c1..a03ebb83d5 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -1360,6 +1360,11 @@ sub cron_next_execution { # Get day of the week and month from cron config my ($wday) = (split (/\s/, $cron))[4]; + # Check the wday values to avoid infinite loop + my ($wday_down, $wday_up) = cron_get_interval($wday); + if ($wday_down ne "*" && ($wday_down > 6 || (defined($wday_up) && $wday_up > 6))) { + $wday = "*"; + } # Get current time and day of the week my $cur_time = time(); From ae3c461da6eb4ccec3ac5df78b36849554a633fc Mon Sep 17 00:00:00 2001 From: fermin831 Date: Fri, 5 Apr 2019 13:09:12 +0200 Subject: [PATCH 8/8] Fixed problems with inverse cron intervals in Windows agent Former-commit-id: 0573c83dd346ef9076504688071d4678e554a6d5 --- pandora_agents/win32/misc/cron.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora_agents/win32/misc/cron.cc b/pandora_agents/win32/misc/cron.cc index 2d3e5e0440..409a43f726 100644 --- a/pandora_agents/win32/misc/cron.cc +++ b/pandora_agents/win32/misc/cron.cc @@ -214,7 +214,7 @@ int Cron::getResetValue (int position) { int default_value = 0; // Days start in 1 if (position == 2) default_value = 1; - return isWildCard(position) + return (isWildCard(position) || !isNormalInterval(position)) ? default_value : this->params[position][CRDOWN]; }