Icinga-core 1.4.0
next gen monitoring
xdata/xdddefault.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * XDDDEFAULT.C - Default scheduled downtime data routines for Icinga
00004  *
00005  * Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
00006  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00007  *
00008  * License:
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License version 2 as
00012  * published by the Free Software Foundation.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  *
00023  *****************************************************************************/
00024 
00025 
00026 /*********** COMMON HEADER FILES ***********/
00027 
00028 #include "../include/config.h"
00029 #include "../include/common.h"
00030 #include "../include/locations.h"
00031 #include "../include/downtime.h"
00032 #include "../include/macros.h"
00033 
00034 #ifdef NSCORE
00035 #include "../include/objects.h"
00036 #include "../include/icinga.h"
00037 #endif
00038 
00039 #ifdef NSCGI
00040 #include "../include/cgiutils.h"
00041 #endif
00042 
00043 
00044 /**** IMPLEMENTATION SPECIFIC HEADER FILES ****/
00045 #include "xdddefault.h"
00046 
00047 
00048 
00049 #ifdef NSCORE
00050 extern unsigned long next_downtime_id;
00051 extern scheduled_downtime *scheduled_downtime_list;
00052 #endif
00053 
00054 
00055 
00056 
00057 #ifdef NSCORE
00058 
00059 
00060 /******************************************************************/
00061 /*********** DOWNTIME INITIALIZATION/CLEANUP FUNCTIONS ************/
00062 /******************************************************************/
00063 
00064 
00065 /* initialize downtime data */
00066 int xdddefault_initialize_downtime_data(char *main_config_file){
00067         scheduled_downtime *temp_downtime=NULL;
00068 
00069         /* clean up the old downtime data */
00070         xdddefault_validate_downtime_data();
00071 
00072         /* find the new starting index for downtime id if its missing*/
00073         if(next_downtime_id==0L){
00074                 for(temp_downtime=scheduled_downtime_list;temp_downtime!=NULL;temp_downtime=temp_downtime->next){
00075                         if(temp_downtime->downtime_id>=next_downtime_id)
00076                                 next_downtime_id=temp_downtime->downtime_id+1;
00077                         }
00078                 }
00079 
00080         /* initialize next downtime id if necessary */
00081         if(next_downtime_id==0L)
00082                 next_downtime_id=1;
00083 
00084         return OK;
00085         }
00086 
00087 
00088 
00089 /* removes invalid and old downtime entries from the downtime file */
00090 int xdddefault_validate_downtime_data(void){
00091         scheduled_downtime *temp_downtime;
00092         scheduled_downtime *next_downtime;
00093         int update_file=FALSE;
00094         int save=TRUE;
00095 
00096         /* remove stale downtimes */
00097         for(temp_downtime=scheduled_downtime_list;temp_downtime!=NULL;temp_downtime=next_downtime){
00098 
00099                 next_downtime=temp_downtime->next;
00100                 save=TRUE;
00101 
00102                 /* delete downtimes with invalid host names */
00103                 if(find_host(temp_downtime->host_name)==NULL)
00104                         save=FALSE;
00105 
00106                 /* delete downtimes with invalid service descriptions */
00107                 if(temp_downtime->type==SERVICE_DOWNTIME && find_service(temp_downtime->host_name,temp_downtime->service_description)==NULL)
00108                         save=FALSE;
00109 
00110                 /* delete downtimes that have expired */
00111                 if(temp_downtime->end_time<time(NULL))
00112                         save=FALSE;
00113 
00114                 /* delete the downtime */
00115                 if(save==FALSE){
00116                         update_file=TRUE;
00117                         delete_downtime(temp_downtime->type,temp_downtime->downtime_id);
00118                         }
00119                 }
00120 
00121         /* remove triggered downtimes without valid parents */
00122         for(temp_downtime=scheduled_downtime_list;temp_downtime!=NULL;temp_downtime=next_downtime){
00123 
00124                 next_downtime=temp_downtime->next;
00125                 save=TRUE;
00126 
00127                 if(temp_downtime->triggered_by==0)
00128                         continue;
00129 
00130                 if(find_host_downtime(temp_downtime->triggered_by)==NULL && find_service_downtime(temp_downtime->triggered_by)==NULL)
00131                         save=FALSE;
00132 
00133                 /* delete the downtime */
00134                 if(save==FALSE){
00135                         update_file=TRUE;
00136                         delete_downtime(temp_downtime->type,temp_downtime->downtime_id);
00137                         }
00138                 }
00139 
00140         /* update downtime file */
00141         if(update_file==TRUE)
00142                 xdddefault_save_downtime_data();
00143 
00144         return OK;
00145         }
00146 
00147 
00148 
00149 /* removes invalid and old downtime entries from the downtime file */
00150 int xdddefault_cleanup_downtime_data(char *main_config_file){
00151 
00152         /* we don't need to do any cleanup... */
00153         return OK;
00154         }
00155 
00156 
00157 
00158 /******************************************************************/
00159 /************************ SAVE FUNCTIONS **************************/
00160 /******************************************************************/
00161 
00162 /* adds a new scheduled host downtime entry */
00163 int xdddefault_add_new_host_downtime(char *host_name, time_t entry_time, char *author, char *comment, time_t start_time, time_t end_time, int fixed, unsigned long triggered_by, unsigned long duration, unsigned long *downtime_id,int is_in_effect){
00164 
00165         /* find the next valid downtime id */
00166         while(find_host_downtime(next_downtime_id)!=NULL)
00167                 next_downtime_id++;
00168 
00169         /* add downtime to list in memory */
00170         add_host_downtime(host_name,entry_time,author,comment,start_time,end_time,fixed,triggered_by,duration,next_downtime_id,is_in_effect);
00171 
00172         /* update downtime file */
00173         xdddefault_save_downtime_data();
00174 
00175         /* return the id for the downtime we are about to add (this happens in the main code) */
00176         if(downtime_id!=NULL)
00177                 *downtime_id=next_downtime_id;
00178 
00179         /* increment the downtime id */
00180         next_downtime_id++;
00181 
00182         return OK;
00183         }
00184 
00185 
00186 
00187 /* adds a new scheduled service downtime entry */
00188 int xdddefault_add_new_service_downtime(char *host_name, char *service_description, time_t entry_time, char *author, char *comment, time_t start_time, time_t end_time, int fixed, unsigned long triggered_by, unsigned long duration, unsigned long *downtime_id,int is_in_effect){
00189 
00190         /* find the next valid downtime id */
00191         while(find_service_downtime(next_downtime_id)!=NULL)
00192                 next_downtime_id++;
00193 
00194         /* add downtime to list in memory */
00195         add_service_downtime(host_name,service_description,entry_time,author,comment,start_time,end_time,fixed,triggered_by,duration,next_downtime_id,is_in_effect);
00196 
00197         /* update downtime file */
00198         xdddefault_save_downtime_data();
00199 
00200         /* return the id for the downtime we are about to add (this happens in the main code) */
00201         if(downtime_id!=NULL)
00202                 *downtime_id=next_downtime_id;
00203 
00204         /* increment the downtime id */
00205         next_downtime_id++;
00206 
00207         return OK;
00208         }
00209 
00210 
00211 /******************************************************************/
00212 /********************** DELETION FUNCTIONS ************************/
00213 /******************************************************************/
00214 
00215 /* deletes a scheduled host downtime entry */
00216 int xdddefault_delete_host_downtime(unsigned long downtime_id){
00217         int result;
00218 
00219         result=xdddefault_delete_downtime(HOST_DOWNTIME,downtime_id);
00220 
00221         return result;
00222         }
00223 
00224 
00225 /* deletes a scheduled service downtime entry */
00226 int xdddefault_delete_service_downtime(unsigned long downtime_id){
00227         int result;
00228 
00229         result=xdddefault_delete_downtime(SERVICE_DOWNTIME,downtime_id);
00230 
00231         return result;
00232         }
00233 
00234 
00235 /* deletes a scheduled host or service downtime entry */
00236 int xdddefault_delete_downtime(int type, unsigned long downtime_id){
00237 
00238         /* rewrite the downtime file (downtime was already removed from memory) */
00239         xdddefault_save_downtime_data();
00240 
00241         return OK;
00242         }
00243 
00244 
00245 
00246 /******************************************************************/
00247 /****************** DOWNTIME OUTPUT FUNCTIONS *********************/
00248 /******************************************************************/
00249 
00250 /* writes downtime data to file */
00251 int xdddefault_save_downtime_data(void){
00252 
00253         /* don't update the status file now (too inefficent), let aggregated status updates do it */
00254         return OK;
00255         }
00256 
00257 #endif
00258 
00259 
 All Data Structures Files Functions Variables Typedefs Defines