1 /*****************************************************************************
2  *
3  * NAGIOSTATS.C - Displays Nagios Statistics
4  *
5  * Program: Nagiostats
6  * Version: 3.5.1
7  * License: GPL
8  * Copyright (c) 2003-2008 Ethan Galstad (egalstad@nagios.org)
9  *
10  * Last Modified: 12-20-2008
11  *
12  * License:
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  *****************************************************************************/
28 
29 #include "../include/config.h"
30 #include "../include/common.h"
31 #include "../include/nagios.h"
32 #include "../include/locations.h"
33 
34 #define STATUS_NO_DATA             0
35 #define STATUS_INFO_DATA           1
36 #define STATUS_PROGRAM_DATA        2
37 #define STATUS_HOST_DATA           3
38 #define STATUS_SERVICE_DATA        4
39 
40 
41 char *main_config_file = NULL;
42 char *status_file = NULL;
43 char *nagiostats_file = NULL;
44 char *mrtg_variables = NULL;
45 char *mrtg_delimiter = "\n";
46 
47 int mrtg_mode = FALSE;
48 
49 time_t status_creation_date = 0L;
50 char *status_version = NULL;
51 time_t program_start = 0L;
52 int status_service_entries = 0;
53 int status_host_entries = 0;
54 unsigned long nagios_pid = 0L;
55 
56 double min_service_state_change = 0.0;
57 int have_min_service_state_change = FALSE;
58 double max_service_state_change = 0.0;
59 int have_max_service_state_change = FALSE;
60 double average_service_state_change = 0.0;
61 double min_active_service_state_change = 0.0;
62 int have_min_active_service_state_change = FALSE;
63 double max_active_service_state_change = 0.0;
64 int have_max_active_service_state_change = FALSE;
65 double average_active_service_state_change = 0.0;
66 double min_active_service_latency = 0.0;
67 int have_min_active_service_latency = FALSE;
68 double max_active_service_latency = 0.0;
69 int have_max_active_service_latency = FALSE;
70 double average_active_service_latency = 0.0;
71 double min_active_service_execution_time = 0.0;
72 int have_min_active_service_execution_time = FALSE;
73 double max_active_service_execution_time = 0.0;
74 int have_max_active_service_execution_time = FALSE;
75 double average_active_service_execution_time = 0.0;
76 double min_passive_service_state_change = 0.0;
77 int have_min_passive_service_state_change = FALSE;
78 double max_passive_service_state_change = 0.0;
79 int have_max_passive_service_state_change = FALSE;
80 double average_passive_service_state_change = 0.0;
81 double min_passive_service_latency = 0.0;
82 int have_min_passive_service_latency = FALSE;
83 double max_passive_service_latency = 0.0;
84 int have_max_passive_service_latency = FALSE;
85 double average_passive_service_latency = 0.0;
86 
87 int have_min_host_state_change = FALSE;
88 double min_host_state_change = 0.0;
89 int have_max_host_state_change = FALSE;
90 double max_host_state_change = 0.0;
91 double average_host_state_change = 0.0;
92 int have_min_active_host_state_change = FALSE;
93 double min_active_host_state_change = 0.0;
94 int have_max_active_host_state_change = FALSE;
95 double max_active_host_state_change = 0.0;
96 double average_active_host_state_change = 0.0;
97 int have_min_active_host_latency = FALSE;
98 double min_active_host_latency = 0.0;
99 int have_max_active_host_latency = FALSE;
100 double max_active_host_latency = 0.0;
101 double average_active_host_latency = 0.0;
102 int have_min_active_host_execution_time = FALSE;
103 double min_active_host_execution_time = 0.0;
104 int have_max_active_host_execution_time = FALSE;
105 double max_active_host_execution_time = 0.0;
106 double average_active_host_execution_time = 0.0;
107 int have_min_passive_host_latency = FALSE;
108 double min_passive_host_latency = 0.0;
109 int have_max_passive_host_latency = FALSE;
110 double max_passive_host_latency = 0.0;
111 double average_passive_host_latency = 0.0;
112 double min_passive_host_state_change = 0.0;
113 int have_min_passive_host_state_change = FALSE;
114 double max_passive_host_state_change = 0.0;
115 int have_max_passive_host_state_change = FALSE;
116 double average_passive_host_state_change = 0.0;
117 
118 int passive_service_checks = 0;
119 int active_service_checks = 0;
120 int services_ok = 0;
121 int services_warning = 0;
122 int services_unknown = 0;
123 int services_critical = 0;
124 int services_flapping = 0;
125 int services_in_downtime = 0;
126 int services_checked = 0;
127 int services_scheduled = 0;
128 int passive_host_checks = 0;
129 int active_host_checks = 0;
130 int hosts_up = 0;
131 int hosts_down = 0;
132 int hosts_unreachable = 0;
133 int hosts_flapping = 0;
134 int hosts_in_downtime = 0;
135 int hosts_checked = 0;
136 int hosts_scheduled = 0;
137 
138 int passive_services_checked_last_1min = 0;
139 int passive_services_checked_last_5min = 0;
140 int passive_services_checked_last_15min = 0;
141 int passive_services_checked_last_1hour = 0;
142 int active_services_checked_last_1min = 0;
143 int active_services_checked_last_5min = 0;
144 int active_services_checked_last_15min = 0;
145 int active_services_checked_last_1hour = 0;
146 int passive_hosts_checked_last_1min = 0;
147 int passive_hosts_checked_last_5min = 0;
148 int passive_hosts_checked_last_15min = 0;
149 int passive_hosts_checked_last_1hour = 0;
150 int active_hosts_checked_last_1min = 0;
151 int active_hosts_checked_last_5min = 0;
152 int active_hosts_checked_last_15min = 0;
153 int active_hosts_checked_last_1hour = 0;
154 
155 int active_host_checks_last_1min = 0;
156 int active_host_checks_last_5min = 0;
157 int active_host_checks_last_15min = 0;
158 int active_ondemand_host_checks_last_1min = 0;
159 int active_ondemand_host_checks_last_5min = 0;
160 int active_ondemand_host_checks_last_15min = 0;
161 int active_scheduled_host_checks_last_1min = 0;
162 int active_scheduled_host_checks_last_5min = 0;
163 int active_scheduled_host_checks_last_15min = 0;
164 int passive_host_checks_last_1min = 0;
165 int passive_host_checks_last_5min = 0;
166 int passive_host_checks_last_15min = 0;
167 int active_cached_host_checks_last_1min = 0;
168 int active_cached_host_checks_last_5min = 0;
169 int active_cached_host_checks_last_15min = 0;
170 int parallel_host_checks_last_1min = 0;
171 int parallel_host_checks_last_5min = 0;
172 int parallel_host_checks_last_15min = 0;
173 int serial_host_checks_last_1min = 0;
174 int serial_host_checks_last_5min = 0;
175 int serial_host_checks_last_15min = 0;
176 
177 int active_service_checks_last_1min = 0;
178 int active_service_checks_last_5min = 0;
179 int active_service_checks_last_15min = 0;
180 int active_ondemand_service_checks_last_1min = 0;
181 int active_ondemand_service_checks_last_5min = 0;
182 int active_ondemand_service_checks_last_15min = 0;
183 int active_scheduled_service_checks_last_1min = 0;
184 int active_scheduled_service_checks_last_5min = 0;
185 int active_scheduled_service_checks_last_15min = 0;
186 int passive_service_checks_last_1min = 0;
187 int passive_service_checks_last_5min = 0;
188 int passive_service_checks_last_15min = 0;
189 int active_cached_service_checks_last_1min = 0;
190 int active_cached_service_checks_last_5min = 0;
191 int active_cached_service_checks_last_15min = 0;
192 
193 int external_commands_last_1min = 0;
194 int external_commands_last_5min = 0;
195 int external_commands_last_15min = 0;
196 
197 int total_external_command_buffer_slots = 0;
198 int used_external_command_buffer_slots = 0;
199 int high_external_command_buffer_slots = 0;
200 
201 
202 
203 int display_mrtg_values(void);
204 int display_stats(void);
205 int read_config_file(void);
206 int read_status_file(void);
207 void strip(char *);
208 void get_time_breakdown(unsigned long, int *, int *, int *, int *);
209 int read_nagiostats_file(void);
210 
211 
main(int argc,char ** argv)212 int main(int argc, char **argv) {
213 	int result;
214 	int error = FALSE;
215 	int display_license = FALSE;
216 	int display_help = FALSE;
217 	int c;
218 
219 #ifdef HAVE_GETOPT_H
220 	int option_index = 0;
221 	static struct option long_options[] = {
222 			{"help", no_argument, 0, 'h'},
223 			{"version", no_argument, 0, 'V'},
224 			{"license", no_argument, 0, 'L'},
225 			{"config", required_argument, 0, 'c'},
226 			{"statsfile", required_argument, 0, 's'},
227 			{"mrtg", no_argument, 0, 'm'},
228 			{"data", required_argument, 0, 'd'},
229 			{"delimiter", required_argument, 0, 'D'},
230 			{0, 0, 0, 0}
231 		};
232 #endif
233 
234 	/* defaults */
235 	main_config_file = strdup(DEFAULT_CONFIG_FILE);
236 	status_file = strdup(DEFAULT_STATUS_FILE);
237 
238 	/* get all command line arguments */
239 	while(1) {
240 
241 #ifdef HAVE_GETOPT_H
242 		c = getopt_long(argc, argv, "+hVLc:ms:d:D:", long_options, &option_index);
243 #else
244 		c = getopt(argc, argv, "+hVLc:ms:d:D:");
245 #endif
246 
247 		if(c == -1 || c == EOF)
248 			break;
249 
250 		switch(c) {
251 
252 			case '?':
253 			case 'h':
254 				display_help = TRUE;
255 				break;
256 			case 'V':
257 				display_license = TRUE;
258 				break;
259 			case 'L':
260 				display_license = TRUE;
261 				break;
262 			case 'c':
263 				if(main_config_file)
264 					free(main_config_file);
265 				main_config_file = strdup(optarg);
266 				break;
267 			case 's':
268 				nagiostats_file = strdup(optarg);
269 				break;
270 			case 'm':
271 				mrtg_mode = TRUE;
272 				break;
273 			case 'd':
274 				mrtg_variables = strdup(optarg);
275 				break;
276 			case 'D':
277 				mrtg_delimiter = strdup(optarg);
278 				break;
279 
280 			default:
281 				break;
282 			}
283 
284 		}
285 
286 	if(mrtg_mode == FALSE) {
287 		printf("\nNagios Stats %s\n", PROGRAM_VERSION);
288 		printf("Copyright (c) 2003-2008 Ethan Galstad (www.nagios.org)\n");
289 		printf("Last Modified: %s\n", PROGRAM_MODIFICATION_DATE);
290 		printf("License: GPL\n\n");
291 		}
292 
293 	/* just display the license */
294 	if(display_license == TRUE) {
295 
296 		printf("This program is free software; you can redistribute it and/or modify\n");
297 		printf("it under the terms of the GNU General Public License version 2 as\n");
298 		printf("published by the Free Software Foundation.\n\n");
299 		printf("This program is distributed in the hope that it will be useful,\n");
300 		printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
301 		printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
302 		printf("GNU General Public License for more details.\n\n");
303 		printf("You should have received a copy of the GNU General Public License\n");
304 		printf("along with this program; if not, write to the Free Software\n");
305 		printf("Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
306 
307 		exit(OK);
308 		}
309 
310 	/* if there are no command line options (or if we encountered an error), print usage */
311 	if(error == TRUE || display_help == TRUE) {
312 
313 		printf("Usage: %s [options]\n", argv[0]);
314 		printf("\n");
315 		printf("Startup:\n");
316 		printf(" -V, --version      display program version information and exit.\n");
317 		printf(" -L, --license      display license information and exit.\n");
318 		printf(" -h, --help         display usage information and exit.\n");
319 		printf("\n");
320 		printf("Input file:\n");
321 		printf(" -c, --config=FILE  specifies location of main Nagios config file.\n");
322 		printf(" -s, --statsfile=FILE  specifies alternate location of file to read Nagios\n");
323 		printf("                       performance data from.\n");
324 		printf("\n");
325 		printf("Output:\n");
326 		printf(" -m, --mrtg         display output in MRTG compatible format.\n");
327 		printf(" -d, --data=VARS    comma-seperated list of variables to output in MRTG\n");
328 		printf("                    (or compatible) format.  See possible values below.\n");
329 		printf("                    Percentages are rounded, times are in milliseconds.\n");
330 		printf(" -D, --delimiter=C  character to use as delimiter in MRTG output mode.\n");
331 		printf("                    Defaults to a newline.\n");
332 		printf("\n");
333 		printf("MRTG DATA VARIABLES (-d option):\n");
334 		printf(" PROGRUNTIME          string with time Nagios process has been running.\n");
335 		printf(" PROGRUNTIMETT        time Nagios process has been running (time_t format).\n");
336 		printf(" STATUSFILEAGE        string with age of status data file.\n");
337 		printf(" STATUSFILEAGETT      string with age of status data file (time_t format).\n");
338 		printf(" NAGIOSVERSION        string with Nagios version.\n");
339 		printf(" NAGIOSPID            pid number of Nagios deamon.\n");
340 		printf(" NAGIOSVERPID         string with Nagios version and PID.\n");
341 		printf(" TOTCMDBUF            total number of external command buffer slots available.\n");
342 		printf(" USEDCMDBUF           number of external command buffer slots currently in use.\n");
343 		printf(" HIGHCMDBUF           highest number of external command buffer slots ever in use.\n");
344 		printf(" NUMSERVICES          total number of services.\n");
345 		printf(" NUMHOSTS             total number of hosts.\n");
346 		printf(" NUMSVCOK             number of services OK.\n");
347 		printf(" NUMSVCWARN           number of services WARNING.\n");
348 		printf(" NUMSVCUNKN           number of services UNKNOWN.\n");
349 		printf(" NUMSVCCRIT           number of services CRITICAL.\n");
350 		printf(" NUMSVCPROB           number of service problems (WARNING, UNKNOWN or CRITIAL).\n");
351 		printf(" NUMSVCCHECKED        number of services that have been checked since start.\n");
352 		printf(" NUMSVCSCHEDULED      number of services that are currently scheduled to be checked.\n");
353 		printf(" NUMSVCFLAPPING       number of services that are currently flapping.\n");
354 		printf(" NUMSVCDOWNTIME       number of services that are currently in downtime.\n");
355 		printf(" NUMHSTUP             number of hosts UP.\n");
356 		printf(" NUMHSTDOWN           number of hosts DOWN.\n");
357 		printf(" NUMHSTUNR            number of hosts UNREACHABLE.\n");
358 		printf(" NUMHSTPROB           number of host problems (DOWN or UNREACHABLE).\n");
359 		printf(" NUMHSTCHECKED        number of hosts that have been checked since start.\n");
360 		printf(" NUMHSTSCHEDULED      number of hosts that are currently scheduled to be checked.\n");
361 		printf(" NUMHSTFLAPPING       number of hosts that are currently flapping.\n");
362 		printf(" NUMHSTDOWNTIME       number of hosts that are currently in downtime.\n");
363 		printf(" NUMHSTACTCHKxM       number of hosts actively checked in last 1/5/15/60 minutes.\n");
364 		printf(" NUMHSTPSVCHKxM       number of hosts passively checked in last 1/5/15/60 minutes.\n");
365 		printf(" NUMSVCACTCHKxM       number of services actively checked in last 1/5/15/60 minutes.\n");
366 		printf(" NUMSVCPSVCHKxM       number of services passively checked in last 1/5/15/60 minutes.\n");
367 		printf(" xxxACTSVCLAT         MIN/MAX/AVG active service check latency (ms).\n");
368 		printf(" xxxACTSVCEXT         MIN/MAX/AVG active service check execution time (ms).\n");
369 		printf(" xxxACTSVCPSC         MIN/MAX/AVG active service check %% state change.\n");
370 		printf(" xxxPSVSVCLAT         MIN/MAX/AVG passive service check latency (ms).\n");
371 		printf(" xxxPSVSVCPSC         MIN/MAX/AVG passive service check %% state change.\n");
372 		printf(" xxxSVCPSC            MIN/MAX/AVG service check %% state change.\n");
373 		printf(" xxxACTHSTLAT         MIN/MAX/AVG active host check latency (ms).\n");
374 		printf(" xxxACTHSTEXT         MIN/MAX/AVG active host check execution time (ms).\n");
375 		printf(" xxxACTHSTPSC         MIN/MAX/AVG active host check %% state change.\n");
376 		printf(" xxxPSVHSTLAT         MIN/MAX/AVG passive host check latency (ms).\n");
377 		printf(" xxxPSVHSTPSC         MIN/MAX/AVG passive host check %% state change.\n");
378 		printf(" xxxHSTPSC            MIN/MAX/AVG host check %% state change.\n");
379 		printf(" NUMACTHSTCHECKSxM    number of total active host checks occuring in last 1/5/15 minutes.\n");
380 		printf(" NUMOACTHSTCHECKSxM   number of on-demand active host checks occuring in last 1/5/15 minutes.\n");
381 		printf(" NUMCACHEDHSTCHECKSxM number of cached host checks occuring in last 1/5/15 minutes.\n");
382 		printf(" NUMSACTHSTCHECKSxM   number of scheduled active host checks occuring in last 1/5/15 minutes.\n");
383 		printf(" NUMPARHSTCHECKSxM    number of parallel host checks occuring in last 1/5/15 minutes.\n");
384 		printf(" NUMSERHSTCHECKSxM    number of serial host checks occuring in last 1/5/15 minutes.\n");
385 		printf(" NUMPSVHSTCHECKSxM    number of passive host checks occuring in last 1/5/15 minutes.\n");
386 		printf(" NUMACTSVCCHECKSxM    number of total active service checks occuring in last 1/5/15 minutes.\n");
387 		printf(" NUMOACTSVCCHECKSxM   number of on-demand active service checks occuring in last 1/5/15 minutes.\n");
388 		printf(" NUMCACHEDSVCCHECKSxM number of cached service checks occuring in last 1/5/15 minutes.\n");
389 		printf(" NUMSACTSVCCHECKSxM   number of scheduled active service checks occuring in last 1/5/15 minutes.\n");
390 		printf(" NUMPSVSVCCHECKSxM    number of passive service checks occuring in last 1/5/15 minutes.\n");
391 		printf(" NUMEXTCMDSxM         number of external commands processed in last 1/5/15 minutes.\n");
392 
393 		printf("\n");
394 		printf(" Note: Replace x's in MRTG variable names with 'MIN', 'MAX', 'AVG', or the\n");
395 		printf("       the appropriate number (i.e. '1', '5', '15', or '60').\n");
396 		printf("\n");
397 
398 		exit(ERROR);
399 		}
400 
401 	/* read pre-processed stats file */
402 	if(nagiostats_file) {
403 		result = read_nagiostats_file();
404 		if(result == ERROR && mrtg_mode == FALSE) {
405 			printf("Error reading stats file '%s': %s\n", nagiostats_file, strerror(errno));
406 			return ERROR;
407 			}
408 		}
409 
410 	/* else read the normal status file */
411 	else {
412 		/* read main config file */
413 		result = read_config_file();
414 		if(result == ERROR && mrtg_mode == FALSE) {
415 			printf("Error processing config file '%s'\n", main_config_file);
416 			return ERROR;
417 			}
418 
419 		/* read status file */
420 		result = read_status_file();
421 		if(result == ERROR && mrtg_mode == FALSE) {
422 			printf("Error reading status file '%s': %s\n", status_file, strerror(errno));
423 			return ERROR;
424 			}
425 		}
426 
427 	/* display stats */
428 	if(mrtg_mode == FALSE)
429 		display_stats();
430 	else
431 		display_mrtg_values();
432 
433 	if(nagiostats_file);
434 	free(nagiostats_file);
435 
436 	/* Opsera patch - return based on error, because mrtg_mode was always returning OK */
437 	if(result == ERROR)
438 		return ERROR;
439 	else
440 		return OK;
441 	}
442 
443 
444 
display_mrtg_values(void)445 int display_mrtg_values(void) {
446 	char *temp_ptr;
447 	time_t current_time;
448 	unsigned long time_difference;
449 	int days;
450 	int hours;
451 	int minutes;
452 	int seconds;
453 
454 	time(&current_time);
455 
456 	if(mrtg_variables == NULL)
457 		return OK;
458 
459 	/* process all variables */
460 	for(temp_ptr = strtok(mrtg_variables, ","); temp_ptr != NULL; temp_ptr = strtok(NULL, ",")) {
461 
462 		if(!strcmp(temp_ptr, "PROGRUNTIME")) {
463 			time_difference = (current_time - program_start);
464 			get_time_breakdown(time_difference, &days, &hours, &minutes, &seconds);
465 			printf("%dd %dh %dm %ds%s", days, hours, minutes, seconds, mrtg_delimiter);
466 			}
467 		else if(!strcmp(temp_ptr, "PROGRUNTIMETT")) {
468 			time_difference = (current_time - program_start);
469 			printf("%lu%s", time_difference, mrtg_delimiter);
470 			}
471 		else if(!strcmp(temp_ptr, "STATUSFILEAGE")) {
472 			time_difference = (current_time - status_creation_date);
473 			get_time_breakdown(time_difference, &days, &hours, &minutes, &seconds);
474 			printf("%dd %dh %dm %ds%s", days, hours, minutes, seconds, mrtg_delimiter);
475 			}
476 		else if(!strcmp(temp_ptr, "STATUSFILEAGETT")) {
477 			time_difference = (current_time - status_creation_date);
478 			printf("%lu%s", time_difference, mrtg_delimiter);
479 			}
480 		else if(!strcmp(temp_ptr, "NAGIOSVERSION"))
481 			printf("%s%s", status_version, mrtg_delimiter);
482 		else if(!strcmp(temp_ptr, "NAGIOSPID"))
483 			printf("%lu%s", nagios_pid, mrtg_delimiter);
484 		else if(!strcmp(temp_ptr, "NAGIOSVERPID"))
485 			printf("Nagios %s (pid=%lu)%s", status_version, nagios_pid, mrtg_delimiter);
486 
487 
488 		else if(!strcmp(temp_ptr, "TOTCMDBUF"))
489 			printf("%d%s", total_external_command_buffer_slots, mrtg_delimiter);
490 		else if(!strcmp(temp_ptr, "USEDCMDBUF"))
491 			printf("%d%s", used_external_command_buffer_slots, mrtg_delimiter);
492 		else if(!strcmp(temp_ptr, "HIGHCMDBUF"))
493 			printf("%d%s", high_external_command_buffer_slots, mrtg_delimiter);
494 
495 		else if(!strcmp(temp_ptr, "NUMSERVICES"))
496 			printf("%d%s", status_service_entries, mrtg_delimiter);
497 		else if(!strcmp(temp_ptr, "NUMHOSTS"))
498 			printf("%d%s", status_host_entries, mrtg_delimiter);
499 
500 		/* active service check latency */
501 		else if(!strcmp(temp_ptr, "MINACTSVCLAT"))
502 			printf("%d%s", (int)(min_active_service_latency * 1000), mrtg_delimiter);
503 		else if(!strcmp(temp_ptr, "MAXACTSVCLAT"))
504 			printf("%d%s", (int)(max_active_service_latency * 1000), mrtg_delimiter);
505 		else if(!strcmp(temp_ptr, "AVGACTSVCLAT"))
506 			printf("%d%s", (int)(average_active_service_latency * 1000), mrtg_delimiter);
507 
508 		/* active service check execution time */
509 		else if(!strcmp(temp_ptr, "MINACTSVCEXT"))
510 			printf("%d%s", (int)(min_active_service_execution_time * 1000), mrtg_delimiter);
511 		else if(!strcmp(temp_ptr, "MAXACTSVCEXT"))
512 			printf("%d%s", (int)(max_active_service_execution_time * 1000), mrtg_delimiter);
513 		else if(!strcmp(temp_ptr, "AVGACTSVCEXT"))
514 			printf("%d%s", (int)(average_active_service_execution_time * 1000), mrtg_delimiter);
515 
516 		/* active service check percent state change */
517 		else if(!strcmp(temp_ptr, "MINACTSVCPSC"))
518 			printf("%d%s", (int)min_active_service_state_change, mrtg_delimiter);
519 		else if(!strcmp(temp_ptr, "MAXACTSVCPSC"))
520 			printf("%d%s", (int)max_active_service_state_change, mrtg_delimiter);
521 		else if(!strcmp(temp_ptr, "AVGACTSVCPSC"))
522 			printf("%d%s", (int)average_active_service_state_change, mrtg_delimiter);
523 
524 		/* passive service check latency */
525 		else if(!strcmp(temp_ptr, "MINPSVSVCLAT"))
526 			printf("%d%s", (int)(min_passive_service_latency * 1000), mrtg_delimiter);
527 		else if(!strcmp(temp_ptr, "MAXPSVSVCLAT"))
528 			printf("%d%s", (int)(max_passive_service_latency * 1000), mrtg_delimiter);
529 		else if(!strcmp(temp_ptr, "AVGPSVSVCLAT"))
530 			printf("%d%s", (int)(average_passive_service_latency * 1000), mrtg_delimiter);
531 
532 		/* passive service check percent state change */
533 		else if(!strcmp(temp_ptr, "MINPSVSVCPSC"))
534 			printf("%d%s", (int)min_passive_service_state_change, mrtg_delimiter);
535 		else if(!strcmp(temp_ptr, "MAXPSVSVCPSC"))
536 			printf("%d%s", (int)max_passive_service_state_change, mrtg_delimiter);
537 		else if(!strcmp(temp_ptr, "AVGPSVSVCPSC"))
538 			printf("%d%s", (int)average_passive_service_state_change, mrtg_delimiter);
539 
540 		/* service check percent state change */
541 		else if(!strcmp(temp_ptr, "MINSVCPSC"))
542 			printf("%d%s", (int)min_service_state_change, mrtg_delimiter);
543 		else if(!strcmp(temp_ptr, "MAXSVCPSC"))
544 			printf("%d%s", (int)max_service_state_change, mrtg_delimiter);
545 		else if(!strcmp(temp_ptr, "AVGSVCPSC"))
546 			printf("%d%s", (int)average_service_state_change, mrtg_delimiter);
547 
548 		/* active host check latency */
549 		else if(!strcmp(temp_ptr, "MINACTHSTLAT"))
550 			printf("%d%s", (int)(min_active_host_latency * 1000), mrtg_delimiter);
551 		else if(!strcmp(temp_ptr, "MAXACTHSTLAT"))
552 			printf("%d%s", (int)(max_active_host_latency * 1000), mrtg_delimiter);
553 		else if(!strcmp(temp_ptr, "AVGACTHSTLAT"))
554 			printf("%d%s", (int)(average_active_host_latency * 1000), mrtg_delimiter);
555 
556 		/* active host check execution time */
557 		else if(!strcmp(temp_ptr, "MINACTHSTEXT"))
558 			printf("%d%s", (int)(min_active_host_execution_time * 1000), mrtg_delimiter);
559 		else if(!strcmp(temp_ptr, "MAXACTHSTEXT"))
560 			printf("%d%s", (int)(max_active_host_execution_time * 1000), mrtg_delimiter);
561 		else if(!strcmp(temp_ptr, "AVGACTHSTEXT"))
562 			printf("%d%s", (int)(average_active_host_execution_time * 1000), mrtg_delimiter);
563 
564 		/* active host check percent state change */
565 		else if(!strcmp(temp_ptr, "MINACTHSTPSC"))
566 			printf("%d%s", (int)min_active_host_state_change, mrtg_delimiter);
567 		else if(!strcmp(temp_ptr, "MAXACTHSTPSC"))
568 			printf("%d%s", (int)max_active_host_state_change, mrtg_delimiter);
569 		else if(!strcmp(temp_ptr, "AVGACTHSTPSC"))
570 			printf("%d%s", (int)average_active_host_state_change, mrtg_delimiter);
571 
572 		/* passive host check latency */
573 		else if(!strcmp(temp_ptr, "MINPSVHSTLAT"))
574 			printf("%d%s", (int)(min_passive_host_latency * 1000), mrtg_delimiter);
575 		else if(!strcmp(temp_ptr, "MAXPSVHSTLAT"))
576 			printf("%d%s", (int)(max_passive_host_latency * 1000), mrtg_delimiter);
577 		else if(!strcmp(temp_ptr, "AVGPSVHSTLAT"))
578 			printf("%d%s", (int)(average_passive_host_latency * 1000), mrtg_delimiter);
579 
580 		/* passive host check percent state change */
581 		else if(!strcmp(temp_ptr, "MINPSVHSTPSC"))
582 			printf("%d%s", (int)min_passive_host_state_change, mrtg_delimiter);
583 		else if(!strcmp(temp_ptr, "MAXPSVHSTPSC"))
584 			printf("%d%s", (int)max_passive_host_state_change, mrtg_delimiter);
585 		else if(!strcmp(temp_ptr, "AVGPSVHSTPSC"))
586 			printf("%d%s", (int)average_passive_host_state_change, mrtg_delimiter);
587 
588 		/* host check percent state change */
589 		else if(!strcmp(temp_ptr, "MINHSTPSC"))
590 			printf("%d%s", (int)min_host_state_change, mrtg_delimiter);
591 		else if(!strcmp(temp_ptr, "MAXHSTPSC"))
592 			printf("%d%s", (int)max_host_state_change, mrtg_delimiter);
593 		else if(!strcmp(temp_ptr, "AVGHSTPSC"))
594 			printf("%d%s", (int)average_host_state_change, mrtg_delimiter);
595 
596 		/* active host checks over time */
597 		else if(!strcmp(temp_ptr, "NUMHSTACTCHK1M"))
598 			printf("%d%s", active_hosts_checked_last_1min, mrtg_delimiter);
599 		else if(!strcmp(temp_ptr, "NUMHSTACTCHK5M"))
600 			printf("%d%s", active_hosts_checked_last_5min, mrtg_delimiter);
601 		else if(!strcmp(temp_ptr, "NUMHSTACTCHK15M"))
602 			printf("%d%s", active_hosts_checked_last_15min, mrtg_delimiter);
603 		else if(!strcmp(temp_ptr, "NUMHSTACTCHK60M"))
604 			printf("%d%s", active_hosts_checked_last_1hour, mrtg_delimiter);
605 
606 		/* passive host checks over time */
607 		else if(!strcmp(temp_ptr, "NUMHSTPSVCHK1M"))
608 			printf("%d%s", passive_hosts_checked_last_1min, mrtg_delimiter);
609 		else if(!strcmp(temp_ptr, "NUMHSTPSVCHK5M"))
610 			printf("%d%s", passive_hosts_checked_last_5min, mrtg_delimiter);
611 		else if(!strcmp(temp_ptr, "NUMHSTPSVCHK15M"))
612 			printf("%d%s", passive_hosts_checked_last_15min, mrtg_delimiter);
613 		else if(!strcmp(temp_ptr, "NUMHSTPSVCHK60M"))
614 			printf("%d%s", passive_hosts_checked_last_1hour, mrtg_delimiter);
615 
616 		/* active service checks over time */
617 		else if(!strcmp(temp_ptr, "NUMSVCACTCHK1M"))
618 			printf("%d%s", active_services_checked_last_1min, mrtg_delimiter);
619 		else if(!strcmp(temp_ptr, "NUMSVCACTCHK5M"))
620 			printf("%d%s", active_services_checked_last_5min, mrtg_delimiter);
621 		else if(!strcmp(temp_ptr, "NUMSVCACTCHK15M"))
622 			printf("%d%s", active_services_checked_last_15min, mrtg_delimiter);
623 		else if(!strcmp(temp_ptr, "NUMSVCACTCHK60M"))
624 			printf("%d%s", active_services_checked_last_1hour, mrtg_delimiter);
625 
626 		/* passive service checks over time */
627 		else if(!strcmp(temp_ptr, "NUMSVCPSVCHK1M"))
628 			printf("%d%s", passive_services_checked_last_1min, mrtg_delimiter);
629 		else if(!strcmp(temp_ptr, "NUMSVCPSVCHK5M"))
630 			printf("%d%s", passive_services_checked_last_5min, mrtg_delimiter);
631 		else if(!strcmp(temp_ptr, "NUMSVCPSVCHK15M"))
632 			printf("%d%s", passive_services_checked_last_15min, mrtg_delimiter);
633 		else if(!strcmp(temp_ptr, "NUMSVCPSVCHK60M"))
634 			printf("%d%s", passive_services_checked_last_1hour, mrtg_delimiter);
635 
636 		/* host check statistics */
637 		else if(!strcmp(temp_ptr, "NUMACTHSTCHECKS1M"))
638 			printf("%d%s", active_host_checks_last_1min, mrtg_delimiter);
639 		else if(!strcmp(temp_ptr, "NUMACTHSTCHECKS5M"))
640 			printf("%d%s", active_host_checks_last_5min, mrtg_delimiter);
641 		else if(!strcmp(temp_ptr, "NUMACTHSTCHECKS15M"))
642 			printf("%d%s", active_host_checks_last_15min, mrtg_delimiter);
643 		else if(!strcmp(temp_ptr, "NUMOACTHSTCHECKS1M"))
644 			printf("%d%s", active_ondemand_host_checks_last_1min, mrtg_delimiter);
645 		else if(!strcmp(temp_ptr, "NUMOACTHSTCHECKS5M"))
646 			printf("%d%s", active_ondemand_host_checks_last_5min, mrtg_delimiter);
647 		else if(!strcmp(temp_ptr, "NUMOACTHSTCHECKS15M"))
648 			printf("%d%s", active_ondemand_host_checks_last_15min, mrtg_delimiter);
649 		else if(!strcmp(temp_ptr, "NUMSACTHSTCHECKS1M"))
650 			printf("%d%s", active_scheduled_host_checks_last_1min, mrtg_delimiter);
651 		else if(!strcmp(temp_ptr, "NUMSACTHSTCHECKS5M"))
652 			printf("%d%s", active_scheduled_host_checks_last_5min, mrtg_delimiter);
653 		else if(!strcmp(temp_ptr, "NUMSACTHSTCHECKS15M"))
654 			printf("%d%s", active_scheduled_host_checks_last_15min, mrtg_delimiter);
655 		else if(!strcmp(temp_ptr, "NUMPARHSTCHECKS1M"))
656 			printf("%d%s", parallel_host_checks_last_1min, mrtg_delimiter);
657 		else if(!strcmp(temp_ptr, "NUMPARHSTCHECKS5M"))
658 			printf("%d%s", parallel_host_checks_last_5min, mrtg_delimiter);
659 		else if(!strcmp(temp_ptr, "NUMPARHSTCHECKS15M"))
660 			printf("%d%s", parallel_host_checks_last_15min, mrtg_delimiter);
661 		else if(!strcmp(temp_ptr, "NUMSERHSTCHECKS1M"))
662 			printf("%d%s", serial_host_checks_last_1min, mrtg_delimiter);
663 		else if(!strcmp(temp_ptr, "NUMSERHSTCHECKS5M"))
664 			printf("%d%s", serial_host_checks_last_5min, mrtg_delimiter);
665 		else if(!strcmp(temp_ptr, "NUMSERHSTCHECKS15M"))
666 			printf("%d%s", serial_host_checks_last_15min, mrtg_delimiter);
667 		else if(!strcmp(temp_ptr, "NUMPSVHSTCHECKS1M"))
668 			printf("%d%s", passive_host_checks_last_1min, mrtg_delimiter);
669 		else if(!strcmp(temp_ptr, "NUMPSVHSTCHECKS5M"))
670 			printf("%d%s", passive_host_checks_last_5min, mrtg_delimiter);
671 		else if(!strcmp(temp_ptr, "NUMPSVHSTCHECKS15M"))
672 			printf("%d%s", passive_host_checks_last_15min, mrtg_delimiter);
673 		else if(!strcmp(temp_ptr, "NUMCACHEDHSTCHECKS1M"))
674 			printf("%d%s", active_cached_host_checks_last_1min, mrtg_delimiter);
675 		else if(!strcmp(temp_ptr, "NUMCACHEDHSTCHECKS5M"))
676 			printf("%d%s", active_cached_host_checks_last_5min, mrtg_delimiter);
677 		else if(!strcmp(temp_ptr, "NUMCACHEDHSTCHECKS15M"))
678 			printf("%d%s", active_cached_host_checks_last_15min, mrtg_delimiter);
679 
680 		/* service check statistics */
681 		else if(!strcmp(temp_ptr, "NUMACTSVCCHECKS1M"))
682 			printf("%d%s", active_service_checks_last_1min, mrtg_delimiter);
683 		else if(!strcmp(temp_ptr, "NUMACTSVCCHECKS5M"))
684 			printf("%d%s", active_service_checks_last_5min, mrtg_delimiter);
685 		else if(!strcmp(temp_ptr, "NUMACTSVCCHECKS15M"))
686 			printf("%d%s", active_service_checks_last_15min, mrtg_delimiter);
687 		else if(!strcmp(temp_ptr, "NUMOACTSVCCHECKS1M"))
688 			printf("%d%s", active_ondemand_service_checks_last_1min, mrtg_delimiter);
689 		else if(!strcmp(temp_ptr, "NUMOACTSVCCHECKS5M"))
690 			printf("%d%s", active_ondemand_service_checks_last_5min, mrtg_delimiter);
691 		else if(!strcmp(temp_ptr, "NUMOACTSVCCHECKS15M"))
692 			printf("%d%s", active_ondemand_service_checks_last_15min, mrtg_delimiter);
693 		else if(!strcmp(temp_ptr, "NUMSACTSVCCHECKS1M"))
694 			printf("%d%s", active_scheduled_service_checks_last_1min, mrtg_delimiter);
695 		else if(!strcmp(temp_ptr, "NUMSACTSVCCHECKS5M"))
696 			printf("%d%s", active_scheduled_service_checks_last_5min, mrtg_delimiter);
697 		else if(!strcmp(temp_ptr, "NUMSACTSVCCHECKS15M"))
698 			printf("%d%s", active_scheduled_service_checks_last_15min, mrtg_delimiter);
699 		else if(!strcmp(temp_ptr, "NUMPSVSVCCHECKS1M"))
700 			printf("%d%s", passive_service_checks_last_1min, mrtg_delimiter);
701 		else if(!strcmp(temp_ptr, "NUMPSVSVCCHECKS5M"))
702 			printf("%d%s", passive_service_checks_last_5min, mrtg_delimiter);
703 		else if(!strcmp(temp_ptr, "NUMPSVSVCCHECKS15M"))
704 			printf("%d%s", passive_service_checks_last_15min, mrtg_delimiter);
705 		else if(!strcmp(temp_ptr, "NUMCACHEDSVCCHECKS1M"))
706 			printf("%d%s", active_cached_service_checks_last_1min, mrtg_delimiter);
707 		else if(!strcmp(temp_ptr, "NUMCACHEDSVCCHECKS5M"))
708 			printf("%d%s", active_cached_service_checks_last_5min, mrtg_delimiter);
709 		else if(!strcmp(temp_ptr, "NUMCACHEDSVCCHECKS15M"))
710 			printf("%d%s", active_cached_service_checks_last_15min, mrtg_delimiter);
711 
712 		/* external command stats */
713 		else if(!strcmp(temp_ptr, "NUMEXTCMDS1M"))
714 			printf("%d%s", external_commands_last_1min, mrtg_delimiter);
715 		else if(!strcmp(temp_ptr, "NUMEXTCMDS5M"))
716 			printf("%d%s", external_commands_last_5min, mrtg_delimiter);
717 		else if(!strcmp(temp_ptr, "NUMEXTCMDS15M"))
718 			printf("%d%s", external_commands_last_15min, mrtg_delimiter);
719 
720 		/* service states */
721 		else if(!strcmp(temp_ptr, "NUMSVCOK"))
722 			printf("%d%s", services_ok, mrtg_delimiter);
723 		else if(!strcmp(temp_ptr, "NUMSVCWARN"))
724 			printf("%d%s", services_warning, mrtg_delimiter);
725 		else if(!strcmp(temp_ptr, "NUMSVCUNKN"))
726 			printf("%d%s", services_unknown, mrtg_delimiter);
727 		else if(!strcmp(temp_ptr, "NUMSVCCRIT"))
728 			printf("%d%s", services_critical, mrtg_delimiter);
729 		else if(!strcmp(temp_ptr, "NUMSVCPROB"))
730 			printf("%d%s", services_warning + services_unknown + services_critical, mrtg_delimiter);
731 
732 		/* misc service info */
733 		else if(!strcmp(temp_ptr, "NUMSVCCHECKED"))
734 			printf("%d%s", services_checked, mrtg_delimiter);
735 		else if(!strcmp(temp_ptr, "NUMSVCSCHEDULED"))
736 			printf("%d%s", services_scheduled, mrtg_delimiter);
737 		else if(!strcmp(temp_ptr, "NUMSVCFLAPPING"))
738 			printf("%d%s", services_flapping, mrtg_delimiter);
739 		else if(!strcmp(temp_ptr, "NUMSVCDOWNTIME"))
740 			printf("%d%s", services_in_downtime, mrtg_delimiter);
741 
742 		/* host states */
743 		else if(!strcmp(temp_ptr, "NUMHSTUP"))
744 			printf("%d%s", hosts_up, mrtg_delimiter);
745 		else if(!strcmp(temp_ptr, "NUMHSTDOWN"))
746 			printf("%d%s", hosts_down, mrtg_delimiter);
747 		else if(!strcmp(temp_ptr, "NUMHSTUNR"))
748 			printf("%d%s", hosts_unreachable, mrtg_delimiter);
749 		else if(!strcmp(temp_ptr, "NUMHSTPROB"))
750 			printf("%d%s", hosts_down + hosts_unreachable, mrtg_delimiter);
751 
752 		/* misc host info */
753 		else if(!strcmp(temp_ptr, "NUMHSTCHECKED"))
754 			printf("%d%s", hosts_checked, mrtg_delimiter);
755 		else if(!strcmp(temp_ptr, "NUMHSTSCHEDULED"))
756 			printf("%d%s", hosts_scheduled, mrtg_delimiter);
757 		else if(!strcmp(temp_ptr, "NUMHSTFLAPPING"))
758 			printf("%d%s", hosts_flapping, mrtg_delimiter);
759 		else if(!strcmp(temp_ptr, "NUMHSTDOWNTIME"))
760 			printf("%d%s", hosts_in_downtime, mrtg_delimiter);
761 
762 		else
763 			printf("%s%s", temp_ptr, mrtg_delimiter);
764 		}
765 
766 	/* add a newline if necessary */
767 	if(strcmp(mrtg_delimiter, "\n"))
768 		printf("\n");
769 
770 	return OK;
771 	}
772 
773 
display_stats(void)774 int display_stats(void) {
775 	time_t current_time;
776 	unsigned long time_difference;
777 	int days;
778 	int hours;
779 	int minutes;
780 	int seconds;
781 
782 	time(&current_time);
783 
784 	printf("CURRENT STATUS DATA\n");
785 	printf("------------------------------------------------------\n");
786 	printf("Status File:                            %s\n", (nagiostats_file != NULL) ? nagiostats_file : status_file);
787 	time_difference = (current_time - status_creation_date);
788 	get_time_breakdown(time_difference, &days, &hours, &minutes, &seconds);
789 	printf("Status File Age:                        %dd %dh %dm %ds\n", days, hours, minutes, seconds);
790 	printf("Status File Version:                    %s\n", status_version);
791 	printf("\n");
792 	time_difference = (current_time - program_start);
793 	get_time_breakdown(time_difference, &days, &hours, &minutes, &seconds);
794 	printf("Program Running Time:                   %dd %dh %dm %ds\n", days, hours, minutes, seconds);
795 	printf("Nagios PID:                             %lu\n", nagios_pid);
796 	printf("Used/High/Total Command Buffers:        %d / %d / %d\n", used_external_command_buffer_slots, high_external_command_buffer_slots, total_external_command_buffer_slots);
797 	printf("\n");
798 	printf("Total Services:                         %d\n", status_service_entries);
799 	printf("Services Checked:                       %d\n", services_checked);
800 	printf("Services Scheduled:                     %d\n", services_scheduled);
801 	printf("Services Actively Checked:              %d\n", active_service_checks);
802 	printf("Services Passively Checked:             %d\n", passive_service_checks);
803 	printf("Total Service State Change:             %.3f / %.3f / %.3f %%\n", min_service_state_change, max_service_state_change, average_service_state_change);
804 	printf("Active Service Latency:                 %.3f / %.3f / %.3f sec\n", min_active_service_latency, max_active_service_latency, average_active_service_latency);
805 	printf("Active Service Execution Time:          %.3f / %.3f / %.3f sec\n", min_active_service_execution_time, max_active_service_execution_time, average_active_service_execution_time);
806 	printf("Active Service State Change:            %.3f / %.3f / %.3f %%\n", min_active_service_state_change, max_active_service_state_change, average_active_service_state_change);
807 	printf("Active Services Last 1/5/15/60 min:     %d / %d / %d / %d\n", active_services_checked_last_1min, active_services_checked_last_5min, active_services_checked_last_15min, active_services_checked_last_1hour);
808 	printf("Passive Service Latency:                %.3f / %.3f / %.3f sec\n", min_passive_service_latency, max_passive_service_latency, average_passive_service_latency);
809 	printf("Passive Service State Change:           %.3f / %.3f / %.3f %%\n", min_passive_service_state_change, max_passive_service_state_change, average_passive_service_state_change);
810 	printf("Passive Services Last 1/5/15/60 min:    %d / %d / %d / %d\n", passive_services_checked_last_1min, passive_services_checked_last_5min, passive_services_checked_last_15min, passive_services_checked_last_1hour);
811 	printf("Services Ok/Warn/Unk/Crit:              %d / %d / %d / %d\n", services_ok, services_warning, services_unknown, services_critical);
812 	printf("Services Flapping:                      %d\n", services_flapping);
813 	printf("Services In Downtime:                   %d\n", services_in_downtime);
814 	printf("\n");
815 	printf("Total Hosts:                            %d\n", status_host_entries);
816 	printf("Hosts Checked:                          %d\n", hosts_checked);
817 	printf("Hosts Scheduled:                        %d\n", hosts_scheduled);
818 	printf("Hosts Actively Checked:                 %d\n", active_host_checks);
819 	printf("Host Passively Checked:                 %d\n", passive_host_checks);
820 	printf("Total Host State Change:                %.3f / %.3f / %.3f %%\n", min_host_state_change, max_host_state_change, average_host_state_change);
821 	printf("Active Host Latency:                    %.3f / %.3f / %.3f sec\n", min_active_host_latency, max_active_host_latency, average_active_host_latency);
822 	printf("Active Host Execution Time:             %.3f / %.3f / %.3f sec\n", min_active_host_execution_time, max_active_host_execution_time, average_active_host_execution_time);
823 	printf("Active Host State Change:               %.3f / %.3f / %.3f %%\n", min_active_host_state_change, max_active_host_state_change, average_active_host_state_change);
824 	printf("Active Hosts Last 1/5/15/60 min:        %d / %d / %d / %d\n", active_hosts_checked_last_1min, active_hosts_checked_last_5min, active_hosts_checked_last_15min, active_hosts_checked_last_1hour);
825 	printf("Passive Host Latency:                   %.3f / %.3f / %.3f sec\n", min_passive_host_latency, max_passive_host_latency, average_passive_host_latency);
826 	printf("Passive Host State Change:              %.3f / %.3f / %.3f %%\n", min_passive_host_state_change, max_passive_host_state_change, average_passive_host_state_change);
827 	printf("Passive Hosts Last 1/5/15/60 min:       %d / %d / %d / %d\n", passive_hosts_checked_last_1min, passive_hosts_checked_last_5min, passive_hosts_checked_last_15min, passive_hosts_checked_last_1hour);
828 	printf("Hosts Up/Down/Unreach:                  %d / %d / %d\n", hosts_up, hosts_down, hosts_unreachable);
829 	printf("Hosts Flapping:                         %d\n", hosts_flapping);
830 	printf("Hosts In Downtime:                      %d\n", hosts_in_downtime);
831 	printf("\n");
832 	printf("Active Host Checks Last 1/5/15 min:     %d / %d / %d\n", active_host_checks_last_1min, active_host_checks_last_5min, active_host_checks_last_15min);
833 	printf("   Scheduled:                           %d / %d / %d\n", active_scheduled_host_checks_last_1min, active_scheduled_host_checks_last_5min, active_scheduled_host_checks_last_15min);
834 	printf("   On-demand:                           %d / %d / %d\n", active_ondemand_host_checks_last_1min, active_ondemand_host_checks_last_5min, active_ondemand_host_checks_last_15min);
835 	printf("   Parallel:                            %d / %d / %d\n", parallel_host_checks_last_1min, parallel_host_checks_last_5min, parallel_host_checks_last_15min);
836 	printf("   Serial:                              %d / %d / %d\n", serial_host_checks_last_1min, serial_host_checks_last_5min, serial_host_checks_last_15min);
837 	printf("   Cached:                              %d / %d / %d\n", active_cached_host_checks_last_1min, active_cached_host_checks_last_5min, active_cached_host_checks_last_15min);
838 	printf("Passive Host Checks Last 1/5/15 min:    %d / %d / %d\n", passive_host_checks_last_1min, passive_host_checks_last_5min, passive_host_checks_last_15min);
839 
840 	printf("Active Service Checks Last 1/5/15 min:  %d / %d / %d\n", active_service_checks_last_1min, active_service_checks_last_5min, active_service_checks_last_15min);
841 	printf("   Scheduled:                           %d / %d / %d\n", active_scheduled_service_checks_last_1min, active_scheduled_service_checks_last_5min, active_scheduled_service_checks_last_15min);
842 	printf("   On-demand:                           %d / %d / %d\n", active_ondemand_service_checks_last_1min, active_ondemand_service_checks_last_5min, active_ondemand_service_checks_last_15min);
843 	printf("   Cached:                              %d / %d / %d\n", active_cached_service_checks_last_1min, active_cached_service_checks_last_5min, active_cached_service_checks_last_15min);
844 	printf("Passive Service Checks Last 1/5/15 min: %d / %d / %d\n", passive_service_checks_last_1min, passive_service_checks_last_5min, passive_service_checks_last_15min);
845 	printf("\n");
846 	printf("External Commands Last 1/5/15 min:      %d / %d / %d\n", external_commands_last_1min, external_commands_last_5min, external_commands_last_15min);
847 	printf("\n");
848 	printf("\n");
849 
850 
851 	/*
852 	printf("CURRENT COMMENT DATA\n");
853 	printf("----------------------------------------------------\n");
854 	printf("\n");
855 	printf("\n");
856 
857 	printf("CURRENT DOWNTIME DATA\n");
858 	printf("----------------------------------------------------\n");
859 	printf("\n");
860 	*/
861 
862 	return OK;
863 	}
864 
865 
read_config_file(void)866 int read_config_file(void) {
867 	char temp_buffer[MAX_INPUT_BUFFER];
868 	FILE *fp;
869 	char *var;
870 	char *val;
871 
872 
873 	fp = fopen(main_config_file, "r");
874 	if(fp == NULL)
875 		return ERROR;
876 
877 	/* read all lines from the main nagios config file */
878 	while(fgets(temp_buffer, sizeof(temp_buffer) - 1, fp)) {
879 
880 		strip(temp_buffer);
881 
882 		/* skip blank lines and comments */
883 		if(temp_buffer[0] == '#' || temp_buffer[0] == '\x0')
884 			continue;
885 
886 		var = strtok(temp_buffer, "=");
887 		val = strtok(NULL, "\n");
888 		if(val == NULL)
889 			continue;
890 
891 		if(!strcmp(var, "status_file") || !strcmp(var, "status_log") || !strcmp(var, "xsddefault_status_log")) {
892 			if(status_file)
893 				free(status_file);
894 			status_file = strdup(val);
895 			}
896 
897 		}
898 
899 	fclose(fp);
900 
901 	return OK;
902 	}
903 
904 
read_status_file(void)905 int read_status_file(void) {
906 	char temp_buffer[MAX_INPUT_BUFFER];
907 	FILE *fp = NULL;
908 	int data_type = STATUS_NO_DATA;
909 	char *var = NULL;
910 	char *val = NULL;
911 	char *temp_ptr = NULL;
912 	time_t current_time;
913 	unsigned long time_difference = 0L;
914 
915 	double execution_time = 0.0;
916 	double latency = 0.0;
917 	int check_type = SERVICE_CHECK_ACTIVE;
918 	int current_state = STATE_OK;
919 	double state_change = 0.0;
920 	int is_flapping = FALSE;
921 	int downtime_depth = 0;
922 	time_t last_check = 0L;
923 	int should_be_scheduled = TRUE;
924 	int has_been_checked = TRUE;
925 
926 
927 	time(&current_time);
928 
929 	fp = fopen(status_file, "r");
930 	if(fp == NULL)
931 		return ERROR;
932 
933 	/* read all lines in the status file */
934 	while(fgets(temp_buffer, sizeof(temp_buffer) - 1, fp)) {
935 
936 		/* skip blank lines and comments */
937 		if(temp_buffer[0] == '#' || temp_buffer[0] == '\x0')
938 			continue;
939 
940 		strip(temp_buffer);
941 
942 		/* start of definition */
943 		if(!strcmp(temp_buffer, "servicestatus {")) {
944 			data_type = STATUS_SERVICE_DATA;
945 			status_service_entries++;
946 			}
947 		else if(!strcmp(temp_buffer, "hoststatus {")) {
948 			data_type = STATUS_HOST_DATA;
949 			status_host_entries++;
950 			}
951 		else if(!strcmp(temp_buffer, "info {"))
952 			data_type = STATUS_INFO_DATA;
953 		else if(!strcmp(temp_buffer, "programstatus {"))
954 			data_type = STATUS_PROGRAM_DATA;
955 
956 
957 		/* end of definition */
958 		else if(!strcmp(temp_buffer, "}")) {
959 
960 			switch(data_type) {
961 
962 				case STATUS_INFO_DATA:
963 					break;
964 
965 				case STATUS_PROGRAM_DATA:
966 					/* 02-15-2008 exclude cached host checks from total (they were ondemand checks that never actually executed) */
967 					active_host_checks_last_1min = active_scheduled_host_checks_last_1min + active_ondemand_host_checks_last_1min;
968 					active_host_checks_last_5min = active_scheduled_host_checks_last_5min + active_ondemand_host_checks_last_5min;
969 					active_host_checks_last_15min = active_scheduled_host_checks_last_15min + active_ondemand_host_checks_last_15min;
970 
971 					/* 02-15-2008 exclude cached service checks from total (they were ondemand checks that never actually executed) */
972 					active_service_checks_last_1min = active_scheduled_service_checks_last_1min + active_ondemand_service_checks_last_1min;
973 					active_service_checks_last_5min = active_scheduled_service_checks_last_5min + active_ondemand_service_checks_last_5min;
974 					active_service_checks_last_15min = active_scheduled_service_checks_last_15min + active_ondemand_service_checks_last_15min;
975 					break;
976 
977 				case STATUS_HOST_DATA:
978 					average_host_state_change = (((average_host_state_change * ((double)status_host_entries - 1.0)) + state_change) / (double)status_host_entries);
979 					if(have_min_host_state_change == FALSE || min_host_state_change > state_change) {
980 						have_min_host_state_change = TRUE;
981 						min_host_state_change = state_change;
982 						}
983 					if(have_max_host_state_change == FALSE || max_host_state_change < state_change) {
984 						have_max_host_state_change = TRUE;
985 						max_host_state_change = state_change;
986 						}
987 					if(check_type == HOST_CHECK_ACTIVE) {
988 						active_host_checks++;
989 						average_active_host_latency = (((average_active_host_latency * ((double)active_host_checks - 1.0)) + latency) / (double)active_host_checks);
990 						if(have_min_active_host_latency == FALSE || min_active_host_latency > latency) {
991 							have_min_active_host_latency = TRUE;
992 							min_active_host_latency = latency;
993 							}
994 						if(have_max_active_host_latency == FALSE || max_active_host_latency < latency) {
995 							have_max_active_host_latency = TRUE;
996 							max_active_host_latency = latency;
997 							}
998 						average_active_host_execution_time = (((average_active_host_execution_time * ((double)active_host_checks - 1.0)) + execution_time) / (double)active_host_checks);
999 						if(have_min_active_host_execution_time == FALSE || min_active_host_execution_time > execution_time) {
1000 							have_min_active_host_execution_time = TRUE;
1001 							min_active_host_execution_time = execution_time;
1002 							}
1003 						if(have_max_active_host_execution_time == FALSE || max_active_host_execution_time < execution_time) {
1004 							have_max_active_host_execution_time = TRUE;
1005 							max_active_host_execution_time = execution_time;
1006 							}
1007 						average_active_host_state_change = (((average_active_host_state_change * ((double)active_host_checks - 1.0)) + state_change) / (double)active_host_checks);
1008 						if(have_min_active_host_state_change == FALSE || min_active_host_state_change > state_change) {
1009 							have_min_active_host_state_change = TRUE;
1010 							min_active_host_state_change = state_change;
1011 							}
1012 						if(have_max_active_host_state_change == FALSE || max_active_host_state_change < state_change) {
1013 							have_max_active_host_state_change = TRUE;
1014 							max_active_host_state_change = state_change;
1015 							}
1016 						time_difference = current_time - last_check;
1017 						if(time_difference <= 3600)
1018 							active_hosts_checked_last_1hour++;
1019 						if(time_difference <= 900)
1020 							active_hosts_checked_last_15min++;
1021 						if(time_difference <= 300)
1022 							active_hosts_checked_last_5min++;
1023 						if(time_difference <= 60)
1024 							active_hosts_checked_last_1min++;
1025 						}
1026 					else {
1027 						passive_host_checks++;
1028 						average_passive_host_latency = (((average_passive_host_latency * ((double)passive_host_checks - 1.0)) + latency) / (double)passive_host_checks);
1029 						if(have_min_passive_host_latency == FALSE || min_passive_host_latency > latency) {
1030 							have_min_passive_host_latency = TRUE;
1031 							min_passive_host_latency = latency;
1032 							}
1033 						if(have_max_passive_host_latency == FALSE || max_passive_host_latency < latency) {
1034 							have_max_passive_host_latency = TRUE;
1035 							max_passive_host_latency = latency;
1036 							}
1037 						average_passive_host_state_change = (((average_passive_host_state_change * ((double)passive_host_checks - 1.0)) + state_change) / (double)passive_host_checks);
1038 						if(have_min_passive_host_state_change == FALSE || min_passive_host_state_change > state_change) {
1039 							have_min_passive_host_state_change = TRUE;
1040 							min_passive_host_state_change = state_change;
1041 							}
1042 						if(have_max_passive_host_state_change == FALSE || max_passive_host_state_change < state_change) {
1043 							have_max_passive_host_state_change = TRUE;
1044 							max_passive_host_state_change = state_change;
1045 							}
1046 						time_difference = current_time - last_check;
1047 						if(time_difference <= 3600)
1048 							passive_hosts_checked_last_1hour++;
1049 						if(time_difference <= 900)
1050 							passive_hosts_checked_last_15min++;
1051 						if(time_difference <= 300)
1052 							passive_hosts_checked_last_5min++;
1053 						if(time_difference <= 60)
1054 							passive_hosts_checked_last_1min++;
1055 						}
1056 					switch(current_state) {
1057 						case HOST_UP:
1058 							hosts_up++;
1059 							break;
1060 						case HOST_DOWN:
1061 							hosts_down++;
1062 							break;
1063 						case HOST_UNREACHABLE:
1064 							hosts_unreachable++;
1065 							break;
1066 						default:
1067 							break;
1068 						}
1069 					if(is_flapping == TRUE)
1070 						hosts_flapping++;
1071 					if(downtime_depth > 0)
1072 						hosts_in_downtime++;
1073 					if(has_been_checked == TRUE)
1074 						hosts_checked++;
1075 					if(should_be_scheduled == TRUE)
1076 						hosts_scheduled++;
1077 					break;
1078 
1079 				case STATUS_SERVICE_DATA:
1080 					average_service_state_change = (((average_service_state_change * ((double)status_service_entries - 1.0)) + state_change) / (double)status_service_entries);
1081 					if(have_min_service_state_change == FALSE || min_service_state_change > state_change) {
1082 						have_min_service_state_change = TRUE;
1083 						min_service_state_change = state_change;
1084 						}
1085 					if(have_max_service_state_change == FALSE || max_service_state_change < state_change) {
1086 						have_max_service_state_change = TRUE;
1087 						max_service_state_change = state_change;
1088 						}
1089 					if(check_type == SERVICE_CHECK_ACTIVE) {
1090 						active_service_checks++;
1091 						average_active_service_latency = (((average_active_service_latency * ((double)active_service_checks - 1.0)) + latency) / (double)active_service_checks);
1092 						if(have_min_active_service_latency == FALSE || min_active_service_latency > latency) {
1093 							have_min_active_service_latency = TRUE;
1094 							min_active_service_latency = latency;
1095 							}
1096 						if(have_max_active_service_latency == FALSE || max_active_service_latency < latency) {
1097 							have_max_active_service_latency = TRUE;
1098 							max_active_service_latency = latency;
1099 							}
1100 						average_active_service_execution_time = (((average_active_service_execution_time * ((double)active_service_checks - 1.0)) + execution_time) / (double)active_service_checks);
1101 						if(have_min_active_service_execution_time == FALSE || min_active_service_execution_time > execution_time) {
1102 							have_min_active_service_execution_time = TRUE;
1103 							min_active_service_execution_time = execution_time;
1104 							}
1105 						if(have_max_active_service_execution_time == FALSE || max_active_service_execution_time < execution_time) {
1106 							have_max_active_service_execution_time = TRUE;
1107 							max_active_service_execution_time = execution_time;
1108 							}
1109 						average_active_service_state_change = (((average_active_service_state_change * ((double)active_service_checks - 1.0)) + state_change) / (double)active_service_checks);
1110 						if(have_min_active_service_state_change == FALSE || min_active_service_state_change > state_change) {
1111 							have_min_active_service_state_change = TRUE;
1112 							min_active_service_state_change = state_change;
1113 							}
1114 						if(have_max_active_service_state_change == FALSE || max_active_service_state_change < state_change) {
1115 							have_max_active_service_state_change = TRUE;
1116 							max_active_service_state_change = state_change;
1117 							}
1118 						time_difference = current_time - last_check;
1119 						if(time_difference <= 3600)
1120 							active_services_checked_last_1hour++;
1121 						if(time_difference <= 900)
1122 							active_services_checked_last_15min++;
1123 						if(time_difference <= 300)
1124 							active_services_checked_last_5min++;
1125 						if(time_difference <= 60)
1126 							active_services_checked_last_1min++;
1127 						}
1128 					else {
1129 						passive_service_checks++;
1130 						average_passive_service_latency = (((average_passive_service_latency * ((double)passive_service_checks - 1.0)) + latency) / (double)passive_service_checks);
1131 						if(have_min_passive_service_latency == FALSE || min_passive_service_latency > latency) {
1132 							have_min_passive_service_latency = TRUE;
1133 							min_passive_service_latency = latency;
1134 							}
1135 						if(have_max_passive_service_latency == FALSE || max_passive_service_latency < latency) {
1136 							have_max_passive_service_latency = TRUE;
1137 							max_passive_service_latency = latency;
1138 							}
1139 						average_passive_service_state_change = (((average_passive_service_state_change * ((double)passive_service_checks - 1.0)) + state_change) / (double)passive_service_checks);
1140 						if(have_min_passive_service_state_change == FALSE || min_passive_service_state_change > state_change) {
1141 							have_min_passive_service_state_change = TRUE;
1142 							min_passive_service_state_change = state_change;
1143 							}
1144 						if(have_max_passive_service_state_change == FALSE || max_passive_service_state_change < state_change) {
1145 							have_max_passive_service_state_change = TRUE;
1146 							max_passive_service_state_change = state_change;
1147 							}
1148 						time_difference = current_time - last_check;
1149 						if(time_difference <= 3600)
1150 							passive_services_checked_last_1hour++;
1151 						if(time_difference <= 900)
1152 							passive_services_checked_last_15min++;
1153 						if(time_difference <= 300)
1154 							passive_services_checked_last_5min++;
1155 						if(time_difference <= 60)
1156 							passive_services_checked_last_1min++;
1157 						}
1158 					switch(current_state) {
1159 						case STATE_OK:
1160 							services_ok++;
1161 							break;
1162 						case STATE_WARNING:
1163 							services_warning++;
1164 							break;
1165 						case STATE_UNKNOWN:
1166 							services_unknown++;
1167 							break;
1168 						case STATE_CRITICAL:
1169 							services_critical++;
1170 							break;
1171 						default:
1172 							break;
1173 						}
1174 					if(is_flapping == TRUE)
1175 						services_flapping++;
1176 					if(downtime_depth > 0)
1177 						services_in_downtime++;
1178 					if(has_been_checked == TRUE)
1179 						services_checked++;
1180 					if(should_be_scheduled == TRUE)
1181 						services_scheduled++;
1182 					break;
1183 
1184 				default:
1185 					break;
1186 				}
1187 
1188 			data_type = STATUS_NO_DATA;
1189 
1190 			execution_time = 0.0;
1191 			latency = 0.0;
1192 			check_type = 0;
1193 			current_state = 0;
1194 			state_change = 0.0;
1195 			is_flapping = FALSE;
1196 			downtime_depth = 0;
1197 			last_check = (time_t)0;
1198 			has_been_checked = FALSE;
1199 			should_be_scheduled = FALSE;
1200 			}
1201 
1202 
1203 		/* inside definition */
1204 		else if(data_type != STATUS_NO_DATA) {
1205 
1206 			var = strtok(temp_buffer, "=");
1207 			val = strtok(NULL, "\n");
1208 			if(val == NULL)
1209 				continue;
1210 
1211 			switch(data_type) {
1212 
1213 				case STATUS_INFO_DATA:
1214 					if(!strcmp(var, "created"))
1215 						status_creation_date = strtoul(val, NULL, 10);
1216 					else if(!strcmp(var, "version"))
1217 						status_version = strdup(val);
1218 					break;
1219 
1220 				case STATUS_PROGRAM_DATA:
1221 					if(!strcmp(var, "program_start"))
1222 						program_start = strtoul(val, NULL, 10);
1223 					else if(!strcmp(var, "total_external_command_buffer_slots"))
1224 						total_external_command_buffer_slots = atoi(val);
1225 					else if(!strcmp(var, "used_external_command_buffer_slots"))
1226 						used_external_command_buffer_slots = atoi(val);
1227 					else if(!strcmp(var, "high_external_command_buffer_slots"))
1228 						high_external_command_buffer_slots = atoi(val);
1229 					else if(!strcmp(var, "nagios_pid"))
1230 						nagios_pid = strtoul(val, NULL, 10);
1231 					else if(!strcmp(var, "active_scheduled_host_check_stats")) {
1232 						if((temp_ptr = strtok(val, ",")))
1233 							active_scheduled_host_checks_last_1min = atoi(temp_ptr);
1234 						if((temp_ptr = strtok(NULL, ",")))
1235 							active_scheduled_host_checks_last_5min = atoi(temp_ptr);
1236 						if((temp_ptr = strtok(NULL, ",")))
1237 							active_scheduled_host_checks_last_15min = atoi(temp_ptr);
1238 						}
1239 					else if(!strcmp(var, "active_ondemand_host_check_stats")) {
1240 						if((temp_ptr = strtok(val, ",")))
1241 							active_ondemand_host_checks_last_1min = atoi(temp_ptr);
1242 						if((temp_ptr = strtok(NULL, ",")))
1243 							active_ondemand_host_checks_last_5min = atoi(temp_ptr);
1244 						if((temp_ptr = strtok(NULL, ",")))
1245 							active_ondemand_host_checks_last_15min = atoi(temp_ptr);
1246 						}
1247 					else if(!strcmp(var, "cached_host_check_stats")) {
1248 						if((temp_ptr = strtok(val, ",")))
1249 							active_cached_host_checks_last_1min = atoi(temp_ptr);
1250 						if((temp_ptr = strtok(NULL, ",")))
1251 							active_cached_host_checks_last_5min = atoi(temp_ptr);
1252 						if((temp_ptr = strtok(NULL, ",")))
1253 							active_cached_host_checks_last_15min = atoi(temp_ptr);
1254 						}
1255 					else if(!strcmp(var, "passive_host_check_stats")) {
1256 						if((temp_ptr = strtok(val, ",")))
1257 							passive_host_checks_last_1min = atoi(temp_ptr);
1258 						if((temp_ptr = strtok(NULL, ",")))
1259 							passive_host_checks_last_5min = atoi(temp_ptr);
1260 						if((temp_ptr = strtok(NULL, ",")))
1261 							passive_host_checks_last_15min = atoi(temp_ptr);
1262 						}
1263 					else if(!strcmp(var, "active_scheduled_service_check_stats")) {
1264 						if((temp_ptr = strtok(val, ",")))
1265 							active_scheduled_service_checks_last_1min = atoi(temp_ptr);
1266 						if((temp_ptr = strtok(NULL, ",")))
1267 							active_scheduled_service_checks_last_5min = atoi(temp_ptr);
1268 						if((temp_ptr = strtok(NULL, ",")))
1269 							active_scheduled_service_checks_last_15min = atoi(temp_ptr);
1270 						}
1271 					else if(!strcmp(var, "active_ondemand_service_check_stats")) {
1272 						if((temp_ptr = strtok(val, ",")))
1273 							active_ondemand_service_checks_last_1min = atoi(temp_ptr);
1274 						if((temp_ptr = strtok(NULL, ",")))
1275 							active_ondemand_service_checks_last_5min = atoi(temp_ptr);
1276 						if((temp_ptr = strtok(NULL, ",")))
1277 							active_ondemand_service_checks_last_15min = atoi(temp_ptr);
1278 						}
1279 					else if(!strcmp(var, "cached_service_check_stats")) {
1280 						if((temp_ptr = strtok(val, ",")))
1281 							active_cached_service_checks_last_1min = atoi(temp_ptr);
1282 						if((temp_ptr = strtok(NULL, ",")))
1283 							active_cached_service_checks_last_5min = atoi(temp_ptr);
1284 						if((temp_ptr = strtok(NULL, ",")))
1285 							active_cached_service_checks_last_15min = atoi(temp_ptr);
1286 						}
1287 					else if(!strcmp(var, "passive_service_check_stats")) {
1288 						if((temp_ptr = strtok(val, ",")))
1289 							passive_service_checks_last_1min = atoi(temp_ptr);
1290 						if((temp_ptr = strtok(NULL, ",")))
1291 							passive_service_checks_last_5min = atoi(temp_ptr);
1292 						if((temp_ptr = strtok(NULL, ",")))
1293 							passive_service_checks_last_15min = atoi(temp_ptr);
1294 						}
1295 					else if(!strcmp(var, "external_command_stats")) {
1296 						if((temp_ptr = strtok(val, ",")))
1297 							external_commands_last_1min = atoi(temp_ptr);
1298 						if((temp_ptr = strtok(NULL, ",")))
1299 							external_commands_last_5min = atoi(temp_ptr);
1300 						if((temp_ptr = strtok(NULL, ",")))
1301 							external_commands_last_15min = atoi(temp_ptr);
1302 						}
1303 					else if(!strcmp(var, "parallel_host_check_stats")) {
1304 						if((temp_ptr = strtok(val, ",")))
1305 							parallel_host_checks_last_1min = atoi(temp_ptr);
1306 						if((temp_ptr = strtok(NULL, ",")))
1307 							parallel_host_checks_last_5min = atoi(temp_ptr);
1308 						if((temp_ptr = strtok(NULL, ",")))
1309 							parallel_host_checks_last_15min = atoi(temp_ptr);
1310 						}
1311 					else if(!strcmp(var, "serial_host_check_stats")) {
1312 						if((temp_ptr = strtok(val, ",")))
1313 							serial_host_checks_last_1min = atoi(temp_ptr);
1314 						if((temp_ptr = strtok(NULL, ",")))
1315 							serial_host_checks_last_5min = atoi(temp_ptr);
1316 						if((temp_ptr = strtok(NULL, ",")))
1317 							serial_host_checks_last_15min = atoi(temp_ptr);
1318 						}
1319 					break;
1320 
1321 				case STATUS_HOST_DATA:
1322 					if(!strcmp(var, "check_execution_time"))
1323 						execution_time = strtod(val, NULL);
1324 					else if(!strcmp(var, "check_latency"))
1325 						latency = strtod(val, NULL);
1326 					else if(!strcmp(var, "percent_state_change"))
1327 						state_change = strtod(val, NULL);
1328 					else if(!strcmp(var, "check_type"))
1329 						check_type = atoi(val);
1330 					else if(!strcmp(var, "current_state"))
1331 						current_state = atoi(val);
1332 					else if(!strcmp(var, "is_flapping"))
1333 						is_flapping = (atoi(val) > 0) ? TRUE : FALSE;
1334 					else if(!strcmp(var, "scheduled_downtime_depth"))
1335 						downtime_depth = atoi(val);
1336 					else if(!strcmp(var, "last_check"))
1337 						last_check = strtoul(val, NULL, 10);
1338 					else if(!strcmp(var, "has_been_checked"))
1339 						has_been_checked = (atoi(val) > 0) ? TRUE : FALSE;
1340 					else if(!strcmp(var, "should_be_scheduled"))
1341 						should_be_scheduled = (atoi(val) > 0) ? TRUE : FALSE;
1342 					break;
1343 
1344 				case STATUS_SERVICE_DATA:
1345 					if(!strcmp(var, "check_execution_time"))
1346 						execution_time = strtod(val, NULL);
1347 					else if(!strcmp(var, "check_latency"))
1348 						latency = strtod(val, NULL);
1349 					else if(!strcmp(var, "percent_state_change"))
1350 						state_change = strtod(val, NULL);
1351 					else if(!strcmp(var, "check_type"))
1352 						check_type = atoi(val);
1353 					else if(!strcmp(var, "current_state"))
1354 						current_state = atoi(val);
1355 					else if(!strcmp(var, "is_flapping"))
1356 						is_flapping = (atoi(val) > 0) ? TRUE : FALSE;
1357 					else if(!strcmp(var, "scheduled_downtime_depth"))
1358 						downtime_depth = atoi(val);
1359 					else if(!strcmp(var, "last_check"))
1360 						last_check = strtoul(val, NULL, 10);
1361 					else if(!strcmp(var, "has_been_checked"))
1362 						has_been_checked = (atoi(val) > 0) ? TRUE : FALSE;
1363 					else if(!strcmp(var, "should_be_scheduled"))
1364 						should_be_scheduled = (atoi(val) > 0) ? TRUE : FALSE;
1365 					break;
1366 
1367 				default:
1368 					break;
1369 				}
1370 
1371 			}
1372 		}
1373 
1374 	fclose(fp);
1375 
1376 	return OK;
1377 	}
1378 
1379 
read_nagiostats_file(void)1380 int read_nagiostats_file(void) {
1381 	char temp_buffer[MAX_INPUT_BUFFER];
1382 	FILE *fp = NULL;
1383 	char *var = NULL;
1384 	char *val = NULL;
1385 	char *temp_ptr = NULL;
1386 	time_t current_time;
1387 
1388 	time(&current_time);
1389 
1390 	fp = fopen(nagiostats_file, "r");
1391 	if(fp == NULL)
1392 		return ERROR;
1393 
1394 	/* read all lines in the status file */
1395 	while(fgets(temp_buffer, sizeof(temp_buffer) - 1, fp)) {
1396 
1397 		/* skip comments */
1398 		if(temp_buffer[0] == '#')
1399 			continue;
1400 
1401 		strip(temp_buffer);
1402 
1403 		var = strtok(temp_buffer, "=");
1404 		val = strtok(NULL, "\n");
1405 		if(val == NULL)
1406 			continue;
1407 
1408 		/**** INFO ****/
1409 		if(!strcmp(var, "created"))
1410 			status_creation_date = strtoul(val, NULL, 10);
1411 		else if(!strcmp(var, "nagios_version"))
1412 			status_version = strdup(val);
1413 
1414 		/****  PROGRAM INFO ****/
1415 		else if(!strcmp(var, "program_start"))
1416 			program_start = strtoul(val, NULL, 10);
1417 		else if(!strcmp(var, "total_external_command_buffer_slots"))
1418 			total_external_command_buffer_slots = atoi(val);
1419 		else if(!strcmp(var, "used_external_command_buffer_slots"))
1420 			used_external_command_buffer_slots = atoi(val);
1421 		else if(!strcmp(var, "high_external_command_buffer_slots"))
1422 			high_external_command_buffer_slots = atoi(val);
1423 		else if(!strcmp(var, "nagios_pid"))
1424 			nagios_pid = strtoul(val, NULL, 10);
1425 		else if(!strcmp(var, "active_scheduled_host_check_stats")) {
1426 			if((temp_ptr = strtok(val, ",")))
1427 				active_scheduled_host_checks_last_1min = atoi(temp_ptr);
1428 			if((temp_ptr = strtok(NULL, ",")))
1429 				active_scheduled_host_checks_last_5min = atoi(temp_ptr);
1430 			if((temp_ptr = strtok(NULL, ",")))
1431 				active_scheduled_host_checks_last_15min = atoi(temp_ptr);
1432 			}
1433 		else if(!strcmp(var, "active_ondemand_host_check_stats")) {
1434 			if((temp_ptr = strtok(val, ",")))
1435 				active_ondemand_host_checks_last_1min = atoi(temp_ptr);
1436 			if((temp_ptr = strtok(NULL, ",")))
1437 				active_ondemand_host_checks_last_5min = atoi(temp_ptr);
1438 			if((temp_ptr = strtok(NULL, ",")))
1439 				active_ondemand_host_checks_last_15min = atoi(temp_ptr);
1440 			}
1441 		else if(!strcmp(var, "cached_host_check_stats")) {
1442 			if((temp_ptr = strtok(val, ",")))
1443 				active_cached_host_checks_last_1min = atoi(temp_ptr);
1444 			if((temp_ptr = strtok(NULL, ",")))
1445 				active_cached_host_checks_last_5min = atoi(temp_ptr);
1446 			if((temp_ptr = strtok(NULL, ",")))
1447 				active_cached_host_checks_last_15min = atoi(temp_ptr);
1448 			}
1449 		else if(!strcmp(var, "passive_host_check_stats")) {
1450 			if((temp_ptr = strtok(val, ",")))
1451 				passive_host_checks_last_1min = atoi(temp_ptr);
1452 			if((temp_ptr = strtok(NULL, ",")))
1453 				passive_host_checks_last_5min = atoi(temp_ptr);
1454 			if((temp_ptr = strtok(NULL, ",")))
1455 				passive_host_checks_last_15min = atoi(temp_ptr);
1456 			}
1457 		else if(!strcmp(var, "active_scheduled_service_check_stats")) {
1458 			if((temp_ptr = strtok(val, ",")))
1459 				active_scheduled_service_checks_last_1min = atoi(temp_ptr);
1460 			if((temp_ptr = strtok(NULL, ",")))
1461 				active_scheduled_service_checks_last_5min = atoi(temp_ptr);
1462 			if((temp_ptr = strtok(NULL, ",")))
1463 				active_scheduled_service_checks_last_15min = atoi(temp_ptr);
1464 			}
1465 		else if(!strcmp(var, "active_ondemand_service_check_stats")) {
1466 			if((temp_ptr = strtok(val, ",")))
1467 				active_ondemand_service_checks_last_1min = atoi(temp_ptr);
1468 			if((temp_ptr = strtok(NULL, ",")))
1469 				active_ondemand_service_checks_last_5min = atoi(temp_ptr);
1470 			if((temp_ptr = strtok(NULL, ",")))
1471 				active_ondemand_service_checks_last_15min = atoi(temp_ptr);
1472 			}
1473 		else if(!strcmp(var, "cached_service_check_stats")) {
1474 			if((temp_ptr = strtok(val, ",")))
1475 				active_cached_service_checks_last_1min = atoi(temp_ptr);
1476 			if((temp_ptr = strtok(NULL, ",")))
1477 				active_cached_service_checks_last_5min = atoi(temp_ptr);
1478 			if((temp_ptr = strtok(NULL, ",")))
1479 				active_cached_service_checks_last_15min = atoi(temp_ptr);
1480 			}
1481 		else if(!strcmp(var, "passive_service_check_stats")) {
1482 			if((temp_ptr = strtok(val, ",")))
1483 				passive_service_checks_last_1min = atoi(temp_ptr);
1484 			if((temp_ptr = strtok(NULL, ",")))
1485 				passive_service_checks_last_5min = atoi(temp_ptr);
1486 			if((temp_ptr = strtok(NULL, ",")))
1487 				passive_service_checks_last_15min = atoi(temp_ptr);
1488 			}
1489 		else if(!strcmp(var, "external_command_stats")) {
1490 			if((temp_ptr = strtok(val, ",")))
1491 				external_commands_last_1min = atoi(temp_ptr);
1492 			if((temp_ptr = strtok(NULL, ",")))
1493 				external_commands_last_5min = atoi(temp_ptr);
1494 			if((temp_ptr = strtok(NULL, ",")))
1495 				external_commands_last_15min = atoi(temp_ptr);
1496 			}
1497 		else if(!strcmp(var, "parallel_host_check_stats")) {
1498 			if((temp_ptr = strtok(val, ",")))
1499 				parallel_host_checks_last_1min = atoi(temp_ptr);
1500 			if((temp_ptr = strtok(NULL, ",")))
1501 				parallel_host_checks_last_5min = atoi(temp_ptr);
1502 			if((temp_ptr = strtok(NULL, ",")))
1503 				parallel_host_checks_last_15min = atoi(temp_ptr);
1504 			}
1505 		else if(!strcmp(var, "serial_host_check_stats")) {
1506 			if((temp_ptr = strtok(val, ",")))
1507 				serial_host_checks_last_1min = atoi(temp_ptr);
1508 			if((temp_ptr = strtok(NULL, ",")))
1509 				serial_host_checks_last_5min = atoi(temp_ptr);
1510 			if((temp_ptr = strtok(NULL, ",")))
1511 				serial_host_checks_last_15min = atoi(temp_ptr);
1512 			}
1513 
1514 		/***** HOST INFO *****/
1515 
1516 		else if(!strcmp(var, "total_hosts"))
1517 			status_host_entries = atoi(val);
1518 		else if(!strcmp(var, "hosts_checked"))
1519 			hosts_checked = atoi(val);
1520 		else if(!strcmp(var, "hosts_scheduled"))
1521 			hosts_scheduled = atoi(val);
1522 		else if(!strcmp(var, "hosts_flapping"))
1523 			hosts_flapping = atoi(val);
1524 		else if(!strcmp(var, "hosts_in_downtime"))
1525 			hosts_in_downtime = atoi(val);
1526 		else if(!strcmp(var, "hosts_up"))
1527 			hosts_up = atoi(val);
1528 		else if(!strcmp(var, "hosts_down"))
1529 			hosts_down = atoi(val);
1530 		else if(!strcmp(var, "hosts_unreachable"))
1531 			hosts_unreachable = atoi(val);
1532 		else if(!strcmp(var, "hosts_actively_checked"))
1533 			active_host_checks = atoi(val);
1534 		else if(!strcmp(var, "hosts_passively_checked"))
1535 			passive_host_checks = atoi(val);
1536 		else if(!strcmp(var, "total_host_state_change")) {
1537 			if((temp_ptr = strtok(val, ",")))
1538 				min_host_state_change = strtod(temp_ptr, NULL);
1539 			if((temp_ptr = strtok(NULL, ",")))
1540 				max_host_state_change = strtod(temp_ptr, NULL);
1541 			if((temp_ptr = strtok(NULL, ",")))
1542 				average_host_state_change = strtod(temp_ptr, NULL);
1543 			}
1544 		else if(!strcmp(var, "active_host_latency")) {
1545 			if((temp_ptr = strtok(val, ",")))
1546 				min_active_host_latency = strtod(temp_ptr, NULL);
1547 			if((temp_ptr = strtok(NULL, ",")))
1548 				max_active_host_latency = strtod(temp_ptr, NULL);
1549 			if((temp_ptr = strtok(NULL, ",")))
1550 				average_active_host_latency = strtod(temp_ptr, NULL);
1551 			}
1552 		else if(!strcmp(var, "active_host_execution_time")) {
1553 			if((temp_ptr = strtok(val, ",")))
1554 				min_active_host_execution_time = strtod(temp_ptr, NULL);
1555 			if((temp_ptr = strtok(NULL, ",")))
1556 				max_active_host_execution_time = strtod(temp_ptr, NULL);
1557 			if((temp_ptr = strtok(NULL, ",")))
1558 				average_active_host_execution_time = strtod(temp_ptr, NULL);
1559 			}
1560 		else if(!strcmp(var, "active_host_state_change")) {
1561 			if((temp_ptr = strtok(val, ",")))
1562 				min_active_host_state_change = strtod(temp_ptr, NULL);
1563 			if((temp_ptr = strtok(NULL, ",")))
1564 				max_active_host_state_change = strtod(temp_ptr, NULL);
1565 			if((temp_ptr = strtok(NULL, ",")))
1566 				average_active_host_state_change = strtod(temp_ptr, NULL);
1567 			}
1568 		else if(!strcmp(var, "active_hosts_last_x")) {
1569 			if((temp_ptr = strtok(val, ",")))
1570 				active_hosts_checked_last_1min = atoi(temp_ptr);
1571 			if((temp_ptr = strtok(NULL, ",")))
1572 				active_hosts_checked_last_5min = atoi(temp_ptr);
1573 			if((temp_ptr = strtok(NULL, ",")))
1574 				active_hosts_checked_last_15min = atoi(temp_ptr);
1575 			if((temp_ptr = strtok(NULL, ",")))
1576 				active_hosts_checked_last_1hour = atoi(temp_ptr);
1577 			}
1578 		else if(!strcmp(var, "passive_host_latency")) {
1579 			if((temp_ptr = strtok(val, ",")))
1580 				min_passive_host_latency = strtod(temp_ptr, NULL);
1581 			if((temp_ptr = strtok(NULL, ",")))
1582 				max_passive_host_latency = strtod(temp_ptr, NULL);
1583 			if((temp_ptr = strtok(NULL, ",")))
1584 				average_passive_host_latency = strtod(temp_ptr, NULL);
1585 			}
1586 		else if(!strcmp(var, "passive_host_state_change")) {
1587 			if((temp_ptr = strtok(val, ",")))
1588 				min_passive_host_state_change = strtod(temp_ptr, NULL);
1589 			if((temp_ptr = strtok(NULL, ",")))
1590 				max_passive_host_state_change = strtod(temp_ptr, NULL);
1591 			if((temp_ptr = strtok(NULL, ",")))
1592 				average_passive_host_state_change = strtod(temp_ptr, NULL);
1593 			}
1594 		else if(!strcmp(var, "passive_hosts_last_x")) {
1595 			if((temp_ptr = strtok(val, ",")))
1596 				passive_hosts_checked_last_1min = atoi(temp_ptr);
1597 			if((temp_ptr = strtok(NULL, ",")))
1598 				passive_hosts_checked_last_5min = atoi(temp_ptr);
1599 			if((temp_ptr = strtok(NULL, ",")))
1600 				passive_hosts_checked_last_15min = atoi(temp_ptr);
1601 			if((temp_ptr = strtok(NULL, ",")))
1602 				passive_hosts_checked_last_1hour = atoi(temp_ptr);
1603 			}
1604 
1605 
1606 		/***** SERVICE INFO *****/
1607 
1608 		else if(!strcmp(var, "total_services"))
1609 			status_service_entries = atoi(val);
1610 		else if(!strcmp(var, "services_checked"))
1611 			services_checked = atoi(val);
1612 		else if(!strcmp(var, "services_scheduled"))
1613 			services_scheduled = atoi(val);
1614 		else if(!strcmp(var, "services_flapping"))
1615 			services_flapping = atoi(val);
1616 		else if(!strcmp(var, "services_in_downtime"))
1617 			services_in_downtime = atoi(val);
1618 		else if(!strcmp(var, "services_ok"))
1619 			services_ok = atoi(val);
1620 		else if(!strcmp(var, "services_warning"))
1621 			services_warning = atoi(val);
1622 		else if(!strcmp(var, "services_critical"))
1623 			services_critical = atoi(val);
1624 		else if(!strcmp(var, "services_unknown"))
1625 			services_unknown = atoi(val);
1626 		else if(!strcmp(var, "services_actively_checked"))
1627 			active_service_checks = atoi(val);
1628 		else if(!strcmp(var, "services_passively_checked"))
1629 			passive_service_checks = atoi(val);
1630 		else if(!strcmp(var, "total_service_state_change")) {
1631 			if((temp_ptr = strtok(val, ",")))
1632 				min_service_state_change = strtod(temp_ptr, NULL);
1633 			if((temp_ptr = strtok(NULL, ",")))
1634 				max_service_state_change = strtod(temp_ptr, NULL);
1635 			if((temp_ptr = strtok(NULL, ",")))
1636 				average_service_state_change = strtod(temp_ptr, NULL);
1637 			}
1638 		else if(!strcmp(var, "active_service_latency")) {
1639 			if((temp_ptr = strtok(val, ",")))
1640 				min_active_service_latency = strtod(temp_ptr, NULL);
1641 			if((temp_ptr = strtok(NULL, ",")))
1642 				max_active_service_latency = strtod(temp_ptr, NULL);
1643 			if((temp_ptr = strtok(NULL, ",")))
1644 				average_active_service_latency = strtod(temp_ptr, NULL);
1645 			}
1646 		else if(!strcmp(var, "active_service_execution_time")) {
1647 			if((temp_ptr = strtok(val, ",")))
1648 				min_active_service_execution_time = strtod(temp_ptr, NULL);
1649 			if((temp_ptr = strtok(NULL, ",")))
1650 				max_active_service_execution_time = strtod(temp_ptr, NULL);
1651 			if((temp_ptr = strtok(NULL, ",")))
1652 				average_active_service_execution_time = strtod(temp_ptr, NULL);
1653 			}
1654 		else if(!strcmp(var, "active_service_state_change")) {
1655 			if((temp_ptr = strtok(val, ",")))
1656 				min_active_service_state_change = strtod(temp_ptr, NULL);
1657 			if((temp_ptr = strtok(NULL, ",")))
1658 				max_active_service_state_change = strtod(temp_ptr, NULL);
1659 			if((temp_ptr = strtok(NULL, ",")))
1660 				average_active_service_state_change = strtod(temp_ptr, NULL);
1661 			}
1662 		else if(!strcmp(var, "active_services_last_x")) {
1663 			if((temp_ptr = strtok(val, ",")))
1664 				active_services_checked_last_1min = atoi(temp_ptr);
1665 			if((temp_ptr = strtok(NULL, ",")))
1666 				active_services_checked_last_5min = atoi(temp_ptr);
1667 			if((temp_ptr = strtok(NULL, ",")))
1668 				active_services_checked_last_15min = atoi(temp_ptr);
1669 			if((temp_ptr = strtok(NULL, ",")))
1670 				active_services_checked_last_1hour = atoi(temp_ptr);
1671 			}
1672 		else if(!strcmp(var, "passive_service_latency")) {
1673 			if((temp_ptr = strtok(val, ",")))
1674 				min_passive_service_latency = strtod(temp_ptr, NULL);
1675 			if((temp_ptr = strtok(NULL, ",")))
1676 				max_passive_service_latency = strtod(temp_ptr, NULL);
1677 			if((temp_ptr = strtok(NULL, ",")))
1678 				average_passive_service_latency = strtod(temp_ptr, NULL);
1679 			}
1680 		else if(!strcmp(var, "passive_service_state_change")) {
1681 			if((temp_ptr = strtok(val, ",")))
1682 				min_passive_service_state_change = strtod(temp_ptr, NULL);
1683 			if((temp_ptr = strtok(NULL, ",")))
1684 				max_passive_service_state_change = strtod(temp_ptr, NULL);
1685 			if((temp_ptr = strtok(NULL, ",")))
1686 				average_passive_service_state_change = strtod(temp_ptr, NULL);
1687 			}
1688 		else if(!strcmp(var, "passive_services_last_x")) {
1689 			if((temp_ptr = strtok(val, ",")))
1690 				passive_services_checked_last_1min = atoi(temp_ptr);
1691 			if((temp_ptr = strtok(NULL, ",")))
1692 				passive_services_checked_last_5min = atoi(temp_ptr);
1693 			if((temp_ptr = strtok(NULL, ",")))
1694 				passive_services_checked_last_15min = atoi(temp_ptr);
1695 			if((temp_ptr = strtok(NULL, ",")))
1696 				passive_services_checked_last_1hour = atoi(temp_ptr);
1697 			}
1698 		}
1699 
1700 	fclose(fp);
1701 
1702 	/* 02-15-2008 exclude cached host checks from total (they were ondemand checks that never actually executed) */
1703 	active_host_checks_last_1min = active_scheduled_host_checks_last_1min + active_ondemand_host_checks_last_1min;
1704 	active_host_checks_last_5min = active_scheduled_host_checks_last_5min + active_ondemand_host_checks_last_5min;
1705 	active_host_checks_last_15min = active_scheduled_host_checks_last_15min + active_ondemand_host_checks_last_15min;
1706 
1707 	/* 02-15-2008 exclude cached service checks from total (they were ondemand checks that never actually executed) */
1708 	active_service_checks_last_1min = active_scheduled_service_checks_last_1min + active_ondemand_service_checks_last_1min;
1709 	active_service_checks_last_5min = active_scheduled_service_checks_last_5min + active_ondemand_service_checks_last_5min;
1710 	active_service_checks_last_15min = active_scheduled_service_checks_last_15min + active_ondemand_service_checks_last_15min;
1711 
1712 	return OK;
1713 	}
1714 
1715 
1716 /* strip newline, carriage return, and tab characters from beginning and end of a string */
strip(char * buffer)1717 void strip(char *buffer) {
1718 	register int x;
1719 	register int y;
1720 	register int z;
1721 
1722 	if(buffer == NULL || buffer[0] == '\x0')
1723 		return;
1724 
1725 	/* strip end of string */
1726 	y = (int)strlen(buffer);
1727 	for(x = y - 1; x >= 0; x--) {
1728 		if(buffer[x] == ' ' || buffer[x] == '\n' || buffer[x] == '\r' || buffer[x] == '\t' || buffer[x] == 13)
1729 			buffer[x] = '\x0';
1730 		else
1731 			break;
1732 		}
1733 
1734 	/* strip beginning of string (by shifting) */
1735 	y = (int)strlen(buffer);
1736 	for(x = 0; x < y; x++) {
1737 		if(buffer[x] == ' ' || buffer[x] == '\n' || buffer[x] == '\r' || buffer[x] == '\t' || buffer[x] == 13)
1738 			continue;
1739 		else
1740 			break;
1741 		}
1742 	if(x > 0) {
1743 		for(z = x; z < y; z++)
1744 			buffer[z - x] = buffer[z];
1745 		buffer[y - x] = '\x0';
1746 		}
1747 
1748 	return;
1749 	}
1750 
1751 
1752 
1753 /* get days, hours, minutes, and seconds from a raw time_t format or total seconds */
get_time_breakdown(unsigned long raw_time,int * days,int * hours,int * minutes,int * seconds)1754 void get_time_breakdown(unsigned long raw_time, int *days, int *hours, int *minutes, int *seconds) {
1755 	unsigned long temp_time;
1756 	int temp_days;
1757 	int temp_hours;
1758 	int temp_minutes;
1759 	int temp_seconds;
1760 
1761 	temp_time = raw_time;
1762 
1763 	temp_days = temp_time / 86400;
1764 	temp_time -= (temp_days * 86400);
1765 	temp_hours = temp_time / 3600;
1766 	temp_time -= (temp_hours * 3600);
1767 	temp_minutes = temp_time / 60;
1768 	temp_time -= (temp_minutes * 60);
1769 	temp_seconds = (int)temp_time;
1770 
1771 	*days = temp_days;
1772 	*hours = temp_hours;
1773 	*minutes = temp_minutes;
1774 	*seconds = temp_seconds;
1775 
1776 	return;
1777 	}
1778 
1779