Patch to add timezone support for timeperiods

John Lemon John.Lemon at gbst.com
Tue Feb 5 00:30:02 CET 2008


Hello,

	Below is a simple patch to add support for timezones to timeperiods. I see nagios 3  is now at RC2, but will post this patch in the hope it may make it in for the final 3 build, but if not, then the next version.

With this patch you can now define a TimeZone for a timeperiod as show below, this will also handle any daylight time changes etc if the systems tzdata files are current.

No changes are made to how time is displayed within nagios or the cgi problems, the timezone check is only done within the timeperiod checks. This way all times displayed are still using the currently defined Timezone setting for the CGI's or as defined in/for the Nagios configuration.

# 'workhours' in NZ, timeperiod definition
define timeperiod{
        timeperiod_name NZ_workhours
        alias           NZ Normal Work Hours
        monday          09:00-17:00
        tuesday         09:00-17:00
        wednesday       09:00-17:00
        thursday        09:00-17:00
        friday          09:00-17:00
        timezone		Pacific/Auckland
        }

# 'ASX trading hours' in Australia, timeperiod definition
define timeperiod{
        timeperiod_name ASX_tradinghours
        alias           ASX Trading Hours
        monday          07:00-19:00
        tuesday         07:00-19:00
        wednesday       07:00-19:00
        thursday        07:00-19:00
        friday          07:00-19:00
        timezone		Australia/NSW
        }

----------------------------------------------------------------------------

diff -ru nagios-cvs-orig/base/utils.c nagios-cvs/base/utils.c
--- nagios-cvs-orig/base/utils.c        2008-01-25 00:21:52.000000000 +1000
+++ nagios-cvs/base/utils.c     2008-02-04 12:21:16.000000000 +1000
@@ -790,6 +790,7 @@
        int test_time_mday=0;
        int test_time_wday=0;
        int year=0;
+       char *current_timezone=NULL;

        log_debug_info(DEBUGL_FUNCTIONS,0,"check_time_against_period()\n");

@@ -809,6 +810,15 @@
                }
        tperiod->exclusions=first_timeperiodexclusion;

+       /* Run timeperiod checks in the requied timezone */
+       log_debug_info(DEBUGL_FUNCTIONS,0,"New TZ is:%s Old TZ was:%s\n", tperiod->timezone?tperiod->timezone:"None", current_timezone?current_timezone:"None");
+       if (tperiod->timezone!=NULL) {
+               if(getenv("TZ")!=NULL)
+                       current_timezone=strdup(getenv("TZ"));
+               set_environment_var("TZ",tperiod->timezone,1);
+               tzset();
+       }
+
        /* save values for later */
        t=localtime((time_t *)&test_time);
        test_time_year=t->tm_year;
@@ -992,6 +1002,13 @@

                        /* found a day match, so see if time ranges are good */
                        if(found_match==TRUE){
+                               /* restore default timezone */
+                               if (tperiod->timezone!=NULL) {
+                                       set_environment_var("TZ",current_timezone,current_timezone?1:0);
+                                       tzset();
+                                       my_free(current_timezone);
+                                       }
+                               log_debug_info(DEBUGL_FUNCTIONS,0,"TZ reset to:%s\n", getenv("TZ")?getenv("TZ"):"None");

                                for(temp_timerange=temp_daterange->times;temp_timerange!=NULL;temp_timerange=temp_timerange->next){

@@ -1025,6 +1042,14 @@

        /**** check normal, weekly rotating schedule last ****/

+       /* restore default timezone */
+       if (tperiod->timezone!=NULL) {
+               set_environment_var("TZ",current_timezone,current_timezone?1:0);
+               tzset();
+               my_free(current_timezone);
+               }
+       log_debug_info(DEBUGL_FUNCTIONS,0,"TZ reset to:%s\n", getenv("TZ")?getenv("TZ"):"None");
+
        /* check weekday time ranges */
        for(temp_timerange=tperiod->days[test_time_wday];temp_timerange!=NULL;temp_timerange=temp_timerange->next){

@@ -1068,6 +1093,7 @@
        unsigned long advance_interval=0L;
        int year=0; /* new */
        int month=0; /* new */
+       char *current_timezone=NULL;

        int pref_time_year=0;
        int pref_time_mon=0;
@@ -1102,6 +1128,15 @@
                return;
                }

+       /* Run timeperiod checks in the requied timezone */
+       log_debug_info(DEBUGL_FUNCTIONS,0,"New TZ is:%s Old TZ was:%s\n", tperiod->timezone?tperiod->timezone:"None", current_timezone?current_timezone:"None");
+       if (tperiod->timezone!=NULL) {
+               if(getenv("TZ")!=NULL)
+                       current_timezone=strdup(getenv("TZ"));
+               set_environment_var("TZ",tperiod->timezone,1);
+               tzset();
+       }
+
        /* calculate the start of the day (midnight, 00:00 hours) of preferred time */
        t=localtime((time_t *)&preferred_time);
        t->tm_sec=0;
@@ -1444,6 +1479,14 @@
        else
                *valid_time=earliest_time;

+       /* restore default timezone */
+       if (tperiod->timezone!=NULL) {
+               set_environment_var("TZ",current_timezone,current_timezone?1:0);
+               tzset();
+               my_free(current_timezone);
+               }
+       log_debug_info(DEBUGL_FUNCTIONS,0,"TZ reset to:%s\n", getenv("TZ")?getenv("TZ"):"None");
+
        return;
         }

diff -ru nagios-cvs-orig/common/objects.c nagios-cvs/common/objects.c
--- nagios-cvs-orig/common/objects.c    2008-01-26 01:23:12.000000000 +1000
+++ nagios-cvs/common/objects.c 2008-02-04 12:26:35.000000000 +1000
@@ -629,6 +629,7 @@
        /* initialize values */
        new_timeperiod->name=NULL;
        new_timeperiod->alias=NULL;
+       new_timeperiod->timezone=NULL;
        for(x=0;x<DATERANGE_TYPES;x++)
                new_timeperiod->exceptions[x]=NULL;
        for(x=0;x<7;x++)
@@ -3875,6 +3876,7 @@
                next_timeperiod=this_timeperiod->next;
                my_free(this_timeperiod->name);
                my_free(this_timeperiod->alias);
+               my_free(this_timeperiod->timezone);
                my_free(this_timeperiod);
                this_timeperiod=next_timeperiod;
                }
diff -ru nagios-cvs-orig/include/objects.h nagios-cvs/include/objects.h
--- nagios-cvs-orig/include/objects.h   2007-11-11 08:54:38.000000000 +1000
+++ nagios-cvs/include/objects.h        2008-02-04 12:28:03.000000000 +1000
@@ -118,6 +118,7 @@
 typedef struct timeperiod_struct{
        char    *name;
        char    *alias;
+       char    *timezone;
        timerange *days[7];
        daterange *exceptions[DATERANGE_TYPES];
        timeperiodexclusion *exclusions;
diff -ru nagios-cvs-orig/xdata/xodtemplate.c nagios-cvs/xdata/xodtemplate.c
--- nagios-cvs-orig/xdata/xodtemplate.c 2008-01-26 01:23:13.000000000 +1000
+++ nagios-cvs/xdata/xodtemplate.c      2008-02-04 13:35:12.000000000 +1000
@@ -953,6 +953,7 @@

                new_timeperiod->timeperiod_name=NULL;
                new_timeperiod->alias=NULL;
+               new_timeperiod->timezone=NULL;
                for(x=0;x<7;x++)
                        new_timeperiod->timeranges[x]=NULL;
                for(x=0;x<DATERANGE_TYPES;x++)
@@ -2129,6 +2130,10 @@
                        if((temp_timeperiod->alias=(char *)strdup(value))==NULL)
                                result=ERROR;
                        }
+               else if(!strcmp(variable,"timezone")){
+                       if((temp_timeperiod->timezone=(char *)strdup(value))==NULL)
+                               result=ERROR;
+                       }
                /*
                else if(!strcmp(variable,"monday") || !strcmp(variable,"tuesday") || !strcmp(variable,"wednesday") || !strcmp(variable,"thursday") || !strcmp(variable,"friday") || !strcmp(variable,"saturday") || !strcmp(variable,"sunday")){
                        if(!strcmp(variable,"monday"))
@@ -6594,6 +6599,8 @@
                        this_timeperiod->timeperiod_name=(char *)strdup(template_timeperiod->timeperiod_name);
                if(this_timeperiod->alias==NULL && template_timeperiod->alias!=NULL)
                        this_timeperiod->alias=(char *)strdup(template_timeperiod->alias);
+               if(this_timeperiod->timezone==NULL && template_timeperiod->timezone!=NULL)
+                       this_timeperiod->timezone=(char *)strdup(template_timeperiod->timezone);
                if(this_timeperiod->exclusions==NULL && template_timeperiod->exclusions!=NULL)
                        this_timeperiod->exclusions=(char *)strdup(template_timeperiod->exclusions);
                for(x=0;x<7;x++){
@@ -9159,6 +9166,11 @@
                return ERROR;
                }

+       if(this_timeperiod->timezone!=NULL){
+               if((new_timeperiod->timezone=(char *)strdup(this_timeperiod->timezone))==NULL)
+                       return ERROR;
+               }
+
        /* add all exceptions to timeperiod */
        for(x=0;x<DATERANGE_TYPES;x++){
                for(temp_daterange=this_timeperiod->exceptions[x];temp_daterange!=NULL;temp_daterange=temp_daterange->next){
@@ -10901,6 +10913,8 @@
                        fprintf(fp,"\ttimeperiod_name\t%s\n",temp_timeperiod->timeperiod_name);
                if(temp_timeperiod->alias)
                        fprintf(fp,"\talias\t%s\n",temp_timeperiod->alias);
+               if(temp_timeperiod->timezone)
+                       fprintf(fp,"\ttimezone\t%s\n",temp_timeperiod->timezone);
                for(x=0;x<DATERANGE_TYPES;x++){
                        for(temp_daterange=temp_timeperiod->exceptions[x];temp_daterange!=NULL;temp_daterange=temp_daterange->next){

@@ -11566,6 +11580,7 @@
                my_free(this_timeperiod->name);
                my_free(this_timeperiod->timeperiod_name);
                my_free(this_timeperiod->alias);
+               my_free(this_timeperiod->timezone);
                for(x=0;x<7;x++)
                        my_free(this_timeperiod->timeranges[x]);
                for(x=0;x<DATERANGE_TYPES;x++){
diff -ru nagios-cvs-orig/xdata/xodtemplate.h nagios-cvs/xdata/xodtemplate.h
--- nagios-cvs-orig/xdata/xodtemplate.h 2007-11-11 09:34:27.000000000 +1000
+++ nagios-cvs/xdata/xodtemplate.h      2008-02-04 13:35:51.000000000 +1000
@@ -93,6 +93,7 @@

        char       *timeperiod_name;
        char       *alias;
+       char       *timezone;
        char       *timeranges[7];
        xodtemplate_daterange *exceptions[DATERANGE_TYPES];
        char       *exclusions;


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/




More information about the Developers mailing list