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