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(0, "EXTERNAL COMMAND", NEBTYPE_EXTERNALCOMMAND_END
),
84 add_code(5, "HOST ALERT", NEBTYPE_HOSTCHECK_PROCESSED
),
85 add_code(5, "INITIAL HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
86 add_code(5, "CURRENT HOST STATE", NEBTYPE_HOSTCHECK_PROCESSED
),
87 add_code(6, "SERVICE ALERT", NEBTYPE_SERVICECHECK_PROCESSED
),
88 add_code(6, "INITIAL SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
89 add_code(6, "CURRENT SERVICE STATE", NEBTYPE_SERVICECHECK_PROCESSED
),
90 add_code(3, "HOST DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
),
91 add_code(4, "SERVICE DOWNTIME ALERT", NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
),
95 static struct string_code command_codes
[] = {
96 add_cdef(1, DEL_HOST_DOWNTIME
),
97 add_cdef(1, DEL_SVC_DOWNTIME
),
98 add_cdef(8, SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
),
99 add_cdef(8, SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
),
100 add_cdef(8, SCHEDULE_HOSTGROUP_HOST_DOWNTIME
),
101 add_cdef(8, SCHEDULE_HOSTGROUP_SVC_DOWNTIME
),
102 add_cdef(8, SCHEDULE_HOST_DOWNTIME
),
103 add_cdef(8, SCHEDULE_HOST_SVC_DOWNTIME
),
104 add_cdef(8, SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
),
105 add_cdef(8, SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
),
106 add_cdef(8, SCHEDULE_SVC_DOWNTIME
),
109 * These really have one more field than listed here. We omit one
110 * to make author and comment concatenated with a semi-colon by default.
112 add_cdef(6, ACKNOWLEDGE_SVC_PROBLEM
),
113 add_cdef(5, ACKNOWLEDGE_HOST_PROBLEM
),
118 static inline void print_strvec(char **v
, int n
)
122 for (i
= 0; i
< n
; i
++)
123 printf("v[%2d]: %s\n", i
, v
[i
]);
127 static const char *tobytes(uint n
)
129 const char *suffix
= "KMGT";
130 static char tbuf
[2][30];
136 sprintf(tbuf
[t
], "%d bytes", n
);
140 while (n
>> (shift
* 10) > 1024)
143 sprintf(tbuf
[t
], "%0.2f %ciB",
144 (float)n
/ (float)(1 << (shift
* 10)), suffix
[shift
- 1]);
149 static void show_progress(void)
154 totlines
+= lines_since_progress
;
155 lines_since_progress
= 0;
160 elapsed
= time(NULL
) - import_start
.tv_sec
;
164 pct_done
= ((float)imported
/ (float)totsize
) * 100;
165 eta
= (elapsed
/ pct_done
) * (100.0 - pct_done
);
167 printf("\rImporting data: %.2f%% (%s) done ",
168 pct_done
, tobytes(imported
));
172 printf("%lum%lus", eta
/ 60, eta
% 60);
179 static void end_progress(void)
185 gettimeofday(&tv
, NULL
);
188 * If any of the logfiles doesn't have a newline
189 * at end of file, imported will be slightly off.
190 * We set it hard here so as to make sure that
191 * the final progress output stops at exactly 100%
197 secs
= (tv
.tv_sec
- import_start
.tv_sec
) * 1000000;
198 secs
+= tv
.tv_usec
- import_start
.tv_usec
;
199 mins
= (tv
.tv_sec
- import_start
.tv_sec
) / 60;
202 printf("%s in %u lines imported in ", tobytes(totsize
), totlines
);
204 printf("%dm ", mins
);
205 printf("%.3fs\n", secs
);
208 static int use_sql
= 1;
209 static int insert_downtime_event(int type
, char *host
, char *service
, int id
)
211 nebstruct_downtime_data ds
;
214 if (!is_interesting_service(host
, service
))
217 dt_start
+= type
== NEBTYPE_DOWNTIME_START
;
218 dt_stop
+= type
== NEBTYPE_DOWNTIME_STOP
;
219 if (dt_depth
> max_dt_depth
)
220 max_dt_depth
= dt_depth
;
225 memset(&ds
, 0, sizeof(ds
));
228 ds
.timestamp
.tv_sec
= ltime
;
230 ds
.service_description
= service
;
233 result
= hook_downtime(NEBCALLBACK_DOWNTIME_DATA
, (void *)&ds
);
235 crash("Failed to insert downtime:\n type=%d, host=%s, service=%s, id=%d",
236 type
, host
, service
, id
);
241 static int insert_service_check(struct string_code
*sc
)
243 nebstruct_service_check_data ds
;
248 if (!is_interesting_service(strv
[0], strv
[1]))
251 memset(&ds
, 0, sizeof(ds
));
253 ds
.timestamp
.tv_sec
= ltime
;
255 ds
.host_name
= strv
[0];
256 ds
.service_description
= strv
[1];
257 ds
.state
= parse_service_state(strv
[2]);
258 ds
.state_type
= soft_hard(strv
[3]);
259 ds
.current_attempt
= atoi(strv
[4]);
262 return hook_service_result(NEBCALLBACK_SERVICE_CHECK_DATA
, (void *)&ds
);
265 static int insert_host_check(struct string_code
*sc
)
267 nebstruct_host_check_data ds
;
272 if (!is_interesting_host(strv
[0]))
275 memset(&ds
, 0, sizeof(ds
));
277 ds
.timestamp
.tv_sec
= ltime
;
279 ds
.host_name
= strv
[0];
280 ds
.state
= parse_host_state(strv
[1]);
281 ds
.state_type
= soft_hard(strv
[2]);
282 ds
.current_attempt
= atoi(strv
[3]);
285 return hook_host_result(NEBCALLBACK_HOST_CHECK_DATA
, (void *)&ds
);
288 static int insert_process_event(int type
)
290 nebstruct_process_data ds
;
295 memset(&ds
, 0, sizeof(ds
));
296 ds
.timestamp
.tv_sec
= ltime
;
298 return hook_process_data(NEBCALLBACK_PROCESS_DATA
, (void *)&ds
);
301 static int insert_acknowledgement(struct string_code
*sc
)
306 static void dt_print(char *tpc
, time_t when
, struct downtime_entry
*dt
)
311 printf("%s: time=%lu started=%lu start=%lu stop=%lu duration=%lu id=%d ",
312 tpc
, when
, dt
->started
, dt
->start
, dt
->stop
, dt
->duration
, dt
->id
);
313 printf("%s", dt
->host
);
315 printf(";%s", dt
->service
);
319 static struct downtime_entry
*last_dte
;
320 static struct downtime_entry
*del_dte
;
322 static void remove_downtime(struct downtime_entry
*dt
);
323 static int del_matching_dt(void *data
)
325 struct downtime_entry
*dt
= data
;
327 if (del_dte
->id
== dt
->id
) {
328 dt_print("ALSO", 0, dt
);
335 static void stash_downtime_command(struct downtime_entry
*dt
)
337 dt
->slot
= dt
->start
% NUM_DENTRIES
;
338 dt
->next
= dentry
[dt
->slot
];
339 dentry
[dt
->slot
] = dt
;
342 static void remove_downtime(struct downtime_entry
*dt
)
344 struct downtime_entry
*old
;
346 if (!is_interesting_service(dt
->host
, dt
->service
))
349 insert_downtime_event(NEBTYPE_DOWNTIME_STOP
, dt
->host
, dt
->service
, dt
->id
);
352 old
= hash_remove(host_downtime
, dt
->host
);
354 old
= hash_remove2(service_downtime
, dt
->host
, dt
->service
);
356 dt_print("RM_DT", ltime
, dt
);
360 static struct downtime_entry
*
361 dt_matches_command(struct downtime_entry
*dt
, char *host
, char *service
)
363 for (; dt
; dt
= dt
->next
) {
366 if (ltime
> dt
->stop
|| ltime
< dt
->start
) {
371 case SCHEDULE_SVC_DOWNTIME
:
372 if (service
&& strcmp(service
, dt
->service
))
376 case SCHEDULE_HOST_DOWNTIME
:
377 case SCHEDULE_HOST_SVC_DOWNTIME
:
378 if (strcmp(host
, dt
->host
)) {
382 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
383 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
384 /* these two have host set in dt, but
385 * it will not match all the possible hosts */
388 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
389 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
390 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
391 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
394 crash("dt->code not set properly\n");
398 * Once we get here all the various other criteria have
399 * been matched, so we need to check if the daemon was
400 * running when this downtime was supposed to have
401 * started, and otherwise use the daemon start time
402 * as the value to diff against
404 if (daemon_stop
< dt
->start
&& daemon_start
> dt
->start
) {
405 debug("Adjusting dt->start (%lu) to (%lu)\n",
406 dt
->start
, daemon_start
);
407 dt
->start
= daemon_start
;
408 if (dt
->trigger
&& dt
->duration
)
409 dt
->stop
= dt
->start
+ dt
->duration
;
412 diff
= ltime
- dt
->start
;
413 if (diff
< 3 || dt
->trigger
|| !dt
->fixed
)
420 static struct downtime_entry
*
421 find_downtime_command(char *host
, char *service
)
424 struct downtime_entry
*shortcut
= NULL
;
426 if (last_dte
&& last_dte
->start
== ltime
) {
430 for (i
= 0; i
< NUM_DENTRIES
; i
++) {
431 struct downtime_entry
*dt
;
432 dt
= dt_matches_command(dentry
[i
], host
, service
);
434 if (shortcut
&& dt
!= shortcut
)
436 printf("FIND shortcut no good\n");
446 static int print_downtime(void *data
)
448 struct downtime_entry
*dt
= (struct downtime_entry
*)data
;
450 dt_print("UNCLOSED", ltime
, dt
);
455 static inline void set_next_dt_purge(time_t base
, time_t add
)
457 if (!next_dt_purge
|| next_dt_purge
> base
+ add
)
458 next_dt_purge
= base
+ add
;
460 if (next_dt_purge
<= ltime
)
461 next_dt_purge
= ltime
+ 1;
464 static inline void add_downtime(char *host
, char *service
, int id
)
466 struct downtime_entry
*dt
, *cmd
, *old
;
468 if (!is_interesting_service(host
, service
))
471 dt
= malloc(sizeof(*dt
));
472 cmd
= find_downtime_command(host
, service
);
474 warn("DT with no ext cmd? %lu %s;%s", ltime
, host
, service
);
475 memset(dt
, 0, sizeof(*dt
));
476 dt
->duration
= 7200; /* the default downtime duration in nagios */
478 dt
->stop
= dt
->start
+ dt
->duration
;
481 memcpy(dt
, cmd
, sizeof(*dt
));
483 dt
->host
= strdup(host
);
487 set_next_dt_purge(ltime
, dt
->duration
);
491 old
= hash_update(host_downtime
, dt
->host
, dt
);
494 dt
->service
= strdup(service
);
495 old
= hash_update2(service_downtime
, dt
->host
, dt
->service
, dt
);
498 if (old
&& old
!= dt
) {
505 dt_print("IN_DT", ltime
, dt
);
506 insert_downtime_event(NEBTYPE_DOWNTIME_START
, dt
->host
, dt
->service
, dt
->id
);
509 static time_t last_host_dt_del
, last_svc_dt_del
;
510 static int register_downtime_command(struct string_code
*sc
)
512 struct downtime_entry
*dt
;
513 char *start_time
, *end_time
, *duration
= NULL
;
514 char *host
= NULL
, *service
= NULL
, *fixed
, *triggered_by
= NULL
;
518 case DEL_HOST_DOWNTIME
:
519 last_host_dt_del
= ltime
;
521 case DEL_SVC_DOWNTIME
:
522 last_svc_dt_del
= ltime
;
525 case SCHEDULE_HOST_DOWNTIME
:
526 if (strtotimet(strv
[5], &foo
))
529 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME
:
530 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME
:
531 case SCHEDULE_HOST_SVC_DOWNTIME
:
534 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME
:
535 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME
:
536 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME
:
537 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME
:
538 start_time
= strv
[1];
541 if (strtotimet(strv
[5], &foo
))
542 triggered_by
= strv
[4];
548 case SCHEDULE_SVC_DOWNTIME
:
551 start_time
= strv
[2];
554 if (strtotimet(strv
[6], &foo
)) {
555 triggered_by
= strv
[5];
564 crash("Unknown downtime type: %d", sc
->code
);
567 if (!(dt
= calloc(sizeof(*dt
), 1)))
568 crash("calloc(%u, 1) failed: %s", (uint
)sizeof(*dt
), strerror(errno
));
572 dt
->host
= strdup(host
);
574 dt
->service
= strdup(service
);
576 dt
->trigger
= triggered_by
? !!(*triggered_by
- '0') : 0;
577 if (strtotimet(start_time
, &dt
->start
) || strtotimet(end_time
, &dt
->stop
))
579 print_strvec(strv
, sc
->nvecs
);
580 crash("strtotime(): type: %s; start_time='%s'; end_time='%s'; duration='%s';",
581 command_codes
[sc
->code
- 1].str
, start_time
, end_time
, duration
);
585 * sometimes downtime commands can be logged according to
586 * log version 1, while the log still claims to be version 2.
587 * Apparently, this happens when using a daemon supporting
588 * version 2 logging but a downtime command is added that
589 * follows the version 1 standard.
590 * As such, we simply ignore the result of the "duration"
591 * field conversion and just accept that it might not work
593 (void)strtotimet(duration
, &dt
->duration
);
594 dt
->fixed
= *fixed
- '0';
597 * ignore downtime scheduled to take place in the future.
598 * It will be picked up by the module anyways
600 if (dt
->start
> time(NULL
)) {
605 if (dt
->duration
> time(NULL
)) {
606 warn("Bizarrely large duration (%lu)", dt
->duration
);
608 if (dt
->start
< ltime
) {
609 if (dt
->duration
&& dt
->duration
> ltime
- dt
->start
)
610 dt
->duration
-= ltime
- dt
->start
;
614 if (dt
->stop
< ltime
|| dt
->stop
< dt
->start
) {
615 /* retroactively scheduled downtime, or just plain wrong */
616 dt
->stop
= dt
->start
;
620 if (dt
->fixed
&& dt
->duration
!= dt
->stop
- dt
->start
) {
621 // warn("duration doesn't match stop - start: (%lu : %lu)",
622 // dt->duration, dt->stop - dt->start);
624 dt
->duration
= dt
->stop
- dt
->start
;
626 else if (dt
->duration
> 86400 * 14) {
627 warn("Oddly long duration: %lu", dt
->duration
);
630 debug("start=%lu; stop=%lu; duration=%lu; fixed=%d; trigger=%d; host=%s service=%s\n",
631 dt
->start
, dt
->stop
, dt
->duration
, dt
->fixed
, dt
->trigger
, dt
->host
, dt
->service
);
633 stash_downtime_command(dt
);
637 static int insert_downtime(struct string_code
*sc
)
640 struct downtime_entry
*dt
= NULL
;
643 char *host
, *service
= NULL
;
646 if (sc
->nvecs
== 4) {
648 dt
= hash_find2(service_downtime
, host
, service
);
651 dt
= hash_find(host_downtime
, host
);
654 * to stop a downtime we can either get STOPPED or
655 * CANCELLED. So far, I've only ever seen STARTED
656 * for when it actually starts though, and since
657 * the Nagios daemon is reponsible for launching
658 * it, it's unlikely there are more variants of
661 type
= NEBTYPE_DOWNTIME_STOP
;
662 if (!strcmp(strv
[sc
->nvecs
- 2], "STARTED"))
663 type
= NEBTYPE_DOWNTIME_START
;
666 case NEBTYPE_DOWNTIME_START
:
668 if (!probably_ignore_downtime
)
669 dt_print("ALRDY", ltime
, dt
);
673 if (probably_ignore_downtime
)
674 debug("Should probably ignore this downtime: %lu : %lu %s;%s\n",
675 probably_ignore_downtime
, ltime
, host
, service
);
677 if (ltime
- last_downtime_start
> 1)
681 add_downtime(host
, service
, id
);
682 last_downtime_start
= ltime
;
685 case NEBTYPE_DOWNTIME_STOP
:
688 * this can happen when overlapping downtime entries
689 * occur, and the start event for the second (or nth)
690 * downtime starts before the first downtime has had
691 * a stop event. It basically means we've almost
692 * certainly done something wrong.
694 //printf("no dt. ds.host_name == '%s'\n", ds.host_name);
695 //fprintf(stderr, "CRASHING: %s;%s\n", ds.host_name, ds.service_description);
696 //crash("DOWNTIME_STOP without matching DOWNTIME_START");
700 dt_del_cmd
= !dt
->service
? last_host_dt_del
: last_svc_dt_del
;
702 if ((ltime
- dt_del_cmd
) > 1 && dt
->duration
- (ltime
- dt
->started
) > 60) {
703 debug("Short dt duration (%lu) for %s;%s (dt->duration=%lu)\n",
704 ltime
- dt
->started
, dt
->host
, dt
->service
, dt
->duration
);
706 if (ltime
- dt
->started
> dt
->duration
+ DT_PURGE_GRACETIME
)
707 dt_print("Long", ltime
, dt
);
711 * Now delete whatever matching downtimes we can find.
712 * this must be here, or we'll recurse like crazy into
713 * remove_downtime(), possibly exhausting the stack
718 hash_walk_data(host_downtime
, del_matching_dt
);
720 hash_walk_data(service_downtime
, del_matching_dt
);
730 static int dt_purged
;
731 static int purge_expired_dt(void *data
)
733 struct downtime_entry
*dt
= data
;
739 if (ltime
+ DT_PURGE_GRACETIME
> dt
->stop
) {
741 debug("PURGE %lu: purging expired dt %d (start=%lu; started=%lu; stop=%lu; duration=%lu; host=%s; service=%s",
742 ltime
, dt
->id
, dt
->start
, dt
->started
, dt
->stop
, dt
->duration
, dt
->host
, dt
->service
);
746 dt_print("PURGED_NOT_TIME", ltime
, dt
);
749 set_next_dt_purge(dt
->started
, dt
->duration
);
754 static int purged_downtimes
;
755 static void purge_expired_downtime(void)
761 hash_walk_data(host_downtime
, purge_expired_dt
);
763 debug("PURGE %d host downtimes purged", dt_purged
);
764 tot_purged
+= dt_purged
;
766 hash_walk_data(service_downtime
, purge_expired_dt
);
768 debug("PURGE %d service downtimes purged", dt_purged
);
769 tot_purged
+= dt_purged
;
771 debug("PURGE total %d entries purged", tot_purged
);
774 debug("PURGE next downtime purge supposed to run @ %lu, in %lu seconds",
775 next_dt_purge
, next_dt_purge
- ltime
);
777 purged_downtimes
+= tot_purged
;
780 static inline void handle_start_event(void)
782 if (!daemon_is_running
)
783 insert_process_event(NEBTYPE_PROCESS_START
);
785 probably_ignore_downtime
= daemon_start
= ltime
;
786 daemon_is_running
= 1;
789 static inline void handle_stop_event(void)
791 if (daemon_is_running
) {
792 insert_process_event(NEBTYPE_PROCESS_SHUTDOWN
);
793 daemon_is_running
= 0;
798 static int parse_line(char *line
, uint len
)
802 struct string_code
*sc
;
803 static time_t last_ltime
= 0;
805 imported
+= len
+ 1; /* make up for 1 lost byte per newline */
807 /* ignore empty lines */
811 if (++lines_since_progress
>= PROGRESS_INTERVAL
)
814 /* skip obviously bogus lines */
815 if (len
< 12 || *line
!= '[') {
816 warn("line %d; len too short, or line doesn't start with '[' (%s)", line_no
, line
);
820 ltime
= strtoul(line
+ 1, &ptr
, 10);
821 if (line
+ 1 == ptr
) {
822 crash("Failed to parse log timestamp from '%s'. I can't handle malformed logdata", line
);
826 if (ltime
< last_ltime
) {
827 // warn("ltime < last_ltime (%lu < %lu) by %lu. Compensating...",
828 // ltime, last_ltime, last_ltime - ltime);
835 * Incremental will be 0 if not set, or 1 if set but
836 * the database is currently empty.
837 * Note that this will not always do the correct thing,
838 * as downtime entries that might have been scheduled for
839 * purging may never show up as "stopped" in the database
840 * with this scheme. As such, incremental imports absolutely
841 * require that nothing is in scheduled downtime when the
842 * import is running (well, started really, but it amounts
843 * to the same thing).
845 if (ltime
< incremental
)
848 if (next_dt_purge
&& ltime
>= next_dt_purge
)
849 purge_expired_downtime();
851 if (probably_ignore_downtime
&& ltime
- probably_ignore_downtime
> 1)
852 probably_ignore_downtime
= 0;
854 while (*ptr
== ']' || *ptr
== ' ')
857 if (!is_interesting(ptr
))
860 if (!(colon
= strchr(ptr
, ':'))) {
861 /* stupid heuristic, but might be good for something,
862 * somewhere, sometime. if nothing else, it should suppress
864 if (is_start_event(ptr
)) {
865 handle_start_event();
868 if (is_stop_event(ptr
)) {
874 * An unhandled event. We should probably crash here
876 handle_unknown_event(line
);
880 /* an event happened without us having gotten a start-event */
881 if (!daemon_is_running
) {
882 insert_process_event(NEBTYPE_PROCESS_START
);
883 daemon_start
= ltime
;
884 daemon_is_running
= 1;
887 if (!(sc
= get_event_type(ptr
, colon
- ptr
))) {
888 handle_unknown_event(line
);
892 if (sc
->code
== IGNORE_LINE
)
903 nvecs
= vectorize_string(ptr
, sc
->nvecs
);
905 if (nvecs
!= sc
->nvecs
) {
907 warn("Line %d in %s seems to not have all the fields it should",
908 line_no
, cur_file
->path
);
912 for (i
= 0; i
< sc
->nvecs
; i
++) {
914 /* this should never happen */
915 warn("Line %d in %s seems to be broken, or we failed to parse it into a vector",
916 line_no
, cur_file
->path
);
925 case NEBTYPE_EXTERNALCOMMAND_END
:
926 semi_colon
= strchr(ptr
, ';');
929 if (!(sc
= get_command_type(ptr
, semi_colon
- ptr
))) {
932 if (sc
->code
== RESTART_PROGRAM
) {
937 nvecs
= vectorize_string(semi_colon
+ 1, sc
->nvecs
);
938 if (nvecs
!= sc
->nvecs
) {
939 warn("nvecs discrepancy: %d vs %d (%s)\n", nvecs
, sc
->nvecs
, ptr
);
941 if (sc
->code
!= ACKNOWLEDGE_HOST_PROBLEM
&&
942 sc
->code
!= ACKNOWLEDGE_SVC_PROBLEM
)
944 register_downtime_command(sc
);
946 insert_acknowledgement(sc
);
950 case NEBTYPE_HOSTCHECK_PROCESSED
:
951 return insert_host_check(sc
);
953 case NEBTYPE_SERVICECHECK_PROCESSED
:
954 return insert_service_check(sc
);
956 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_HOST
:
957 case NEBTYPE_DOWNTIME_LOAD
+ CONCERNS_SERVICE
:
958 return insert_downtime(sc
);
967 static int parse_one_line(char *str
, uint len
)
969 if (parse_line(str
, len
) && use_sql
&& sql_errno())
970 crash("sql error: %s", sql_error());
975 static int hash_one_line(char *line
, uint len
)
977 return add_interesting_object(line
);
980 static int hash_interesting(const char *path
)
984 if (stat(path
, &st
) < 0)
985 crash("failed to stat %s: %s", path
, strerror(errno
));
987 lparse_path(path
, st
.st_size
, hash_one_line
);
992 extern const char *__progname
;
993 int main(int argc
, char **argv
)
995 int i
, truncate_db
= 0;
996 struct naglog_file
*nfile
;
997 char *db_name
= "monitor_reports";
998 char *db_user
= "monitor";
999 char *db_pass
= "monitor";
1000 char *db_table
= "report_data";
1002 do_progress
= isatty(fileno(stdout
));
1004 strv
= calloc(sizeof(char *), MAX_NVECS
);
1005 nfile
= calloc(sizeof(*nfile
), argc
- 1);
1006 dentry
= calloc(sizeof(*dentry
), NUM_DENTRIES
);
1007 if (!strv
|| !nfile
|| !dentry
)
1008 crash("Failed to alloc initial structs");
1011 for (num_nfile
= 0,i
= 1; i
< argc
; i
++) {
1012 char *opt
, *arg
= argv
[i
];
1013 struct naglog_file
*nf
;
1016 if ((opt
= strchr(arg
, '='))) {
1020 else if (i
< argc
- 1) {
1024 if (!prefixcmp(arg
, "--incremental")) {
1028 if (!prefixcmp(arg
, "--no-sql")) {
1032 if (!prefixcmp(arg
, "--no-progress")) {
1036 if (!prefixcmp(arg
, "--debug") || !prefixcmp(arg
, "-d")) {
1041 if (!prefixcmp(arg
, "--truncate-db")) {
1045 if (!prefixcmp(arg
, "--db-name")) {
1047 crash("%s requires a database name as an argument", arg
);
1053 if (!prefixcmp(arg
, "--db-user")) {
1055 crash("%s requires a database username as argument", arg
);
1061 if (!prefixcmp(arg
, "--db-pass")) {
1063 crash("%s requires a database username as argument", arg
);
1069 if (!prefixcmp(arg
, "--db-table")) {
1071 crash("%s requires a database table name as argument", arg
);
1077 if (!prefixcmp(arg
, "--interesting") || !prefixcmp(arg
, "-i")) {
1079 crash("%s requires a filename as argument", arg
);
1080 hash_interesting(opt
);
1086 /* non-argument, so treat as file */
1087 nf
= &nfile
[num_nfile
++];
1090 totsize
+= nf
->size
;
1094 sql_config("db_database", db_name
);
1095 sql_config("db_user", db_user
);
1096 sql_config("db_pass", db_pass
);
1097 sql_config("db_table", db_table
);
1100 crash("sql_init() failed");
1102 sql_query("TRUNCATE %s", sql_table_name());
1107 sql_query("SELECT timestamp FROM %s.%s ORDER BY timestamp DESC LIMIT 1",
1110 if (!(result
= sql_get_result()))
1111 crash("Failed to get last timestamp: %s\n", sql_error());
1113 /* someone might use --incremental with an empty
1114 * database. We shouldn't crash in that case */
1115 if ((row
= sql_fetch_row(result
)))
1116 incremental
= strtoul(row
[0], NULL
, 0);
1118 sql_free_result(result
);
1121 * We lock the table we'll be working with and disable
1122 * indexes on it. Otherwise doing the actual inserts
1123 * will take just about forever, as MySQL has to update
1124 * and flush the index cache between each operation.
1126 if (sql_query("ALTER TABLE %s DISABLE KEYS", sql_table_name()))
1127 crash("Failed to disable keys: %s", sql_error());
1128 if (sql_query("LOCK TABLES %s WRITE", sql_table_name()))
1129 crash("Failed to lock report_data table: %s", sql_error());
1132 log_grok_var("logfile", "/dev/null");
1133 log_grok_var("log_levels", "warn");
1136 crash("Usage: %s [--incremental] [--interesting <file>] [--truncate-db] logfiles\n",
1140 crash("log_init() failed");
1142 qsort(nfile
, num_nfile
, sizeof(*nfile
), nfile_cmp
);
1144 host_downtime
= hash_init(HASH_TABLE_SIZE
);
1145 service_downtime
= hash_init(HASH_TABLE_SIZE
);
1147 if (hook_init() < 0)
1148 crash("Failed to initialize hooks");
1150 gettimeofday(&import_start
, NULL
);
1151 printf("Importing %s of data from %d files\n",
1152 tobytes(totsize
), num_nfile
);
1154 for (i
= 0; i
< num_nfile
; i
++) {
1155 struct naglog_file
*nf
= &nfile
[i
];
1158 debug("importing from %s (%lu : %u)\n", nf
->path
, nf
->first
, nf
->cmp
);
1160 lparse_path(nf
->path
, nf
->size
, parse_one_line
);
1161 imported
++; /* make up for one lost byte per file */
1168 printf("Unclosed host downtimes:\n");
1169 puts("------------------------");
1170 hash_walk_data(host_downtime
, print_downtime
);
1171 printf("Unclosed service downtimes:\n");
1172 puts("---------------------------");
1173 hash_walk_data(service_downtime
, print_downtime
);
1175 printf("dt_depth: %d\n", dt_depth
);
1177 printf("purged downtimes: %d\n", purged_downtimes
);
1178 printf("max simultaneous host downtime hashes: %u\n",
1179 hash_get_max_entries(host_downtime
));
1180 printf("max simultaneous service downtime hashes: %u\n",
1181 hash_get_max_entries(service_downtime
));
1182 printf("max downtime depth: %u\n", max_dt_depth
);
1189 unsigned long entries
;
1191 sql_query("SELECT id FROM report_data ORDER BY id DESC LIMIT 1");
1192 if (!(res
= sql_get_result()))
1195 row
= sql_fetch_row(res
);
1196 entries
= strtoul(row
[0], NULL
, 0);
1197 sql_free_result(res
);
1200 signal(SIGINT
, SIG_IGN
);
1201 sql_query("UNLOCK TABLES");
1203 printf("Creating sql table indexes. This will likely take ~%lu seconds\n",
1204 (entries
/ 50000) + 1);
1205 sql_query("ALTER TABLE %s ENABLE KEYS", sql_table_name());
1206 printf("%lu database entries indexed in %lu seconds\n",
1207 entries
, time(NULL
) - start
);
1211 if (warnings
&& debug_level
)
1212 fprintf(stderr
, "Total warnings: %d\n", warnings
);
1214 if (debug_level
|| dt_start
!= dt_stop
)
1215 fprintf(stderr
, "Downtime data %s\n started: %d\n stopped: %d\n",
1216 dt_depth
? "mismatch!" : "consistent", dt_start
, dt_stop
);
1217 if (hash_check_table(host_downtime
))
1218 fprintf(stderr
, "Hash table inconsistencies for host_downtime\n");
1219 if (hash_check_table(service_downtime
))
1220 fprintf(stderr
, "Hash table inconsistencies for service_downtime\n");
1222 print_unhandled_events();