import: Make sure desc is always initialized properly
[nagios-reports-module.git] / import.c
bloba870eeeda2a6cd8f605cfba563ad4ca5f493395a
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;
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 return 0;
801 dt_del_cmd = !dt->service ? last_host_dt_del : last_svc_dt_del;
803 if ((ltime - dt_del_cmd) > 1 && dt->duration - (ltime - dt->started) > 60) {
804 debug("Short dt duration (%lu) for %s;%s (dt->duration=%lu)\n",
805 ltime - dt->started, dt->host, dt->service, dt->duration);
807 if (ltime - dt->started > dt->duration + DT_PURGE_GRACETIME)
808 dt_print("Long", ltime, dt);
810 remove_downtime(dt);
812 * Now delete whatever matching downtimes we can find.
813 * this must be here, or we'll recurse like crazy into
814 * remove_downtime(), possibly exhausting the stack
815 * frame buffer
817 del_dte = dt;
818 if (!dt->service)
819 hash_walk_data(host_downtime, del_matching_dt);
820 else
821 hash_walk_data(service_downtime, del_matching_dt);
822 break;
824 default:
825 return -1;
828 return 0;
831 static int dt_purged;
832 static int purge_expired_dt(void *data)
834 struct downtime_entry *dt = data;
836 if (dt->purged) {
837 return 0;
840 if (ltime + DT_PURGE_GRACETIME > dt->stop) {
841 dt_purged++;
842 debug("PURGE %lu: purging expired dt %d (start=%lu; started=%lu; stop=%lu; duration=%lu; host=%s; service=%s",
843 ltime, dt->id, dt->start, dt->started, dt->stop, dt->duration, dt->host, dt->service);
844 remove_downtime(dt);
846 else {
847 dt_print("PURGED_NOT_TIME", ltime, dt);
850 set_next_dt_purge(dt->started, dt->duration);
852 return 0;
855 static int purged_downtimes;
856 static void purge_expired_downtime(void)
858 int tot_purged = 0;
860 next_dt_purge = 0;
861 dt_purged = 0;
862 hash_walk_data(host_downtime, purge_expired_dt);
863 if (dt_purged)
864 debug("PURGE %d host downtimes purged", dt_purged);
865 tot_purged += dt_purged;
866 dt_purged = 0;
867 hash_walk_data(service_downtime, purge_expired_dt);
868 if (dt_purged)
869 debug("PURGE %d service downtimes purged", dt_purged);
870 tot_purged += dt_purged;
871 if (tot_purged)
872 debug("PURGE total %d entries purged", tot_purged);
874 if (next_dt_purge)
875 debug("PURGE next downtime purge supposed to run @ %lu, in %lu seconds",
876 next_dt_purge, next_dt_purge - ltime);
878 purged_downtimes += tot_purged;
881 static inline void handle_start_event(void)
883 if (!daemon_is_running)
884 insert_process_event(NEBTYPE_PROCESS_START);
886 probably_ignore_downtime = daemon_start = ltime;
887 daemon_is_running = 1;
890 static inline void handle_stop_event(void)
892 if (daemon_is_running) {
893 insert_process_event(NEBTYPE_PROCESS_SHUTDOWN);
894 daemon_is_running = 0;
896 daemon_stop = ltime;
899 static int parse_line(char *line, uint len)
901 char *ptr, *colon;
902 int nvecs = 0;
903 struct string_code *sc;
904 static time_t last_ltime = 0;
906 imported += len + 1; /* make up for 1 lost byte per newline */
908 /* ignore empty lines */
909 if (!len)
910 return 0;
912 if (++lines_since_progress >= PROGRESS_INTERVAL)
913 show_progress();
915 /* skip obviously bogus lines */
916 if (len < 12 || *line != '[') {
917 warn("line %d; len too short, or line doesn't start with '[' (%s)", line_no, line);
918 return -1;
921 ltime = strtoul(line + 1, &ptr, 10);
922 if (line + 1 == ptr) {
923 crash("Failed to parse log timestamp from '%s'. I can't handle malformed logdata", line);
924 return -1;
927 if (ltime < last_ltime) {
928 // warn("ltime < last_ltime (%lu < %lu) by %lu. Compensating...",
929 // ltime, last_ltime, last_ltime - ltime);
930 ltime = last_ltime;
932 else
933 last_ltime = ltime;
936 * Incremental will be 0 if not set, or 1 if set but
937 * the database is currently empty.
938 * Note that this will not always do the correct thing,
939 * as downtime entries that might have been scheduled for
940 * purging may never show up as "stopped" in the database
941 * with this scheme. As such, incremental imports absolutely
942 * require that nothing is in scheduled downtime when the
943 * import is running (well, started really, but it amounts
944 * to the same thing).
946 if (ltime < incremental)
947 return 0;
949 if (next_dt_purge && ltime >= next_dt_purge)
950 purge_expired_downtime();
952 if (probably_ignore_downtime && ltime - probably_ignore_downtime > 1)
953 probably_ignore_downtime = 0;
955 while (*ptr == ']' || *ptr == ' ')
956 ptr++;
958 if (!is_interesting(ptr))
959 return 0;
961 if (!(colon = strchr(ptr, ':'))) {
962 /* stupid heuristic, but might be good for something,
963 * somewhere, sometime. if nothing else, it should suppress
964 * annoying output */
965 if (is_start_event(ptr)) {
966 handle_start_event();
967 return 0;
969 if (is_stop_event(ptr)) {
970 handle_stop_event();
971 return 0;
975 * An unhandled event. We should probably crash here
977 handle_unknown_event(line);
978 return -1;
981 /* an event happened without us having gotten a start-event */
982 if (!daemon_is_running) {
983 insert_process_event(NEBTYPE_PROCESS_START);
984 daemon_start = ltime;
985 daemon_is_running = 1;
988 if (!(sc = get_event_type(ptr, colon - ptr))) {
989 handle_unknown_event(line);
990 return -1;
993 if (sc->code == IGNORE_LINE)
994 return 0;
996 *colon = 0;
997 ptr = colon + 1;
998 while (*ptr == ' ')
999 ptr++;
1001 if (sc->nvecs) {
1002 int i;
1004 nvecs = vectorize_string(ptr, sc->nvecs);
1006 if (nvecs != sc->nvecs) {
1007 /* broken line */
1008 warn("Line %d in %s seems to not have all the fields it should",
1009 line_no, cur_file->path);
1010 return -1;
1013 for (i = 0; i < sc->nvecs; i++) {
1014 if (!strv[i]) {
1015 /* this should never happen */
1016 warn("Line %d in %s seems to be broken, or we failed to parse it into a vector",
1017 line_no, cur_file->path);
1018 return -1;
1023 switch (sc->code) {
1024 char *semi_colon;
1026 case NEBTYPE_EXTERNALCOMMAND_END:
1027 semi_colon = strchr(ptr, ';');
1028 if (!semi_colon)
1029 return 0;
1030 if (!(sc = get_command_type(ptr, semi_colon - ptr))) {
1031 return 0;
1033 if (sc->code == RESTART_PROGRAM) {
1034 handle_stop_event();
1035 return 0;
1038 nvecs = vectorize_string(semi_colon + 1, sc->nvecs);
1039 if (nvecs != sc->nvecs) {
1040 warn("nvecs discrepancy: %d vs %d (%s)\n", nvecs, sc->nvecs, ptr);
1042 if (sc->code != ACKNOWLEDGE_HOST_PROBLEM &&
1043 sc->code != ACKNOWLEDGE_SVC_PROBLEM)
1045 register_downtime_command(sc);
1046 } else {
1047 insert_acknowledgement(sc);
1049 break;
1051 case NEBTYPE_HOSTCHECK_PROCESSED:
1052 return insert_host_check(sc);
1054 case NEBTYPE_SERVICECHECK_PROCESSED:
1055 return insert_service_check(sc);
1057 case NEBTYPE_DOWNTIME_LOAD + CONCERNS_HOST:
1058 case NEBTYPE_DOWNTIME_LOAD + CONCERNS_SERVICE:
1059 return insert_downtime(sc);
1061 case NEBTYPE_NOTIFICATION_END + CONCERNS_HOST:
1062 case NEBTYPE_NOTIFICATION_END + CONCERNS_SERVICE:
1063 return insert_notification(sc);
1065 case IGNORE_LINE:
1066 return 0;
1069 return 0;
1072 static int parse_one_line(char *str, uint len)
1074 if (parse_line(str, len) && use_sql && sql_errno())
1075 crash("sql error: %s", sql_error());
1077 return 0;
1080 static int hash_one_line(char *line, uint len)
1082 return add_interesting_object(line);
1085 static int hash_interesting(const char *path)
1087 struct stat st;
1089 if (stat(path, &st) < 0)
1090 crash("failed to stat %s: %s", path, strerror(errno));
1092 lparse_path(path, st.st_size, hash_one_line);
1094 return 0;
1097 extern const char *__progname;
1098 int main(int argc, char **argv)
1100 int i, truncate_db = 0;
1101 const char *nagios_cfg = NULL;
1102 char *db_name, *db_user, *db_pass, *db_table;
1104 db_name = db_user = db_pass = db_table = NULL;
1106 do_progress = isatty(fileno(stdout));
1108 strv = calloc(sizeof(char *), MAX_NVECS);
1109 dentry = calloc(sizeof(*dentry), NUM_DENTRIES);
1110 if (!strv || !dentry)
1111 crash("Failed to alloc initial structs");
1114 for (num_nfile = 0,i = 1; i < argc; i++) {
1115 char *opt, *arg = argv[i];
1116 int arg_len, eq_opt = 0;
1118 if ((opt = strchr(arg, '='))) {
1119 *opt++ = '\0';
1120 eq_opt = 1;
1122 else if (i < argc - 1) {
1123 opt = argv[i + 1];
1126 if (!prefixcmp(arg, "--incremental")) {
1127 incremental = 1;
1128 continue;
1130 if (!prefixcmp(arg, "--no-sql")) {
1131 use_sql = 0;
1132 continue;
1134 if (!prefixcmp(arg, "--only-notifications")) {
1135 only_notifications = 1;
1136 db_name = db_name ? db_name : "merlin";
1137 db_user = db_user ? db_user : "merlin";
1138 db_pass = db_pass ? db_pass : "merlin";
1139 db_table = db_table ? db_table : "notification";
1140 continue;
1142 if (!prefixcmp(arg, "--no-progress")) {
1143 do_progress = 0;
1144 continue;
1146 if (!prefixcmp(arg, "--debug") || !prefixcmp(arg, "-d")) {
1147 do_progress = 0;
1148 debug_level++;
1149 continue;
1151 if (!prefixcmp(arg, "--truncate-db")) {
1152 truncate_db = 1;
1153 continue;
1155 if (!prefixcmp(arg, "--nagios-cfg")) {
1156 if (!opt || !*opt) {
1157 crash("%s requires the path to nagios.cfg as argument", arg);
1159 nagios_cfg = opt;
1160 if (opt && !eq_opt)
1161 i++;
1162 continue;
1164 if (!prefixcmp(arg, "--db-name")) {
1165 if (!opt || !*opt)
1166 crash("%s requires a database name as an argument", arg);
1167 db_name = opt;
1168 if (opt && !eq_opt)
1169 i++;
1170 continue;
1172 if (!prefixcmp(arg, "--db-user")) {
1173 if (!opt || !*opt)
1174 crash("%s requires a database username as argument", arg);
1175 db_user = opt;
1176 if (opt && !eq_opt)
1177 i++;
1178 continue;
1180 if (!prefixcmp(arg, "--db-pass")) {
1181 if (!opt || !*opt)
1182 crash("%s requires a database username as argument", arg);
1183 db_pass = opt;
1184 if (opt && !eq_opt)
1185 i++;
1186 continue;
1188 if (!prefixcmp(arg, "--db-table")) {
1189 if (!opt || !*opt)
1190 crash("%s requires a database table name as argument", arg);
1191 db_table = opt;
1192 if (opt && !eq_opt)
1193 i++;
1194 continue;
1196 if (!prefixcmp(arg, "--interesting") || !prefixcmp(arg, "-i")) {
1197 if (!opt || !*opt)
1198 crash("%s requires a filename as argument", arg);
1199 hash_interesting(opt);
1200 if (opt && !eq_opt)
1201 i++;
1202 continue;
1205 /* non-argument, so treat as a config- or log-file */
1206 arg_len = strlen(arg);
1207 if (arg_len >= 10 && !strcmp(&arg[arg_len - 10], "nagios.cfg")) {
1208 nagios_cfg = arg;
1209 } else {
1210 add_naglog_path(arg);
1214 /* fallback for op5 systems */
1215 if (!nagios_cfg && !num_nfile) {
1216 nagios_cfg = "/opt/monitor/etc/nagios.cfg";
1218 if (nagios_cfg) {
1219 struct cfg_comp *conf;
1220 conf = cfg_parse_file(nagios_cfg);
1221 for (i = 0; i < conf->vars; i++) {
1222 struct cfg_var *v = conf->vlist[i];
1223 if (!strcmp(v->key, "log_file")) {
1224 add_naglog_path(v->value);
1226 if (!strcmp(v->key, "log_archive_path")) {
1227 add_naglog_path(v->value);
1232 if (use_sql) {
1233 db_name = db_name ? db_name : "monitor_reports";
1234 db_user = db_user ? db_user : "monitor";
1235 db_pass = db_pass ? db_pass : "monitor";
1236 db_table = db_table ? db_table : "report_data";
1237 sql_config("db_database", db_name);
1238 sql_config("db_user", db_user);
1239 sql_config("db_pass", db_pass);
1240 sql_config("db_table", db_table);
1242 if (sql_init() < 0)
1243 crash("sql_init() failed");
1244 if (truncate_db)
1245 sql_query("TRUNCATE %s", sql_table_name());
1247 if (incremental) {
1248 MYSQL_RES *result;
1249 MYSQL_ROW row;
1250 sql_query("SELECT %s FROM %s.%s ORDER BY %s DESC LIMIT 1",
1251 only_notifications ? "end_time" : "timestamp",
1252 db_name, db_table,
1253 only_notifications ? "end_time" : "timestamp");
1255 if (!(result = sql_get_result()))
1256 crash("Failed to get last timestamp: %s\n", sql_error());
1258 /* someone might use --incremental with an empty
1259 * database. We shouldn't crash in that case */
1260 if ((row = sql_fetch_row(result)))
1261 incremental = strtoul(row[0], NULL, 0);
1263 sql_free_result(result);
1266 * We lock the table we'll be working with and disable
1267 * indexes on it. Otherwise doing the actual inserts
1268 * will take just about forever, as MySQL has to update
1269 * and flush the index cache between each operation.
1271 if (sql_query("ALTER TABLE %s DISABLE KEYS", sql_table_name()))
1272 crash("Failed to disable keys: %s", sql_error());
1273 if (sql_query("LOCK TABLES %s WRITE", sql_table_name()))
1274 crash("Failed to lock table %s: %s", sql_table_name(), sql_error());
1277 log_grok_var("logfile", "/dev/null");
1278 log_grok_var("log_levels", "warn");
1280 if (!num_nfile)
1281 crash("Usage: %s [--incremental] [--interesting <file>] [--truncate-db] logfiles\n",
1282 __progname);
1284 if (log_init() < 0)
1285 crash("log_init() failed");
1287 qsort(nfile, num_nfile, sizeof(*nfile), nfile_cmp);
1289 host_downtime = hash_init(HASH_TABLE_SIZE);
1290 service_downtime = hash_init(HASH_TABLE_SIZE);
1292 if (hook_init() < 0)
1293 crash("Failed to initialize hooks");
1295 /* go through them once to count the total size for progress output */
1296 for (i = 0; i < num_nfile; i++) {
1297 totsize += nfile[i].size;
1300 gettimeofday(&import_start, NULL);
1301 printf("Importing %s of data from %d files\n",
1302 tobytes(totsize), num_nfile);
1304 for (i = 0; i < num_nfile; i++) {
1305 struct naglog_file *nf = &nfile[i];
1306 cur_file = nf;
1307 show_progress();
1308 debug("importing from %s (%lu : %u)\n", nf->path, nf->first, nf->cmp);
1309 line_no = 0;
1310 lparse_path(nf->path, nf->size, parse_one_line);
1311 imported++; /* make up for one lost byte per file */
1314 end_progress();
1316 if (debug_level) {
1317 if (dt_depth) {
1318 printf("Unclosed host downtimes:\n");
1319 puts("------------------------");
1320 hash_walk_data(host_downtime, print_downtime);
1321 printf("Unclosed service downtimes:\n");
1322 puts("---------------------------");
1323 hash_walk_data(service_downtime, print_downtime);
1325 printf("dt_depth: %d\n", dt_depth);
1327 printf("purged downtimes: %d\n", purged_downtimes);
1328 printf("max simultaneous host downtime hashes: %u\n",
1329 hash_get_max_entries(host_downtime));
1330 printf("max simultaneous service downtime hashes: %u\n",
1331 hash_get_max_entries(service_downtime));
1332 printf("max downtime depth: %u\n", max_dt_depth);
1335 if (use_sql) {
1336 SQL_RESULT *res;
1337 SQL_ROW row;
1338 time_t start;
1339 unsigned long entries;
1341 sql_query("SELECT id FROM %s ORDER BY id DESC LIMIT 1", sql_table_name());
1342 if (!(res = sql_get_result()))
1343 entries = 0;
1344 else {
1345 row = sql_fetch_row(res);
1346 entries = strtoul(row[0], NULL, 0);
1347 sql_free_result(res);
1350 signal(SIGINT, SIG_IGN);
1351 sql_query("UNLOCK TABLES");
1352 start = time(NULL);
1353 printf("Creating sql table indexes. This will likely take ~%lu seconds\n",
1354 (entries / 50000) + 1);
1355 sql_query("ALTER TABLE %s ENABLE KEYS", sql_table_name());
1356 printf("%lu database entries indexed in %lu seconds\n",
1357 entries, time(NULL) - start);
1358 sql_close();
1361 if (warnings && debug_level)
1362 fprintf(stderr, "Total warnings: %d\n", warnings);
1364 if (debug_level || dt_start != dt_stop)
1365 fprintf(stderr, "Downtime data %s\n started: %d\n stopped: %d\n",
1366 dt_depth ? "mismatch!" : "consistent", dt_start, dt_stop);
1367 if (hash_check_table(host_downtime))
1368 fprintf(stderr, "Hash table inconsistencies for host_downtime\n");
1369 if (hash_check_table(service_downtime))
1370 fprintf(stderr, "Hash table inconsistencies for service_downtime\n");
1372 print_unhandled_events();
1374 return 0;