5 #include <nagios/broker.h>
6 #include <nagios/nebcallbacks.h>
16 #define CONCERNS_HOST 50
17 #define CONCERNS_SERVICE 60
20 #define HASH_TABLE_SIZE 128
22 /* for some reason these aren't defined inside Nagios' headers */
24 #define SERVICE_WARNING 1
25 #define SERVICE_CRITICAL 2
26 #define SERVICE_UNKNOWN 3
28 #define PROGRESS_INTERVAL 500 /* lines to parse between progress updates */
31 static uint imported
, totsize
, totlines
;
32 static int lines_since_progress
, do_progress
;
33 static struct timeval import_start
;
34 static time_t daemon_start
, daemon_stop
, incremental
;
35 static int daemon_is_running
;
36 static uint max_dt_depth
;
38 static time_t next_dt_purge
; /* when next to purge expired downtime */
39 #define DT_PURGE_GRACETIME 300 /* seconds to add to next_dt_purge */
41 static time_t ltime
; /* the timestamp from the current log-line */
43 static int dt_start
, dt_stop
;
44 #define dt_depth (dt_start - dt_stop)
45 static hash_table
*host_downtime
;
46 static hash_table
*service_downtime
;
47 static int downtime_id
;
48 static time_t probably_ignore_downtime
;
50 struct downtime_entry
{
64 struct downtime_entry
*next
;
67 #define NUM_DENTRIES 1024
68 static struct downtime_entry
**dentry
;
69 static time_t last_downtime_start
;
71 static struct string_code event_codes
[] = {
73 add_ignored("Warning"),
74 add_ignored("LOG ROTATION"),
75 add_ignored("HOST NOTIFICATION"),
76 add_ignored("HOST FLAPPING ALERT"),
77 add_ignored("SERVICE NOTIFICATION"),
78 add_ignored("SERVICE FLAPPING ALERT"),
79 add_ignored("SERVICE EVENT HANDLER"),
80 add_ignored("HOST EVENT HANDLER"),
81 add_ignored("LOG VERSION"),
83 add_code(3, "PASSIVE HOST CHECK", NEBTYPE_HOSTCHECK_PROCESSED
),
84 add_code(4, "PASSIVE SERVICE CHECK", NEBTYPE_SERVICECHECK_PROCESSED
),
85 add_code(0, "EXTERNAL COMMAND", NEBTYPE_EXTERNALCOMMAND_END
),
86 add_code(5, "HOST ALERT", NEBTYPE_HOSTCHECK_PROCESSED
),
87 add_code(5, "INITIAL HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
88 add_code(5, "CURRENT HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
89 add_code(6, "SERVICE ALERT", NEBTYPE_SERVICECHECK_PROCESSED
),
90 add_code(6, "INITIAL SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
91 add_code(6, "CURRENT SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
92 add_code(3, "HOST DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
),
93 add_code(4, "SERVICE DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
),
97 static struct string_code command_codes
[] = {
98 add_cdef(1, DEL_HOST_DOWNTIME
),
99 add_cdef(1, DEL_SVC_DOWNTIME
),
100 add_cdef(8, SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
),
101 add_cdef(8, SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
),
102 add_cdef(8, SCHEDULE_HOSTGROUP_HOST_DOWNTIME
),
103 add_cdef(8, SCHEDULE_HOSTGROUP_SVC_DOWNTIME
),
104 add_cdef(8, SCHEDULE_HOST_DOWNTIME
),
105 add_cdef(8, SCHEDULE_HOST_SVC_DOWNTIME
),
106 add_cdef(8, SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
),
107 add_cdef(8, SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
),
108 add_cdef(8, SCHEDULE_SVC_DOWNTIME
),
111 * These really have one more field than listed here. We omit one
112 * to make author and comment concatenated with a semi-colon by default.
114 add_cdef(6, ACKNOWLEDGE_SVC_PROBLEM
),
115 add_cdef(5, ACKNOWLEDGE_HOST_PROBLEM
),
120 static inline void print_strvec(char **v
, int n
)
124 for (i
= 0; i
< n
; i
++)
125 printf("v[%2d]: %s\n", i
, v
[i
]);
129 static const char *tobytes(uint n
)
131 const char *suffix
= "KMGT";
132 static char tbuf
[2][30];
138 sprintf(tbuf
[t
], "%d bytes", n
);
142 while (n
>> (shift
* 10) > 1024)
145 sprintf(tbuf
[t
], "%0.2f %ciB",
146 (float)n
/ (float)(1 << (shift
* 10)), suffix
[shift
- 1]);
151 static void show_progress(void)
156 totlines
+= lines_since_progress
;
157 lines_since_progress
= 0;
162 elapsed
= time(NULL
) - import_start
.tv_sec
;
166 pct_done
= ((float)imported
/ (float)totsize
) * 100;
167 eta
= (elapsed
/ pct_done
) * (100.0 - pct_done
);
169 printf("\rImporting data: %.2f%% (%s) done ",
170 pct_done
, tobytes(imported
));
174 printf("%lum%lus", eta
/ 60, eta
% 60);
181 static void end_progress(void)
187 gettimeofday(&tv
, NULL
);
190 * If any of the logfiles doesn't have a newline
191 * at end of file, imported will be slightly off.
192 * We set it hard here so as to make sure that
193 * the final progress output stops at exactly 100%
199 secs
= (tv
.tv_sec
- import_start
.tv_sec
) * 1000000;
200 secs
+= tv
.tv_usec
- import_start
.tv_usec
;
201 mins
= (tv
.tv_sec
- import_start
.tv_sec
) / 60;
204 printf("%s in %u lines imported in ", tobytes(totsize
), totlines
);
206 printf("%dm ", mins
);
207 printf("%.3fs\n", secs
);
210 static int use_sql
= 1;
211 static int insert_downtime_event(int type
, char *host
, char *service
, int id
)
213 nebstruct_downtime_data ds
;
216 if (!is_interesting_service(host
, service
))
219 dt_start
+= type
== NEBTYPE_DOWNTIME_START
;
220 dt_stop
+= type
== NEBTYPE_DOWNTIME_STOP
;
221 if (dt_depth
> max_dt_depth
)
222 max_dt_depth
= dt_depth
;
227 memset(&ds
, 0, sizeof(ds
));
230 ds
.timestamp
.tv_sec
= ltime
;
232 ds
.service_description
= service
;
235 result
= hook_downtime(NEBCALLBACK_DOWNTIME_DATA
, (void *)&ds
);
237 crash("Failed to insert downtime:\n type=%d, host=%s, service=%s, id=%d",
238 type
, host
, service
, id
);
243 static int insert_service_check(struct string_code
*sc
)
245 nebstruct_service_check_data ds
;
247 if (!is_interesting_service(strv
[0], strv
[1]))
250 memset(&ds
, 0, sizeof(ds
));
252 ds
.timestamp
.tv_sec
= ltime
;
254 ds
.host_name
= strv
[0];
255 ds
.service_description
= strv
[1];
256 if (sc
->nvecs
== 4) {
257 /* passive service check result */
258 if (*strv
[2] >= '0' && *strv
[2] <= '9')
259 ds
.state
= atoi(strv
[2]);
261 ds
.state
= parse_service_state(strv
[2]);
262 ds
.state_type
= HARD_STATE
;
263 ds
.current_attempt
= 1;
266 ds
.state
= parse_service_state(strv
[2]);
267 ds
.state_type
= soft_hard(strv
[3]);
268 ds
.current_attempt
= atoi(strv
[4]);
275 return hook_service_result(NEBCALLBACK_SERVICE_CHECK_DATA
, (void *)&ds
);
278 static int insert_host_check(struct string_code
*sc
)
280 nebstruct_host_check_data ds
;
282 if (!is_interesting_host(strv
[0]))
285 memset(&ds
, 0, sizeof(ds
));
287 ds
.timestamp
.tv_sec
= ltime
;
289 ds
.host_name
= strv
[0];
290 if (sc
->nvecs
== 3) {
291 if (*strv
[1] >= '0' && *strv
[1] <= '9')
292 ds
.state
= atoi(strv
[1]);
294 ds
.state
= parse_host_state(strv
[1]);
295 /* passive host check result */
297 ds
.current_attempt
= 1;
298 ds
.state_type
= HARD_STATE
;
300 ds
.state
= parse_host_state(strv
[1]);
301 ds
.state_type
= soft_hard(strv
[2]);
302 ds
.current_attempt
= atoi(strv
[3]);
309 return hook_host_result(NEBCALLBACK_HOST_CHECK_DATA
, (void *)&ds
);
312 static int insert_process_event(int type
)
314 nebstruct_process_data ds
;
319 memset(&ds
, 0, sizeof(ds
));
320 ds
.timestamp
.tv_sec
= ltime
;
322 return hook_process_data(NEBCALLBACK_PROCESS_DATA
, (void *)&ds
);
325 static int insert_acknowledgement(struct string_code
*sc
)
330 static void dt_print(char *tpc
, time_t when
, struct downtime_entry
*dt
)
335 printf("%s: time=%lu started=%lu start=%lu stop=%lu duration=%lu id=%d ",
336 tpc
, when
, dt
->started
, dt
->start
, dt
->stop
, dt
->duration
, dt
->id
);
337 printf("%s", dt
->host
);
339 printf(";%s", dt
->service
);
343 static struct downtime_entry
*last_dte
;
344 static struct downtime_entry
*del_dte
;
346 static void remove_downtime(struct downtime_entry
*dt
);
347 static int del_matching_dt(void *data
)
349 struct downtime_entry
*dt
= data
;
351 if (del_dte
->id
== dt
->id
) {
352 dt_print("ALSO", 0, dt
);
359 static void stash_downtime_command(struct downtime_entry
*dt
)
361 dt
->slot
= dt
->start
% NUM_DENTRIES
;
362 dt
->next
= dentry
[dt
->slot
];
363 dentry
[dt
->slot
] = dt
;
366 static void remove_downtime(struct downtime_entry
*dt
)
368 struct downtime_entry
*old
;
370 if (!is_interesting_service(dt
->host
, dt
->service
))
373 insert_downtime_event(NEBTYPE_DOWNTIME_STOP
, dt
->host
, dt
->service
, dt
->id
);
376 old
= hash_remove(host_downtime
, dt
->host
);
378 old
= hash_remove2(service_downtime
, dt
->host
, dt
->service
);
380 dt_print("RM_DT", ltime
, dt
);
384 static struct downtime_entry
*
385 dt_matches_command(struct downtime_entry
*dt
, char *host
, char *service
)
387 for (; dt
; dt
= dt
->next
) {
390 if (ltime
> dt
->stop
|| ltime
< dt
->start
) {
395 case SCHEDULE_SVC_DOWNTIME
:
396 if (service
&& strcmp(service
, dt
->service
))
400 case SCHEDULE_HOST_DOWNTIME
:
401 case SCHEDULE_HOST_SVC_DOWNTIME
:
402 if (strcmp(host
, dt
->host
)) {
406 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
407 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
408 /* these two have host set in dt, but
409 * it will not match all the possible hosts */
412 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
413 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
414 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
415 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
418 crash("dt->code not set properly\n");
422 * Once we get here all the various other criteria have
423 * been matched, so we need to check if the daemon was
424 * running when this downtime was supposed to have
425 * started, and otherwise use the daemon start time
426 * as the value to diff against
428 if (daemon_stop
< dt
->start
&& daemon_start
> dt
->start
) {
429 debug("Adjusting dt->start (%lu) to (%lu)\n",
430 dt
->start
, daemon_start
);
431 dt
->start
= daemon_start
;
432 if (dt
->trigger
&& dt
->duration
)
433 dt
->stop
= dt
->start
+ dt
->duration
;
436 diff
= ltime
- dt
->start
;
437 if (diff
< 3 || dt
->trigger
|| !dt
->fixed
)
444 static struct downtime_entry
*
445 find_downtime_command(char *host
, char *service
)
448 struct downtime_entry
*shortcut
= NULL
;
450 if (last_dte
&& last_dte
->start
== ltime
) {
454 for (i
= 0; i
< NUM_DENTRIES
; i
++) {
455 struct downtime_entry
*dt
;
456 dt
= dt_matches_command(dentry
[i
], host
, service
);
458 if (shortcut
&& dt
!= shortcut
)
460 printf("FIND shortcut no good\n");
470 static int print_downtime(void *data
)
472 struct downtime_entry
*dt
= (struct downtime_entry
*)data
;
474 dt_print("UNCLOSED", ltime
, dt
);
479 static inline void set_next_dt_purge(time_t base
, time_t add
)
481 if (!next_dt_purge
|| next_dt_purge
> base
+ add
)
482 next_dt_purge
= base
+ add
;
484 if (next_dt_purge
<= ltime
)
485 next_dt_purge
= ltime
+ 1;
488 static inline void add_downtime(char *host
, char *service
, int id
)
490 struct downtime_entry
*dt
, *cmd
, *old
;
492 if (!is_interesting_service(host
, service
))
495 dt
= malloc(sizeof(*dt
));
496 cmd
= find_downtime_command(host
, service
);
498 warn("DT with no ext cmd? %lu %s;%s", ltime
, host
, service
);
499 memset(dt
, 0, sizeof(*dt
));
500 dt
->duration
= 7200; /* the default downtime duration in nagios */
502 dt
->stop
= dt
->start
+ dt
->duration
;
505 memcpy(dt
, cmd
, sizeof(*dt
));
507 dt
->host
= strdup(host
);
511 set_next_dt_purge(ltime
, dt
->duration
);
515 old
= hash_update(host_downtime
, dt
->host
, dt
);
518 dt
->service
= strdup(service
);
519 old
= hash_update2(service_downtime
, dt
->host
, dt
->service
, dt
);
522 if (old
&& old
!= dt
) {
529 dt_print("IN_DT", ltime
, dt
);
530 insert_downtime_event(NEBTYPE_DOWNTIME_START
, dt
->host
, dt
->service
, dt
->id
);
533 static time_t last_host_dt_del
, last_svc_dt_del
;
534 static int register_downtime_command(struct string_code
*sc
)
536 struct downtime_entry
*dt
;
537 char *start_time
, *end_time
, *duration
= NULL
;
538 char *host
= NULL
, *service
= NULL
, *fixed
, *triggered_by
= NULL
;
542 case DEL_HOST_DOWNTIME
:
543 last_host_dt_del
= ltime
;
545 case DEL_SVC_DOWNTIME
:
546 last_svc_dt_del
= ltime
;
549 case SCHEDULE_HOST_DOWNTIME
:
550 if (strtotimet(strv
[5], &foo
))
553 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
554 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
555 case SCHEDULE_HOST_SVC_DOWNTIME
:
558 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
559 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
560 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
561 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
562 start_time
= strv
[1];
565 if (strtotimet(strv
[5], &foo
))
566 triggered_by
= strv
[4];
572 case SCHEDULE_SVC_DOWNTIME
:
575 start_time
= strv
[2];
578 if (strtotimet(strv
[6], &foo
)) {
579 triggered_by
= strv
[5];
588 crash("Unknown downtime type: %d", sc
->code
);
591 if (!(dt
= calloc(sizeof(*dt
), 1)))
592 crash("calloc(%u, 1) failed: %s", (uint
)sizeof(*dt
), strerror(errno
));
596 dt
->host
= strdup(host
);
598 dt
->service
= strdup(service
);
600 dt
->trigger
= triggered_by
? !!(*triggered_by
- '0') : 0;
601 if (strtotimet(start_time
, &dt
->start
) || strtotimet(end_time
, &dt
->stop
))
603 print_strvec(strv
, sc
->nvecs
);
604 crash("strtotime(): type: %s; start_time='%s'; end_time='%s'; duration='%s';",
605 command_codes
[sc
->code
- 1].str
, start_time
, end_time
, duration
);
609 * sometimes downtime commands can be logged according to
610 * log version 1, while the log still claims to be version 2.
611 * Apparently, this happens when using a daemon supporting
612 * version 2 logging but a downtime command is added that
613 * follows the version 1 standard.
614 * As such, we simply ignore the result of the "duration"
615 * field conversion and just accept that it might not work
617 (void)strtotimet(duration
, &dt
->duration
);
618 dt
->fixed
= *fixed
- '0';
621 * ignore downtime scheduled to take place in the future.
622 * It will be picked up by the module anyways
624 if (dt
->start
> time(NULL
)) {
629 if (dt
->duration
> time(NULL
)) {
630 warn("Bizarrely large duration (%lu)", dt
->duration
);
632 if (dt
->start
< ltime
) {
633 if (dt
->duration
&& dt
->duration
> ltime
- dt
->start
)
634 dt
->duration
-= ltime
- dt
->start
;
638 if (dt
->stop
< ltime
|| dt
->stop
< dt
->start
) {
639 /* retroactively scheduled downtime, or just plain wrong */
640 dt
->stop
= dt
->start
;
644 if (dt
->fixed
&& dt
->duration
!= dt
->stop
- dt
->start
) {
645 // warn("duration doesn't match stop - start: (%lu : %lu)",
646 // dt->duration, dt->stop - dt->start);
648 dt
->duration
= dt
->stop
- dt
->start
;
650 else if (dt
->duration
> 86400 * 14) {
651 warn("Oddly long duration: %lu", dt
->duration
);
654 debug("start=%lu; stop=%lu; duration=%lu; fixed=%d; trigger=%d; host=%s service=%s\n",
655 dt
->start
, dt
->stop
, dt
->duration
, dt
->fixed
, dt
->trigger
, dt
->host
, dt
->service
);
657 stash_downtime_command(dt
);
661 static int insert_downtime(struct string_code
*sc
)
664 struct downtime_entry
*dt
= NULL
;
667 char *host
, *service
= NULL
;
670 if (sc
->nvecs
== 4) {
672 dt
= hash_find2(service_downtime
, host
, service
);
675 dt
= hash_find(host_downtime
, host
);
678 * to stop a downtime we can either get STOPPED or
679 * CANCELLED. So far, I've only ever seen STARTED
680 * for when it actually starts though, and since
681 * the Nagios daemon is reponsible for launching
682 * it, it's unlikely there are more variants of
685 type
= NEBTYPE_DOWNTIME_STOP
;
686 if (!strcmp(strv
[sc
->nvecs
- 2], "STARTED"))
687 type
= NEBTYPE_DOWNTIME_START
;
690 case NEBTYPE_DOWNTIME_START
:
692 if (!probably_ignore_downtime
)
693 dt_print("ALRDY", ltime
, dt
);
697 if (probably_ignore_downtime
)
698 debug("Should probably ignore this downtime: %lu : %lu %s;%s\n",
699 probably_ignore_downtime
, ltime
, host
, service
);
701 if (ltime
- last_downtime_start
> 1)
705 add_downtime(host
, service
, id
);
706 last_downtime_start
= ltime
;
709 case NEBTYPE_DOWNTIME_STOP
:
712 * this can happen when overlapping downtime entries
713 * occur, and the start event for the second (or nth)
714 * downtime starts before the first downtime has had
715 * a stop event. It basically means we've almost
716 * certainly done something wrong.
718 //printf("no dt. ds.host_name == '%s'\n", ds.host_name);
719 //fprintf(stderr, "CRASHING: %s;%s\n", ds.host_name, ds.service_description);
720 //crash("DOWNTIME_STOP without matching DOWNTIME_START");
724 dt_del_cmd
= !dt
->service
? last_host_dt_del
: last_svc_dt_del
;
726 if ((ltime
- dt_del_cmd
) > 1 && dt
->duration
- (ltime
- dt
->started
) > 60) {
727 debug("Short dt duration (%lu) for %s;%s (dt->duration=%lu)\n",
728 ltime
- dt
->started
, dt
->host
, dt
->service
, dt
->duration
);
730 if (ltime
- dt
->started
> dt
->duration
+ DT_PURGE_GRACETIME
)
731 dt_print("Long", ltime
, dt
);
735 * Now delete whatever matching downtimes we can find.
736 * this must be here, or we'll recurse like crazy into
737 * remove_downtime(), possibly exhausting the stack
742 hash_walk_data(host_downtime
, del_matching_dt
);
744 hash_walk_data(service_downtime
, del_matching_dt
);
754 static int dt_purged
;
755 static int purge_expired_dt(void *data
)
757 struct downtime_entry
*dt
= data
;
763 if (ltime
+ DT_PURGE_GRACETIME
> dt
->stop
) {
765 debug("PURGE %lu: purging expired dt %d (start=%lu; started=%lu; stop=%lu; duration=%lu; host=%s; service=%s",
766 ltime
, dt
->id
, dt
->start
, dt
->started
, dt
->stop
, dt
->duration
, dt
->host
, dt
->service
);
770 dt_print("PURGED_NOT_TIME", ltime
, dt
);
773 set_next_dt_purge(dt
->started
, dt
->duration
);
778 static int purged_downtimes
;
779 static void purge_expired_downtime(void)
785 hash_walk_data(host_downtime
, purge_expired_dt
);
787 debug("PURGE %d host downtimes purged", dt_purged
);
788 tot_purged
+= dt_purged
;
790 hash_walk_data(service_downtime
, purge_expired_dt
);
792 debug("PURGE %d service downtimes purged", dt_purged
);
793 tot_purged
+= dt_purged
;
795 debug("PURGE total %d entries purged", tot_purged
);
798 debug("PURGE next downtime purge supposed to run @ %lu, in %lu seconds",
799 next_dt_purge
, next_dt_purge
- ltime
);
801 purged_downtimes
+= tot_purged
;
804 static inline void handle_start_event(void)
806 if (!daemon_is_running
)
807 insert_process_event(NEBTYPE_PROCESS_START
);
809 probably_ignore_downtime
= daemon_start
= ltime
;
810 daemon_is_running
= 1;
813 static inline void handle_stop_event(void)
815 if (daemon_is_running
) {
816 insert_process_event(NEBTYPE_PROCESS_SHUTDOWN
);
817 daemon_is_running
= 0;
822 static int parse_line(char *line
, uint len
)
826 struct string_code
*sc
;
827 static time_t last_ltime
= 0;
829 imported
+= len
+ 1; /* make up for 1 lost byte per newline */
831 /* ignore empty lines */
835 if (++lines_since_progress
>= PROGRESS_INTERVAL
)
838 /* skip obviously bogus lines */
839 if (len
< 12 || *line
!= '[') {
840 warn("line %d; len too short, or line doesn't start with '[' (%s)", line_no
, line
);
844 ltime
= strtoul(line
+ 1, &ptr
, 10);
845 if (line
+ 1 == ptr
) {
846 crash("Failed to parse log timestamp from '%s'. I can't handle malformed logdata", line
);
850 if (ltime
< last_ltime
) {
851 // warn("ltime < last_ltime (%lu < %lu) by %lu. Compensating...",
852 // ltime, last_ltime, last_ltime - ltime);
859 * Incremental will be 0 if not set, or 1 if set but
860 * the database is currently empty.
861 * Note that this will not always do the correct thing,
862 * as downtime entries that might have been scheduled for
863 * purging may never show up as "stopped" in the database
864 * with this scheme. As such, incremental imports absolutely
865 * require that nothing is in scheduled downtime when the
866 * import is running (well, started really, but it amounts
867 * to the same thing).
869 if (ltime
< incremental
)
872 if (next_dt_purge
&& ltime
>= next_dt_purge
)
873 purge_expired_downtime();
875 if (probably_ignore_downtime
&& ltime
- probably_ignore_downtime
> 1)
876 probably_ignore_downtime
= 0;
878 while (*ptr
== ']' || *ptr
== ' ')
881 if (!is_interesting(ptr
))
884 if (!(colon
= strchr(ptr
, ':'))) {
885 /* stupid heuristic, but might be good for something,
886 * somewhere, sometime. if nothing else, it should suppress
888 if (is_start_event(ptr
)) {
889 handle_start_event();
892 if (is_stop_event(ptr
)) {
898 * An unhandled event. We should probably crash here
900 handle_unknown_event(line
);
904 /* an event happened without us having gotten a start-event */
905 if (!daemon_is_running
) {
906 insert_process_event(NEBTYPE_PROCESS_START
);
907 daemon_start
= ltime
;
908 daemon_is_running
= 1;
911 if (!(sc
= get_event_type(ptr
, colon
- ptr
))) {
912 handle_unknown_event(line
);
916 if (sc
->code
== IGNORE_LINE
)
927 nvecs
= vectorize_string(ptr
, sc
->nvecs
);
929 if (nvecs
!= sc
->nvecs
) {
931 warn("Line %d in %s seems to not have all the fields it should",
932 line_no
, cur_file
->path
);
936 for (i
= 0; i
< sc
->nvecs
; i
++) {
938 /* this should never happen */
939 warn("Line %d in %s seems to be broken, or we failed to parse it into a vector",
940 line_no
, cur_file
->path
);
949 case NEBTYPE_EXTERNALCOMMAND_END
:
950 semi_colon
= strchr(ptr
, ';');
953 if (!(sc
= get_command_type(ptr
, semi_colon
- ptr
))) {
956 if (sc
->code
== RESTART_PROGRAM
) {
961 nvecs
= vectorize_string(semi_colon
+ 1, sc
->nvecs
);
962 if (nvecs
!= sc
->nvecs
) {
963 warn("nvecs discrepancy: %d vs %d (%s)\n", nvecs
, sc
->nvecs
, ptr
);
965 if (sc
->code
!= ACKNOWLEDGE_HOST_PROBLEM
&&
966 sc
->code
!= ACKNOWLEDGE_SVC_PROBLEM
)
968 register_downtime_command(sc
);
970 insert_acknowledgement(sc
);
974 case NEBTYPE_HOSTCHECK_PROCESSED
:
975 return insert_host_check(sc
);
977 case NEBTYPE_SERVICECHECK_PROCESSED
:
978 return insert_service_check(sc
);
980 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
:
981 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
:
982 return insert_downtime(sc
);
991 static int parse_one_line(char *str
, uint len
)
993 if (parse_line(str
, len
) && use_sql
&& sql_errno())
994 crash("sql error: %s", sql_error());
999 static int hash_one_line(char *line
, uint len
)
1001 return add_interesting_object(line
);
1004 static int hash_interesting(const char *path
)
1008 if (stat(path
, &st
) < 0)
1009 crash("failed to stat %s: %s", path
, strerror(errno
));
1011 lparse_path(path
, st
.st_size
, hash_one_line
);
1016 extern const char *__progname
;
1017 int main(int argc
, char **argv
)
1019 int i
, truncate_db
= 0;
1020 struct naglog_file
*nfile
;
1021 char *db_name
= "monitor_reports";
1022 char *db_user
= "monitor";
1023 char *db_pass
= "monitor";
1024 char *db_table
= "report_data";
1026 do_progress
= isatty(fileno(stdout
));
1028 strv
= calloc(sizeof(char *), MAX_NVECS
);
1029 nfile
= calloc(sizeof(*nfile
), argc
- 1);
1030 dentry
= calloc(sizeof(*dentry
), NUM_DENTRIES
);
1031 if (!strv
|| !nfile
|| !dentry
)
1032 crash("Failed to alloc initial structs");
1035 for (num_nfile
= 0,i
= 1; i
< argc
; i
++) {
1036 char *opt
, *arg
= argv
[i
];
1037 struct naglog_file
*nf
;
1040 if ((opt
= strchr(arg
, '='))) {
1044 else if (i
< argc
- 1) {
1048 if (!prefixcmp(arg
, "--incremental")) {
1052 if (!prefixcmp(arg
, "--no-sql")) {
1056 if (!prefixcmp(arg
, "--no-progress")) {
1060 if (!prefixcmp(arg
, "--debug") || !prefixcmp(arg
, "-d")) {
1065 if (!prefixcmp(arg
, "--truncate-db")) {
1069 if (!prefixcmp(arg
, "--db-name")) {
1071 crash("%s requires a database name as an argument", arg
);
1077 if (!prefixcmp(arg
, "--db-user")) {
1079 crash("%s requires a database username as argument", arg
);
1085 if (!prefixcmp(arg
, "--db-pass")) {
1087 crash("%s requires a database username as argument", arg
);
1093 if (!prefixcmp(arg
, "--db-table")) {
1095 crash("%s requires a database table name as argument", arg
);
1101 if (!prefixcmp(arg
, "--interesting") || !prefixcmp(arg
, "-i")) {
1103 crash("%s requires a filename as argument", arg
);
1104 hash_interesting(opt
);
1110 /* non-argument, so treat as file */
1111 nf
= &nfile
[num_nfile
++];
1114 totsize
+= nf
->size
;
1118 sql_config("db_database", db_name
);
1119 sql_config("db_user", db_user
);
1120 sql_config("db_pass", db_pass
);
1121 sql_config("db_table", db_table
);
1124 crash("sql_init() failed");
1126 sql_query("TRUNCATE %s", sql_table_name());
1131 sql_query("SELECT timestamp FROM %s.%s ORDER BY timestamp DESC LIMIT 1",
1134 if (!(result
= sql_get_result()))
1135 crash("Failed to get last timestamp: %s\n", sql_error());
1137 /* someone might use --incremental with an empty
1138 * database. We shouldn't crash in that case */
1139 if ((row
= sql_fetch_row(result
)))
1140 incremental
= strtoul(row
[0], NULL
, 0);
1142 sql_free_result(result
);
1145 * We lock the table we'll be working with and disable
1146 * indexes on it. Otherwise doing the actual inserts
1147 * will take just about forever, as MySQL has to update
1148 * and flush the index cache between each operation.
1150 if (sql_query("ALTER TABLE %s DISABLE KEYS", sql_table_name()))
1151 crash("Failed to disable keys: %s", sql_error());
1152 if (sql_query("LOCK TABLES %s WRITE", sql_table_name()))
1153 crash("Failed to lock table %s: %s", sql_table_name(), sql_error());
1156 log_grok_var("logfile", "/dev/null");
1157 log_grok_var("log_levels", "warn");
1160 crash("Usage: %s [--incremental] [--interesting <file>] [--truncate-db] logfiles\n",
1164 crash("log_init() failed");
1166 qsort(nfile
, num_nfile
, sizeof(*nfile
), nfile_cmp
);
1168 host_downtime
= hash_init(HASH_TABLE_SIZE
);
1169 service_downtime
= hash_init(HASH_TABLE_SIZE
);
1171 if (hook_init() < 0)
1172 crash("Failed to initialize hooks");
1174 gettimeofday(&import_start
, NULL
);
1175 printf("Importing %s of data from %d files\n",
1176 tobytes(totsize
), num_nfile
);
1178 for (i
= 0; i
< num_nfile
; i
++) {
1179 struct naglog_file
*nf
= &nfile
[i
];
1182 debug("importing from %s (%lu : %u)\n", nf
->path
, nf
->first
, nf
->cmp
);
1184 lparse_path(nf
->path
, nf
->size
, parse_one_line
);
1185 imported
++; /* make up for one lost byte per file */
1192 printf("Unclosed host downtimes:\n");
1193 puts("------------------------");
1194 hash_walk_data(host_downtime
, print_downtime
);
1195 printf("Unclosed service downtimes:\n");
1196 puts("---------------------------");
1197 hash_walk_data(service_downtime
, print_downtime
);
1199 printf("dt_depth: %d\n", dt_depth
);
1201 printf("purged downtimes: %d\n", purged_downtimes
);
1202 printf("max simultaneous host downtime hashes: %u\n",
1203 hash_get_max_entries(host_downtime
));
1204 printf("max simultaneous service downtime hashes: %u\n",
1205 hash_get_max_entries(service_downtime
));
1206 printf("max downtime depth: %u\n", max_dt_depth
);
1213 unsigned long entries
;
1215 sql_query("SELECT id FROM %s ORDER BY id DESC LIMIT 1", sql_table_name());
1216 if (!(res
= sql_get_result()))
1219 row
= sql_fetch_row(res
);
1220 entries
= strtoul(row
[0], NULL
, 0);
1221 sql_free_result(res
);
1224 signal(SIGINT
, SIG_IGN
);
1225 sql_query("UNLOCK TABLES");
1227 printf("Creating sql table indexes. This will likely take ~%lu seconds\n",
1228 (entries
/ 50000) + 1);
1229 sql_query("ALTER TABLE %s ENABLE KEYS", sql_table_name());
1230 printf("%lu database entries indexed in %lu seconds\n",
1231 entries
, time(NULL
) - start
);
1235 if (warnings
&& debug_level
)
1236 fprintf(stderr
, "Total warnings: %d\n", warnings
);
1238 if (debug_level
|| dt_start
!= dt_stop
)
1239 fprintf(stderr
, "Downtime data %s\n started: %d\n stopped: %d\n",
1240 dt_depth
? "mismatch!" : "consistent", dt_start
, dt_stop
);
1241 if (hash_check_table(host_downtime
))
1242 fprintf(stderr
, "Hash table inconsistencies for host_downtime\n");
1243 if (hash_check_table(service_downtime
))
1244 fprintf(stderr
, "Hash table inconsistencies for service_downtime\n");
1246 print_unhandled_events();