import: Purge expired downtime when we're done importing
[nagios-reports-module.git] / import.c
blobec990b1258bd11e6f36cf4f7ef95c8bf76d7b93c
1 #define _GNU_SOURCE 1
2 #include <sys/types.h>
3 #include <signal.h>
5 #include "nagios/broker.h"
6 #include "nagios/nebcallbacks.h"
7 #include "sql.h"
8 #include "hooks.h"
9 #include "logging.h"
10 #include "hash.h"
11 #include "lparse.h"
12 #include "logutils.h"
13 #include "cfgfile.h"
15 #define IGNORE_LINE 0
17 #define CONCERNS_HOST 50
18 #define CONCERNS_SERVICE 60
20 #define MAX_NVECS 16
21 #define HASH_TABLE_SIZE 128
23 /* for some reason these aren't defined inside Nagios' headers */
24 #define SERVICE_OK 0
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 {
53 int id;
54 int code;
55 char *host;
56 char *service;
57 time_t start;
58 time_t stop;
59 int fixed;
60 time_t duration;
61 time_t started;
62 time_t ended;
63 int purged;
64 int trigger;
65 int slot;
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[] = {
74 add_ignored("Error"),
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),
96 { 0, NULL, 0, 0 },
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),
118 { 0, NULL, 0, 0 },
122 static inline void print_strvec(char **v, int n)
124 int i;
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];
135 static int t = 0;
136 int shift = 1;
138 t ^= 1;
139 if (n < 1024) {
140 sprintf(tbuf[t], "%d bytes", n);
141 return tbuf[t];
144 while (n >> (shift * 10) > 1024)
145 shift++;
147 sprintf(tbuf[t], "%0.2f %ciB",
148 (float)n / (float)(1 << (shift * 10)), suffix[shift - 1]);
150 return tbuf[t];
153 static void show_progress(void)
155 time_t eta, elapsed;
156 float pct_done;
158 totlines += lines_since_progress;
159 lines_since_progress = 0;
161 if (!do_progress)
162 return;
164 elapsed = time(NULL) - import_start.tv_sec;
165 if (!elapsed)
166 elapsed = 1;
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));
173 if (elapsed > 10) {
174 printf("ETA: ");
175 if (eta > 60)
176 printf("%lum%lus", eta / 60, eta % 60);
177 else
178 printf("%lus", eta);
180 printf(" ");
183 static void end_progress(void)
185 struct timeval tv;
186 int mins;
187 float secs;
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%
197 imported = totsize;
199 show_progress();
200 putchar('\n');
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;
204 secs /= 1000000;
205 secs -= (mins * 60);
206 printf("%s in %u lines imported in ", tobytes(totsize), totlines);
207 if (mins)
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;
216 int result;
218 if (!is_interesting_service(host, service))
219 return 0;
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)
227 return 0;
229 memset(&ds, 0, sizeof(ds));
231 ds.type = type;
232 ds.timestamp.tv_sec = ltime;
233 ds.host_name = host;
234 ds.service_description = service;
235 ds.downtime_id = id;
237 result = hook_downtime(NEBCALLBACK_DOWNTIME_DATA, (void *)&ds);
238 if (result < 0)
239 crash("Failed to insert downtime:\n type=%d, host=%s, service=%s, id=%d",
240 type, host, service, id);
242 return result;
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) {
255 char *space, *paren;
257 space = strchr(str, ' ');
258 if (!space)
259 return -1;
260 paren = strchr(space, ')');
261 if (!paren)
262 return -1;
263 *paren = '\0';
265 state_str = space + 2;
268 n->type = SERVICE_NOTIFICATION;
269 n->state = parse_service_state_gently(state_str);
270 if (n->state < 0) {
271 n->type = HOST_NOTIFICATION;
272 n->state = parse_host_state_gently(state_str);
275 return 0;
278 static int insert_notification(struct string_code *sc)
280 int base_idx;
281 const char *desc;
282 struct import_notification n;
284 if (!only_notifications)
285 return 0;
287 if (sc->code - NEBTYPE_NOTIFICATION_END == CONCERNS_SERVICE) {
288 base_idx = 1;
289 desc = strv[2];
290 } else {
291 base_idx = 0;
292 desc = 0;
294 if (parse_import_notification(strv[base_idx + 2], &n) < 0) {
295 handle_unknown_event(strv[base_idx + 2]);
296 return 0;
299 if (!use_sql)
300 return 0;
302 return sql_query
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) "
308 "VALUES("
309 "%d, %lu, %lu, '%s', "
310 "'%s', '%s', "
311 "'%s', '%s', "
312 "%d, %d)",
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]),
317 n.state, n.reason);
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]))
325 return 0;
327 memset(&ds, 0, sizeof(ds));
329 ds.timestamp.tv_sec = ltime;
330 ds.type = sc->code;
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]);
337 else
338 ds.state = parse_service_state(strv[2]);
339 ds.state_type = HARD_STATE;
340 ds.current_attempt = 1;
341 ds.output = strv[3];
342 } else {
343 ds.state = parse_service_state(strv[2]);
344 ds.state_type = soft_hard(strv[3]);
345 ds.current_attempt = atoi(strv[4]);
346 ds.output = strv[5];
349 if (!use_sql || only_notifications)
350 return 0;
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]))
360 return 0;
362 memset(&ds, 0, sizeof(ds));
364 ds.timestamp.tv_sec = ltime;
365 ds.type = sc->code;
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]);
370 else
371 ds.state = parse_host_state(strv[1]);
372 /* passive host check result */
373 ds.output = strv[2];
374 ds.current_attempt = 1;
375 ds.state_type = HARD_STATE;
376 } else {
377 ds.state = parse_host_state(strv[1]);
378 ds.state_type = soft_hard(strv[2]);
379 ds.current_attempt = atoi(strv[3]);
380 ds.output = strv[4];
383 if (!use_sql || only_notifications)
384 return 0;
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)
394 return 0;
396 memset(&ds, 0, sizeof(ds));
397 ds.timestamp.tv_sec = ltime;
398 ds.type = type;
399 return hook_process_data(NEBCALLBACK_PROCESS_DATA, (void *)&ds);
402 static int insert_acknowledgement(struct string_code *sc)
404 return 0;
407 static void dt_print(char *tpc, time_t when, struct downtime_entry *dt)
409 if (!debug_level)
410 return;
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);
415 if (dt->service)
416 printf(";%s", dt->service);
417 putchar('\n');
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);
430 remove_downtime(dt);
433 return 0;
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))
448 return;
450 insert_downtime_event(NEBTYPE_DOWNTIME_STOP, dt->host, dt->service, dt->id);
452 if (!dt->service)
453 old = hash_remove(host_downtime, dt->host);
454 else
455 old = hash_remove2(service_downtime, dt->host, dt->service);
457 dt_print("RM_DT", ltime, dt);
458 dt->purged = 1;
461 static struct downtime_entry *
462 dt_matches_command(struct downtime_entry *dt, char *host, char *service)
464 for (; dt; dt = dt->next) {
465 time_t diff;
467 if (ltime > dt->stop || ltime < dt->start) {
468 continue;
471 switch (dt->code) {
472 case SCHEDULE_SVC_DOWNTIME:
473 if (service && strcmp(service, dt->service))
474 continue;
476 /* fallthrough */
477 case SCHEDULE_HOST_DOWNTIME:
478 case SCHEDULE_HOST_SVC_DOWNTIME:
479 if (strcmp(host, dt->host)) {
480 continue;
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 */
488 /* fallthrough */
489 case SCHEDULE_HOSTGROUP_HOST_DOWNTIME:
490 case SCHEDULE_HOSTGROUP_SVC_DOWNTIME:
491 case SCHEDULE_SERVICEGROUP_HOST_DOWNTIME:
492 case SCHEDULE_SERVICEGROUP_SVC_DOWNTIME:
493 break;
494 default:
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)
515 return dt;
518 return NULL;
521 static struct downtime_entry *
522 find_downtime_command(char *host, char *service)
524 int i;
525 struct downtime_entry *shortcut = NULL;
527 if (last_dte && last_dte->start == ltime) {
528 shortcut = last_dte;
529 // return last_dte;
531 for (i = 0; i < NUM_DENTRIES; i++) {
532 struct downtime_entry *dt;
533 dt = dt_matches_command(dentry[i], host, service);
534 if (dt) {
535 if (shortcut && dt != shortcut)
536 if (debug_level)
537 printf("FIND shortcut no good\n");
538 last_dte = dt;
539 return dt;
543 debug("FIND not\n");
544 return NULL;
547 static int print_downtime(void *data)
549 struct downtime_entry *dt = (struct downtime_entry *)data;
551 dt_print("UNCLOSED", ltime, dt);
553 return 0;
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))
570 return;
572 dt = malloc(sizeof(*dt));
573 cmd = find_downtime_command(host, service);
574 if (!cmd) {
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 */
578 dt->start = ltime;
579 dt->stop = dt->start + dt->duration;
581 else
582 memcpy(dt, cmd, sizeof(*dt));
584 dt->host = strdup(host);
585 dt->id = id;
586 dt->started = ltime;
588 set_next_dt_purge(ltime, dt->duration);
590 if (!service) {
591 dt->service = NULL;
592 old = hash_update(host_downtime, dt->host, dt);
594 else {
595 dt->service = strdup(service);
596 old = hash_update2(service_downtime, dt->host, dt->service, dt);
599 if (old && old != dt) {
600 free(old->host);
601 if (old->service)
602 free(old->service);
603 free(old);
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;
616 time_t foo;
618 switch (sc->code) {
619 case DEL_HOST_DOWNTIME:
620 last_host_dt_del = ltime;
621 return 0;
622 case DEL_SVC_DOWNTIME:
623 last_svc_dt_del = ltime;
624 return 0;
626 case SCHEDULE_HOST_DOWNTIME:
627 if (strtotimet(strv[5], &foo))
628 duration = strv[4];
629 /* fallthrough */
630 case SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME:
631 case SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME:
632 case SCHEDULE_HOST_SVC_DOWNTIME:
633 host = strv[0];
634 /* fallthrough */
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];
640 end_time = strv[2];
641 fixed = strv[3];
642 if (strtotimet(strv[5], &foo))
643 triggered_by = strv[4];
644 if (!duration)
645 duration = strv[5];
647 break;
649 case SCHEDULE_SVC_DOWNTIME:
650 host = strv[0];
651 service = strv[1];
652 start_time = strv[2];
653 end_time = strv[3];
654 fixed = strv[4];
655 if (strtotimet(strv[6], &foo)) {
656 triggered_by = strv[5];
657 duration = strv[6];
659 else {
660 duration = strv[5];
662 break;
664 default:
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));
671 dt->code = sc->code;
672 if (host)
673 dt->host = strdup(host);
674 if (service)
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)) {
702 free(dt);
703 return 0;
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;
713 dt->start = ltime;
715 if (dt->stop < ltime || dt->stop < dt->start) {
716 /* retroactively scheduled downtime, or just plain wrong */
717 dt->stop = dt->start;
718 dt->duration = 0;
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);
735 return 0;
738 static int insert_downtime(struct string_code *sc)
740 int type;
741 struct downtime_entry *dt = NULL;
742 int id = 0;
743 time_t dt_del_cmd;
744 char *host, *service = NULL;
746 host = strv[0];
747 if (sc->nvecs == 4) {
748 service = strv[1];
749 dt = hash_find2(service_downtime, host, service);
751 else
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
760 * that string
762 type = NEBTYPE_DOWNTIME_STOP;
763 if (!strcmp(strv[sc->nvecs - 2], "STARTED"))
764 type = NEBTYPE_DOWNTIME_START;
766 switch (type) {
767 case NEBTYPE_DOWNTIME_START:
768 if (dt) {
769 if (!probably_ignore_downtime)
770 dt_print("ALRDY", ltime, dt);
771 return 0;
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)
779 downtime_id++;
781 id = downtime_id;
782 add_downtime(host, service, id);
783 last_downtime_start = ltime;
784 break;
786 case NEBTYPE_DOWNTIME_STOP:
787 if (!dt) {
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");
798 dt_skip++;
799 return 0;
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);
811 remove_downtime(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
816 * frame buffer
818 del_dte = dt;
819 if (!dt->service)
820 hash_walk_data(host_downtime, del_matching_dt);
821 else
822 hash_walk_data(service_downtime, del_matching_dt);
823 break;
825 default:
826 return -1;
829 return 0;
832 static int dt_purged;
833 static int purge_expired_dt(void *data)
835 struct downtime_entry *dt = data;
837 if (dt->purged) {
838 dt_skip++;
839 return 0;
842 set_next_dt_purge(dt->started, dt->duration);
844 if (ltime + DT_PURGE_GRACETIME > dt->stop) {
845 dt_purged++;
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);
848 remove_downtime(dt);
850 else {
851 dt_print("PURGED_NOT_TIME", ltime, dt);
854 return 0;
857 static int purged_downtimes;
858 static void purge_expired_downtime(void)
860 int tot_purged = 0;
862 next_dt_purge = 0;
863 dt_purged = 0;
864 hash_walk_data(host_downtime, purge_expired_dt);
865 if (dt_purged)
866 debug("PURGE %d host downtimes purged", dt_purged);
867 tot_purged += dt_purged;
868 dt_purged = 0;
869 hash_walk_data(service_downtime, purge_expired_dt);
870 if (dt_purged)
871 debug("PURGE %d service downtimes purged", dt_purged);
872 tot_purged += dt_purged;
873 if (tot_purged)
874 debug("PURGE total %d entries purged", tot_purged);
876 if (next_dt_purge)
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;
898 daemon_stop = ltime;
901 static int parse_line(char *line, uint len)
903 char *ptr, *colon;
904 int nvecs = 0;
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 */
911 if (!len)
912 return 0;
914 if (++lines_since_progress >= PROGRESS_INTERVAL)
915 show_progress();
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);
920 return -1;
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);
926 return -1;
929 if (ltime < last_ltime) {
930 // warn("ltime < last_ltime (%lu < %lu) by %lu. Compensating...",
931 // ltime, last_ltime, last_ltime - ltime);
932 ltime = last_ltime;
934 else
935 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)
949 return 0;
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 == ' ')
958 ptr++;
960 if (!is_interesting(ptr))
961 return 0;
963 if (!(colon = strchr(ptr, ':'))) {
964 /* stupid heuristic, but might be good for something,
965 * somewhere, sometime. if nothing else, it should suppress
966 * annoying output */
967 if (is_start_event(ptr)) {
968 handle_start_event();
969 return 0;
971 if (is_stop_event(ptr)) {
972 handle_stop_event();
973 return 0;
977 * An unhandled event. We should probably crash here
979 handle_unknown_event(line);
980 return -1;
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);
992 return -1;
995 if (sc->code == IGNORE_LINE)
996 return 0;
998 *colon = 0;
999 ptr = colon + 1;
1000 while (*ptr == ' ')
1001 ptr++;
1003 if (sc->nvecs) {
1004 int i;
1006 nvecs = vectorize_string(ptr, sc->nvecs);
1008 if (nvecs != sc->nvecs) {
1009 /* broken line */
1010 warn("Line %d in %s seems to not have all the fields it should",
1011 line_no, cur_file->path);
1012 return -1;
1015 for (i = 0; i < sc->nvecs; i++) {
1016 if (!strv[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);
1020 return -1;
1025 switch (sc->code) {
1026 char *semi_colon;
1028 case NEBTYPE_EXTERNALCOMMAND_END:
1029 semi_colon = strchr(ptr, ';');
1030 if (!semi_colon)
1031 return 0;
1032 if (!(sc = get_command_type(ptr, semi_colon - ptr))) {
1033 return 0;
1035 if (sc->code == RESTART_PROGRAM) {
1036 handle_stop_event();
1037 return 0;
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);
1048 } else {
1049 insert_acknowledgement(sc);
1051 break;
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);
1067 case IGNORE_LINE:
1068 return 0;
1071 return 0;
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());
1079 return 0;
1082 static int hash_one_line(char *line, uint len)
1084 return add_interesting_object(line);
1087 static int hash_interesting(const char *path)
1089 struct stat st;
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);
1096 return 0;
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, '='))) {
1121 *opt++ = '\0';
1122 eq_opt = 1;
1124 else if (i < argc - 1) {
1125 opt = argv[i + 1];
1128 if (!prefixcmp(arg, "--incremental")) {
1129 incremental = 1;
1130 continue;
1132 if (!prefixcmp(arg, "--no-sql")) {
1133 use_sql = 0;
1134 continue;
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";
1142 continue;
1144 if (!prefixcmp(arg, "--no-progress")) {
1145 do_progress = 0;
1146 continue;
1148 if (!prefixcmp(arg, "--debug") || !prefixcmp(arg, "-d")) {
1149 do_progress = 0;
1150 debug_level++;
1151 continue;
1153 if (!prefixcmp(arg, "--truncate-db")) {
1154 truncate_db = 1;
1155 continue;
1157 if (!prefixcmp(arg, "--nagios-cfg")) {
1158 if (!opt || !*opt) {
1159 crash("%s requires the path to nagios.cfg as argument", arg);
1161 nagios_cfg = opt;
1162 if (opt && !eq_opt)
1163 i++;
1164 continue;
1166 if (!prefixcmp(arg, "--db-name")) {
1167 if (!opt || !*opt)
1168 crash("%s requires a database name as an argument", arg);
1169 db_name = opt;
1170 if (opt && !eq_opt)
1171 i++;
1172 continue;
1174 if (!prefixcmp(arg, "--db-user")) {
1175 if (!opt || !*opt)
1176 crash("%s requires a database username as argument", arg);
1177 db_user = opt;
1178 if (opt && !eq_opt)
1179 i++;
1180 continue;
1182 if (!prefixcmp(arg, "--db-pass")) {
1183 if (!opt || !*opt)
1184 crash("%s requires a database username as argument", arg);
1185 db_pass = opt;
1186 if (opt && !eq_opt)
1187 i++;
1188 continue;
1190 if (!prefixcmp(arg, "--db-table")) {
1191 if (!opt || !*opt)
1192 crash("%s requires a database table name as argument", arg);
1193 db_table = opt;
1194 if (opt && !eq_opt)
1195 i++;
1196 continue;
1198 if (!prefixcmp(arg, "--interesting") || !prefixcmp(arg, "-i")) {
1199 if (!opt || !*opt)
1200 crash("%s requires a filename as argument", arg);
1201 hash_interesting(opt);
1202 if (opt && !eq_opt)
1203 i++;
1204 continue;
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")) {
1210 nagios_cfg = arg;
1211 } else {
1212 add_naglog_path(arg);
1216 /* fallback for op5 systems */
1217 if (!nagios_cfg && !num_nfile) {
1218 nagios_cfg = "/opt/monitor/etc/nagios.cfg";
1220 if (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);
1234 if (use_sql) {
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);
1244 if (sql_init() < 0)
1245 crash("sql_init() failed");
1246 if (truncate_db)
1247 sql_query("TRUNCATE %s", sql_table_name());
1249 if (incremental) {
1250 MYSQL_RES *result;
1251 MYSQL_ROW row;
1252 sql_query("SELECT %s FROM %s.%s ORDER BY %s DESC LIMIT 1",
1253 only_notifications ? "end_time" : "timestamp",
1254 db_name, db_table,
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");
1282 if (!num_nfile)
1283 crash("Usage: %s [--incremental] [--interesting <file>] [--truncate-db] logfiles\n",
1284 __progname);
1286 if (log_init() < 0)
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];
1308 cur_file = nf;
1309 show_progress();
1310 debug("importing from %s (%lu : %u)\n", nf->path, nf->first, nf->cmp);
1311 line_no = 0;
1312 lparse_path(nf->path, nf->size, parse_one_line);
1313 imported++; /* make up for one lost byte per file */
1316 ltime = time(NULL);
1317 purge_expired_downtime();
1318 end_progress();
1320 if (debug_level) {
1321 if (dt_depth) {
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);
1339 if (use_sql) {
1340 SQL_RESULT *res;
1341 SQL_ROW row;
1342 time_t start;
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()))
1347 entries = 0;
1348 else {
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");
1356 start = time(NULL);
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);
1362 sql_close();
1365 if (warnings && debug_level)
1366 fprintf(stderr, "Total warnings: %d\n", warnings);
1368 if (debug_level || dt_start != dt_stop) {
1369 uint count;
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();
1384 return 0;