[Nagios-devel] [PATCH 2/6] utils: Refactor get_next_valid_time

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

[Nagios-devel] [PATCH 2/6] utils: Refactor get_next_valid_time

Post by Guest »

There were several problems with how it worked, when faced with
exceptions and exclusions together with regular logic - Jean Gabes had
contributed several examples to the test_timeperiod suite that were
marked as TODO, that now passes just fine.

This code wraps the old logic in a loop. Once an exclusion hits, we
start over with checking for exceptions or weekday rules. If any rule
moves the match to tomorrow, we start over checking for new exclusions,
exceptions and weekday rules. Before, we didn't start over from scratch,
only on the "level" we had already reached.

Given that your timeperiod is crazy enough, or you use an empty
timeperiod to mean 24x7, nagios might spend slightly more time than
before figuring out your next valid time. The first case didn't behave
correctly before, so that's not a problem. For the second case, nagios
will only run it once, realize the check can't be scheduled, and leave
it alone, so that shouldn't affect things in practice.

The net line reduction comes from introducing a new helper, as
get_next_valid_time and check_time_against_period were using exactly the
same code to find rules to match against.

Signed-off-by: Robin Sonefors
---
base/utils.c | 586 +++++++++++++++++------------------------------------------
1 file changed, 169 insertions(+), 417 deletions(-)

diff --git a/base/utils.c b/base/utils.c
index 73ac13b..939235c 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -711,46 +711,25 @@ static int get_dst_shift(time_t *start, time_t *end) {

/*#define TEST_TIMEPERIODS_A 1*/

-/* see if the specified time falls into a valid time range in the given time period */
-int check_time_against_period(time_t test_time, timeperiod *tperiod) {
- timeperiodexclusion *temp_timeperiodexclusion = NULL;
- timeperiodexclusion *first_timeperiodexclusion = NULL;
+static timerange* _get_matching_timerange(time_t test_time, timeperiod *tperiod) {
daterange *temp_daterange = NULL;
- timerange *temp_timerange = NULL;
- time_t midnight = (time_t)0L;
time_t start_time = (time_t)0L;
time_t end_time = (time_t)0L;
- int found_match = FALSE;
+ unsigned long days = 0L;
+ int year = 0;
+ int shift = 0;
+ time_t midnight = (time_t)0L;
struct tm *t, tm_s;
int daterange_type = 0;
- unsigned long days = 0L;
- time_t day_range_start = (time_t)0L;
- time_t day_range_end = (time_t)0L;
int test_time_year = 0;
int test_time_mon = 0;
int test_time_wday = 0;
- int year = 0;
- int shift;

- log_debug_info(DEBUGL_FUNCTIONS, 0, "check_time_against_period()\n");
+ log_debug_info(DEBUGL_FUNCTIONS, 0, "_get_matching_timerange()\n");

- /* if no period was specified, assume the time is good */
if(tperiod == NULL)
- return OK;
-
- /* test exclusions first - if exclusions match current time, bail out with an error */
- /* clear exclusions list before recursing (and restore afterwards) to prevent endless loops... */
- first_timeperiodexclusion = tperiod->exclusions;
- tperiod->exclusions = NULL;
- for(temp_timeperiodexclusion = first_timeperiodexclusion; temp_timeperiodexclusion != NULL; temp_timeperiodexclusion = temp_timeperiodexclusion->next) {
- if(check_time_against_period(test_time, temp_timeperiodexclusion->timeperiod_ptr) == OK) {
- tperiod->exclusions = first_timeperiodexclusion;
- return ERROR;
- }
- }
- tperiod->exclusions = first_timeperiodexclusion;
+ return NULL;

- /* save values for later */
t = localtime_r((time_t *)&test_time, &tm_s);
test_time_year = t->tm_year;
test_time_mon = t->tm_mon;
@@ -930,59 +909,65 @@ int check_time_against_period(time_t test_time, timeperiod *tperiod) {
printf("NEW START: %lu = %s", (unsigned long)start_time, ctime(&start_time));
printf("NEW END: %lu = %s", (unsigned long)end_time, ctime(&end_time));
printf("%d DAYS PASSED\n", days);
- printf("DLST SHIFT: %d", shift);
+ printf("DLST SHIFT: %i\n", shift);
#endif

- /* time falls into the range of days */
- if(midnight >= start_time && midnight <= end_time)
- found_match = TRUE;
+ /* time falls inside the range of days
+ * end time < start_time when range covers end-of-$unit
+

...[email truncated]...


This post was automatically imported from historical nagios-devel mailing list archives
Original poster: robin.sonefors@op5.com
Locked