5 #include "nagios/broker.h"
6 #include "nagios/nebcallbacks.h"
17 #define CONCERNS_HOST 50
18 #define CONCERNS_SERVICE 60
21 #define HASH_TABLE_SIZE 128
23 /* for some reason these aren't defined inside Nagios' headers */
25 #define SERVICE_WARNING 1
26 #define SERVICE_CRITICAL 2
27 #define SERVICE_UNKNOWN 3
29 #define PROGRESS_INTERVAL 500 /* lines to parse between progress updates */
32 static int only_notifications
;
33 static uint imported
, totsize
, totlines
;
34 static int lines_since_progress
, do_progress
;
35 static struct timeval import_start
;
36 static time_t daemon_start
, daemon_stop
, incremental
;
37 static int daemon_is_running
;
38 static uint max_dt_depth
;
40 static time_t next_dt_purge
; /* when next to purge expired downtime */
41 #define DT_PURGE_GRACETIME 300 /* seconds to add to next_dt_purge */
43 static time_t ltime
; /* the timestamp from the current log-line */
45 static int dt_start
, dt_stop
, dt_skip
;
46 #define dt_depth (dt_start - dt_stop)
47 static hash_table
*host_downtime
;
48 static hash_table
*service_downtime
;
49 static int downtime_id
;
50 static time_t probably_ignore_downtime
;
52 struct downtime_entry
{
66 struct downtime_entry
*next
;
69 #define NUM_DENTRIES 1024
70 static struct downtime_entry
**dentry
;
71 static time_t last_downtime_start
;
73 static struct string_code event_codes
[] = {
75 add_ignored("Warning"),
76 add_ignored("LOG ROTATION"),
77 add_ignored("HOST FLAPPING ALERT"),
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(5, "HOST NOTIFICATION", NEBTYPE_NOTIFICATION_END
+ CONCERNS_HOST
),
84 add_code(6, "SERVICE NOTIFICATION", NEBTYPE_NOTIFICATION_END
+ CONCERNS_SERVICE
),
85 add_code(3, "PASSIVE HOST CHECK", NEBTYPE_HOSTCHECK_PROCESSED
),
86 add_code(4, "PASSIVE SERVICE CHECK", NEBTYPE_SERVICECHECK_PROCESSED
),
87 add_code(0, "EXTERNAL COMMAND", NEBTYPE_EXTERNALCOMMAND_END
),
88 add_code(5, "HOST ALERT", NEBTYPE_HOSTCHECK_PROCESSED
),
89 add_code(5, "INITIAL HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
90 add_code(5, "CURRENT HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
91 add_code(6, "SERVICE ALERT", NEBTYPE_SERVICECHECK_PROCESSED
),
92 add_code(6, "INITIAL SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
93 add_code(6, "CURRENT SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
94 add_code(3, "HOST DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
),
95 add_code(4, "SERVICE DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
),
99 static struct string_code command_codes
[] = {
100 add_cdef(1, DEL_HOST_DOWNTIME
),
101 add_cdef(1, DEL_SVC_DOWNTIME
),
102 add_cdef(8, SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
),
103 add_cdef(8, SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
),
104 add_cdef(8, SCHEDULE_HOSTGROUP_HOST_DOWNTIME
),
105 add_cdef(8, SCHEDULE_HOSTGROUP_SVC_DOWNTIME
),
106 add_cdef(8, SCHEDULE_HOST_DOWNTIME
),
107 add_cdef(8, SCHEDULE_HOST_SVC_DOWNTIME
),
108 add_cdef(8, SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
),
109 add_cdef(8, SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
),
110 add_cdef(8, SCHEDULE_SVC_DOWNTIME
),
113 * These really have one more field than listed here. We omit one
114 * to make author and comment concatenated with a semi-colon by default.
116 add_cdef(6, ACKNOWLEDGE_SVC_PROBLEM
),
117 add_cdef(5, ACKNOWLEDGE_HOST_PROBLEM
),
122 static inline void print_strvec(char **v
, int n
)
126 for (i
= 0; i
< n
; i
++)
127 printf("v[%2d]: %s\n", i
, v
[i
]);
131 static const char *tobytes(uint n
)
133 const char *suffix
= "KMGT";
134 static char tbuf
[2][30];
140 sprintf(tbuf
[t
], "%d bytes", n
);
144 while (n
>> (shift
* 10) > 1024)
147 sprintf(tbuf
[t
], "%0.2f %ciB",
148 (float)n
/ (float)(1 << (shift
* 10)), suffix
[shift
- 1]);
153 static void show_progress(void)
158 totlines
+= lines_since_progress
;
159 lines_since_progress
= 0;
164 elapsed
= time(NULL
) - import_start
.tv_sec
;
168 pct_done
= ((float)imported
/ (float)totsize
) * 100;
169 eta
= (elapsed
/ pct_done
) * (100.0 - pct_done
);
171 printf("\rImporting data: %.2f%% (%s) done ",
172 pct_done
, tobytes(imported
));
176 printf("%lum%lus", eta
/ 60, eta
% 60);
183 static void end_progress(void)
189 gettimeofday(&tv
, NULL
);
192 * If any of the logfiles doesn't have a newline
193 * at end of file, imported will be slightly off.
194 * We set it hard here so as to make sure that
195 * the final progress output stops at exactly 100%
201 secs
= (tv
.tv_sec
- import_start
.tv_sec
) * 1000000;
202 secs
+= tv
.tv_usec
- import_start
.tv_usec
;
203 mins
= (tv
.tv_sec
- import_start
.tv_sec
) / 60;
206 printf("%s in %u lines imported in ", tobytes(totsize
), totlines
);
208 printf("%dm ", mins
);
209 printf("%.3fs\n", secs
);
212 static int use_sql
= 1;
213 static int insert_downtime_event(int type
, char *host
, char *service
, int id
)
215 nebstruct_downtime_data ds
;
218 if (!is_interesting_service(host
, service
))
221 dt_start
+= type
== NEBTYPE_DOWNTIME_START
;
222 dt_stop
+= type
== NEBTYPE_DOWNTIME_STOP
;
223 if (dt_depth
> max_dt_depth
)
224 max_dt_depth
= dt_depth
;
226 if (!use_sql
|| only_notifications
)
229 memset(&ds
, 0, sizeof(ds
));
232 ds
.timestamp
.tv_sec
= ltime
;
234 ds
.service_description
= service
;
237 result
= hook_downtime(NEBCALLBACK_DOWNTIME_DATA
, (void *)&ds
);
239 crash("Failed to insert downtime:\n type=%d, host=%s, service=%s, id=%d",
240 type
, host
, service
, id
);
245 typedef struct import_notification
{
246 int type
, reason
, state
;
247 } import_notification
;
249 static int parse_import_notification(char *str
, import_notification
*n
)
251 char *state_str
= str
;
253 n
->reason
= parse_notification_reason(str
);
254 if (n
->reason
!= NOTIFICATION_NORMAL
) {
257 space
= strchr(str
, ' ');
260 paren
= strchr(space
, ')');
265 state_str
= space
+ 2;
268 n
->type
= SERVICE_NOTIFICATION
;
269 n
->state
= parse_service_state_gently(state_str
);
271 n
->type
= HOST_NOTIFICATION
;
272 n
->state
= parse_host_state_gently(state_str
);
278 static int insert_notification(struct string_code
*sc
)
282 struct import_notification n
;
284 if (!only_notifications
)
287 if (sc
->code
- NEBTYPE_NOTIFICATION_END
== CONCERNS_SERVICE
) {
294 if (parse_import_notification(strv
[base_idx
+ 2], &n
) < 0) {
295 handle_unknown_event(strv
[base_idx
+ 2]);
303 ("INSERT INTO %s.%s("
304 "notification_type, start_time, end_time, contact_name, "
305 "host_name, service_description, "
306 "command_name, output, "
307 "state, reason_type) "
309 "%d, %lu, %lu, '%s', "
313 sql_db_name(), sql_table_name(),
314 n
.type
, ltime
, ltime
, sql_escape(strv
[0]),
315 sql_escape(strv
[1]), desc
? sql_escape(desc
) : "",
316 sql_escape(strv
[base_idx
+ 3]), sql_escape(strv
[base_idx
+ 4]),
320 static int insert_service_check(struct string_code
*sc
)
322 nebstruct_service_check_data ds
;
324 if (!is_interesting_service(strv
[0], strv
[1]))
327 memset(&ds
, 0, sizeof(ds
));
329 ds
.timestamp
.tv_sec
= ltime
;
331 ds
.host_name
= strv
[0];
332 ds
.service_description
= strv
[1];
333 if (sc
->nvecs
== 4) {
334 /* passive service check result */
335 if (*strv
[2] >= '0' && *strv
[2] <= '9')
336 ds
.state
= atoi(strv
[2]);
338 ds
.state
= parse_service_state(strv
[2]);
339 ds
.state_type
= HARD_STATE
;
340 ds
.current_attempt
= 1;
343 ds
.state
= parse_service_state(strv
[2]);
344 ds
.state_type
= soft_hard(strv
[3]);
345 ds
.current_attempt
= atoi(strv
[4]);
349 if (!use_sql
|| only_notifications
)
352 return hook_service_result(NEBCALLBACK_SERVICE_CHECK_DATA
, (void *)&ds
);
355 static int insert_host_check(struct string_code
*sc
)
357 nebstruct_host_check_data ds
;
359 if (!is_interesting_host(strv
[0]))
362 memset(&ds
, 0, sizeof(ds
));
364 ds
.timestamp
.tv_sec
= ltime
;
366 ds
.host_name
= strv
[0];
367 if (sc
->nvecs
== 3) {
368 if (*strv
[1] >= '0' && *strv
[1] <= '9')
369 ds
.state
= atoi(strv
[1]);
371 ds
.state
= parse_host_state(strv
[1]);
372 /* passive host check result */
374 ds
.current_attempt
= 1;
375 ds
.state_type
= HARD_STATE
;
377 ds
.state
= parse_host_state(strv
[1]);
378 ds
.state_type
= soft_hard(strv
[2]);
379 ds
.current_attempt
= atoi(strv
[3]);
383 if (!use_sql
|| only_notifications
)
386 return hook_host_result(NEBCALLBACK_HOST_CHECK_DATA
, (void *)&ds
);
389 static int insert_process_event(int type
)
391 nebstruct_process_data ds
;
393 if (!use_sql
|| only_notifications
)
396 memset(&ds
, 0, sizeof(ds
));
397 ds
.timestamp
.tv_sec
= ltime
;
399 return hook_process_data(NEBCALLBACK_PROCESS_DATA
, (void *)&ds
);
402 static int insert_acknowledgement(struct string_code
*sc
)
407 static void dt_print(char *tpc
, time_t when
, struct downtime_entry
*dt
)
412 printf("%s: time=%lu started=%lu start=%lu stop=%lu duration=%lu id=%d ",
413 tpc
, when
, dt
->started
, dt
->start
, dt
->stop
, dt
->duration
, dt
->id
);
414 printf("%s", dt
->host
);
416 printf(";%s", dt
->service
);
420 static struct downtime_entry
*last_dte
;
421 static struct downtime_entry
*del_dte
;
423 static void remove_downtime(struct downtime_entry
*dt
);
424 static int del_matching_dt(void *data
)
426 struct downtime_entry
*dt
= data
;
428 if (del_dte
->id
== dt
->id
) {
429 dt_print("ALSO", 0, dt
);
436 static void stash_downtime_command(struct downtime_entry
*dt
)
438 dt
->slot
= dt
->start
% NUM_DENTRIES
;
439 dt
->next
= dentry
[dt
->slot
];
440 dentry
[dt
->slot
] = dt
;
443 static void remove_downtime(struct downtime_entry
*dt
)
445 struct downtime_entry
*old
;
447 if (!is_interesting_service(dt
->host
, dt
->service
))
450 insert_downtime_event(NEBTYPE_DOWNTIME_STOP
, dt
->host
, dt
->service
, dt
->id
);
453 old
= hash_remove(host_downtime
, dt
->host
);
455 old
= hash_remove2(service_downtime
, dt
->host
, dt
->service
);
457 dt_print("RM_DT", ltime
, dt
);
461 static struct downtime_entry
*
462 dt_matches_command(struct downtime_entry
*dt
, char *host
, char *service
)
464 for (; dt
; dt
= dt
->next
) {
467 if (ltime
> dt
->stop
|| ltime
< dt
->start
) {
472 case SCHEDULE_SVC_DOWNTIME
:
473 if (service
&& strcmp(service
, dt
->service
))
477 case SCHEDULE_HOST_DOWNTIME
:
478 case SCHEDULE_HOST_SVC_DOWNTIME
:
479 if (strcmp(host
, dt
->host
)) {
483 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
484 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
485 /* these two have host set in dt, but
486 * it will not match all the possible hosts */
489 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
490 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
491 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
492 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
495 crash("dt->code not set properly\n");
499 * Once we get here all the various other criteria have
500 * been matched, so we need to check if the daemon was
501 * running when this downtime was supposed to have
502 * started, and otherwise use the daemon start time
503 * as the value to diff against
505 if (daemon_stop
< dt
->start
&& daemon_start
> dt
->start
) {
506 debug("Adjusting dt->start (%lu) to (%lu)\n",
507 dt
->start
, daemon_start
);
508 dt
->start
= daemon_start
;
509 if (dt
->trigger
&& dt
->duration
)
510 dt
->stop
= dt
->start
+ dt
->duration
;
513 diff
= ltime
- dt
->start
;
514 if (diff
< 3 || dt
->trigger
|| !dt
->fixed
)
521 static struct downtime_entry
*
522 find_downtime_command(char *host
, char *service
)
525 struct downtime_entry
*shortcut
= NULL
;
527 if (last_dte
&& last_dte
->start
== ltime
) {
531 for (i
= 0; i
< NUM_DENTRIES
; i
++) {
532 struct downtime_entry
*dt
;
533 dt
= dt_matches_command(dentry
[i
], host
, service
);
535 if (shortcut
&& dt
!= shortcut
)
537 printf("FIND shortcut no good\n");
547 static int print_downtime(void *data
)
549 struct downtime_entry
*dt
= (struct downtime_entry
*)data
;
551 dt_print("UNCLOSED", ltime
, dt
);
556 static inline void set_next_dt_purge(time_t base
, time_t add
)
558 if (!next_dt_purge
|| next_dt_purge
> base
+ add
)
559 next_dt_purge
= base
+ add
;
561 if (next_dt_purge
<= ltime
)
562 next_dt_purge
= ltime
+ 1;
565 static inline void add_downtime(char *host
, char *service
, int id
)
567 struct downtime_entry
*dt
, *cmd
, *old
;
569 if (!is_interesting_service(host
, service
))
572 dt
= malloc(sizeof(*dt
));
573 cmd
= find_downtime_command(host
, service
);
575 warn("DT with no ext cmd? %lu %s;%s", ltime
, host
, service
);
576 memset(dt
, 0, sizeof(*dt
));
577 dt
->duration
= 7200; /* the default downtime duration in nagios */
579 dt
->stop
= dt
->start
+ dt
->duration
;
582 memcpy(dt
, cmd
, sizeof(*dt
));
584 dt
->host
= strdup(host
);
588 set_next_dt_purge(ltime
, dt
->duration
);
592 old
= hash_update(host_downtime
, dt
->host
, dt
);
595 dt
->service
= strdup(service
);
596 old
= hash_update2(service_downtime
, dt
->host
, dt
->service
, dt
);
599 if (old
&& old
!= dt
) {
606 dt_print("IN_DT", ltime
, dt
);
607 insert_downtime_event(NEBTYPE_DOWNTIME_START
, dt
->host
, dt
->service
, dt
->id
);
610 static time_t last_host_dt_del
, last_svc_dt_del
;
611 static int register_downtime_command(struct string_code
*sc
)
613 struct downtime_entry
*dt
;
614 char *start_time
, *end_time
, *duration
= NULL
;
615 char *host
= NULL
, *service
= NULL
, *fixed
, *triggered_by
= NULL
;
619 case DEL_HOST_DOWNTIME
:
620 last_host_dt_del
= ltime
;
622 case DEL_SVC_DOWNTIME
:
623 last_svc_dt_del
= ltime
;
626 case SCHEDULE_HOST_DOWNTIME
:
627 if (strtotimet(strv
[5], &foo
))
630 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
631 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
632 case SCHEDULE_HOST_SVC_DOWNTIME
:
635 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
636 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
637 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
638 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
639 start_time
= strv
[1];
642 if (strtotimet(strv
[5], &foo
))
643 triggered_by
= strv
[4];
649 case SCHEDULE_SVC_DOWNTIME
:
652 start_time
= strv
[2];
655 if (strtotimet(strv
[6], &foo
)) {
656 triggered_by
= strv
[5];
665 crash("Unknown downtime type: %d", sc
->code
);
668 if (!(dt
= calloc(sizeof(*dt
), 1)))
669 crash("calloc(%u, 1) failed: %s", (uint
)sizeof(*dt
), strerror(errno
));
673 dt
->host
= strdup(host
);
675 dt
->service
= strdup(service
);
677 dt
->trigger
= triggered_by
? !!(*triggered_by
- '0') : 0;
678 if (strtotimet(start_time
, &dt
->start
) || strtotimet(end_time
, &dt
->stop
))
680 print_strvec(strv
, sc
->nvecs
);
681 crash("strtotime(): type: %s; start_time='%s'; end_time='%s'; duration='%s';",
682 command_codes
[sc
->code
- 1].str
, start_time
, end_time
, duration
);
686 * sometimes downtime commands can be logged according to
687 * log version 1, while the log still claims to be version 2.
688 * Apparently, this happens when using a daemon supporting
689 * version 2 logging but a downtime command is added that
690 * follows the version 1 standard.
691 * As such, we simply ignore the result of the "duration"
692 * field conversion and just accept that it might not work
694 (void)strtotimet(duration
, &dt
->duration
);
695 dt
->fixed
= *fixed
- '0';
698 * ignore downtime scheduled to take place in the future.
699 * It will be picked up by the module anyways
701 if (dt
->start
> time(NULL
)) {
706 if (dt
->duration
> time(NULL
)) {
707 warn("Bizarrely large duration (%lu)", dt
->duration
);
709 if (dt
->start
< ltime
) {
710 if (dt
->duration
&& dt
->duration
> ltime
- dt
->start
)
711 dt
->duration
-= ltime
- dt
->start
;
715 if (dt
->stop
< ltime
|| dt
->stop
< dt
->start
) {
716 /* retroactively scheduled downtime, or just plain wrong */
717 dt
->stop
= dt
->start
;
721 if (dt
->fixed
&& dt
->duration
!= dt
->stop
- dt
->start
) {
722 // warn("duration doesn't match stop - start: (%lu : %lu)",
723 // dt->duration, dt->stop - dt->start);
725 dt
->duration
= dt
->stop
- dt
->start
;
727 else if (dt
->duration
> 86400 * 14) {
728 warn("Oddly long duration: %lu", dt
->duration
);
731 debug("start=%lu; stop=%lu; duration=%lu; fixed=%d; trigger=%d; host=%s service=%s\n",
732 dt
->start
, dt
->stop
, dt
->duration
, dt
->fixed
, dt
->trigger
, dt
->host
, dt
->service
);
734 stash_downtime_command(dt
);
738 static int insert_downtime(struct string_code
*sc
)
741 struct downtime_entry
*dt
= NULL
;
744 char *host
, *service
= NULL
;
747 if (sc
->nvecs
== 4) {
749 dt
= hash_find2(service_downtime
, host
, service
);
752 dt
= hash_find(host_downtime
, host
);
755 * to stop a downtime we can either get STOPPED or
756 * CANCELLED. So far, I've only ever seen STARTED
757 * for when it actually starts though, and since
758 * the Nagios daemon is reponsible for launching
759 * it, it's unlikely there are more variants of
762 type
= NEBTYPE_DOWNTIME_STOP
;
763 if (!strcmp(strv
[sc
->nvecs
- 2], "STARTED"))
764 type
= NEBTYPE_DOWNTIME_START
;
767 case NEBTYPE_DOWNTIME_START
:
769 if (!probably_ignore_downtime
)
770 dt_print("ALRDY", ltime
, dt
);
774 if (probably_ignore_downtime
)
775 debug("Should probably ignore this downtime: %lu : %lu %s;%s\n",
776 probably_ignore_downtime
, ltime
, host
, service
);
778 if (ltime
- last_downtime_start
> 1)
782 add_downtime(host
, service
, id
);
783 last_downtime_start
= ltime
;
786 case NEBTYPE_DOWNTIME_STOP
:
789 * this can happen when overlapping downtime entries
790 * occur, and the start event for the second (or nth)
791 * downtime starts before the first downtime has had
792 * a stop event. It basically means we've almost
793 * certainly done something wrong.
795 //printf("no dt. ds.host_name == '%s'\n", ds.host_name);
796 //fprintf(stderr, "CRASHING: %s;%s\n", ds.host_name, ds.service_description);
797 //crash("DOWNTIME_STOP without matching DOWNTIME_START");
802 dt_del_cmd
= !dt
->service
? last_host_dt_del
: last_svc_dt_del
;
804 if ((ltime
- dt_del_cmd
) > 1 && dt
->duration
- (ltime
- dt
->started
) > 60) {
805 debug("Short dt duration (%lu) for %s;%s (dt->duration=%lu)\n",
806 ltime
- dt
->started
, dt
->host
, dt
->service
, dt
->duration
);
808 if (ltime
- dt
->started
> dt
->duration
+ DT_PURGE_GRACETIME
)
809 dt_print("Long", ltime
, dt
);
813 * Now delete whatever matching downtimes we can find.
814 * this must be here, or we'll recurse like crazy into
815 * remove_downtime(), possibly exhausting the stack
820 hash_walk_data(host_downtime
, del_matching_dt
);
822 hash_walk_data(service_downtime
, del_matching_dt
);
832 static int dt_purged
;
833 static int purge_expired_dt(void *data
)
835 struct downtime_entry
*dt
= data
;
842 set_next_dt_purge(dt
->started
, dt
->duration
);
844 if (ltime
+ DT_PURGE_GRACETIME
> dt
->stop
) {
846 debug("PURGE %lu: purging expired dt %d (start=%lu; started=%lu; stop=%lu; duration=%lu; host=%s; service=%s",
847 ltime
, dt
->id
, dt
->start
, dt
->started
, dt
->stop
, dt
->duration
, dt
->host
, dt
->service
);
851 dt_print("PURGED_NOT_TIME", ltime
, dt
);
857 static int purged_downtimes
;
858 static void purge_expired_downtime(void)
864 hash_walk_data(host_downtime
, purge_expired_dt
);
866 debug("PURGE %d host downtimes purged", dt_purged
);
867 tot_purged
+= dt_purged
;
869 hash_walk_data(service_downtime
, purge_expired_dt
);
871 debug("PURGE %d service downtimes purged", dt_purged
);
872 tot_purged
+= dt_purged
;
874 debug("PURGE total %d entries purged", tot_purged
);
877 debug("PURGE next downtime purge supposed to run @ %lu, in %lu seconds",
878 next_dt_purge
, next_dt_purge
- ltime
);
880 purged_downtimes
+= tot_purged
;
883 static inline void handle_start_event(void)
885 if (!daemon_is_running
)
886 insert_process_event(NEBTYPE_PROCESS_START
);
888 probably_ignore_downtime
= daemon_start
= ltime
;
889 daemon_is_running
= 1;
892 static inline void handle_stop_event(void)
894 if (daemon_is_running
) {
895 insert_process_event(NEBTYPE_PROCESS_SHUTDOWN
);
896 daemon_is_running
= 0;
901 static int parse_line(char *line
, uint len
)
905 struct string_code
*sc
;
906 static time_t last_ltime
= 0;
908 imported
+= len
+ 1; /* make up for 1 lost byte per newline */
910 /* ignore empty lines */
914 if (++lines_since_progress
>= PROGRESS_INTERVAL
)
917 /* skip obviously bogus lines */
918 if (len
< 12 || *line
!= '[') {
919 warn("line %d; len too short, or line doesn't start with '[' (%s)", line_no
, line
);
923 ltime
= strtoul(line
+ 1, &ptr
, 10);
924 if (line
+ 1 == ptr
) {
925 crash("Failed to parse log timestamp from '%s'. I can't handle malformed logdata", line
);
929 if (ltime
< last_ltime
) {
930 // warn("ltime < last_ltime (%lu < %lu) by %lu. Compensating...",
931 // ltime, last_ltime, last_ltime - ltime);
938 * Incremental will be 0 if not set, or 1 if set but
939 * the database is currently empty.
940 * Note that this will not always do the correct thing,
941 * as downtime entries that might have been scheduled for
942 * purging may never show up as "stopped" in the database
943 * with this scheme. As such, incremental imports absolutely
944 * require that nothing is in scheduled downtime when the
945 * import is running (well, started really, but it amounts
946 * to the same thing).
948 if (ltime
< incremental
)
951 if (next_dt_purge
&& ltime
>= next_dt_purge
)
952 purge_expired_downtime();
954 if (probably_ignore_downtime
&& ltime
- probably_ignore_downtime
> 1)
955 probably_ignore_downtime
= 0;
957 while (*ptr
== ']' || *ptr
== ' ')
960 if (!is_interesting(ptr
))
963 if (!(colon
= strchr(ptr
, ':'))) {
964 /* stupid heuristic, but might be good for something,
965 * somewhere, sometime. if nothing else, it should suppress
967 if (is_start_event(ptr
)) {
968 handle_start_event();
971 if (is_stop_event(ptr
)) {
977 * An unhandled event. We should probably crash here
979 handle_unknown_event(line
);
983 /* an event happened without us having gotten a start-event */
984 if (!daemon_is_running
) {
985 insert_process_event(NEBTYPE_PROCESS_START
);
986 daemon_start
= ltime
;
987 daemon_is_running
= 1;
990 if (!(sc
= get_event_type(ptr
, colon
- ptr
))) {
991 handle_unknown_event(line
);
995 if (sc
->code
== IGNORE_LINE
)
1006 nvecs
= vectorize_string(ptr
, sc
->nvecs
);
1008 if (nvecs
!= sc
->nvecs
) {
1010 warn("Line %d in %s seems to not have all the fields it should",
1011 line_no
, cur_file
->path
);
1015 for (i
= 0; i
< sc
->nvecs
; i
++) {
1017 /* this should never happen */
1018 warn("Line %d in %s seems to be broken, or we failed to parse it into a vector",
1019 line_no
, cur_file
->path
);
1028 case NEBTYPE_EXTERNALCOMMAND_END
:
1029 semi_colon
= strchr(ptr
, ';');
1032 if (!(sc
= get_command_type(ptr
, semi_colon
- ptr
))) {
1035 if (sc
->code
== RESTART_PROGRAM
) {
1036 handle_stop_event();
1040 nvecs
= vectorize_string(semi_colon
+ 1, sc
->nvecs
);
1041 if (nvecs
!= sc
->nvecs
) {
1042 warn("nvecs discrepancy: %d vs %d (%s)\n", nvecs
, sc
->nvecs
, ptr
);
1044 if (sc
->code
!= ACKNOWLEDGE_HOST_PROBLEM
&&
1045 sc
->code
!= ACKNOWLEDGE_SVC_PROBLEM
)
1047 register_downtime_command(sc
);
1049 insert_acknowledgement(sc
);
1053 case NEBTYPE_HOSTCHECK_PROCESSED
:
1054 return insert_host_check(sc
);
1056 case NEBTYPE_SERVICECHECK_PROCESSED
:
1057 return insert_service_check(sc
);
1059 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
:
1060 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
:
1061 return insert_downtime(sc
);
1063 case NEBTYPE_NOTIFICATION_END
+ CONCERNS_HOST
:
1064 case NEBTYPE_NOTIFICATION_END
+ CONCERNS_SERVICE
:
1065 return insert_notification(sc
);
1074 static int parse_one_line(char *str
, uint len
)
1076 if (parse_line(str
, len
) && use_sql
&& sql_errno())
1077 crash("sql error: %s", sql_error());
1082 static int hash_one_line(char *line
, uint len
)
1084 return add_interesting_object(line
);
1087 static int hash_interesting(const char *path
)
1091 if (stat(path
, &st
) < 0)
1092 crash("failed to stat %s: %s", path
, strerror(errno
));
1094 lparse_path(path
, st
.st_size
, hash_one_line
);
1099 extern const char *__progname
;
1100 int main(int argc
, char **argv
)
1102 int i
, truncate_db
= 0;
1103 const char *nagios_cfg
= NULL
;
1104 char *db_name
, *db_user
, *db_pass
, *db_table
;
1106 db_name
= db_user
= db_pass
= db_table
= NULL
;
1108 do_progress
= isatty(fileno(stdout
));
1110 strv
= calloc(sizeof(char *), MAX_NVECS
);
1111 dentry
= calloc(sizeof(*dentry
), NUM_DENTRIES
);
1112 if (!strv
|| !dentry
)
1113 crash("Failed to alloc initial structs");
1116 for (num_nfile
= 0,i
= 1; i
< argc
; i
++) {
1117 char *opt
, *arg
= argv
[i
];
1118 int arg_len
, eq_opt
= 0;
1120 if ((opt
= strchr(arg
, '='))) {
1124 else if (i
< argc
- 1) {
1128 if (!prefixcmp(arg
, "--incremental")) {
1132 if (!prefixcmp(arg
, "--no-sql")) {
1136 if (!prefixcmp(arg
, "--only-notifications")) {
1137 only_notifications
= 1;
1138 db_name
= db_name
? db_name
: "merlin";
1139 db_user
= db_user
? db_user
: "merlin";
1140 db_pass
= db_pass
? db_pass
: "merlin";
1141 db_table
= db_table
? db_table
: "notification";
1144 if (!prefixcmp(arg
, "--no-progress")) {
1148 if (!prefixcmp(arg
, "--debug") || !prefixcmp(arg
, "-d")) {
1153 if (!prefixcmp(arg
, "--truncate-db")) {
1157 if (!prefixcmp(arg
, "--nagios-cfg")) {
1158 if (!opt
|| !*opt
) {
1159 crash("%s requires the path to nagios.cfg as argument", arg
);
1166 if (!prefixcmp(arg
, "--db-name")) {
1168 crash("%s requires a database name as an argument", arg
);
1174 if (!prefixcmp(arg
, "--db-user")) {
1176 crash("%s requires a database username as argument", arg
);
1182 if (!prefixcmp(arg
, "--db-pass")) {
1184 crash("%s requires a database username as argument", arg
);
1190 if (!prefixcmp(arg
, "--db-table")) {
1192 crash("%s requires a database table name as argument", arg
);
1198 if (!prefixcmp(arg
, "--interesting") || !prefixcmp(arg
, "-i")) {
1200 crash("%s requires a filename as argument", arg
);
1201 hash_interesting(opt
);
1207 /* non-argument, so treat as a config- or log-file */
1208 arg_len
= strlen(arg
);
1209 if (arg_len
>= 10 && !strcmp(&arg
[arg_len
- 10], "nagios.cfg")) {
1212 add_naglog_path(arg
);
1216 /* fallback for op5 systems */
1217 if (!nagios_cfg
&& !num_nfile
) {
1218 nagios_cfg
= "/opt/monitor/etc/nagios.cfg";
1221 struct cfg_comp
*conf
;
1222 conf
= cfg_parse_file(nagios_cfg
);
1223 for (i
= 0; i
< conf
->vars
; i
++) {
1224 struct cfg_var
*v
= conf
->vlist
[i
];
1225 if (!strcmp(v
->key
, "log_file")) {
1226 add_naglog_path(v
->value
);
1228 if (!strcmp(v
->key
, "log_archive_path")) {
1229 add_naglog_path(v
->value
);
1235 db_name
= db_name
? db_name
: "monitor_reports";
1236 db_user
= db_user
? db_user
: "monitor";
1237 db_pass
= db_pass
? db_pass
: "monitor";
1238 db_table
= db_table
? db_table
: "report_data";
1239 sql_config("db_database", db_name
);
1240 sql_config("db_user", db_user
);
1241 sql_config("db_pass", db_pass
);
1242 sql_config("db_table", db_table
);
1245 crash("sql_init() failed");
1247 sql_query("TRUNCATE %s", sql_table_name());
1252 sql_query("SELECT %s FROM %s.%s ORDER BY %s DESC LIMIT 1",
1253 only_notifications
? "end_time" : "timestamp",
1255 only_notifications
? "end_time" : "timestamp");
1257 if (!(result
= sql_get_result()))
1258 crash("Failed to get last timestamp: %s\n", sql_error());
1260 /* someone might use --incremental with an empty
1261 * database. We shouldn't crash in that case */
1262 if ((row
= sql_fetch_row(result
)))
1263 incremental
= strtoul(row
[0], NULL
, 0);
1265 sql_free_result(result
);
1268 * We lock the table we'll be working with and disable
1269 * indexes on it. Otherwise doing the actual inserts
1270 * will take just about forever, as MySQL has to update
1271 * and flush the index cache between each operation.
1273 if (sql_query("ALTER TABLE %s DISABLE KEYS", sql_table_name()))
1274 crash("Failed to disable keys: %s", sql_error());
1275 if (sql_query("LOCK TABLES %s WRITE", sql_table_name()))
1276 crash("Failed to lock table %s: %s", sql_table_name(), sql_error());
1279 log_grok_var("logfile", "/dev/null");
1280 log_grok_var("log_levels", "warn");
1283 crash("Usage: %s [--incremental] [--interesting <file>] [--truncate-db] logfiles\n",
1287 crash("log_init() failed");
1289 qsort(nfile
, num_nfile
, sizeof(*nfile
), nfile_cmp
);
1291 host_downtime
= hash_init(HASH_TABLE_SIZE
);
1292 service_downtime
= hash_init(HASH_TABLE_SIZE
);
1294 if (hook_init() < 0)
1295 crash("Failed to initialize hooks");
1297 /* go through them once to count the total size for progress output */
1298 for (i
= 0; i
< num_nfile
; i
++) {
1299 totsize
+= nfile
[i
].size
;
1302 gettimeofday(&import_start
, NULL
);
1303 printf("Importing %s of data from %d files\n",
1304 tobytes(totsize
), num_nfile
);
1306 for (i
= 0; i
< num_nfile
; i
++) {
1307 struct naglog_file
*nf
= &nfile
[i
];
1310 debug("importing from %s (%lu : %u)\n", nf
->path
, nf
->first
, nf
->cmp
);
1312 lparse_path(nf
->path
, nf
->size
, parse_one_line
);
1313 imported
++; /* make up for one lost byte per file */
1317 purge_expired_downtime();
1322 printf("Unclosed host downtimes:\n");
1323 puts("------------------------");
1324 hash_walk_data(host_downtime
, print_downtime
);
1325 printf("Unclosed service downtimes:\n");
1326 puts("---------------------------");
1327 hash_walk_data(service_downtime
, print_downtime
);
1329 printf("dt_depth: %d\n", dt_depth
);
1331 printf("purged downtimes: %d\n", purged_downtimes
);
1332 printf("max simultaneous host downtime hashes: %u\n",
1333 hash_entries_max(host_downtime
));
1334 printf("max simultaneous service downtime hashes: %u\n",
1335 hash_entries_max(service_downtime
));
1336 printf("max downtime depth: %u\n", max_dt_depth
);
1343 unsigned long entries
;
1345 sql_query("SELECT id FROM %s ORDER BY id DESC LIMIT 1", sql_table_name());
1346 if (!(res
= sql_get_result()))
1349 row
= sql_fetch_row(res
);
1350 entries
= strtoul(row
[0], NULL
, 0);
1351 sql_free_result(res
);
1354 signal(SIGINT
, SIG_IGN
);
1355 sql_query("UNLOCK TABLES");
1357 printf("Creating sql table indexes. This will likely take ~%lu seconds\n",
1358 (entries
/ 50000) + 1);
1359 sql_query("ALTER TABLE %s ENABLE KEYS", sql_table_name());
1360 printf("%lu database entries indexed in %lu seconds\n",
1361 entries
, time(NULL
) - start
);
1365 if (warnings
&& debug_level
)
1366 fprintf(stderr
, "Total warnings: %d\n", warnings
);
1368 if (debug_level
|| dt_start
!= dt_stop
) {
1370 fprintf(stderr
, "Downtime data %s\n started: %d\n stopped: %d\n delta : %d\n skipped: %d\n",
1371 dt_depth
? "mismatch!" : "consistent", dt_start
, dt_stop
, dt_depth
, dt_skip
);
1372 hash_debug_table(host_downtime
, 0);
1373 hash_debug_table(service_downtime
, 0);
1374 if ((count
= hash_entries(host_downtime
))) {
1375 fprintf(stderr
, "host_downtime as %u entries remaining\n", count
);
1377 if ((count
= hash_entries(service_downtime
))) {
1378 fprintf(stderr
, "service_downtime has %u entries remaining\n", count
);
1382 print_unhandled_events();