void find_candidate(ghost_t *rec) { /* * Try to find an existing host definition for this ghost. * We look at the IP-address. * If the ghost reports with a FQDN, look for a host with * the same hostname without domain. * If the ghost reports a plain hostname (no domain), look * for a FQDN record with the same plain hostname. */ void *hrec; int found; char *gdelim, *tdelim, *ghostnofqdn, *hname; ghostnofqdn = rec->name; if ((gdelim = strchr(ghostnofqdn, '.')) != NULL) *gdelim = '\0'; for (hrec = first_host(), found = 0; (hrec && !found); hrec = next_host(hrec, 0)) { /* Check the IP */ found = (strcmp(rec->sender, xmh_item(hrec, XMH_IP)) == 0); if (!found) { /* Check if hostnames w/o domain match */ hname = xmh_item(hrec, XMH_HOSTNAME); if ((tdelim = strchr(hname, '.')) != NULL) *tdelim = '\0'; found = (strcasecmp(ghostnofqdn, hname) == 0); if (tdelim) *tdelim = '.'; } if (found) rec->candidate = hrec; } if (gdelim) *gdelim = '.'; }
int main(int argc, char *argv[]) { void *walk; load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); for (walk = first_host(); (walk); walk=next_host(walk, 0)) { char *nk, *nktime, *tok; nk = xmh_item(walk, XMH_NK); if (!nk) continue; nktime = xmh_item(walk, XMH_NKTIME); nk = strdup(nk); tok = strtok(nk, ","); while (tok) { char *hostname = xmh_item(walk, XMH_HOSTNAME); char *startstr = "", *endstr = "", *ttgroup = "", *ttextra = "", *updinfo = "Migrated"; int priority = 2; fprintf(stdout, "%s|%s|%s|%s|%s|%d|%s|%s|%s\n", hostname, tok, startstr, endstr, (nktime ? nktime : ""), priority, ttgroup, ttextra, updinfo); tok = strtok(NULL, ","); } xfree(nk); } return 0; }
static void count_events(countlist_t **hostcounthead, countlist_t **svccounthead) { void *hostwalk; for (hostwalk = first_host(); (hostwalk); hostwalk = next_host(hostwalk, 0)) { eventcount_t *swalk; countlist_t *hrec, *srec; swalk = (eventcount_t *)xmh_item(hostwalk, XMH_DATA); if (!swalk) continue; hrec = (countlist_t *)malloc(sizeof(countlist_t)); hrec->src = hostwalk; hrec->total = 0; hrec->next = *hostcounthead; *hostcounthead = hrec; for (swalk = (eventcount_t *)xmh_item(hostwalk, XMH_DATA); (swalk); swalk = swalk->next) { hrec->total += swalk->count; for (srec = *svccounthead; (srec && (srec->src != (void *)swalk->service)); srec = srec->next) ; if (!srec) { srec = (countlist_t *)malloc(sizeof(countlist_t)); srec->src = (void *)swalk->service; srec->total = 0; srec->next = *svccounthead; *svccounthead = srec; } srec->total += swalk->count; } } }
static void dump_eventtree(void) { void *hwalk; elist_t *lwalk; ed_t *ewalk; for (hwalk = first_host(); (hwalk); hwalk = next_host(hwalk, 0)) { printf("%s\n", xmh_item(hwalk, XMH_HOSTNAME)); lwalk = (elist_t *)xmh_item(hwalk, XMH_DATA); while (lwalk) { printf("\t%s\n", lwalk->svc->name); ewalk = lwalk->head; while (ewalk) { printf("\t\t%ld->%ld = %6ld %s\n", (long) ewalk->event->changetime, (long) ewalk->event->eventtime, (long) ewalk->event->duration, colorname(ewalk->event->oldcolor)); ewalk = ewalk->next; } lwalk = lwalk->next; } } }
void dump_countlists(countlist_t *hosthead, countlist_t *svchead) { countlist_t *cwalk; printf("Hosts\n"); for (cwalk = hosthead; (cwalk); cwalk = cwalk->next) { printf("\t%20s : %lu\n", xmh_item(cwalk->src, XMH_HOSTNAME), cwalk->total); } printf("\n"); printf("Services\n"); for (cwalk = svchead; (cwalk); cwalk = cwalk->next) { printf("\t%20s : %lu\n", ((htnames_t *)cwalk->src)->name, cwalk->total); } printf("\n"); }
static void add_to_sub_queue(myconn_t *rec, char *moduleid, char *location, ...) { va_list extraparams; char *s; strbuffer_t *extrastr = newstrbuffer(0); if (!rec || !moduleid) return; va_start(extraparams, location); while ((s = va_arg(extraparams, char *)) != NULL) { if (STRBUFLEN(extrastr) > 0) addtobuffer(extrastr, "\t"); addtobuffer(extrastr, s); } va_end(extraparams); xymon_sqldb_netmodule_additem(moduleid, location, xmh_item(rec->hostinfo, XMH_HOSTNAME), rec->netparams.destinationip, rec->testspec, STRBUF(extrastr), rec->interval*1000, rec->timeout*1000); freestrbuffer(extrastr); }
int main(int argc, char *argv[]) { int argi; char *envarea = NULL; char *hffile = "ghosts"; int bgcolor = COL_BLUE; char *ghosts = NULL; sendreturn_t *sres; for (argi = 1; (argi < argc); argi++) { if (argnmatch(argv[argi], "--env=")) { char *p = strchr(argv[argi], '='); loadenv(p+1, envarea); } else if (argnmatch(argv[argi], "--area=")) { char *p = strchr(argv[argi], '='); envarea = strdup(p+1); } else if (strcmp(argv[argi], "--debug") == 0) { debug = 1; } else if (argnmatch(argv[argi], "--hffile=")) { char *p = strchr(argv[argi], '='); hffile = strdup(p+1); } } load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); parse_query(); switch (outform) { case O_HTML: fprintf(stdout, "Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE")); headfoot(stdout, hffile, "", "header", bgcolor); break; case O_TXT: fprintf(stdout, "Content-type: text/plain\n\n"); break; } sres = newsendreturnbuf(1, NULL); if (sendmessage("ghostlist", NULL, XYMON_TIMEOUT, sres) == XYMONSEND_OK) { char *bol, *eoln, *name, *sender, *timestr; time_t tstamp, now; int count, idx; ghost_t *ghosttable; ghosts = getsendreturnstr(sres, 1); /* Count the number of lines */ for (bol = ghosts, count=0; (bol); bol = strchr(bol, '\n')) { if (*bol == '\n') bol++; count++; } ghosttable = (ghost_t *)calloc(count+1, sizeof(ghost_t)); idx = count = 0; tstamp = now = getcurrenttime(NULL); bol = ghosts; while (bol) { name = sender = timestr = NULL; eoln = strchr(bol, '\n'); if (eoln) *eoln = '\0'; name = strtok(bol, "|"); if (name) sender = strtok(NULL, "|"); if (sender) timestr = strtok(NULL, "|"); if (timestr) tstamp = atol(timestr); if (name && sender && timestr && (tstamp > (now - maxage))) { int i1, i2, i3, i4; sscanf(sender, "%d.%d.%d.%d", &i1, &i2, &i3, &i4); ghosttable[idx].sender = sender; ghosttable[idx].senderval = (i1 << 24) + (i2 << 16) + (i3 << 8) + i4; ghosttable[idx].name = name; ghosttable[idx].tstamp = tstamp; find_candidate(&ghosttable[idx]); idx++; count++; } if (eoln) eoln++; bol = eoln; } switch (sorttype) { case S_NAME: qsort(&ghosttable[0], count, sizeof(ghost_t), hostname_compare); break; case S_SENDER: qsort(&ghosttable[0], count, sizeof(ghost_t), sender_compare); break; case S_TIME: qsort(&ghosttable[0], count, sizeof(ghost_t), time_compare); break; } if (outform == O_HTML) { fprintf(stdout, "<table align=center>\n"); fprintf(stdout, "<tr>"); fprintf(stdout, "<th align=left><a href=\"ghostlist.sh?SORT=name&MAXAGE=%d\">Hostname</a></th>", maxage); fprintf(stdout, "<th align=left><a href=\"ghostlist.sh?SORT=sender&MAXAGE=%d\">Sent from</a></th>", maxage); fprintf(stdout, "<th align=left>Candidate</th>"); fprintf(stdout, "<th align=right><a href=\"ghostlist.sh?SORT=time&MAXAGE=%d\">Report age</a></th>", maxage); fprintf(stdout, "</tr>\n"); } for (idx = 0; (idx < count); idx++) { if (!ghosttable[idx].name) continue; if (!ghosttable[idx].sender) continue; switch (outform) { case O_HTML: fprintf(stdout, "<tr><td align=left>%s</td><td align=left>%s</td>", ghosttable[idx].name, ghosttable[idx].sender); if (ghosttable[idx].candidate) { fprintf(stdout, "<td align=left><a href=\"%s\">%s</a></td>", hostsvcurl(xmh_item(ghosttable[idx].candidate, XMH_HOSTNAME), xgetenv("INFOCOLUMN"), 1), xmh_item(ghosttable[idx].candidate, XMH_HOSTNAME)); } else { fprintf(stdout, "<td> </td>"); } fprintf(stdout, "<td align=right>%ld:%02ld</td></tr>\n", (now - ghosttable[idx].tstamp)/60, (now - ghosttable[idx].tstamp)%60); break; case O_TXT: fprintf(stdout, "%s\t\t%s\n", ghosttable[idx].sender, ghosttable[idx].name); break; } } if (outform == O_HTML) { fprintf(stdout, "</table>\n"); fprintf(stdout, "<br><br><center><a href=\"ghostlist.sh?SORT=%s&MAXAGE=%d&TEXT\">Text report</a></center>\n", htmlquoted(sortstring), maxage); } } else fprintf(stdout, "<h3><center>Failed to retrieve ghostlist from server</center></h3>\n"); freesendreturnbuf(sres); if (outform == O_HTML) { headfoot(stdout, hffile, "", "footer", bgcolor); } return 0; }
int main(int argc, char *argv[]) { void *hostwalk, *clonewalk; int argi; char *envarea = NULL; strbuffer_t *outbuf; char msgline[4096]; char oneurl[10240]; int gotany = 0; enum { OP_INITIAL, OP_YES, OP_NO } gotonepage = OP_INITIAL; /* Tracks if all matches are on one page */ char *onepage = NULL; /* If gotonepage==OP_YES, then this is the page */ /*[wm] regex support */ #define BUFSIZE 256 regex_t re; char re_errstr[BUFSIZE]; int re_status; for (argi=1; (argi < argc); argi++) { if (argnmatch(argv[argi], "--env=")) { char *p = strchr(argv[argi], '='); loadenv(p+1, envarea); } else if (argnmatch(argv[argi], "--area=")) { char *p = strchr(argv[argi], '='); envarea = strdup(p+1); } } redirect_cgilog("findhost"); cgidata = cgi_request(); if (cgidata == NULL) { /* Present the query form */ sethostenv("", "", "", colorname(COL_BLUE), NULL); printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE")); showform(stdout, "findhost", "findhost_form", COL_BLUE, getcurrenttime(NULL), NULL, NULL); return 0; } parse_query(); if ( (re_status = regcomp(&re, pSearchPat, re_flag)) != 0 ) { regerror(re_status, &re, re_errstr, BUFSIZE); print_header(); printf("<tr><td align=left><font color=red>%s</font></td>\n", pSearchPat); printf("<td align=left><font color=red>%s</font></td></tr>\n", re_errstr); print_footer(); return 0; } outbuf = newstrbuffer(0); load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); hostwalk = first_host(); while (hostwalk) { /* * [wm] - Allow the search to be done on the hostname * also on the "displayname" and the host comment * Maybe this should be implemented by changing the HTML form, but until than.. * we're supposing that hostname will NEVER be null */ char *hostname, *displayname, *comment, *ip; hostname = xmh_item(hostwalk, XMH_HOSTNAME); displayname = xmh_item(hostwalk, XMH_DISPLAYNAME); comment = xmh_item(hostwalk, XMH_COMMENT); ip = xmh_item(hostwalk, XMH_IP); if ( regexec (&re, hostname, (size_t)0, NULL, 0) == 0 || (regexec(&re, ip, (size_t)0, NULL, 0) == 0) || (displayname && regexec (&re, displayname, (size_t)0, NULL, 0) == 0) || (comment && regexec (&re, comment, (size_t)0, NULL, 0) == 0) ) { /* match */ addtobuffer(outbuf, "<tr>\n"); sprintf(msgline, "<td align=left> %s </td>\n", displayname ? displayname : hostname); addtobuffer(outbuf, msgline); sprintf(oneurl, "%s/%s/#%s", xgetenv("XYMONWEB"), xmh_item(hostwalk, XMH_PAGEPATH), hostname); sprintf(msgline, "<td align=left> <a href=\"%s\">%s</a>\n", oneurl, xmh_item(hostwalk, XMH_PAGEPATHTITLE)); addtobuffer(outbuf, msgline); gotany++; /* See if all of the matches so far are on one page */ switch (gotonepage) { case OP_INITIAL: gotonepage = OP_YES; onepage = xmh_item(hostwalk, XMH_PAGEPATH); break; case OP_YES: if (strcmp(onepage, xmh_item(hostwalk, XMH_PAGEPATH)) != 0) gotonepage = OP_NO; break; case OP_NO: break; } clonewalk = next_host(hostwalk, 1); while (clonewalk && (strcmp(xmh_item(hostwalk, XMH_HOSTNAME), xmh_item(clonewalk, XMH_HOSTNAME)) == 0)) { sprintf(msgline, "<br><a href=\"%s/%s/#%s\">%s</a>\n", xgetenv("XYMONWEB"), xmh_item(clonewalk, XMH_PAGEPATH), xmh_item(clonewalk, XMH_HOSTNAME), xmh_item(clonewalk, XMH_PAGEPATHTITLE)); addtobuffer(outbuf, msgline); clonewalk = next_host(clonewalk, 1); gotany++; } addtobuffer(outbuf, "</td>\n</tr>\n"); hostwalk = clonewalk; } else { hostwalk = next_host(hostwalk, 0); } } regfree (&re); /*[wm] - free regex compiled patern */ if (dojump) { if (gotany == 1) { printf("Location: %s%s\n\n", xgetenv("XYMONWEBHOST"), oneurl); return 0; } else if ((gotany > 1) && (gotonepage == OP_YES)) { printf("Location: %s%s/%s/\n\n", xgetenv("XYMONWEBHOST"), xgetenv("XYMONWEB"), onepage); return 0; } } print_header(); if (!gotany) { printf("<tr><td align=left>%s</td><td align=left>Not found</td></tr>\n", pSearchPat); } else { printf("%s", grabstrbuffer(outbuf)); } print_footer(); /* [wm] - Free the strdup allocated memory */ if (pSearchPat) xfree(pSearchPat); return 0; }
listhead_t *run_net_tests(int concurrency, char *sourceip4, char *sourceip6) { int maxfd; list_shuffle(pendingtests); /* * Determine how many tests can run in parallel. * If no --concurrency set by user, default to (FD_SETSIZE / 4) - typically 256. * But never go above the ressource limit that is set, or above FD_SETSIZE. * And we save some fd's - 20 - for stdio, libs etc. */ { int absmaxconcurrency = (FD_SETSIZE - 20); struct rlimit lim; getrlimit(RLIMIT_NOFILE, &lim); if ((lim.rlim_cur > 20) && ((lim.rlim_cur - 20) < absmaxconcurrency)) absmaxconcurrency = (lim.rlim_cur - 20); if (concurrency == 0) concurrency = (FD_SETSIZE / 4); if (concurrency > absmaxconcurrency) concurrency = absmaxconcurrency; } /* Loop to process data */ do { fd_set fdread, fdwrite; int n; struct timeval tmo; myconn_t *rec; listitem_t *pcur, *pnext; int lookupsposted = 0; dbgprintf("*** Starting test loop ***\n"); /* Start some more tests */ pcur = pendingtests->head; while (pcur && (activetests->len < concurrency) && (lookupsposted < concurrency)) { rec = (myconn_t *)pcur->data; dbgprintf(" Test: %s\n", rec->testspec); /* * Must save the pointer to the next pending test now, * since we may move the current item from the pending * list to the active list before going to the next * item in the pending-list. */ pnext = pcur->next; if (rec->netparams.lookupstatus == LOOKUP_NEEDED) { dbgprintf(" LOOKUP_NEEDED\n"); lookupsposted++; dns_lookup(rec); } if ((rec->netparams.lookupstatus == LOOKUP_ACTIVE) || (rec->netparams.lookupstatus == LOOKUP_NEEDED)) { /* DNS lookup in progress, skip this test until lookup completes */ dbgprintf(" lookup in progress: %s\n", (rec->netparams.lookupstatus == LOOKUP_ACTIVE) ? "ACTIVE" : "NEEDED"); pcur = pnext; continue; } else if (rec->netparams.lookupstatus == LOOKUP_FAILED) { /* DNS lookup determined that this host does not have a valid IP. */ dbgprintf(" LOOKUP_FAILED\n"); switch (dnsstrategy) { case DNS_STRATEGY_HOSTNAME: /* DNS failed -> test failed */ list_item_move(donetests, pcur, rec->testspec); rec->talkresult = TALK_CANNOT_RESOLVE; break; case DNS_STRATEGY_STANDARD: case DNS_STRATEGY_IP: /* This one cannot really happen */ /* Use IP from hosts.cfg, if it is valid */ if (!conn_null_ip(xmh_item(rec->hostinfo, XMH_IP))) { xfree(rec->netparams.destinationip); rec->netparams.destinationip = strdup(xmh_item(rec->hostinfo, XMH_IP)); rec->netparams.lookupstatus = LOOKUP_COMPLETED; } else { list_item_move(donetests, pcur, rec->testspec); rec->talkresult = TALK_CANNOT_RESOLVE; } break; } pcur = pnext; continue; } switch (rec->talkprotocol) { case TALK_PROTO_PLAIN: case TALK_PROTO_HTTP: case TALK_PROTO_NTP: case TALK_PROTO_LDAP: case TALK_PROTO_EXTERNAL: if (!rec->teststarttime) rec->teststarttime = getcurrenttime(NULL); dbgprintf(" conn_prepare_connection()\n"); if (!rec->netparams.sourceip && (sourceip4 || sourceip6)) { switch (conn_is_ip(rec->netparams.destinationip)) { case 4: rec->netparams.sourceip = sourceip4; break; case 6: rec->netparams.sourceip = sourceip6; break; } } if (conn_prepare_connection(rec->netparams.destinationip, rec->netparams.destinationport, rec->netparams.socktype, rec->netparams.sourceip, rec->netparams.sslhandling, rec->netparams.sslname, rec->netparams.sslcertfn, rec->netparams.sslkeyfn, rec->timeout*1000000, rec->netparams.callback, rec)) { dbgprintf("\tmoved to activetests, target %s, timeout %d\n", rec->netparams.destinationip, rec->timeout); list_item_move(activetests, pcur, rec->testspec); } else { dbgprintf("\tmoved to failedtests\n"); rec->talkresult = TALK_CONN_FAILED; list_item_move(donetests, pcur, rec->testspec); } break; case TALK_PROTO_DNSQUERY: dbgprintf(" dns_start_query()\n"); if (dns_start_query(rec, rec->netparams.destinationip)) { dbgprintf("\tmoved to activetests\n"); list_item_move(activetests, pcur, rec->testspec); } else { dbgprintf("\tmoved to failedtests\n"); rec->talkresult = TALK_CONN_FAILED; list_item_move(donetests, pcur, rec->testspec); } break; case TALK_PROTO_PING: dbgprintf(" PING test, queued\n"); rec->talkresult = TALK_OK; list_item_move(donetests, pcur, rec->testspec); break; default: dbgprintf(" Huh?\n"); break; } pcur = pnext; } maxfd = conn_fdset(&fdread, &fdwrite); // dbgprintf("Setting up select - conn_fdset has maxfd=%d\n", maxfd); dns_add_active_fds(activetests, &maxfd, &fdread, &fdwrite); // dbgprintf("Setting up select - dns_add_active_fds set maxfd=%d\n", maxfd); if (maxfd > 0) { tmo.tv_sec = 1; tmo.tv_usec = 0; n = select(maxfd+1, &fdread, &fdwrite, NULL, &tmo); if (n < 0) { if (errno != EINTR) { errprintf("FATAL: select() returned error %s\n", strerror(errno)); return NULL; } } conn_process_active(&fdread, &fdwrite); dns_process_active(activetests, &fdread, &fdwrite); } conn_trimactive(); dns_finish_queries(activetests); dbgprintf("Active: %d, pending: %d\n", activetests->len, pendingtests->len); } while ((activetests->len + pendingtests->len) > 0); dns_finish_queries(donetests); return donetests; }
int main(int argc, char *argv[]) { char *msg; int seq; int argi; int alertcolors, alertinterval; char *configfn = NULL; char *checkfn = NULL; int checkpointinterval = 900; char acklogfn[PATH_MAX]; FILE *acklogfd = NULL; char notiflogfn[PATH_MAX]; FILE *notiflogfd = NULL; char *tracefn = NULL; struct sigaction sa; int configchanged; time_t lastxmit = 0; MEMDEFINE(acklogfn); MEMDEFINE(notiflogfn); libxymon_init(argv[0]); /* Dont save the error buffer */ save_errbuf = 0; /* Load alert config */ alertcolors = colorset(xgetenv("ALERTCOLORS"), ((1 << COL_GREEN) | (1 << COL_BLUE))); alertinterval = 60*atoi(xgetenv("ALERTREPEAT")); /* Create our loookup-trees */ hostnames = xtreeNew(strcasecmp); testnames = xtreeNew(strcasecmp); locations = xtreeNew(strcasecmp); for (argi=1; (argi < argc); argi++) { if (argnmatch(argv[argi], "--config=")) { configfn = strdup(strchr(argv[argi], '=')+1); } else if (argnmatch(argv[argi], "--checkpoint-file=")) { checkfn = strdup(strchr(argv[argi], '=')+1); } else if (argnmatch(argv[argi], "--checkpoint-interval=")) { char *p = strchr(argv[argi], '=') + 1; checkpointinterval = atoi(p); } else if (argnmatch(argv[argi], "--dump-config")) { load_alertconfig(configfn, alertcolors, alertinterval); dump_alertconfig(1); return 0; } else if (argnmatch(argv[argi], "--cfid")) { include_configid = 1; } else if (argnmatch(argv[argi], "--test")) { char *testhost = NULL, *testservice = NULL, *testpage = NULL, *testcolor = "red", *testgroups = NULL; void *hinfo; int testdur = 0; FILE *logfd = NULL; activealerts_t *awalk = NULL; int paramno = 0; argi++; if (argi < argc) testhost = argv[argi]; argi++; if (argi < argc) testservice = argv[argi]; argi++; while (argi < argc) { if (strncasecmp(argv[argi], "--duration=", 11) == 0) { testdur = durationvalue(strchr(argv[argi], '=')+1); } else if (strncasecmp(argv[argi], "--color=", 8) == 0) { testcolor = strchr(argv[argi], '=')+1; } else if (strncasecmp(argv[argi], "--group=", 8) == 0) { testgroups = strchr(argv[argi], '=')+1; } else if (strncasecmp(argv[argi], "--time=", 7) == 0) { fakestarttime = (time_t)atoi(strchr(argv[argi], '=')+1); } else { paramno++; if (paramno == 1) testdur = atoi(argv[argi]); else if (paramno == 2) testcolor = argv[argi]; else if (paramno == 3) fakestarttime = (time_t) atoi(argv[argi]); } argi++; } if ((testhost == NULL) || (testservice == NULL)) { printf("Usage: xymond_alert --test HOST SERVICE [options]\n"); printf("Possible options:\n\t[--duration=MINUTES]\n\t[--color=COLOR]\n\t[--group=GROUPNAME]\n\t[--time=TIMESPEC]\n"); return 1; } load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); hinfo = hostinfo(testhost); if (hinfo) { testpage = strdup(xmh_item(hinfo, XMH_ALLPAGEPATHS)); } else { errprintf("Host not found in hosts.cfg - assuming it is on the top page\n"); testpage = ""; } awalk = (activealerts_t *)calloc(1, sizeof(activealerts_t)); awalk->hostname = find_name(hostnames, testhost); awalk->testname = find_name(testnames, testservice); awalk->location = find_name(locations, testpage); awalk->ip = strdup("127.0.0.1"); awalk->color = awalk->maxcolor = parse_color(testcolor); awalk->pagemessage = "Test of the alert configuration"; awalk->eventstart = getcurrenttime(NULL) - testdur*60; awalk->groups = (testgroups ? strdup(testgroups) : NULL); awalk->state = A_PAGING; awalk->cookie = 12345; awalk->next = NULL; logfd = fopen("/dev/null", "w"); starttrace(NULL); testonly = 1; load_alertconfig(configfn, alertcolors, alertinterval); load_holidays(0); send_alert(awalk, logfd); return 0; } else if (argnmatch(argv[argi], "--trace=")) { tracefn = strdup(strchr(argv[argi], '=')+1); starttrace(tracefn); } else if (net_worker_option(argv[argi])) { /* Handled in the subroutine */ } else if (standardoption(argv[argi])) { if (showhelp) return 0; } else { errprintf("Unknown option '%s'\n", argv[argi]); } } /* Do the network stuff if needed */ net_worker_run(ST_ALERT, LOC_SINGLESERVER, NULL); if (checkfn) { load_checkpoint(checkfn); nextcheckpoint = gettimer() + checkpointinterval; dbgprintf("Next checkpoint at %d, interval %d\n", (int) nextcheckpoint, checkpointinterval); } setup_signalhandler("xymond_alert"); /* Need to handle these ourselves, so we can shutdown and save state-info */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = sig_handler; sigaction(SIGPIPE, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGCHLD, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); sigaction(SIGHUP, &sa, NULL); if (xgetenv("XYMONSERVERLOGS")) { sprintf(acklogfn, "%s/acknowledge.log", xgetenv("XYMONSERVERLOGS")); acklogfd = fopen(acklogfn, "a"); sprintf(notiflogfn, "%s/notifications.log", xgetenv("XYMONSERVERLOGS")); notiflogfd = fopen(notiflogfn, "a"); } /* * The general idea here is that this loop handles receiving of alert- * and ack-messages from the master daemon, and maintains a list of * host+test combinations that may have alerts going out. * * This module does not deal with any specific alert-configuration, * it just picks up the alert messages, maintains the list of * known tests that are in some sort of critical condition, and * periodically pushes alerts to the do_alert.c module for handling. * * The only modification of alerts that happen here is the handling * of when the next alert is due. It calls into the next_alert() * routine to learn when an alert should be repeated, and also * deals with Acknowledgments that stop alerts from going out for * a period of time. */ while (running) { char *eoln, *restofmsg; char *metadata[20]; char *p; int metacount; char *hostname = NULL, *testname = NULL; struct timespec timeout; time_t now, nowtimer; int anytogo; activealerts_t *awalk; int childstat; nowtimer = gettimer(); if (checkfn && (nowtimer > nextcheckpoint)) { dbgprintf("Saving checkpoint\n"); nextcheckpoint = nowtimer + checkpointinterval; save_checkpoint(checkfn); if (acklogfd) acklogfd = freopen(acklogfn, "a", acklogfd); if (notiflogfd) notiflogfd = freopen(notiflogfn, "a", notiflogfd); } timeout.tv_sec = 60; timeout.tv_nsec = 0; msg = get_xymond_message(C_PAGE, "xymond_alert", &seq, &timeout); if (msg == NULL) { running = 0; continue; } /* See what time it is - must happen AFTER the timeout */ now = getcurrenttime(NULL); /* Split the message in the first line (with meta-data), and the rest */ eoln = strchr(msg, '\n'); if (eoln) { *eoln = '\0'; restofmsg = eoln+1; } else { restofmsg = ""; } /* * Now parse the meta-data into elements. * We use our own "gettok()" routine which works * like strtok(), but can handle empty elements. */ metacount = 0; memset(&metadata, 0, sizeof(metadata)); p = gettok(msg, "|"); while (p && (metacount < 19)) { metadata[metacount] = p; metacount++; p = gettok(NULL, "|"); } metadata[metacount] = NULL; if (metacount > 3) hostname = metadata[3]; if (metacount > 4) testname = metadata[4]; if ((metacount > 10) && (strncmp(metadata[0], "@@page", 6) == 0)) { /* @@page|timestamp|sender|hostname|testname|hostip|expiretime|color|prevcolor|changetime|location|cookie|osname|classname|grouplist|modifiers */ int newcolor, newalertstatus, oldalertstatus; dbgprintf("Got page message from %s:%s\n", hostname, testname); traceprintf("@@page %s:%s:%s=%s\n", hostname, testname, metadata[10], metadata[7]); awalk = find_active(hostname, testname); if (awalk == NULL) { char *hwalk = find_name(hostnames, hostname); char *twalk = find_name(testnames, testname); char *pwalk = find_name(locations, metadata[10]); awalk = (activealerts_t *)calloc(1, sizeof(activealerts_t)); awalk->hostname = hwalk; awalk->testname = twalk; awalk->location = pwalk; awalk->cookie = -1; awalk->state = A_DEAD; /* * Use changetime here, if we restart the alert module then * this gets the duration values more right than using "now". * Also, define this only when a new alert arrives - we should * NOT clear this when a status goes yellow->red, or if it * flaps between yellow and red. */ awalk->eventstart = atoi(metadata[9]); add_active(awalk->hostname, awalk); traceprintf("New record\n"); } newcolor = parse_color(metadata[7]); oldalertstatus = ((alertcolors & (1 << awalk->color)) != 0); newalertstatus = ((alertcolors & (1 << newcolor)) != 0); traceprintf("state %d->%d\n", oldalertstatus, newalertstatus); if (newalertstatus) { /* It's in an alert state. */ awalk->color = newcolor; awalk->state = A_PAGING; if (newcolor > awalk->maxcolor) { if (awalk->maxcolor != 0) { /* * Severity has increased (yellow -> red). * Clear the repeat-interval, and set maxcolor to * the new color. If it drops to yellow again, * maxcolor stays at red, so a test that flaps * between yellow and red will only alert on red * the first time, and then follow the repeat * interval. */ dbgprintf("Severity increased, cleared repeat interval: %s/%s %s->%s\n", awalk->hostname, awalk->testname, colorname(awalk->maxcolor), colorname(newcolor)); clear_interval(awalk); } awalk->maxcolor = newcolor; } } else { /* * Send one "recovered" message out now, then go to A_DEAD. * Dont update the color here - we want recoveries to go out * only if the alert color triggered an alert */ awalk->state = (newcolor == COL_BLUE) ? A_DISABLED : A_RECOVERED; } if (oldalertstatus != newalertstatus) { dbgprintf("Alert status changed from %d to %d\n", oldalertstatus, newalertstatus); clear_interval(awalk); } if (awalk->ip) xfree(awalk->ip); awalk->ip = strdup(metadata[5]); awalk->cookie = atoi(metadata[11]); if (awalk->osname) xfree(awalk->osname); awalk->osname = (metadata[12] ? strdup(metadata[12]) : NULL); if (awalk->classname) xfree(awalk->classname); awalk->classname = (metadata[13] ? strdup(metadata[13]) : NULL); if (awalk->groups) xfree(awalk->groups); awalk->groups = (metadata[14] ? strdup(metadata[14]) : NULL); if (awalk->pagemessage) xfree(awalk->pagemessage); if (metadata[15]) { /* Modifiers are more interesting than the message itself */ awalk->pagemessage = (char *)malloc(strlen(awalk->hostname) + strlen(awalk->testname) + strlen(colorname(awalk->color)) + strlen(metadata[15]) + strlen(restofmsg) + 10); sprintf(awalk->pagemessage, "%s:%s %s\n%s\n%s", awalk->hostname, awalk->testname, colorname(awalk->color), metadata[15], restofmsg); } else { awalk->pagemessage = strdup(restofmsg); } } else if ((metacount > 5) && (strncmp(metadata[0], "@@ack", 5) == 0)) { /* @@ack|timestamp|sender|hostname|testname|hostip|expiretime */ /* * An ack is handled simply by setting the next * alert-time to when the ack expires. */ time_t nextalert = atoi(metadata[6]); dbgprintf("Got ack message from %s:%s\n", hostname, testname); traceprintf("@@ack: %s:%s now=%d, ackeduntil %d\n", hostname, testname, (int)now, (int)nextalert); awalk = find_active(hostname, testname); if (acklogfd) { int cookie = (awalk ? awalk->cookie : -1); int color = (awalk ? awalk->color : 0); fprintf(acklogfd, "%d\t%d\t%d\t%d\t%s\t%s.%s\t%s\t%s\n", (int)now, cookie, (int)((nextalert - now) / 60), cookie, "np_filename_not_used", hostname, testname, colorname(color), nlencode(restofmsg)); fflush(acklogfd); } if (awalk && (awalk->state == A_PAGING)) { traceprintf("Record updated\n"); awalk->state = A_ACKED; awalk->nextalerttime = nextalert; if (awalk->ackmessage) xfree(awalk->ackmessage); awalk->ackmessage = strdup(restofmsg); } else { traceprintf("No record\n"); } } else if ((metacount > 4) && (strncmp(metadata[0], "@@notify", 5) == 0)) { /* @@notify|timestamp|sender|hostname|testname|pagepath */ char *hwalk = find_name(hostnames, hostname); char *twalk = find_name(testnames, testname); char *pwalk = find_name(locations, (metadata[5] ? metadata[5] : "")); awalk = (activealerts_t *)calloc(1, sizeof(activealerts_t)); awalk->hostname = hwalk; awalk->testname = twalk; awalk->location = pwalk; awalk->cookie = -1; awalk->pagemessage = strdup(restofmsg); awalk->eventstart = getcurrenttime(NULL); awalk->state = A_NOTIFY; add_active(awalk->hostname, awalk); } else if ((metacount > 3) && ((strncmp(metadata[0], "@@drophost", 10) == 0) || (strncmp(metadata[0], "@@dropstate", 11) == 0))) { /* @@drophost|timestamp|sender|hostname */ /* @@dropstate|timestamp|sender|hostname */ xtreePos_t handle; handle = xtreeFind(hostnames, hostname); if (handle != xtreeEnd(hostnames)) { alertanchor_t *anchor = (alertanchor_t *)xtreeData(hostnames, handle); for (awalk = anchor->head; (awalk); awalk = awalk->next) awalk->state = A_DEAD; } } else if ((metacount > 4) && (strncmp(metadata[0], "@@droptest", 10) == 0)) { /* @@droptest|timestamp|sender|hostname|testname */ awalk = find_active(hostname, testname); if (awalk) awalk->state = A_DEAD; } else if ((metacount > 4) && (strncmp(metadata[0], "@@renamehost", 12) == 0)) { /* @@renamehost|timestamp|sender|hostname|newhostname */ /* * We handle rename's simply by dropping the alert. If there is still an * active alert for the host, it will have to be dealt with when the next * status update arrives. */ xtreePos_t handle; handle = xtreeFind(hostnames, hostname); if (handle != xtreeEnd(hostnames)) { alertanchor_t *anchor = (alertanchor_t *)xtreeData(hostnames, handle); for (awalk = anchor->head; (awalk); awalk = awalk->next) awalk->state = A_DEAD; } } else if ((metacount > 5) && (strncmp(metadata[0], "@@renametest", 12) == 0)) { /* @@renametest|timestamp|sender|hostname|oldtestname|newtestname */ /* * We handle rename's simply by dropping the alert. If there is still an * active alert for the host, it will have to be dealt with when the next * status update arrives. */ awalk = find_active(hostname, testname); if (awalk) awalk->state = A_DEAD; } else if (strncmp(metadata[0], "@@shutdown", 10) == 0) { running = 0; errprintf("Got a shutdown message\n"); continue; } else if (strncmp(metadata[0], "@@logrotate", 11) == 0) { char *fn = xgetenv("XYMONCHANNEL_LOGFILENAME"); if (fn && strlen(fn)) { reopen_file(fn, "a", stdout); reopen_file(fn, "a", stderr); if (tracefn) { stoptrace(); starttrace(tracefn); } } continue; } else if (strncmp(metadata[0], "@@reload", 8) == 0) { /* Nothing ... right now */ } else if (strncmp(metadata[0], "@@idle", 6) == 0) { /* Timeout */ } /* * When a burst of alerts happen, we get lots of alert messages * coming in quickly. So lets handle them in bunches and only * do the full alert handling once every 10 secs - that lets us * combine a bunch of alerts into one transmission process. */ if (nowtimer < (lastxmit+10)) continue; lastxmit = nowtimer; /* * Loop through the activealerts list and see if anything is pending. * This is an optimization, we could just as well just fork off the * notification child and let it handle all of it. But there is no * reason to fork a child process unless it is going to do something. */ configchanged = load_alertconfig(configfn, alertcolors, alertinterval); configchanged += load_holidays(0); anytogo = 0; for (awalk = alistBegin(); (awalk); awalk = alistNext()) { int anymatch = 0; switch (awalk->state) { case A_NORECIP: if (!configchanged) break; /* The configuration has changed - switch NORECIP -> PAGING */ awalk->state = A_PAGING; clear_interval(awalk); /* Fall through */ case A_PAGING: if (have_recipient(awalk, &anymatch)) { if (awalk->nextalerttime <= now) anytogo++; } else { if (!anymatch) { awalk->state = A_NORECIP; cleanup_alert(awalk); } } break; case A_ACKED: if (awalk->nextalerttime <= now) { /* An ack has expired, so drop the ack message and switch to A_PAGING */ anytogo++; if (awalk->ackmessage) xfree(awalk->ackmessage); awalk->state = A_PAGING; } break; case A_RECOVERED: case A_DISABLED: case A_NOTIFY: anytogo++; break; case A_DEAD: break; } } dbgprintf("%d alerts to go\n", anytogo); if (anytogo) { pid_t childpid; childpid = fork(); if (childpid == 0) { /* The child */ start_alerts(); for (awalk = alistBegin(); (awalk); awalk = alistNext()) { switch (awalk->state) { case A_PAGING: if (awalk->nextalerttime <= now) { send_alert(awalk, notiflogfd); } break; case A_ACKED: /* Cannot be A_ACKED unless the ack is still valid, so no alert. */ break; case A_RECOVERED: case A_DISABLED: case A_NOTIFY: send_alert(awalk, notiflogfd); break; case A_NORECIP: case A_DEAD: break; } } finish_alerts(); /* Child does not continue */ exit(0); } else if (childpid < 0) { errprintf("Fork failed, cannot send alerts: %s\n", strerror(errno)); } } /* Update the state flag and the next-alert timestamp */ for (awalk = alistBegin(); (awalk); awalk = alistNext()) { switch (awalk->state) { case A_PAGING: if (awalk->nextalerttime <= now) awalk->nextalerttime = next_alert(awalk); break; case A_NORECIP: break; case A_ACKED: /* Still cannot get here except if ack is still valid */ break; case A_RECOVERED: case A_DISABLED: case A_NOTIFY: awalk->state = A_DEAD; /* Fall through */ case A_DEAD: cleanup_alert(awalk); break; } } clean_all_active(); /* Pickup any finished child processes to avoid zombies */ while (wait3(&childstat, WNOHANG, NULL) > 0) ; } if (checkfn) save_checkpoint(checkfn); if (acklogfd) fclose(acklogfd); if (notiflogfd) fclose(notiflogfd); stoptrace(); MEMUNDEFINE(notiflogfn); MEMUNDEFINE(acklogfn); if (termsig >= 0) { errprintf("Terminated by signal %d\n", termsig); } return 0; }
void show_topchanges(FILE *output, countlist_t *hostcounthead, countlist_t *svccounthead, event_t *eventhead, int topcount, time_t firstevent, time_t lastevent) { fprintf(output, "<p><font size=+1>%s</font></p>\n", periodstring); fprintf(output, "<table summary=\"Top changing hosts and services\" border=1>\n"); fprintf(output, "<tr>\n"); if (hostcounthead && (output != NULL)) { countlist_t *cwalk; int i; unsigned long others = 0, totalcount = 0; strbuffer_t *s = newstrbuffer(0); strbuffer_t *othercriteria = newstrbuffer(0); if (hostregex) { addtobuffer(othercriteria, "&HOSTMATCH="); addtobuffer(othercriteria, hostregex); } if (exhostregex) addtobuffer(s, exhostregex); if (testregex) { addtobuffer(othercriteria, "&TESTMATCH="); addtobuffer(othercriteria, testregex); } if (extestregex) { addtobuffer(othercriteria, "&EXTESTMATCH="); addtobuffer(othercriteria, extestregex); } if (pageregex) { addtobuffer(othercriteria, "&PAGEMATCH="); addtobuffer(othercriteria, pageregex); } if (expageregex) { addtobuffer(othercriteria, "&EXPAGEMATCH="); addtobuffer(othercriteria, expageregex); } if (colorregex) { addtobuffer(othercriteria, "&COLORMATCH="); addtobuffer(othercriteria, colorregex); } if (ignoredialups) { addtobuffer(othercriteria, "&NODIALUPS=on"); } addtobuffer(othercriteria, "&SUMMARY=services"); addtobuffer(othercriteria, "&TIMETXT="); addtobuffer(othercriteria, periodstring); if (counttype == XYMON_COUNT_EVENTS) addtobuffer(othercriteria, "&COUNTTYPE=events"); else if (counttype == XYMON_COUNT_DURATION) addtobuffer(othercriteria, "&COUNTTYPE=duration"); fprintf(output, "<td width=40%% align=center valign=top>\n"); fprintf(output, " <table summary=\"Top %d hosts\" border=0>\n", topcount); fprintf(output, " <tr><th colspan=3>Top %d hosts</th></tr>\n", topcount); fprintf(output, " <tr><th align=left>Host</th><th align=left colspan=2>%s</th></tr>\n", (counttype == XYMON_COUNT_EVENTS) ? "State changes" : "Seconds red/yellow"); /* Compute the total count */ for (i=0, cwalk=hostcounthead; (cwalk); i++, cwalk=cwalk->next) totalcount += cwalk->total; for (i=0, cwalk=hostcounthead; (cwalk && (cwalk->total > 0)); i++, cwalk=cwalk->next) { if (i < topcount) { fprintf(output, " <tr><td align=left><a href=\"eventlog.sh?HOSTMATCH=^%s$&MAXCOUNT=-1&MAXTIME=-1&FROMTIME=%lu&TOTIME=%lu%s\">%s</a></td><td align=right>%lu</td><td align=right>(%6.2f %%)</td></tr>\n", xmh_item(cwalk->src, XMH_HOSTNAME), (unsigned long)firstevent, (unsigned long)lastevent, STRBUF(othercriteria), xmh_item(cwalk->src, XMH_HOSTNAME), cwalk->total, ((100.0 * cwalk->total) / totalcount)); if (STRBUFLEN(s) > 0) addtobuffer(s, "|"); addtobuffer(s, "^"); addtobuffer(s, xmh_item(cwalk->src, XMH_HOSTNAME)); addtobuffer(s, "$"); } else { others += cwalk->total; } } fprintf(output, " <tr><td align=left><a href=\"eventlog.sh?EXHOSTMATCH=%s&MAXCOUNT=-1&MAXTIME=-1&FROMTIME=%lu&TOTIME=%lu%s\">%s</a></td><td align=right>%lu</td><td align=right>(%6.2f %%)</td></tr>\n", STRBUF(s), (unsigned long)firstevent, (unsigned long)lastevent, STRBUF(othercriteria), "Other hosts", others, ((100.0 * others) / totalcount)); fprintf(output, " <tr><td colspan=3><hr width=\"100%%\"></td></tr>\n"); fprintf(output, " <tr><th>Total</th><th>%lu</th><th> </th></tr>\n", totalcount); fprintf(output, " </table>\n"); fprintf(output, "</td>\n"); freestrbuffer(s); freestrbuffer(othercriteria); } if (svccounthead && (output != NULL)) { countlist_t *cwalk; int i; unsigned long others = 0, totalcount = 0; strbuffer_t *s = newstrbuffer(0); strbuffer_t *othercriteria = newstrbuffer(0); if (hostregex) { addtobuffer(othercriteria, "&HOSTMATCH="); addtobuffer(othercriteria, hostregex); } if (exhostregex) { addtobuffer(othercriteria, "&EXHOSTMATCH="); addtobuffer(othercriteria, exhostregex); } if (testregex) { addtobuffer(othercriteria, "&TESTMATCH="); addtobuffer(othercriteria, testregex); } if (extestregex) addtobuffer(s, extestregex); if (pageregex) { addtobuffer(othercriteria, "&PAGEMATCH="); addtobuffer(othercriteria, pageregex); } if (expageregex) { addtobuffer(othercriteria, "&EXPAGEMATCH="); addtobuffer(othercriteria, expageregex); } if (colorregex) { addtobuffer(othercriteria, "&COLORMATCH="); addtobuffer(othercriteria, colorregex); } if (ignoredialups) { addtobuffer(othercriteria, "&NODIALUPS=on"); } addtobuffer(othercriteria, "&SUMMARY=hosts"); addtobuffer(othercriteria, "&TIMETXT="); addtobuffer(othercriteria, periodstring); if (counttype == XYMON_COUNT_EVENTS) addtobuffer(othercriteria, "&COUNTTYPE=events"); else if (counttype == XYMON_COUNT_DURATION) addtobuffer(othercriteria, "&COUNTTYPE=duration"); fprintf(output, "<td width=40%% align=center valign=top>\n"); fprintf(output, " <table summary=\"Top %d services\" border=0>\n", topcount); fprintf(output, " <tr><th colspan=3>Top %d services</th></tr>\n", topcount); fprintf(output, " <tr><th align=left>Service</th><th align=left colspan=2>%s</th></tr>\n", (counttype == XYMON_COUNT_EVENTS) ? "State changes" : "Seconds red/yellow"); /* Compute the total count */ for (i=0, cwalk=svccounthead; (cwalk); i++, cwalk=cwalk->next) totalcount += cwalk->total; for (i=0, cwalk=svccounthead; (cwalk && (cwalk->total > 0)); i++, cwalk=cwalk->next) { if (i < topcount) { fprintf(output, " <tr><td align=left><a href=\"eventlog.sh?TESTMATCH=^%s$&MAXCOUNT=-1&MAXTIME=-1&FROMTIME=%lu&TOTIME=%lu%s\">%s</a></td><td align=right>%lu</td><td align=right>(%6.2f %%)</td></tr>\n", ((htnames_t *)cwalk->src)->name, (unsigned long)firstevent, (unsigned long)lastevent, STRBUF(othercriteria), ((htnames_t *)cwalk->src)->name, cwalk->total, ((100.0 * cwalk->total) / totalcount)); if (STRBUFLEN(s) > 0) addtobuffer(s, "|"); addtobuffer(s, "^"); addtobuffer(s, ((htnames_t *)cwalk->src)->name); addtobuffer(s, "$"); } else { others += cwalk->total; } } fprintf(output, " <tr><td align=left><a href=\"eventlog.sh?EXTESTMATCH=%s&MAXCOUNT=-1&MAXTIME=-1&FROMTIME=%lu&TOTIME=%lu%s\">%s</td><td align=right>%lu</td><td align=right>(%6.2f %%)</td></tr>\n", STRBUF(s), (unsigned long)firstevent, (unsigned long)lastevent, STRBUF(othercriteria), "Other services", others, ((100.0 * others) / totalcount)); fprintf(output, " <tr><td colspan=3><hr width=\"100%%\"></td></tr>\n"); fprintf(output, " <tr><th>Total</th><th>%lu</th><th> </th></tr>\n", totalcount); fprintf(output, " </table>\n"); fprintf(output, "</td>\n"); freestrbuffer(s); freestrbuffer(othercriteria); } fprintf(output, "</tr>\n"); fprintf(output, "</table>\n"); }
static int criteriamatch(activealerts_t *alert, criteria_t *crit, criteria_t *rulecrit, int *anymatch, time_t *nexttime) { /* * See if the "crit" matches the "alert". * Match on pagespec, dgspec, hostspec, svcspec, classspec, groupspec, colors, timespec, minduration, maxduration, sendrecovered */ static char *pgnames = NULL; int pgmatchres, pgexclres; time_t duration = (getcurrenttime(NULL) - alert->eventstart); int result, cfid = 0; char *pgtok, *cfline = NULL; void *hinfo = hostinfo(alert->hostname); /* The top-level page needs a name - cannot match against an empty string */ if (pgnames) xfree(pgnames); pgnames = strdup((*alert->location == '\0') ? "/" : alert->location); if (crit) { cfid = crit->cfid; cfline = crit->cfline; } if (!cfid && rulecrit) cfid = rulecrit->cfid; if (!cfline && rulecrit) cfline = rulecrit->cfline; if (!cfline) cfline = "<undefined>"; traceprintf("Matching host:service:dgroup:page '%s:%s:%s:%s' against rule line %d\n", alert->hostname, alert->testname, xmh_item(hinfo, XMH_DGNAME), alert->location, cfid); if (alert->state == A_PAGING) { /* Check max-duration now - it's fast and easy. */ if (crit && crit->maxduration && (duration > crit->maxduration)) { traceprintf("Failed '%s' (max. duration %d>%d)\n", cfline, duration, crit->maxduration); if (!printmode) return 0; } } if (crit && crit->classspec && !namematch(alert->classname, crit->classspec, crit->classspecre)) { traceprintf("Failed '%s' (class not in include list)\n", cfline); return 0; } if (crit && crit->exclassspec && namematch(alert->classname, crit->exclassspec, crit->exclassspecre)) { traceprintf("Failed '%s' (class excluded)\n", cfline); return 0; } /* alert->groups is a comma-separated list of groups, so it needs some special handling */ /* * NB: Dont check groups when RECOVERED - the group list for recovery messages is always empty. * It doesn't matter if we match a recipient who was not in the group that originally * got the alert - we will later check who has received the alert, and only those that * have will get the recovery message. */ if (crit && (crit->groupspec || crit->exgroupspec) && (alert->state != A_RECOVERED)) { char *grouplist; char *tokptr; grouplist = (alert->groups && (*(alert->groups))) ? strdup(alert->groups) : NULL; if (crit->groupspec) { char *onegroup; int iswanted = 0; if (grouplist) { /* There is a group label on the alert, so it must match */ onegroup = strtok_r(grouplist, ",", &tokptr); while (onegroup && !iswanted) { iswanted = (namematch(onegroup, crit->groupspec, crit->groupspecre)); onegroup = strtok_r(NULL, ",", &tokptr); } } if (!iswanted) { /* * Either the alert had a group list that didn't match, or * there was no group list and the rule listed one. * In both cases, it's a failed match. */ traceprintf("Failed '%s' (group not in include list)\n", cfline); if (grouplist) xfree(grouplist); return 0; } } if (crit->exgroupspec && grouplist) { char *onegroup; /* Excluded groups are only handled when the alert does have a group list */ strcpy(grouplist, alert->groups); /* Might have been used in the include list */ onegroup = strtok_r(grouplist, ",", &tokptr); while (onegroup) { if (namematch(onegroup, crit->exgroupspec, crit->exgroupspecre)) { traceprintf("Failed '%s' (group excluded)\n", cfline); xfree(grouplist); return 0; } onegroup = strtok_r(NULL, ",", &tokptr); } } if (grouplist) xfree(grouplist); } pgmatchres = pgexclres = -1; pgtok = strtok(pgnames, ","); while (pgtok) { if (crit && crit->pagespec && (pgmatchres != 1)) pgmatchres = (namematch(pgtok, crit->pagespec, crit->pagespecre) ? 1 : 0); if (crit && crit->expagespec && (pgexclres != 1)) pgexclres = (namematch(pgtok, crit->expagespec, crit->expagespecre) ? 1 : 0); pgtok = strtok(NULL, ","); } if (pgexclres == 1) { traceprintf("Failed '%s' (pagename excluded)\n", cfline); return 0; } if (pgmatchres == 0) { traceprintf("Failed '%s' (pagename not in include list)\n", cfline); return 0; } if (crit && crit->dgspec && !namematch(xmh_item(hinfo, XMH_DGNAME), crit->dgspec, crit->dgspecre)) { traceprintf("Failed '%s' (displaygroup not in include list)\n", cfline); return 0; } if (crit && crit->exdgspec && namematch(xmh_item(hinfo, XMH_DGNAME), crit->exdgspec, crit->exdgspecre)) { traceprintf("Failed '%s' (displaygroup excluded)\n", cfline); return 0; } if (crit && crit->hostspec && !namematch(alert->hostname, crit->hostspec, crit->hostspecre)) { traceprintf("Failed '%s' (hostname not in include list)\n", cfline); return 0; } if (crit && crit->exhostspec && namematch(alert->hostname, crit->exhostspec, crit->exhostspecre)) { traceprintf("Failed '%s' (hostname excluded)\n", cfline); return 0; } if (crit && crit->svcspec && !namematch(alert->testname, crit->svcspec, crit->svcspecre)) { traceprintf("Failed '%s' (service not in include list)\n", cfline); return 0; } if (crit && crit->exsvcspec && namematch(alert->testname, crit->exsvcspec, crit->exsvcspecre)) { traceprintf("Failed '%s' (service excluded)\n", cfline); return 0; } if (alert->state == A_NOTIFY) { /* * Dont do the check until we are checking individual recipients (rulecrit is set). * You dont need to have NOTICE on the top-level rule, it's enough if a recipient * has it set. However, we do want to allow there to be a default defined in the * rule; but it doesn't take effect until we start checking the recipients. */ if (rulecrit) { int n = (crit ? crit->sendnotice : -1); traceprintf("Checking NOTICE setting %d (rule:%d)\n", n, rulecrit->sendnotice); if (crit && (crit->sendnotice == SR_NOTWANTED)) result = 0; /* Explicit NONOTICE */ else if (crit && (crit->sendnotice == SR_WANTED)) result = 1; /* Explicit NOTICE */ else result = (rulecrit->sendnotice == SR_WANTED); /* Not set, but rule has NOTICE */ } else { result = 1; } if (!result) traceprintf("Failed '%s' (notice not wanted)\n", cfline); return result; } /* At this point, we know the configuration may result in an alert. */ if (anymatch) (*anymatch)++; /* * Duration checks should be done on real paging messages only. * Not on recovery- or notify-messages. */ if (alert->state == A_PAGING) { if (crit && crit->minduration && (duration < crit->minduration)) { if (nexttime) { time_t mynext = alert->eventstart + crit->minduration; if ((*nexttime == -1) || (*nexttime > mynext)) *nexttime = mynext; } traceprintf("Failed '%s' (min. duration %d<%d)\n", cfline, duration, crit->minduration); if (!printmode) return 0; } } /* * Time restrictions apply to ALL messages. * Before 4.2, these were only applied to ALERT messages, * not RECOVERED and NOTIFY messages. This caused some * unfortunate alerts in the middle of the night because * some random system recovered ... not good. So apply * this check to all messages. */ if (crit && crit->timespec && !timematch(xmh_item(hinfo, XMH_HOLIDAYS), crit->timespec)) { traceprintf("Failed '%s' (time criteria)\n", cfline); if (!printmode) return 0; } /* Check color. For RECOVERED messages, this holds the color of the alert, not the recovery state */ if (crit && crit->colors) { result = (((1 << alert->color) & crit->colors) != 0); if (printmode) return 1; } else { result = (((1 << alert->color) & defaultcolors) != 0); if (printmode) return 1; } if (!result) { traceprintf("Failed '%s' (color)\n", cfline); return result; } if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { /* * Dont do the check until we are checking individual recipients (rulecrit is set). * You dont need to have RECOVERED on the top-level rule, it's enough if a recipient * has it set. However, we do want to allow there to be a default defined in the * rule; but it doesn't take effect until we start checking the recipients. */ if (rulecrit) { int n = (crit ? crit->sendrecovered : -1); traceprintf("Checking recovered setting %d (rule:%d)\n", n, rulecrit->sendrecovered); if (crit && (crit->sendrecovered == SR_NOTWANTED)) result = 0; /* Explicit NORECOVERED */ else if (crit && (crit->sendrecovered == SR_WANTED)) result = 1; /* Explicit RECOVERED */ else result = (rulecrit->sendrecovered == SR_WANTED); /* Not set, but rule has RECOVERED */ } else { result = 1; } if (printmode) return result; } if (result) { traceprintf("*** Match with '%s' ***\n", cfline); } return result; }
void send_alert(activealerts_t *alert, FILE *logfd) { recip_t *recip; int first = 1; int alertcount = 0; time_t now = getcurrenttime(NULL); /* A_PAGING, A_NORECIP, A_ACKED, A_RECOVERED, A_DISABLED, A_NOTIFY, A_DEAD */ char *alerttxt[A_DEAD+1] = { "Paging", "Norecip", "Acked", "Recovered", "Disabled", "Notify", "Dead" }; dbgprintf("send_alert %s:%s state %d\n", alert->hostname, alert->testname, (int)alert->state); traceprintf("send_alert %s:%s state %s\n", alert->hostname, alert->testname, alerttxt[alert->state]); stoprulefound = 0; while (!stoprulefound && ((recip = next_recipient(alert, &first, NULL, NULL)) != NULL)) { /* If this is an "UNMATCHED" rule, ignore it if we have already sent out some alert */ if (recip->unmatchedonly && (alertcount != 0)) { traceprintf("Recipient '%s' dropped, not unmatched (count=%d)\n", recip->recipient, alertcount); continue; } if (recip->noalerts && ((alert->state == A_PAGING) || (alert->state == A_RECOVERED) || (alert->state == A_DISABLED))) { traceprintf("Recipient '%s' dropped (NOALERT)\n", recip->recipient); continue; } if (recip->method == M_IGNORE) { traceprintf("IGNORE rule found\n"); continue; } if (alert->state == A_PAGING) { repeat_t *rpt = NULL; /* * This runs in a child-process context, so the record we * might create here is NOT used later on. */ rpt = find_repeatinfo(alert, recip, 1); if (!rpt) continue; /* Happens for e.g. M_IGNORE recipients */ dbgprintf(" repeat %s at %d\n", rpt->recipid, rpt->nextalert); if (rpt->nextalert > now) { traceprintf("Recipient '%s' dropped, next alert due at %ld > %ld\n", rpt->recipid, (long)rpt->nextalert, (long)now); continue; } alertcount++; } else if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { /* RECOVERED messages require that we've sent out an alert before */ repeat_t *rpt = NULL; rpt = find_repeatinfo(alert, recip, 0); if (!rpt) continue; alertcount++; } dbgprintf(" Alert for %s:%s to %s\n", alert->hostname, alert->testname, recip->recipient); switch (recip->method) { case M_IGNORE: break; case M_MAIL: { char cmd[32768]; char *mailsubj; char *mailrecip; FILE *mailpipe; MEMDEFINE(cmd); mailsubj = message_subject(alert, recip); mailrecip = message_recipient(recip->recipient, alert->hostname, alert->testname, colorname(alert->color)); if (mailsubj) { if (xgetenv("MAIL")) sprintf(cmd, "%s \"%s\" ", xgetenv("MAIL"), mailsubj); else if (xgetenv("MAILC")) sprintf(cmd, "%s -s \"%s\" ", xgetenv("MAILC"), mailsubj); else sprintf(cmd, "mail -s \"%s\" ", mailsubj); } else { if (xgetenv("MAILC")) sprintf(cmd, "%s ", xgetenv("MAILC")); else sprintf(cmd, "mail "); } strcat(cmd, mailrecip); traceprintf("Mail alert with command '%s'\n", cmd); if (testonly) { MEMUNDEFINE(cmd); break; } mailpipe = popen(cmd, "w"); if (mailpipe) { fprintf(mailpipe, "%s", message_text(alert, recip)); pclose(mailpipe); if (logfd) { init_timestamp(); fprintf(logfd, "%s %s.%s (%s) %s[%d] %ld %d", timestamp, alert->hostname, alert->testname, alert->ip, mailrecip, recip->cfid, (long)now, servicecode(alert->testname)); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { fprintf(logfd, " %ld\n", (long)(now - alert->eventstart)); } else { fprintf(logfd, "\n"); } fflush(logfd); } } else { errprintf("ERROR: Cannot open command pipe for '%s' - alert lost!\n", cmd); traceprintf("Mail pipe failed - alert lost\n"); } MEMUNDEFINE(cmd); } break; case M_SCRIPT: { pid_t scriptpid; char *scriptrecip; traceprintf("Script alert with command '%s' and recipient %s\n", recip->scriptname, recip->recipient); if (testonly) break; scriptrecip = message_recipient(recip->recipient, alert->hostname, alert->testname, colorname(alert->color)); scriptpid = fork(); if (scriptpid == 0) { /* Setup all of the environment for a paging script */ void *hinfo; char *p; int ip1=0, ip2=0, ip3=0, ip4=0; char *bbalphamsg, *ackcode, *rcpt, *bbhostname, *bbhostsvc, *bbhostsvccommas, *bbnumeric, *machip, *bbsvcname, *bbsvcnum, *bbcolorlevel, *recovered, *downsecs, *eventtstamp, *downsecsmsg, *cfidtxt; char *alertid, *alertidenv; int msglen; cfidtxt = (char *)malloc(strlen("CFID=") + 10); sprintf(cfidtxt, "CFID=%d", recip->cfid); putenv(cfidtxt); p = message_text(alert, recip); msglen = strlen(p); if (msglen > MAX_ALERTMSG_SCRIPTS) { dbgprintf("Cropping large alert message from %d to %d bytes\n", msglen, MAX_ALERTMSG_SCRIPTS); msglen = MAX_ALERTMSG_SCRIPTS; } msglen += strlen("BBALPHAMSG="); bbalphamsg = (char *)malloc(msglen + 1); snprintf(bbalphamsg, msglen+1, "BBALPHAMSG=%s", p); putenv(bbalphamsg); ackcode = (char *)malloc(strlen("ACKCODE=") + 10); sprintf(ackcode, "ACKCODE=%d", alert->cookie); putenv(ackcode); rcpt = (char *)malloc(strlen("RCPT=") + strlen(scriptrecip) + 1); sprintf(rcpt, "RCPT=%s", scriptrecip); putenv(rcpt); bbhostname = (char *)malloc(strlen("BBHOSTNAME=") + strlen(alert->hostname) + 1); sprintf(bbhostname, "BBHOSTNAME=%s", alert->hostname); putenv(bbhostname); bbhostsvc = (char *)malloc(strlen("BBHOSTSVC=") + strlen(alert->hostname) + 1 + strlen(alert->testname) + 1); sprintf(bbhostsvc, "BBHOSTSVC=%s.%s", alert->hostname, alert->testname); putenv(bbhostsvc); bbhostsvccommas = (char *)malloc(strlen("BBHOSTSVCCOMMAS=") + strlen(alert->hostname) + 1 + strlen(alert->testname) + 1); sprintf(bbhostsvccommas, "BBHOSTSVCCOMMAS=%s.%s", commafy(alert->hostname), alert->testname); putenv(bbhostsvccommas); bbnumeric = (char *)malloc(strlen("BBNUMERIC=") + 22 + 1); p = bbnumeric; p += sprintf(p, "BBNUMERIC="); p += sprintf(p, "%03d", servicecode(alert->testname)); sscanf(alert->ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4); p += sprintf(p, "%03d%03d%03d%03d", ip1, ip2, ip3, ip4); p += sprintf(p, "%d", alert->cookie); putenv(bbnumeric); machip = (char *)malloc(strlen("MACHIP=") + 13); sprintf(machip, "MACHIP=%03d%03d%03d%03d", ip1, ip2, ip3, ip4); putenv(machip); bbsvcname = (char *)malloc(strlen("BBSVCNAME=") + strlen(alert->testname) + 1); sprintf(bbsvcname, "BBSVCNAME=%s", alert->testname); putenv(bbsvcname); bbsvcnum = (char *)malloc(strlen("BBSVCNUM=") + 10); sprintf(bbsvcnum, "BBSVCNUM=%d", servicecode(alert->testname)); putenv(bbsvcnum); bbcolorlevel = (char *)malloc(strlen("BBCOLORLEVEL=") + strlen(colorname(alert->color)) + 1); sprintf(bbcolorlevel, "BBCOLORLEVEL=%s", colorname(alert->color)); putenv(bbcolorlevel); recovered = (char *)malloc(strlen("RECOVERED=") + 2); switch (alert->state) { case A_RECOVERED: strcpy(recovered, "RECOVERED=1"); break; case A_DISABLED: strcpy(recovered, "RECOVERED=2"); break; default: strcpy(recovered, "RECOVERED=0"); break; } putenv(recovered); downsecs = (char *)malloc(strlen("DOWNSECS=") + 20); sprintf(downsecs, "DOWNSECS=%ld", (long)(getcurrenttime(NULL) - alert->eventstart)); putenv(downsecs); eventtstamp = (char *)malloc(strlen("EVENTSTART=") + 20); sprintf(eventtstamp, "EVENTSTART=%ld", (long)alert->eventstart); putenv(eventtstamp); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { downsecsmsg = (char *)malloc(strlen("DOWNSECSMSG=Event duration :") + 20); sprintf(downsecsmsg, "DOWNSECSMSG=Event duration : %ld", (long)(getcurrenttime(NULL) - alert->eventstart)); } else { downsecsmsg = strdup("DOWNSECSMSG="); } putenv(downsecsmsg); alertid = make_alertid(alert->hostname, alert->testname, alert->eventstart); alertidenv = (char *)malloc(strlen("ALERTID=") + strlen(alertid) + 10); sprintf(alertidenv, "ALERTID=%s", alertid); putenv(alertidenv); hinfo = hostinfo(alert->hostname); if (hinfo) { enum xmh_item_t walk; char *itm, *id, *bbhenv; for (walk = 0; (walk < XMH_LAST); walk++) { itm = xmh_item(hinfo, walk); id = xmh_item_id(walk); if (itm && id) { bbhenv = (char *)malloc(strlen(id) + strlen(itm) + 2); sprintf(bbhenv, "%s=%s", id, itm); putenv(bbhenv); } } } /* The child starts the script */ execlp(recip->scriptname, recip->scriptname, NULL); errprintf("Could not launch paging script %s: %s\n", recip->scriptname, strerror(errno)); exit(0); } else if (scriptpid > 0) { /* Parent waits for child to complete */ int childstat; wait(&childstat); if (WIFEXITED(childstat) && (WEXITSTATUS(childstat) != 0)) { errprintf("Paging script %s terminated with status %d\n", recip->scriptname, WEXITSTATUS(childstat)); } else if (WIFSIGNALED(childstat)) { errprintf("Paging script %s terminated by signal %d\n", recip->scriptname, WTERMSIG(childstat)); } if (logfd) { init_timestamp(); fprintf(logfd, "%s %s.%s (%s) %s %ld %d", timestamp, alert->hostname, alert->testname, alert->ip, scriptrecip, (long)now, servicecode(alert->testname)); if ((alert->state == A_RECOVERED) || (alert->state == A_DISABLED)) { fprintf(logfd, " %ld\n", (long)(now - alert->eventstart)); } else { fprintf(logfd, "\n"); } fflush(logfd); } } else { errprintf("ERROR: Fork failed to launch script '%s' - alert lost\n", recip->scriptname); traceprintf("Script fork failed - alert lost\n"); } } break; } } }
static void count_duration(time_t fromtime, time_t totime, pcre *pageregexp, pcre *expageregexp, pcre *hostregexp, pcre *exhostregexp, pcre *testregexp, pcre *extestregexp, int ignoredialups, f_hostcheck hostcheck, event_t *eventhead, countlist_t **hostcounthead, countlist_t **svccounthead) { void *hwalk; elist_t *lwalk; event_t *ewalk; ed_t *ed; sendreturn_t *bdata; /* * Restructure the event-list so we have a tree instead: * * HostRecord * | *Data ----> EventList * | | *Service * | | *EventHead --> Event --> Event --> Event * | | *EventTail --------------------------^ * | | * | v * | * v * */ for (ewalk = eventhead; (ewalk); ewalk = ewalk->next) { lwalk = (elist_t *)xmh_item(ewalk->host, XMH_DATA); while (lwalk && (lwalk->svc != ewalk->service)) lwalk = lwalk->next; if (lwalk == NULL) { lwalk = (elist_t *)calloc(1, sizeof(elist_t)); lwalk->svc = ewalk->service; lwalk->next = (elist_t *)xmh_item(ewalk->host, XMH_DATA); xmh_set_item(ewalk->host, XMH_DATA, (void *)lwalk); } ed = (ed_t *)calloc(1, sizeof(ed_t)); ed->event = ewalk; ed->next = lwalk->head; if (lwalk->head == NULL) lwalk->tail = ed; lwalk->head = ed; } if (debug) { printf("\n\nEventtree before fixups\n\n"); dump_eventtree(); } /* * Next, we must add a pseudo record for the current state. * This is for those statuses that haven't changed since the * start of our data-collection period - they won't have any events * so we cannot tell what color they are. By grabbing the current * color we can add a pseudo-event that lets us determine what the * color has been since the start of the event-period. */ bdata = newsendreturnbuf(1, NULL); if (sendmessage("xymondboard fields=hostname,testname,color,lastchange", NULL, XYMON_TIMEOUT, bdata) == XYMONSEND_OK) { char *bol, *eol; char *hname, *tname; int color; time_t lastchange; void *hrec; htnames_t *srec; char *icname = xgetenv("INFOCOLUMN"); char *tcname = xgetenv("TRENDSCOLUMN"); bol = getsendreturnstr(bdata, 0); while (bol) { eol = strchr(bol, '\n'); if (eol) *eol = '\0'; hname = strtok(bol, "|"); tname = (hname ? strtok(NULL, "|") : NULL); color = (tname ? parse_color(strtok(NULL, "|")) : -1); lastchange = ((color != -1) ? atol(strtok(NULL, "\n")) : totime+1); if (hname && tname && (color != -1) && (strcmp(tname, icname) != 0) && (strcmp(tname, tcname) != 0)) { hrec = hostinfo(hname); srec = getname(tname, 1); if (eventfilter(hrec, tname, pageregexp, expageregexp, hostregexp, exhostregexp, testregexp, extestregexp, ignoredialups, hostcheck) == 0) goto nextrecord; lwalk = (elist_t *)xmh_item(hrec, XMH_DATA); while (lwalk && (lwalk->svc != srec)) lwalk = lwalk->next; if (lwalk == NULL) { lwalk = (elist_t *)calloc(1, sizeof(elist_t)); lwalk->svc = srec; lwalk->next = (elist_t *)xmh_item(hrec, XMH_DATA); xmh_set_item(hrec, XMH_DATA, (void *)lwalk); } /* See if we already have an event past the "totime" value */ if (lwalk->head) { ed = lwalk->head; while (ed && (ed->event->eventtime < totime)) ed = ed->next; if (ed) { ed->next = NULL; lwalk->tail = ed; } else { ed = (ed_t *)calloc(1, sizeof(ed_t)); ed->event = (event_t *)calloc(1, sizeof(event_t)); lwalk->tail->next = ed; ed->event->host = hrec; ed->event->service = srec; ed->event->eventtime = totime; ed->event->changetime = lwalk->tail->event->eventtime; ed->event->duration = (totime - lwalk->tail->event->eventtime); ed->event->newcolor = -1; ed->event->oldcolor = lwalk->tail->event->newcolor; ed->event->next = NULL; ed->next = NULL; lwalk->tail = ed; } } else if (lastchange < totime) { ed = (ed_t *)calloc(1, sizeof(ed_t)); ed->event = (event_t *)calloc(1, sizeof(event_t)); ed->event->host = hrec; ed->event->service = srec; ed->event->eventtime = totime; ed->event->changetime = (lwalk->tail ? lwalk->tail->event->eventtime : fromtime); ed->event->duration = (totime - ed->event->changetime); ed->event->newcolor = color; ed->event->oldcolor = (lwalk->tail ? lwalk->tail->event->newcolor : color); ed->event->next = NULL; ed->next = NULL; lwalk->head = lwalk->tail = ed; } } nextrecord: bol = (eol ? eol+1 : NULL); } freesendreturnbuf(bdata); } else { errprintf("Cannot get the current state\n"); freesendreturnbuf(bdata); return; } if (debug) { printf("\n\nEventtree after pseudo-events\n\n"); dump_eventtree(); } /* * Fixup the beginning-time (and duration) of the first events recorded. * This is to handle events that begin BEFORE our event-logging period. * Fixup the end-time (and duration) of the last events recorded. * This is to handle events that end AFTER our event-logging period. */ for (hwalk = first_host(); (hwalk); hwalk = next_host(hwalk, 0)) { elist_t *lwalk; event_t *erec; ed_t *ewalk; lwalk = (elist_t *)xmh_item(hwalk, XMH_DATA); while (lwalk) { if (lwalk->head) { erec = lwalk->head->event; if (erec->changetime > totime) { /* First event is after our start-time. Drop the events */ lwalk->head = lwalk->tail = NULL; } else if (erec->changetime < fromtime) { /* First event is before our start-time. Adjust to starttime. */ erec->changetime = fromtime; erec->duration = (erec->eventtime - fromtime); } ewalk = lwalk->head; while (ewalk && (ewalk->event->eventtime < totime)) ewalk = ewalk->next; if (ewalk) { lwalk->tail = ewalk; lwalk->tail->next = 0; } if (lwalk->tail) { erec = lwalk->tail->event; if (erec->eventtime > totime) { /* Last event is after our end-time. Adjust to end-time */ erec->eventtime = totime; erec->duration = (totime - erec->changetime); } } } lwalk = lwalk->next; } } if (debug) { printf("\n\nEventtree after fixups\n\n"); dump_eventtree(); } for (hwalk = first_host(); (hwalk); hwalk = next_host(hwalk, 0)) { countlist_t *hrec, *srec; hrec = (countlist_t *)malloc(sizeof(countlist_t)); hrec->src = hwalk; hrec->total = 0; hrec->next = *hostcounthead; *hostcounthead = hrec; lwalk = (elist_t *)xmh_item(hwalk, XMH_DATA); while (lwalk) { for (srec = *svccounthead; (srec && (srec->src != (void *)lwalk->svc)); srec = srec->next) ; if (!srec) { srec = (countlist_t *)malloc(sizeof(countlist_t)); srec->src = (void *)lwalk->svc; srec->total = 0; srec->next = *svccounthead; *svccounthead = srec; } if (lwalk->head) { ed_t *ewalk = lwalk->head; while (ewalk) { if (ewalk->event->oldcolor >= COL_YELLOW) { hrec->total += ewalk->event->duration; srec->total += ewalk->event->duration; } ewalk = ewalk->next; } } lwalk = lwalk->next; } } if (debug) dump_countlists(*hostcounthead, *svccounthead); }
int main(int argc, char *argv[]) { char histlogfn[PATH_MAX]; FILE *fd; char textrepfullfn[PATH_MAX], textrepfn[1024], textrepurl[PATH_MAX]; FILE *textrep; reportinfo_t repinfo; int argi; char *envarea = NULL; void *hinfo; for (argi=1; (argi < argc); argi++) { if (argnmatch(argv[argi], "--env=")) { char *p = strchr(argv[argi], '='); loadenv(p+1, envarea); } else if (argnmatch(argv[argi], "--area=")) { char *p = strchr(argv[argi], '='); envarea = strdup(p+1); } } redirect_cgilog("reportlog"); cgidata = cgi_request(); parse_query(); load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); if ((hinfo = hostinfo(hostname)) == NULL) { errormsg("No such host"); return 1; } ip = xmh_item(hinfo, XMH_IP); displayname = xmh_item(hinfo, XMH_DISPLAYNAME); if (!displayname) displayname = hostname; sprintf(histlogfn, "%s/%s.%s", xgetenv("XYMONHISTDIR"), commafy(hostname), service); fd = fopen(histlogfn, "r"); if (fd == NULL) { errormsg("Cannot open history file"); } color = parse_historyfile(fd, &repinfo, hostname, service, st, end, 0, reportwarnlevel, reportgreenlevel, reportwarnstops, reporttime); fclose(fd); sprintf(textrepfn, "avail-%s-%s-%u-%u.txt", hostname, service, (unsigned int)getcurrenttime(NULL), (int)getpid()); sprintf(textrepfullfn, "%s/%s", xgetenv("XYMONREPDIR"), textrepfn); sprintf(textrepurl, "%s/%s", xgetenv("XYMONREPURL"), textrepfn); textrep = fopen(textrepfullfn, "w"); /* Now generate the webpage */ printf("Content-Type: %s\n\n", xgetenv("HTMLCONTENTTYPE")); generate_replog(stdout, textrep, textrepurl, hostname, service, color, style, ip, displayname, st, end, reportwarnlevel, reportgreenlevel, reportwarnstops, &repinfo); if (textrep) fclose(textrep); return 0; }
void do_acknowledgementslog(FILE *output, int maxcount, int maxminutes, char *fromtime, char *totime, char *pageregex, char *expageregex, char *hostregex, char *exhostregex, char *testregex, char *extestregex, char *rcptregex, char *exrcptregex) { FILE *acknowledgementslog; char acknowledgementslogfilename[PATH_MAX]; time_t firstevent = 0; time_t lastevent = getcurrenttime(NULL); acknowledgements_t *head, *walk; struct stat st; char l[MAX_LINE_LEN]; char title[200]; /* For the PCRE matching */ const char *errmsg = NULL; int errofs = 0; pcre *pageregexp = NULL; pcre *expageregexp = NULL; pcre *hostregexp = NULL; pcre *exhostregexp = NULL; pcre *testregexp = NULL; pcre *extestregexp = NULL; pcre *rcptregexp = NULL; pcre *exrcptregexp = NULL; if (maxminutes && (fromtime || totime)) { fprintf(output, "<B>Only one time interval type is allowed!</B>"); return; } if (fromtime) { firstevent = convert_time(fromtime); if(firstevent < 0) { fprintf(output,"<B>Invalid 'from' time: %s</B>", htmlquoted(fromtime)); return; } } else if (maxminutes) { firstevent = getcurrenttime(NULL) - maxminutes*60; } else { firstevent = getcurrenttime(NULL) - 86400; } if (totime) { lastevent = convert_time(totime); if (lastevent < 0) { fprintf(output,"<B>Invalid 'to' time: %s</B>", htmlquoted(totime)); return; } if (lastevent < firstevent) { fprintf(output,"<B>'to' time must be after 'from' time.</B>"); return; } } if (!maxcount) maxcount = 100; if (pageregex && *pageregex) pageregexp = pcre_compile(pageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (expageregex && *expageregex) expageregexp = pcre_compile(expageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (hostregex && *hostregex) hostregexp = pcre_compile(hostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (exhostregex && *exhostregex) exhostregexp = pcre_compile(exhostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (testregex && *testregex) testregexp = pcre_compile(testregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (extestregex && *extestregex) extestregexp = pcre_compile(extestregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (rcptregex && *rcptregex) rcptregexp = pcre_compile(rcptregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (exrcptregex && *exrcptregex) exrcptregexp = pcre_compile(exrcptregex, PCRE_CASELESS, &errmsg, &errofs, NULL); sprintf(acknowledgementslogfilename, "%s/acknowledge.log", xgetenv("XYMONSERVERLOGS")); acknowledgementslog = fopen(acknowledgementslogfilename, "r"); if (acknowledgementslog && (stat(acknowledgementslogfilename, &st) == 0)) { time_t curtime; int done = 0; /* Find a spot in the acknowledgements log file close to where the firstevent time is */ fseeko(acknowledgementslog, 0, SEEK_END); do { /* Go back maxcount*80 bytes - one entry is ~80 bytes */ if (ftello(acknowledgementslog) > maxcount*80) { unsigned int uicurtime; fseeko(acknowledgementslog, -maxcount*80, SEEK_CUR); if (fgets(l, sizeof(l), acknowledgementslog) && /* Skip to start of line */ fgets(l, sizeof(l), acknowledgementslog)) { /* 2015-03-07 18:17:03 myserver disk andy 1 1425724570 1425752223 1425838623 testing message */ if ( sscanf(l, "%*u-%*u-%*u %*u:%*u:%*u %*s %*s %*s %*u %*u %u %*u %*s", &uicurtime) == 0 ) { /* that didnt work - try the old format 1430040985 630949 30 630949 np_filename_not_used myserver.procs red testing log format \nAcked by: andy (127.0.0.1) */ sscanf(l, "%u\t%*u\t%*u\t%*u\tnp_filename_not_used\t%*s\t%*s\t%*s", &uicurtime); } curtime = uicurtime; done = (curtime < firstevent); } else { fprintf(output, "Error reading logfile %s: %s\n", acknowledgementslogfilename, strerror(errno)); return; } } else { rewind(acknowledgementslog); done = 1; } } while (!done); } head = NULL; while (acknowledgementslog && (fgets(l, sizeof(l), acknowledgementslog))) { unsigned int etim; unsigned int valid; int duration; time_t eventtime; time_t validity; char host[MAX_LINE_LEN]; char svc[MAX_LINE_LEN]; char recipient[MAX_LINE_LEN]; char message[MAX_LINE_LEN]; char *hostname, *svcname, *p; int itemsfound, pagematch, hostmatch, testmatch, rcptmatch; acknowledgements_t *newrec; void *eventhost; struct htnames_t *eventcolumn; int ovector[30]; /* 2015-03-07 18:17:03 myserver disk andy 1 1425724570 1425752223 1425838623 testing message */ itemsfound = sscanf(l, "%*u-%*u-%*u %*u:%*u:%*u %s %s %s %*u %*u %u %u %[^\t\n]", host, svc, recipient, &etim, &valid, message); if (itemsfound != 6) { /* 1430040985 630949 30 630949 np_filename_not_used myserver.procs red testing log format \nAcked by: andy (127.0.0.1) */ itemsfound = sscanf(l, "%u\t%*u\t%d\t%*u\tnp_filename_not_used\t%s\t%*s\t%[^\n]", &etim, &duration, host, message); if (itemsfound != 4) continue; p = strrchr(host, '.'); if (p) { *p = '\0'; strcpy(svc,p+1); } /* Xymon uses \n in the ack message, for the "acked by" data. Cut it off. */ p = strstr(message, "\\nAcked by:"); if (p) { strcpy(recipient,p+12); *(p-1) = '\0'; } else { strcpy(recipient,"UnknownUser"); } p = strchr(recipient, '('); if (p) *(p-1) = '\0'; } eventtime = etim; if (eventtime < firstevent) continue; if (eventtime > lastevent) break; validity = duration ? (etim + duration * 60) : valid; hostname = host; svcname = svc; eventhost = hostinfo(hostname); if (!eventhost) continue; /* Dont report hosts that no longer exist */ eventcolumn = getname(svcname, 1); p = strchr(recipient, '['); if (p) *p = '\0'; if (pageregexp) { char *pagename; pagename = xmh_item_multi(eventhost, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(pageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 1; if (!pagematch) continue; if (expageregexp) { char *pagename; pagename = xmh_item_multi(eventhost, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(expageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 0; if (pagematch) continue; if (hostregexp) hostmatch = (pcre_exec(hostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 1; if (!hostmatch) continue; if (exhostregexp) hostmatch = (pcre_exec(exhostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 0; if (hostmatch) continue; if (testregexp) testmatch = (pcre_exec(testregexp, NULL, svcname, strlen(svcname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 1; if (!testmatch) continue; if (extestregexp) testmatch = (pcre_exec(extestregexp, NULL, svcname, strlen(svcname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 0; if (testmatch) continue; if (rcptregexp) rcptmatch = (pcre_exec(rcptregexp, NULL, recipient, strlen(recipient), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else rcptmatch = 1; if (!rcptmatch) continue; if (exrcptregexp) rcptmatch = (pcre_exec(exrcptregexp, NULL, recipient, strlen(recipient), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else rcptmatch = 0; if (rcptmatch) continue; newrec = (acknowledgements_t *) malloc(sizeof(acknowledgements_t)); newrec->host = eventhost; newrec->service = eventcolumn; newrec->eventtime = eventtime; newrec->validity = validity; newrec->recipient = strdup(recipient); newrec->message = strdup(message); newrec->next = head; head = newrec; } if (head) { char *bgcolors[2] = { "#000000", "#000066" }; int bgcolor = 0; int count; struct acknowledgements_t *lasttoshow = head; count=0; walk=head; do { count++; lasttoshow = walk; walk = walk->next; } while (walk && (count<maxcount)); if (maxminutes) { sprintf(title, "%d acknowledgements in the past %u minutes", count, (unsigned int)((getcurrenttime(NULL) - lasttoshow->eventtime) / 60)); } else { sprintf(title, "%d events acknowledged.", count); } fprintf(output, "<BR><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"Acknowledgements log\" BORDER=0>\n"); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=6><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD></TR>\n", htmlquoted(title)); fprintf(output, "<TR BGCOLOR=\"#333333\"><TH>Time</TH><TH>Valid Until</TH><TH>Host</TH><TH>Service</TH><TH>Acknowledged By</TH><TH>Message</TH></TR>\n"); for (walk=head; (walk != lasttoshow->next); walk=walk->next) { char *hostname = xmh_item(walk->host, XMH_HOSTNAME); fprintf(output, "<TR BGCOLOR=%s>\n", bgcolors[bgcolor]); bgcolor = ((bgcolor + 1) % 2); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", ctime(&walk->eventtime)); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", ctime(&walk->validity)); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", hostname); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", walk->service->name); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", walk->recipient); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", walk->message); } fprintf(output, "</TABLE>\n"); /* Clean up */ walk = head; do { struct acknowledgements_t *tmp = walk; walk = walk->next; xfree(tmp->recipient); xfree(tmp->message); xfree(tmp); } while (walk); } else { /* No acknowledgements during the past maxminutes */ if (acknowledgementslog) sprintf(title, "No events acknowledged in the last %d minutes", maxminutes); else strcpy(title, "No acknowledgements logged"); fprintf(output, "<CENTER><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"%s\" BORDER=0>\n", title); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=6><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD>\n", htmlquoted(title)); fprintf(output, "</TR>\n"); fprintf(output, "</TABLE>\n"); fprintf(output, "</CENTER>\n"); } if (acknowledgementslog) fclose(acknowledgementslog); if (pageregexp) pcre_free(pageregexp); if (expageregexp) pcre_free(expageregexp); if (hostregexp) pcre_free(hostregexp); if (exhostregexp) pcre_free(exhostregexp); if (testregexp) pcre_free(testregexp); if (extestregexp) pcre_free(extestregexp); if (rcptregexp) pcre_free(rcptregexp); if (exrcptregexp) pcre_free(exrcptregexp); }
void print_hoststatus(FILE *output, hstatus_t *itm, RbtHandle columns, int prio, int firsthost) { void *hinfo; char *dispname, *ip, *key; time_t now; RbtIterator colhandle; now = getcurrenttime(NULL); hinfo = hostinfo(itm->hostname); dispname = xmh_item(hinfo, XMH_DISPLAYNAME); ip = xmh_item(hinfo, XMH_IP); fprintf(output, "<TR>\n"); /* Print the priority */ fprintf(output, "<TD ALIGN=LEFT VALIGN=TOP WIDTH=25%% NOWRAP>"); if (firsthost) fprintf(output, "<FONT %s>PRIO %d</FONT>", xgetenv("XYMONPAGEROWFONT"), prio); else fprintf(output, " "); fprintf(output, "</TD>\n"); /* Print the hostname with a link to the critical systems info page */ fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", hostnamehtml(itm->hostname, NULL, usetooltips)); key = (char *)malloc(strlen(itm->hostname) + 1024); for (colhandle = rbtBegin(columns); (colhandle != rbtEnd(columns)); colhandle = rbtNext(columns, colhandle)) { void *k1, *k2; char *colname; RbtIterator sthandle; fprintf(output, "<TD ALIGN=CENTER>"); rbtKeyValue(columns, colhandle, &k1, &k2); colname = (char *)k1; sprintf(key, "%s|%s", itm->hostname, colname); sthandle = rbtFind(rbstate, key); if (sthandle == rbtEnd(rbstate)) { fprintf(output, "-"); } else { hstatus_t *column; char *htmlalttag; char *htmlackstr; rbtKeyValue(rbstate, sthandle, &k1, &k2); column = (hstatus_t *)k2; if (column->config->priority != prio) fprintf(output, "-"); else { time_t age = now - column->lastchange; char *htmlgroupstr; char *htmlextrastr; htmlalttag = alttag(colname, column->color, 0, 1, agestring(age)); htmlackstr = (column->ackmsg ? column->ackmsg : ""); htmlgroupstr = strdup(urlencode(column->config->ttgroup ? column->config->ttgroup : "")); htmlextrastr = strdup(urlencode(column->config->ttextra ? column->config->ttextra : "")); fprintf(output, "<A HREF=\"%s&NKPRIO=%d&NKTTGROUP=%s&NKTTEXTRA=%s\">", hostsvcurl(itm->hostname, colname, 1), prio, htmlgroupstr, htmlextrastr); fprintf(output, "<IMG SRC=\"%s/%s\" ALT=\"%s\" TITLE=\"%s %s\" HEIGHT=\"%s\" WIDTH=\"%s\" BORDER=0></A>", xgetenv("XYMONSKIN"), dotgiffilename(column->color, (column->acktime > 0), (age > oldlimit)), colorname(column->color), htmlalttag, htmlackstr, xgetenv("DOTHEIGHT"), xgetenv("DOTWIDTH")); xfree(htmlgroupstr); xfree(htmlextrastr); } } fprintf(output, "</TD>\n"); } xfree(key); fprintf(output, "</TR>\n"); }
int web_access_allowed(char *username, char *hostname, char *testname, web_access_type_t acc) { void *hinfo; char *pages, *onepg, *key; hinfo = hostinfo(hostname); if (!hinfo || !acctree || !username) return 0; /* Check for "root" access first */ key = (char *)malloc(strlen(username) + 6); sprintf(key, "root %s", username); if (xtreeFind(acctree, key) != xtreeEnd(acctree)) { xfree(key); return 1; } xfree(key); pages = strdup(xmh_item(hinfo, XMH_ALLPAGEPATHS)); onepg = strtok(pages, ","); while (onepg) { char *p; p = strchr(onepg, '/'); if (p) *p = '\0'; /* Will only look at the top-level path element */ key = (char *)malloc(strlen(onepg) + strlen(username) + 2); sprintf(key, "%s %s", onepg, username); if (xtreeFind(acctree, key) != xtreeEnd(acctree)) { xfree(key); xfree(pages); return 1; } xfree(key); onepg = strtok(NULL, ","); } xfree(pages); if (hostname) { /* See if user is a member of a group named by the hostname */ key = (char *)malloc(strlen(hostname) + strlen(username) + 2); sprintf(key, "%s %s", hostname, username); if (xtreeFind(acctree, key) != xtreeEnd(acctree)) { xfree(key); return 1; } xfree(key); } if (testname) { /* See if user is a member of a group named by the testname */ key = (char *)malloc(strlen(testname) + strlen(username) + 2); sprintf(key, "%s %s", testname, username); if (xtreeFind(acctree, key) != xtreeEnd(acctree)) { xfree(key); return 1; } xfree(key); } return 0; }
void print_hoststatus(FILE *output, hstatus_t *itm, void * statetree, void * columns, int prio, int firsthost, int firsthostever) { void *hinfo; char *dispname, *ip, *key; time_t now; xtreePos_t colhandle; now = getcurrenttime(NULL); hinfo = hostinfo(itm->hostname); dispname = xmh_item(hinfo, XMH_DISPLAYNAME); ip = xmh_item(hinfo, XMH_IP); fprintf(output, "<TR>\n"); /* Print the priority */ fprintf(output, "<TD ALIGN=LEFT VALIGN=TOP WIDTH=10%% NOWRAP>"); if (firsthost) if (prio == 99) { if (firsthostever) /* Only non-prioritised hosts, so just drop that text */ fprintf(output, " "); else fprintf(output, "<FONT %s>No priority</FONT>", xgetenv("XYMONPAGEROWFONT")); } else { fprintf(output, "<FONT %s>PRIO %d</FONT>", xgetenv("XYMONPAGEROWFONT"), prio); } else fprintf(output, " "); fprintf(output, "</TD>\n"); /* Print the hostname with a link to the critical systems info page */ fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", hostnamehtml(itm->hostname, NULL, usetooltips)); key = (char *)malloc(1024); for (colhandle = xtreeFirst(columns); (colhandle != xtreeEnd(columns)); colhandle = xtreeNext(columns, colhandle)) { char *colname; xtreePos_t sthandle; fprintf(output, "<TD ALIGN=CENTER>"); colname = (char *)xtreeKey(columns, colhandle); key = (char *)realloc(key, 2 + strlen(itm->hostname) + strlen(colname)); sprintf(key, "%s|%s", itm->hostname, colname); sthandle = xtreeFind(statetree, key); if (sthandle == xtreeEnd(statetree)) { fprintf(output, "-"); } else { hstatus_t *column; char *htmlalttag; char *htmlackstr; column = (hstatus_t *)xtreeData(statetree, sthandle); if (column->config->priority != prio) fprintf(output, "-"); else { time_t age = now - column->lastchange; char *htmlgroupstr; char *htmlextrastr; htmlalttag = alttag(colname, column->color, 0, 1, agestring(age)); htmlackstr = (column->ackmsg ? column->ackmsg : ""); htmlgroupstr = strdup(urlencode(column->config->ttgroup ? column->config->ttgroup : "")); htmlextrastr = strdup(urlencode(column->config->ttextra ? column->config->ttextra : "")); fprintf(output, "<A HREF=\"%s&NKPRIO=%d&NKTTGROUP=%s&NKTTEXTRA=%s\">", hostsvcurl(itm->hostname, colname, 1), prio, htmlgroupstr, htmlextrastr); fprintf(output, "<IMG SRC=\"%s/%s\" ALT=\"%s\" TITLE=\"%s %s\" HEIGHT=\"%s\" WIDTH=\"%s\" BORDER=0></A>", xgetenv("XYMONSKIN"), dotgiffilename(column->color, (column->acktime > 0), (age > oldlimit)), colorname(column->color), htmlalttag, htmlackstr, xgetenv("DOTHEIGHT"), xgetenv("DOTWIDTH")); xfree(htmlgroupstr); xfree(htmlextrastr); } } fprintf(output, "</TD>\n"); } xfree(key); fprintf(output, "</TR>\n"); }
void send_test_results(listhead_t *head, char *collector, int issubmodule, char *location, int usebackfeed) { char msgline[4096]; listitem_t *walk; xtreePos_t handle; void *hostresults = xtreeNew(strcasecmp); if (usebackfeed) combo_start_local(); else combo_start(); for (walk = head->head; (walk); walk = walk->next) { hostresult_t *hres; myconn_t *rec = (myconn_t *)walk->data; char *s; if (rec->ignoreresult) continue; switch (rec->talkprotocol) { case TALK_PROTO_PING: if (!issubmodule && (rec->talkresult == TALK_OK)) { add_to_sub_queue(rec, "ping", location, NULL); continue; } break; #ifdef HAVE_LDAP case TALK_PROTO_LDAP: if (!issubmodule && (rec->talkresult == TALK_OK)) { char *creds = xmh_item(rec->hostinfo, XMH_LDAPLOGIN); add_to_sub_queue(rec, "ldap", location, creds, NULL); continue; } break; #endif case TALK_PROTO_EXTERNAL: if (!issubmodule && (rec->talkresult == TALK_OK)) { if (strncmp(rec->testspec, "rpc=", 4) == 0) add_to_sub_queue(rec, "rpc", location, NULL); else add_to_sub_queue(rec, rec->testspec, location, NULL); continue; } break; default: break; } handle = xtreeFind(hostresults, xmh_item(rec->hostinfo, XMH_HOSTNAME)); if (handle == xtreeEnd(hostresults)) { hres = (hostresult_t *)calloc(1, sizeof(hostresult_t)); hres->hinfo = rec->hostinfo; hres->txt = newstrbuffer(0); xtreeAdd(hostresults, xmh_item(rec->hostinfo, XMH_HOSTNAME), hres); snprintf(msgline, sizeof(msgline), "client/%s %s.netcollect netcollect\n", collector, xmh_item(rec->hostinfo, XMH_HOSTNAME)); addtobuffer(hres->txt, msgline); } else { hres = xtreeData(hostresults, handle); } switch (rec->talkprotocol) { case TALK_PROTO_DNSQUERY: snprintf(msgline, sizeof(msgline), "\n[dns=%s]\n", rec->testspec); addtobuffer(hres->txt, msgline); break; case TALK_PROTO_PING: snprintf(msgline, sizeof(msgline), "\n[ping=%s]\n", rec->netparams.destinationip); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "SourceSpec: %s\n", rec->testspec); addtobuffer(hres->txt, msgline); break; default: snprintf(msgline, sizeof(msgline), "\n[%s]\n", rec->testspec); addtobuffer(hres->txt, msgline); break; } snprintf(msgline, sizeof(msgline), "Handler: %s\n", talkproto_names[rec->talkprotocol]); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "StartTime: %d\nEndTime: %d\n", (int)rec->teststarttime, (int)rec->testendtime); addtobuffer(hres->txt, msgline); if (rec->netparams.lookupstring) { snprintf(msgline, sizeof(msgline), "TargetHostname: %s\n", rec->netparams.lookupstring); addtobuffer(hres->txt, msgline); } snprintf(msgline, sizeof(msgline), "TargetIP: %s\n", rec->netparams.destinationip); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "TargetPort: %d\n", rec->netparams.destinationport); addtobuffer(hres->txt, msgline); if (rec->netparams.sourceip) { snprintf(msgline, sizeof(msgline), "SourceIP: %s\n", rec->netparams.sourceip); addtobuffer(hres->txt, msgline); } if (location) { snprintf(msgline, sizeof(msgline), "Location: %s\n", location); addtobuffer(hres->txt, msgline); } if (rec->talkprotocol == TALK_PROTO_PING) s = "ICMP"; else { switch (rec->netparams.socktype) { case CONN_SOCKTYPE_STREAM: s = "TCP"; break; case CONN_SOCKTYPE_DGRAM: s = "UDP"; break; default: s = "UNKNOWN"; break; } } snprintf(msgline, sizeof(msgline), "Protocol: %s\n", s); addtobuffer(hres->txt, msgline); switch (rec->netparams.sslhandling) { case CONN_SSL_NO: s = "NO"; break; case CONN_SSL_YES: s = "YES"; break; case CONN_SSL_STARTTLS_CLIENT: s = "STARTTLS_CLIENT"; break; case CONN_SSL_STARTTLS_SERVER: s = "STARTTLS_SERVER"; break; default: s = "UNKNOWN"; break; } snprintf(msgline, sizeof(msgline), "SSL: %s\n", s); addtobuffer(hres->txt, msgline); switch (rec->talkresult) { case TALK_CONN_FAILED: s = "CONN_FAILED"; break; case TALK_CONN_TIMEOUT: s = "CONN_TIMEOUT"; break; case TALK_OK: s = "OK"; break; case TALK_BADDATA: s = "BADDATA"; break; case TALK_BADSSLHANDSHAKE: s = "BADSSLHANDSHAKE"; break; case TALK_INTERRUPTED: s = "INTERRUPTED"; break; case TALK_CANNOT_RESOLVE: s = "CANNOT_RESOLVE"; break; case TALK_MODULE_FAILED: s = "MODULE_FAILED"; break; default: s = "UNKNOWN"; break; } snprintf(msgline, sizeof(msgline), "Status: %s\n", s); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "TestID: %lu\nElapsedMS: %d.%02d\nDNSMS: %d.%02d\nTimeoutMS: %d\nIntervalMS: %d\n", rec->testid, (rec->elapsedus / 1000), (rec->elapsedus % 1000), (rec->dnselapsedus / 1000), (rec->dnselapsedus % 1000), rec->timeout*1000, rec->interval*1000); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "BytesRead: %u\nBytesWritten: %u\n", rec->bytesread, rec->byteswritten); addtobuffer(hres->txt, msgline); if (rec->peercertificate) { char exps[50]; addtobuffer(hres->txt, "PeerCertificateSubject: "); addtobuffer(hres->txt, rec->peercertificate); addtobuffer(hres->txt, "\n"); addtobuffer(hres->txt, "PeerCertificateIssuer: "); addtobuffer(hres->txt, rec->peercertificateissuer); addtobuffer(hres->txt, "\n"); strftime(exps, sizeof(exps), "%Y-%m-%d %H:%M:%S UTC", gmtime(&rec->peercertificatestart)); snprintf(msgline, sizeof(msgline), "PeerCertificateStart: %d %s\n", (int)rec->peercertificatestart, exps); addtobuffer(hres->txt, msgline); strftime(exps, sizeof(exps), "%Y-%m-%d %H:%M:%S UTC", gmtime(&rec->peercertificateexpiry)); snprintf(msgline, sizeof(msgline), "PeerCertificateExpiry: %d %s\n", (int)rec->peercertificateexpiry, exps); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "PeerCertificateKeysize: %d\n", (int)rec->peercertificatekeysize); addtobuffer(hres->txt, msgline); snprintf(msgline, sizeof(msgline), "PeerCertificateDetails: %d\n", (int)strlen(rec->peercertificatedetails)+1); addtobuffer(hres->txt, msgline); addtobuffer(hres->txt, rec->peercertificatedetails); addtobuffer(hres->txt, "\n"); } switch (rec->talkprotocol) { case TALK_PROTO_PLAIN: result_plain(rec, hres->txt); break; case TALK_PROTO_NTP: result_ntp(rec, hres->txt); break; case TALK_PROTO_HTTP: result_http(rec, hres->txt); break; case TALK_PROTO_DNSQUERY: result_dns(rec, hres->txt); break; case TALK_PROTO_PING: result_subqueue("PING", rec, hres->txt); break; #ifdef HAVE_LDAP case TALK_PROTO_LDAP: result_subqueue("LDAP", rec, hres->txt); break; #endif case TALK_PROTO_EXTERNAL: result_subqueue(rec->testspec, rec, hres->txt); break; default: break; } } for (handle = xtreeFirst(hostresults); handle != xtreeEnd(hostresults); handle = xtreeNext(hostresults, handle)) { hostresult_t *hres = xtreeData(hostresults, handle); combo_add(hres->txt); freestrbuffer(hres->txt); xtreeDelete(hostresults, xmh_item(hres->hinfo, XMH_HOSTNAME)); xfree(hres); } xtreeDestroy(hostresults); combo_end(); }
static int eventfilter(void *hinfo, char *testname, pcre *pageregexp, pcre *expageregexp, pcre *hostregexp, pcre *exhostregexp, pcre *testregexp, pcre *extestregexp, int ignoredialups, f_hostcheck hostcheck) { int pagematch, hostmatch, testmatch; char *hostname = xmh_item(hinfo, XMH_HOSTNAME); int ovector[30]; if (ignoredialups && xmh_item(hinfo, XMH_FLAG_DIALUP)) return 0; if (hostcheck && (hostcheck(hostname) == 0)) return 0; if (pageregexp) { char *pagename; pagename = xmh_item_multi(hinfo, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(pageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 1; if (!pagematch) return 0; if (expageregexp) { char *pagename; pagename = xmh_item_multi(hinfo, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(expageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 0; if (pagematch) return 0; if (hostregexp) hostmatch = (pcre_exec(hostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 1; if (!hostmatch) return 0; if (exhostregexp) hostmatch = (pcre_exec(exhostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 0; if (hostmatch) return 0; if (testregexp) testmatch = (pcre_exec(testregexp, NULL, testname, strlen(testname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 1; if (!testmatch) return 0; if (extestregexp) testmatch = (pcre_exec(extestregexp, NULL, testname, strlen(testname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 0; if (testmatch) return 0; return 1; }
void add_http_test(testitem_t *t) { http_data_t *httptest; char *dnsip = NULL; ssloptions_t *sslopt = NULL; char *sslopt_ciphers = NULL; int sslopt_version = SSLVERSION_DEFAULT; char *sslopt_clientcert = NULL; int httpversion = HTTPVER_11; cookielist_t *ck = NULL; int firstcookie = 1; char *decodedurl; strbuffer_t *httprequest = newstrbuffer(0); /* Allocate the private data and initialize it */ httptest = (http_data_t *) calloc(1, sizeof(http_data_t)); t->privdata = (void *) httptest; decodedurl = decode_url(t->testspec, &httptest->weburl); if (!decodedurl) { errprintf("Invalid URL for http check: %s\n", t->testspec); return; } httptest->url = strdup(decodedurl); httptest->contlen = -1; httptest->parsestatus = (httptest->weburl.proxyurl ? httptest->weburl.proxyurl->parseerror : httptest->weburl.desturl->parseerror); /* If there was a parse error in the URL, dont run the test */ if (httptest->parsestatus) return; if (httptest->weburl.proxyurl && (httptest->weburl.proxyurl->ip == NULL)) { dnsip = dnsresolve(httptest->weburl.proxyurl->host); if (dnsip) { httptest->weburl.proxyurl->ip = strdup(dnsip); } else { dbgprintf("Could not resolve URL hostname '%s'\n", httptest->weburl.proxyurl->host); } } else if (httptest->weburl.desturl->ip == NULL) { dnsip = dnsresolve(httptest->weburl.desturl->host); if (dnsip) { httptest->weburl.desturl->ip = strdup(dnsip); } else { dbgprintf("Could not resolve URL hostname '%s'\n", httptest->weburl.desturl->host); } } switch (httptest->weburl.testtype) { case WEBTEST_PLAIN: case WEBTEST_STATUS: httptest->contentcheck = CONTENTCHECK_NONE; break; case WEBTEST_CONTENT: { FILE *contentfd; char contentfn[PATH_MAX]; sprintf(contentfn, "%s/content/%s.substring", xgetenv("XYMONHOME"), commafy(t->host->hostname)); contentfd = fopen(contentfn, "r"); if (contentfd) { char l[MAX_LINE_LEN]; char *p; if (fgets(l, sizeof(l), contentfd)) { p = strchr(l, '\n'); if (p) { *p = '\0'; }; httptest->weburl.expdata = strdup(l); } else { httptest->contstatus = STATUS_CONTENTMATCH_NOFILE; } fclose(contentfd); } else { httptest->contstatus = STATUS_CONTENTMATCH_NOFILE; } httptest->contentcheck = CONTENTCHECK_REGEX; } break; case WEBTEST_CONT: httptest->contentcheck = ((*httptest->weburl.expdata == '#') ? CONTENTCHECK_DIGEST : CONTENTCHECK_REGEX); break; case WEBTEST_NOCONT: httptest->contentcheck = CONTENTCHECK_NOREGEX; break; case WEBTEST_POST: case WEBTEST_SOAP: if (httptest->weburl.expdata == NULL) { httptest->contentcheck = CONTENTCHECK_NONE; } else { httptest->contentcheck = ((*httptest->weburl.expdata == '#') ? CONTENTCHECK_DIGEST : CONTENTCHECK_REGEX); } break; case WEBTEST_NOPOST: case WEBTEST_NOSOAP: if (httptest->weburl.expdata == NULL) { httptest->contentcheck = CONTENTCHECK_NONE; } else { httptest->contentcheck = CONTENTCHECK_NOREGEX; } break; case WEBTEST_TYPE: httptest->contentcheck = CONTENTCHECK_CONTENTTYPE; break; } /* Compile the hashes and regex's for those tests that use it */ switch (httptest->contentcheck) { case CONTENTCHECK_DIGEST: { char *hashfunc; httptest->exp = (void *) strdup(httptest->weburl.expdata+1); hashfunc = strchr(httptest->exp, ':'); if (hashfunc) { *hashfunc = '\0'; httptest->digestctx = digest_init(httptest->exp); *hashfunc = ':'; } } break; case CONTENTCHECK_REGEX: case CONTENTCHECK_NOREGEX: { int status; httptest->exp = (void *) malloc(sizeof(regex_t)); status = regcomp((regex_t *)httptest->exp, httptest->weburl.expdata, REG_EXTENDED|REG_NOSUB); if (status) { errprintf("Failed to compile regexp '%s' for URL %s\n", httptest->weburl.expdata, httptest->url); httptest->contstatus = STATUS_CONTENTMATCH_BADREGEX; } } break; case CONTENTCHECK_CONTENTTYPE: httptest->exp = httptest->weburl.expdata; break; } if (httptest->weburl.desturl->schemeopts) { if (strstr(httptest->weburl.desturl->schemeopts, "3")) sslopt_version = SSLVERSION_V3; else if (strstr(httptest->weburl.desturl->schemeopts, "2")) sslopt_version = SSLVERSION_V2; if (strstr(httptest->weburl.desturl->schemeopts, "h")) sslopt_ciphers = ciphershigh; else if (strstr(httptest->weburl.desturl->schemeopts, "m")) sslopt_ciphers = ciphersmedium; if (strstr(httptest->weburl.desturl->schemeopts, "10")) httpversion = HTTPVER_10; else if (strstr(httptest->weburl.desturl->schemeopts, "11")) httpversion = HTTPVER_11; } /* Get any cookies */ load_cookies(); /* Generate the request */ addtobuffer(httprequest, (httptest->weburl.postdata ? "POST " : "GET ")); switch (httpversion) { case HTTPVER_10: addtobuffer(httprequest, (httptest->weburl.proxyurl ? httptest->url : httptest->weburl.desturl->relurl)); addtobuffer(httprequest, " HTTP/1.0\r\n"); break; case HTTPVER_11: /* * Experience shows that even though HTTP/1.1 says you should send the * full URL, some servers (e.g. SunOne App server 7) choke on it. * So just send the good-old relative URL unless we're proxying. */ addtobuffer(httprequest, (httptest->weburl.proxyurl ? httptest->url : httptest->weburl.desturl->relurl)); addtobuffer(httprequest, " HTTP/1.1\r\n"); addtobuffer(httprequest, "Connection: close\r\n"); break; } addtobuffer(httprequest, "Host: "); addtobuffer(httprequest, httptest->weburl.desturl->host); if ((httptest->weburl.desturl->port != 80) && (httptest->weburl.desturl->port != 443)) { char hostporthdr[20]; sprintf(hostporthdr, ":%d", httptest->weburl.desturl->port); addtobuffer(httprequest, hostporthdr); } addtobuffer(httprequest, "\r\n"); if (httptest->weburl.postdata) { char hdr[100]; int contlen = strlen(httptest->weburl.postdata); if (strncmp(httptest->weburl.postdata, "file:", 5) == 0) { /* Load the POST data from a file */ FILE *pf = fopen(httptest->weburl.postdata+5, "r"); if (pf == NULL) { errprintf("Cannot open POST data file %s\n", httptest->weburl.postdata+5); xfree(httptest->weburl.postdata); httptest->weburl.postdata = strdup(""); contlen = 0; } else { struct stat st; if (fstat(fileno(pf), &st) == 0) { int n; xfree(httptest->weburl.postdata); httptest->weburl.postdata = (char *)malloc(st.st_size + 1); *(httptest->weburl.postdata) = '\0'; n = fread(httptest->weburl.postdata, 1, st.st_size, pf); if (n == st.st_size) { *(httptest->weburl.postdata+n) = '\0'; contlen = n; } else { errprintf("Cannot read file %s: %s\n", httptest->weburl.postdata+5, strerror(errno)); contlen = 0; } } else { errprintf("Cannot stat file %s\n", httptest->weburl.postdata+5); httptest->weburl.postdata = strdup(""); contlen = 0; } fclose(pf); } } addtobuffer(httprequest, "Content-type: "); if (httptest->weburl.postcontenttype) addtobuffer(httprequest, httptest->weburl.postcontenttype); else if ((httptest->weburl.testtype == WEBTEST_SOAP) || (httptest->weburl.testtype == WEBTEST_NOSOAP)) addtobuffer(httprequest, "application/soap+xml; charset=utf-8"); else addtobuffer(httprequest, "application/x-www-form-urlencoded"); addtobuffer(httprequest, "\r\n"); sprintf(hdr, "Content-Length: %d\r\n", contlen); addtobuffer(httprequest, hdr); } { char useragent[100]; void *hinfo; char *browser = NULL; hinfo = hostinfo(t->host->hostname); if (hinfo) browser = xmh_item(hinfo, XMH_BROWSER); if (browser) { sprintf(useragent, "User-Agent: %s\r\n", browser); } else { sprintf(useragent, "User-Agent: Xymon xymonnet/%s\r\n", VERSION); } addtobuffer(httprequest, useragent); } if (httptest->weburl.desturl->auth) { if (strncmp(httptest->weburl.desturl->auth, "CERT:", 5) == 0) { sslopt_clientcert = httptest->weburl.desturl->auth+5; } else { addtobuffer(httprequest, "Authorization: Basic "); addtobuffer(httprequest, base64encode(httptest->weburl.desturl->auth)); addtobuffer(httprequest, "\r\n"); } } if (httptest->weburl.proxyurl && httptest->weburl.proxyurl->auth) { addtobuffer(httprequest, "Proxy-Authorization: Basic "); addtobuffer(httprequest, base64encode(httptest->weburl.proxyurl->auth)); addtobuffer(httprequest, "\r\n"); } for (ck = cookiehead; (ck); ck = ck->next) { int useit = 0; if (ck->tailmatch) { int startpos = strlen(httptest->weburl.desturl->host) - strlen(ck->host); if (startpos > 0) useit = (strcmp(httptest->weburl.desturl->host+startpos, ck->host) == 0); } else useit = (strcmp(httptest->weburl.desturl->host, ck->host) == 0); if (useit) useit = (strncmp(ck->path, httptest->weburl.desturl->relurl, strlen(ck->path)) == 0); if (useit) { if (firstcookie) { addtobuffer(httprequest, "Cookie: "); firstcookie = 0; } addtobuffer(httprequest, ck->name); addtobuffer(httprequest, "="); addtobuffer(httprequest, ck->value); addtobuffer(httprequest, "\r\n"); } } /* Some standard stuff */ addtobuffer(httprequest, "Accept: */*\r\n"); addtobuffer(httprequest, "Pragma: no-cache\r\n"); if ((httptest->weburl.testtype == WEBTEST_SOAP) || (httptest->weburl.testtype == WEBTEST_NOSOAP)) { /* Must provide a SOAPAction header */ addtobuffer(httprequest, "SOAPAction: "); addtobuffer(httprequest, httptest->url); addtobuffer(httprequest, "\r\n"); } /* The final blank line terminates the headers */ addtobuffer(httprequest, "\r\n"); /* Post data goes last */ if (httptest->weburl.postdata) addtobuffer(httprequest, httptest->weburl.postdata); /* Pickup any SSL options the user wants */ if (sslopt_ciphers || (sslopt_version != SSLVERSION_DEFAULT) || sslopt_clientcert){ sslopt = (ssloptions_t *) malloc(sizeof(ssloptions_t)); sslopt->cipherlist = sslopt_ciphers; sslopt->sslversion = sslopt_version; sslopt->clientcert = sslopt_clientcert; } /* Add to TCP test queue */ if (httptest->weburl.proxyurl == NULL) { httptest->tcptest = add_tcp_test(httptest->weburl.desturl->ip, httptest->weburl.desturl->port, httptest->weburl.desturl->scheme, sslopt, t->srcip, t->testspec, t->silenttest, grabstrbuffer(httprequest), httptest, tcp_http_data_callback, tcp_http_final_callback); } else { httptest->tcptest = add_tcp_test(httptest->weburl.proxyurl->ip, httptest->weburl.proxyurl->port, httptest->weburl.proxyurl->scheme, sslopt, t->srcip, t->testspec, t->silenttest, grabstrbuffer(httprequest), httptest, tcp_http_data_callback, tcp_http_final_callback); } }
void do_eventlog(FILE *output, int maxcount, int maxminutes, char *fromtime, char *totime, char *pageregex, char *expageregex, char *hostregex, char *exhostregex, char *testregex, char *extestregex, char *colrregex, int ignoredialups, f_hostcheck hostcheck, event_t **eventlist, countlist_t **hostcounts, countlist_t **servicecounts, countsummary_t counttype, eventsummary_t sumtype, char *periodstring) { FILE *eventlog; char eventlogfilename[PATH_MAX]; time_t firstevent = 0; time_t lastevent = getcurrenttime(NULL); event_t *eventhead = NULL; struct stat st; char l[MAX_LINE_LEN]; char title[200]; /* For the PCRE matching */ const char *errmsg = NULL; int errofs = 0; pcre *pageregexp = NULL; pcre *expageregexp = NULL; pcre *hostregexp = NULL; pcre *exhostregexp = NULL; pcre *testregexp = NULL; pcre *extestregexp = NULL; pcre *colrregexp = NULL; countlist_t *hostcounthead = NULL, *svccounthead = NULL; if (eventlist) *eventlist = NULL; if (hostcounts) *hostcounts = NULL; if (servicecounts) *servicecounts = NULL; havedoneeventlog = 1; if ((maxminutes > 0) && (fromtime || totime)) { fprintf(output, "<B>Only one time interval type is allowed!</B>"); return; } if (fromtime) { firstevent = eventreport_time(fromtime); if(firstevent < 0) { if (output) fprintf(output,"<B>Invalid 'from' time: %s</B>", htmlquoted(fromtime)); return; } } else if (maxminutes == -1) { /* Unlimited number of minutes */ firstevent = 0; } else if (maxminutes > 0) { firstevent = getcurrenttime(NULL) - maxminutes*60; } else { firstevent = getcurrenttime(NULL) - 86400; } if (totime) { lastevent = eventreport_time(totime); if (lastevent < 0) { if (output) fprintf(output,"<B>Invalid 'to' time: %s</B>", htmlquoted(totime)); return; } if (lastevent < firstevent) { if (output) fprintf(output,"<B>'to' time must be after 'from' time.</B>"); return; } } if (!maxcount) maxcount = 100; if (pageregex && *pageregex) pageregexp = pcre_compile(pageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (expageregex && *expageregex) expageregexp = pcre_compile(expageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (hostregex && *hostregex) hostregexp = pcre_compile(hostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (exhostregex && *exhostregex) exhostregexp = pcre_compile(exhostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (testregex && *testregex) testregexp = pcre_compile(testregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (extestregex && *extestregex) extestregexp = pcre_compile(extestregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (colrregex && *colrregex) colrregexp = pcre_compile(colrregex, PCRE_CASELESS, &errmsg, &errofs, NULL); sprintf(eventlogfilename, "%s/allevents", xgetenv("XYMONHISTDIR")); eventlog = fopen(eventlogfilename, "r"); if (eventlog && (stat(eventlogfilename, &st) == 0)) { time_t curtime; int done = 0; int unlimited = (maxcount == -1); if (unlimited) maxcount = 1000; do { /* Find a spot in the eventlog file close to where the firstevent time is */ fseeko(eventlog, 0, SEEK_END); do { /* Go back maxcount*80 bytes - one entry is ~80 bytes */ if (ftello(eventlog) > maxcount*80) { unsigned int uicurtime; fseeko(eventlog, -maxcount*80, SEEK_CUR); if (fgets(l, sizeof(l), eventlog) && /* Skip to start of line */ fgets(l, sizeof(l), eventlog)) { sscanf(l, "%*s %*s %u %*u %*u %*s %*s %*d", &uicurtime); curtime = uicurtime; done = (curtime < firstevent); if (unlimited && !done) maxcount += 1000; } else { if (output) fprintf(output,"Error reading eventlog file %s: %s\n", eventlogfilename, strerror(errno)); return; } } else { rewind(eventlog); curtime = 0; done = 1; } } while (!done); if (unlimited) unlimited = ((curtime > firstevent) && (ftello(eventlog) > 0)); } while (unlimited); } eventhead = NULL; while (eventlog && (fgets(l, sizeof(l), eventlog))) { time_t eventtime, changetime, duration; unsigned int uievt, uicht, uidur; char hostname[MAX_LINE_LEN], svcname[MAX_LINE_LEN], newcol[MAX_LINE_LEN], oldcol[MAX_LINE_LEN]; char *newcolname, *oldcolname; int state, itemsfound, colrmatch; event_t *newevent; void *eventhost; struct htnames_t *eventcolumn; int ovector[30]; eventcount_t *countrec; itemsfound = sscanf(l, "%s %s %u %u %u %s %s %d", hostname, svcname, &uievt, &uicht, &uidur, newcol, oldcol, &state); eventtime = uievt; changetime = uicht; duration = uidur; oldcolname = colorname(eventcolor(oldcol)); newcolname = colorname(eventcolor(newcol)); /* For DURATION counts, we must parse all events until now */ if ((counttype != XYMON_COUNT_DURATION) && (eventtime > lastevent)) break; eventhost = hostinfo(hostname); eventcolumn = getname(svcname, 1); if ( (itemsfound == 8) && (eventtime >= firstevent) && (eventhost && !xmh_item(eventhost, XMH_FLAG_NONONGREEN)) && (wanted_eventcolumn(svcname)) ) { if (eventfilter(eventhost, svcname, pageregexp, expageregexp, hostregexp, exhostregexp, testregexp, extestregexp, ignoredialups, hostcheck) == 0) continue; /* For duration counts, record all events. We'll filter out the colors later. */ if (colrregexp && (counttype != XYMON_COUNT_DURATION)) { colrmatch = ( (pcre_exec(colrregexp, NULL, newcolname, strlen(newcolname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0) || (pcre_exec(colrregexp, NULL, oldcolname, strlen(oldcolname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0) ); } else colrmatch = 1; if (!colrmatch) continue; newevent = (event_t *) malloc(sizeof(event_t)); newevent->host = eventhost; newevent->service = eventcolumn; newevent->eventtime = eventtime; newevent->changetime = changetime; newevent->duration = duration; newevent->newcolor = eventcolor(newcol); newevent->oldcolor = eventcolor(oldcol); newevent->next = eventhead; eventhead = newevent; if (counttype != XYMON_COUNT_DURATION) { countrec = (eventcount_t *)xmh_item(eventhost, XMH_DATA); while (countrec && (countrec->service != eventcolumn)) countrec = countrec->next; if (countrec == NULL) { countrec = (eventcount_t *)calloc(1, sizeof(eventcount_t)); countrec->service = eventcolumn; countrec->next = (eventcount_t *)xmh_item(eventhost, XMH_DATA); xmh_set_item(eventhost, XMH_DATA, (void *)countrec); } countrec->count++; } } } /* Count the state changes per host */ svccounthead = hostcounthead = NULL; switch (counttype) { case XYMON_COUNT_EVENTS: count_events(&hostcounthead, &svccounthead); break; case XYMON_COUNT_DURATION: count_duration(firstevent, lastevent, pageregexp, expageregexp, hostregexp, exhostregexp, testregexp, extestregexp, ignoredialups, hostcheck, eventhead, &hostcounthead, &svccounthead); break; default: break; } if (hostcounthead) hostcounthead = msort(hostcounthead, record_compare, record_getnext, record_setnext); if (svccounthead) svccounthead = msort(svccounthead, record_compare, record_getnext, record_setnext); if (eventhead && (output != NULL)) { char *bgcolors[2] = { "#000000", "#000033" }; int bgcolor = 0; struct event_t *ewalk, *lasttoshow = eventhead; countlist_t *cwalk; unsigned long totalcount = 0; if (periodstring) fprintf(output, "<p><font size=+1>%s</font></p>\n", htmlquoted(periodstring)); switch (sumtype) { case XYMON_S_HOST_BREAKDOWN: /* Request for a specific service, show breakdown by host */ for (cwalk = hostcounthead; (cwalk); cwalk = cwalk->next) totalcount += cwalk->total; fprintf(output, "<table summary=\"Breakdown by host\" border=0>\n"); fprintf(output, "<tr><th align=left>Host</th><th colspan=2>%s</th></tr>\n", (counttype == XYMON_COUNT_EVENTS) ? "State changes" : "Seconds red/yellow"); fprintf(output, "<tr><td colspan=3><hr width=\"100%%\"></td></tr>\n"); for (cwalk = hostcounthead; (cwalk && (cwalk->total > 0)); cwalk = cwalk->next) { fprintf(output, "<tr><td align=left>%s</td><td align=right>%lu</td><td align=right>(%6.2f %%)</tr>\n", xmh_item(cwalk->src, XMH_HOSTNAME), cwalk->total, ((100.0 * cwalk->total) / totalcount)); } fprintf(output, "</table>\n"); break; case XYMON_S_SERVICE_BREAKDOWN: /* Request for a specific host, show breakdown by service */ for (cwalk = svccounthead; (cwalk); cwalk = cwalk->next) totalcount += cwalk->total; fprintf(output, "<table summary=\"Breakdown by service\" border=0>\n"); fprintf(output, "<tr><th align=left>Service</th><th colspan=2>%s</th></tr>\n", (counttype == XYMON_COUNT_EVENTS) ? "State changes" : "Seconds red/yellow"); fprintf(output, "<tr><td colspan=3><hr width=\"100%%\"></td></tr>\n"); for (cwalk = svccounthead; (cwalk && (cwalk->total > 0)); cwalk = cwalk->next) { fprintf(output, "<tr><td align=left>%s</td><td align=right>%lu</td><td align=right>(%6.2f %%)</tr>\n", ((htnames_t *)cwalk->src)->name, cwalk->total, ((100.0 * cwalk->total) / totalcount)); } fprintf(output, "</table>\n"); break; case XYMON_S_NONE: break; } if (sumtype == XYMON_S_NONE) { int count; count=0; ewalk=eventhead; do { count++; lasttoshow = ewalk; ewalk = ewalk->next; } while (ewalk && (count<maxcount)); if (ewalk) ewalk->next = NULL; /* Terminate list if any items left */ if (maxminutes > 0) { sprintf(title, "%d events received in the past %u minutes", count, (unsigned int)((getcurrenttime(NULL) - lasttoshow->eventtime) / 60)); } else { sprintf(title, "%d events received.", count); } } else { strcpy(title, "Events in summary"); } fprintf(output, "<BR><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"$EVENTSTITLE\" BORDER=0>\n"); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=6><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD></TR>\n", htmlquoted(title)); for (ewalk=eventhead; (ewalk); ewalk=ewalk->next) { char *hostname = xmh_item(ewalk->host, XMH_HOSTNAME); if ( (counttype == XYMON_COUNT_DURATION) && (ewalk->oldcolor < COL_YELLOW) && (ewalk->newcolor < COL_YELLOW) ) continue; if ( (counttype == XYMON_COUNT_DURATION) && (ewalk->eventtime >= lastevent) ) continue; fprintf(output, "<TR BGCOLOR=%s>\n", bgcolors[bgcolor]); bgcolor = ((bgcolor + 1) % 2); fprintf(output, "<TD ALIGN=CENTER>%s</TD>\n", ctime(&ewalk->eventtime)); if (ewalk->newcolor == COL_CLEAR) { fprintf(output, "<TD ALIGN=CENTER BGCOLOR=black><FONT COLOR=white>%s</FONT></TD>\n", hostname); } else { fprintf(output, "<TD ALIGN=CENTER BGCOLOR=%s><FONT COLOR=black>%s</FONT></TD>\n", colorname(ewalk->newcolor), hostname); } fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", ewalk->service->name); fprintf(output, "<TD><A HREF=\"%s\">\n", histlogurl(hostname, ewalk->service->name, ewalk->changetime, NULL)); fprintf(output, "<IMG SRC=\"%s/%s\" HEIGHT=\"%s\" WIDTH=\"%s\" BORDER=0 ALT=\"%s\" TITLE=\"%s\"></A>\n", xgetenv("XYMONSKIN"), dotgiffilename(ewalk->oldcolor, 0, 0), xgetenv("DOTHEIGHT"), xgetenv("DOTWIDTH"), colorname(ewalk->oldcolor), colorname(ewalk->oldcolor)); fprintf(output, "<IMG SRC=\"%s/arrow.gif\" BORDER=0 ALT=\"From -> To\">\n", xgetenv("XYMONSKIN")); fprintf(output, "<TD><A HREF=\"%s\">\n", histlogurl(hostname, ewalk->service->name, ewalk->eventtime, NULL)); fprintf(output, "<IMG SRC=\"%s/%s\" HEIGHT=\"%s\" WIDTH=\"%s\" BORDER=0 ALT=\"%s\" TITLE=\"%s\"></A></TD>\n", xgetenv("XYMONSKIN"), dotgiffilename(ewalk->newcolor, 0, 0), xgetenv("DOTHEIGHT"), xgetenv("DOTWIDTH"), colorname(ewalk->newcolor), colorname(ewalk->newcolor)); fprintf(output, "</TR>\n"); } fprintf(output, "</TABLE>\n"); } else if (output != NULL) { /* No events during the past maxminutes */ if (eventlog) sprintf(title, "No events received in the last %d minutes", maxminutes); else strcpy(title, "No events logged"); fprintf(output, "<CENTER><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"%s\" BORDER=0>\n", title); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=6><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD>\n", htmlquoted(title)); fprintf(output, "</TR>\n"); fprintf(output, "</TABLE>\n"); fprintf(output, "</CENTER>\n"); } if (eventlog) fclose(eventlog); if (pageregexp) pcre_free(pageregexp); if (hostregexp) pcre_free(hostregexp); if (testregexp) pcre_free(testregexp); if (colrregexp) pcre_free(colrregexp); /* Return the event- and count-lists, if wanted - or clean them up */ if (eventlist) { *eventlist = eventhead; } else { event_t *zombie, *ewalk = eventhead; while (ewalk) { zombie = ewalk; ewalk = ewalk->next; xfree(zombie); } } if (hostcounts) { *hostcounts = hostcounthead; } else { countlist_t *zombie, *hwalk = hostcounthead; while (hwalk) { zombie = hwalk; hwalk = hwalk->next; xfree(zombie); } } if (servicecounts) { *servicecounts = svccounthead; } else { countlist_t *zombie, *swalk = svccounthead; while (swalk) { zombie = swalk; swalk = swalk->next; xfree(zombie); } } }
int do_ifstat_rrd(char *hostname, char *testname, char *classname, char *pagepaths, char *msg, time_t tstamp) { static int pcres_compiled = 0; static pcre **ifstat_linux_pcres = NULL; static pcre **ifstat_freebsd_pcres = NULL; static pcre **ifstat_freebsdV8_pcres = NULL; static pcre **ifstat_openbsd_pcres = NULL; static pcre **ifstat_netbsd_pcres = NULL; static pcre **ifstat_darwin_pcres = NULL; static pcre **ifstat_solaris_pcres = NULL; static pcre **ifstat_aix_pcres = NULL; static pcre **ifstat_hpux_pcres = NULL; static pcre **ifstat_sco_sv_pcres = NULL; static pcre **ifstat_bbwin_pcres = NULL; enum ostype_t ostype; char *datapart = msg; char *bol, *eoln, *ifname, *rxstr, *txstr, *dummy; int dmatch; void *xmh; pcre *ifname_filter_pcre = NULL; xmh = hostinfo(hostname); if (xmh) { char *ifname_filter_expr = xmh_item(xmh, XMH_INTERFACES); if (ifname_filter_expr && *ifname_filter_expr) ifname_filter_pcre = compileregex(ifname_filter_expr); } if (pcres_compiled == 0) { pcres_compiled = 1; ifstat_linux_pcres = compile_exprs("LINUX", ifstat_linux_exprs, (sizeof(ifstat_linux_exprs) / sizeof(ifstat_linux_exprs[0]))); ifstat_freebsd_pcres = compile_exprs("FREEBSD", ifstat_freebsd_exprs, (sizeof(ifstat_freebsd_exprs) / sizeof(ifstat_freebsd_exprs[0]))); ifstat_freebsdV8_pcres = compile_exprs("FREEBSD", ifstat_freebsdV8_exprs, (sizeof(ifstat_freebsdV8_exprs) / sizeof(ifstat_freebsdV8_exprs[0]))); ifstat_openbsd_pcres = compile_exprs("OPENBSD", ifstat_openbsd_exprs, (sizeof(ifstat_openbsd_exprs) / sizeof(ifstat_openbsd_exprs[0]))); ifstat_netbsd_pcres = compile_exprs("NETBSD", ifstat_netbsd_exprs, (sizeof(ifstat_netbsd_exprs) / sizeof(ifstat_netbsd_exprs[0]))); ifstat_darwin_pcres = compile_exprs("DARWIN", ifstat_darwin_exprs, (sizeof(ifstat_darwin_exprs) / sizeof(ifstat_darwin_exprs[0]))); ifstat_solaris_pcres = compile_exprs("SOLARIS", ifstat_solaris_exprs, (sizeof(ifstat_solaris_exprs) / sizeof(ifstat_solaris_exprs[0]))); ifstat_aix_pcres = compile_exprs("AIX", ifstat_aix_exprs, (sizeof(ifstat_aix_exprs) / sizeof(ifstat_aix_exprs[0]))); ifstat_hpux_pcres = compile_exprs("HPUX", ifstat_hpux_exprs, (sizeof(ifstat_hpux_exprs) / sizeof(ifstat_hpux_exprs[0]))); ifstat_sco_sv_pcres = compile_exprs("SCO_SV", ifstat_sco_sv_exprs, (sizeof(ifstat_sco_sv_exprs) / sizeof(ifstat_sco_sv_exprs[0]))); ifstat_bbwin_pcres = compile_exprs("BBWIN", ifstat_bbwin_exprs, (sizeof(ifstat_bbwin_exprs) / sizeof(ifstat_bbwin_exprs[0]))); } if (ifstat_tpl == NULL) ifstat_tpl = setup_template(ifstat_params); if ((strncmp(msg, "status", 6) == 0) || (strncmp(msg, "data", 4) == 0)) { /* Skip the first line of full status- and data-messages. */ datapart = strchr(msg, '\n'); if (datapart) datapart++; else datapart = msg; } ostype = get_ostype(datapart); datapart = strchr(datapart, '\n'); if (datapart) { datapart++; } else { errprintf("Too few lines in ifstat report from %s\n", hostname); return -1; } dmatch = 0; ifname = rxstr = txstr = dummy = NULL; bol = datapart; while (bol) { eoln = strchr(bol, '\n'); if (eoln) *eoln = '\0'; switch (ostype) { case OS_LINUX22: case OS_LINUX: case OS_RHEL3: case OS_ZVM: case OS_ZVSE: case OS_ZOS: if (pickdata(bol, ifstat_linux_pcres[0], 1, &ifname)) { /* * Linux' netif aliases mess up things. * Clear everything when we see an interface name. * But we dont want to track the "lo" interface. */ /* Strip off the last character if it is a colon (:) */ if (ifname[strlen(ifname)-1] == ':') ifname[strlen(ifname)-1] = '\0'; if (strcmp(ifname, "lo") == 0) { xfree(ifname); ifname = NULL; } else { dmatch = 1; if (rxstr) { xfree(rxstr); rxstr = NULL; } if (txstr) { xfree(txstr); txstr = NULL; } } } else if (pickdata(bol, ifstat_linux_pcres[1], 1, &rxstr, &txstr)) dmatch |= 6; else if (pickdata(bol, ifstat_linux_pcres[2], 1, &rxstr)) dmatch |= 2; else if (pickdata(bol, ifstat_linux_pcres[3], 1, &txstr)) dmatch |= 4; break; case OS_FREEBSD: /* * FreeBSD 8 added an "Idrop" counter in the middle of the data. * See if we match this expression, and if not then fall back to * the old regex without that field. */ if (pickdata(bol, ifstat_freebsdV8_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; else if (pickdata(bol, ifstat_freebsd_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; case OS_OPENBSD: if (pickdata(bol, ifstat_openbsd_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; case OS_NETBSD: if (pickdata(bol, ifstat_netbsd_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; case OS_SOLARIS: if (pickdata(bol, ifstat_solaris_pcres[0], 0, &ifname, &txstr)) dmatch |= 1; else if (pickdata(bol, ifstat_solaris_pcres[1], 0, &dummy, &rxstr)) dmatch |= 6; if (ifname && dummy && (strcmp(ifname, dummy) != 0)) { /* They must match, drop the data */ errprintf("Host %s has weird ifstat data - device name mismatch %s:%s\n", hostname, ifname, dummy); xfree(ifname); xfree(txstr); xfree(rxstr); xfree(dummy); dmatch = 0; } /* Ignore "mac" and "wrsmd" entries - these are for sub-devices for multiple nic's aggregated into one */ /* See http://www.xymon.com/archive/2009/06/msg00204.html for more info */ if (ifname && ((strcmp(ifname, "mac") == 0) || (strcmp(ifname, "wrsmd") == 0)) ) { xfree(ifname); xfree(txstr); dmatch = 0; } if (dummy && ((strcmp(dummy, "mac") == 0) || (strcmp(dummy, "wrsmd") == 0)) ) { xfree(dummy); xfree(rxstr); dmatch = 0; } break; case OS_AIX: if (pickdata(bol, ifstat_aix_pcres[0], 1, &ifname)) { /* Interface names comes first, so any rx/tx data is discarded */ dmatch |= 1; if (rxstr) { xfree(rxstr); rxstr = NULL; } if (txstr) { xfree(txstr); txstr = NULL; } } else if (pickdata(bol, ifstat_aix_pcres[1], 1, &txstr, &rxstr)) dmatch |= 6; break; case OS_HPUX: if (pickdata(bol, ifstat_hpux_pcres[0], 1, &ifname)) { /* Interface names comes first, so any rx/tx data is discarded */ dmatch |= 1; if (rxstr) { xfree(rxstr); rxstr = NULL; } if (txstr) { xfree(txstr); txstr = NULL; } } else if (pickdata(bol, ifstat_hpux_pcres[1], 1, &rxstr)) dmatch |= 2; else if (pickdata(bol, ifstat_hpux_pcres[2], 1, &txstr)) dmatch |= 4; break; case OS_DARWIN: if (pickdata(bol, ifstat_darwin_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; case OS_SCO_SV: if (pickdata(bol, ifstat_sco_sv_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; case OS_WIN32_BBWIN: if (pickdata(bol, ifstat_bbwin_pcres[0], 0, &ifname, &rxstr, &txstr)) dmatch = 7; break; default: break; } if ((dmatch == 7) && ifname && rxstr && txstr) { if (!ifname_filter_pcre || matchregex(ifname, ifname_filter_pcre)) { setupfn2("%s.%s.rrd", "ifstat", ifname); snprintf(rrdvalues, sizeof(rrdvalues), "%d:%s:%s", (int)tstamp, txstr, rxstr); create_and_update_rrd(hostname, testname, classname, pagepaths, ifstat_params, ifstat_tpl); } xfree(ifname); xfree(rxstr); xfree(txstr); if (dummy) xfree(dummy); ifname = rxstr = txstr = dummy = NULL; dmatch = 0; } if (eoln) { *eoln = '\n'; bol = eoln+1; if (*bol == '\0') bol = NULL; } else { bol = NULL; } } if (ifname_filter_pcre) freeregex(ifname_filter_pcre); if (ifname) xfree(ifname); if (rxstr) xfree(rxstr); if (txstr) xfree(txstr); if (dummy) xfree(dummy); return 0; }
int main(int argc, char *argv[]) { void *hwalk; char *hostsfn = NULL; char *netstring = NULL; char *include2 = NULL; int extras = 1; int testuntagged = 0; int nodownhosts = 0; int onlypreferredentry = 0; char *p; char **lookv; int argi, lookc; strbuffer_t *wantedtags; if ((argc <= 1) || (strcmp(argv[1], "--help") == 0)) { printf("Usage:\n%s test1 [test1] [test2] ... \n", argv[0]); exit(1); } lookv = (char **)malloc(argc*sizeof(char *)); lookc = 0; hostsfn = xgetenv("HOSTSCFG"); conncolumn = xgetenv("PINGCOLUMN"); for (argi=1; (argi < argc); argi++) { if (strcmp(argv[argi], "--noextras") == 0) { extras = 0; } else if (strcmp(argv[argi], "--test-untagged") == 0) { testuntagged = 1; } else if (argnmatch(argv[argi], "--no-down")) { char *p; nodownhosts = 1; p = strchr(argv[argi], '='); if (p) testcolumn = strdup(p+1); } else if (strcmp(argv[argi], "--version") == 0) { printf("xymongrep version %s\n", VERSION); exit(0); } else if ((strcmp(argv[argi], "--net") == 0) || (strcmp(argv[argi], "--bbnet") == 0)) { include2 = "netinclude"; onlypreferredentry = 0; } else if ((strcmp(argv[argi], "--web") == 0) || (strcmp(argv[argi], "--bbdisp") == 0)) { include2 = "dispinclude"; onlypreferredentry = 1; } else if (argnmatch(argv[argi], "--hosts=")) { hostsfn = strchr(argv[argi], '=') + 1; } else { lookv[lookc] = strdup(argv[argi]); lookc++; } } lookv[lookc] = NULL; if ((hostsfn == NULL) || (strlen(hostsfn) == 0)) { errprintf("Environment variable HOSTSCFG is not set - aborting\n"); exit(2); } load_hostnames(hostsfn, include2, get_fqdn()); if (first_host() == NULL) { errprintf("Cannot load %s, or file is empty\n", hostsfn); exit(3); } /* If we must avoid downed or disabled hosts, let's find out what those are */ if (nodownhosts) load_hoststatus(); /* Each network test tagged with NET:locationname */ p = xgetenv("XYMONNETWORK"); if ((p == NULL) || (strlen(p) == 0)) p = xgetenv("BBLOCATION"); if (p && strlen(p)) netstring = strdup(p); hwalk = first_host(); wantedtags = newstrbuffer(0); while (hwalk) { char hostip[IP_ADDR_STRLEN]; char *curnet = xmh_item(hwalk, XMH_NET); char *curname = xmh_item(hwalk, XMH_HOSTNAME); /* * Only look at the hosts whose NET: definition matches the wanted one. * Must also check if the host is currently down (not responding to ping). * And if the host is OK with knownhost(), because it may be time-limited. */ if (netok(netstring, curnet, testuntagged) && downok(curname, nodownhosts) && knownhost(curname, hostip, GH_IGNORE)) { char *item; clearstrbuffer(wantedtags); for (item = xmh_item_walk(hwalk); (item); item = xmh_item_walk(NULL)) { int i; char *realitem = item + strspn(item, "!~?"); for (i=0; lookv[i]; i++) { char *outitem = NULL; if (lookv[i][strlen(lookv[i])-1] == '*') { if (strncasecmp(realitem, lookv[i], strlen(lookv[i])-1) == 0) { outitem = (extras ? item : realitem); } } else if (strcasecmp(realitem, lookv[i]) == 0) { outitem = (extras ? item : realitem); } if (outitem) { int needquotes = ((strchr(outitem, ' ') != NULL) || (strchr(outitem, '\t') != NULL)); addtobuffer(wantedtags, " "); if (needquotes) addtobuffer(wantedtags, "\""); addtobuffer(wantedtags, outitem); if (needquotes) addtobuffer(wantedtags, "\""); } } } if (STRBUF(wantedtags) && (*STRBUF(wantedtags) != '\0') && extras) { if (xmh_item(hwalk, XMH_FLAG_DIALUP)) addtobuffer(wantedtags, " dialup"); if (xmh_item(hwalk, XMH_FLAG_TESTIP)) addtobuffer(wantedtags, " testip"); } if (STRBUF(wantedtags) && *STRBUF(wantedtags)) { printf("%s %s #%s\n", xmh_item(hwalk, XMH_IP), xmh_item(hwalk, XMH_HOSTNAME), STRBUF(wantedtags)); } } do { hwalk = next_host(hwalk, 1); } while (hwalk && onlypreferredentry && (strcmp(curname, xmh_item(hwalk, XMH_HOSTNAME)) == 0)); } return 0; }
xymongen_page_t *load_layout(char *pgset) { char pagetag[100], subpagetag[100], subparenttag[100], vpagetag[100], vsubpagetag[100], vsubparenttag[100], grouptag[100], summarytag[100], titletag[100], hosttag[100]; char *name, *link, *onlycols, *exceptcols; char hostname[MAX_LINE_LEN]; xymongen_page_t *toppage, *curpage, *cursubpage, *cursubparent; group_t *curgroup; host_t *curhost; char *curtitle; int ip1, ip2, ip3, ip4; char *p; int fqdn = get_fqdn(); char *cfgdata, *inbol, *ineol, insavchar = '\0'; if (loadhostsfromxymond) { if (load_hostnames("@", NULL, fqdn) != 0) { errprintf("Cannot load host configuration from xymond\n"); return NULL; } } else { if (load_hostnames(xgetenv("HOSTSCFG"), "dispinclude", fqdn) != 0) { errprintf("Cannot load host configuration from %s\n", xgetenv("HOSTSCFG")); return NULL; } } if (first_host() == NULL) { errprintf("Empty configuration from %s\n", (loadhostsfromxymond ? "xymond" : xgetenv("HOSTSCFG"))); return NULL; } dbgprintf("load_layout(pgset=%s)\n", textornull(pgset)); /* * load_hostnames() picks up the hostname definitions, but not the page * layout. So we will scan the file again, this time doing the layout. */ if (pgset == NULL) pgset = ""; sprintf(pagetag, "%spage", pgset); sprintf(subpagetag, "%ssubpage", pgset); sprintf(subparenttag, "%ssubparent", pgset); sprintf(vpagetag, "v%spage", pgset); sprintf(vsubpagetag, "v%ssubpage", pgset); sprintf(vsubparenttag, "v%ssubparent", pgset); sprintf(grouptag, "%sgroup", pgset); sprintf(summarytag, "%ssummary", pgset); sprintf(titletag, "%stitle", pgset); sprintf(hosttag, "%s:", pgset); for (p=hosttag; (*p); p++) *p = toupper((int)*p); toppage = init_page("", "", 0); addtopagelist(toppage); curpage = NULL; cursubpage = NULL; curgroup = NULL; curhost = NULL; cursubparent = NULL; curtitle = NULL; inbol = cfgdata = hostscfg_content(); while (inbol && *inbol) { inbol += strspn(inbol, " \t"); ineol = strchr(inbol, '\n'); if (ineol) { while ((ineol > inbol) && (isspace(*ineol) || (*ineol == '\n'))) ineol--; if (*ineol != '\n') ineol++; insavchar = *ineol; *ineol = '\0'; } dbgprintf("load_layout: -- got line '%s'\n", inbol); if ((strncmp(inbol, pagetag, strlen(pagetag)) == 0) || (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)) { getnamelink(inbol, &name, &link); if (curpage == NULL) { /* First page - hook it on toppage as a subpage from there */ curpage = toppage->subpages = init_page(name, link, (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)); } else { curpage = curpage->next = init_page(name, link, (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)); } curpage->parent = toppage; if (curtitle) { curpage->pretitle = curtitle; curtitle = NULL; } cursubpage = NULL; cursubparent = NULL; curgroup = NULL; curhost = NULL; addtopagelist(curpage); } else if ( (strncmp(inbol, subpagetag, strlen(subpagetag)) == 0) || (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0) ) { if (curpage == NULL) { errprintf("'subpage' ignored, no preceding 'page' tag : %s\n", inbol); goto nextline; } getnamelink(inbol, &name, &link); if (cursubpage == NULL) { cursubpage = curpage->subpages = init_page(name, link, (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0)); } else { cursubpage = cursubpage->next = init_page(name, link, (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0)); } cursubpage->parent = curpage; if (curtitle) { cursubpage->pretitle = curtitle; curtitle = NULL; } cursubparent = NULL; curgroup = NULL; curhost = NULL; addtopagelist(cursubpage); } else if ( (strncmp(inbol, subparenttag, strlen(subparenttag)) == 0) || (strncmp(inbol, vsubparenttag, strlen(vsubparenttag)) == 0) ) { xymongen_page_t *parentpage, *walk; getparentnamelink(inbol, toppage, &parentpage, &name, &link); if (parentpage == NULL) { errprintf("'subparent' ignored, unknown parent page: %s\n", inbol); goto nextline; } cursubparent = init_page(name, link, (strncmp(inbol, vsubparenttag, strlen(vsubparenttag)) == 0)); if (parentpage->subpages == NULL) { parentpage->subpages = cursubparent; } else { for (walk = parentpage->subpages; (walk->next); (walk = walk->next)) ; walk->next = cursubparent; } if (curtitle) { cursubparent->pretitle = curtitle; curtitle = NULL; } cursubparent->parent = parentpage; curgroup = NULL; curhost = NULL; addtopagelist(cursubparent); } else if (strncmp(inbol, grouptag, strlen(grouptag)) == 0) { int sorthosts = (strstr(inbol, "group-sorted") != NULL); getgrouptitle(inbol, pgset, &link, &onlycols, &exceptcols); if (curgroup == NULL) { curgroup = init_group(link, onlycols, exceptcols, sorthosts); if (cursubparent != NULL) { cursubparent->groups = curgroup; } else if (cursubpage != NULL) { /* We're in a subpage */ cursubpage->groups = curgroup; } else if (curpage != NULL) { /* We're on a main page */ curpage->groups = curgroup; } else { /* We're on the top page */ toppage->groups = curgroup; } } else { curgroup->next = init_group(link, onlycols, exceptcols, sorthosts); curgroup = curgroup->next; } if (curtitle) { curgroup->pretitle = curtitle; curtitle = NULL; } curhost = NULL; } else if (sscanf(inbol, "%3d.%3d.%3d.%3d %s", &ip1, &ip2, &ip3, &ip4, hostname) == 5) { void *xymonhost = NULL; int dialup, nonongreen, crittime = 1; double warnpct = reportwarnlevel; int warnstops = reportwarnstops; char *displayname, *clientalias, *comment, *description; char *alertlist, *onwaplist, *reporttime; char *nopropyellowlist, *nopropredlist, *noproppurplelist, *nopropacklist; char *targetpagelist[MAX_TARGETPAGES_PER_HOST]; int targetpagecount; char *hval; /* Check for ".default." hosts - they are ignored. */ if (*hostname == '.') goto nextline; if (!fqdn) { /* Strip any domain from the hostname */ char *p = strchr(hostname, '.'); if (p) *p = '\0'; } /* Get the info */ xymonhost = hostinfo(hostname); if (xymonhost == NULL) { errprintf("Confused - hostname '%s' cannot be found. Ignored\n", hostname); goto nextline; } /* Check for no-display hosts - they are ignored. */ /* But only when we're building the default pageset */ if ((strlen(pgset) == 0) && (xmh_item(xymonhost, XMH_FLAG_NODISP) != NULL)) goto nextline; for (targetpagecount=0; (targetpagecount < MAX_TARGETPAGES_PER_HOST); targetpagecount++) targetpagelist[targetpagecount] = NULL; targetpagecount = 0; dialup = (xmh_item(xymonhost, XMH_FLAG_DIALUP) != NULL); nonongreen = (xmh_item(xymonhost, XMH_FLAG_NONONGREEN) != NULL); alertlist = xmh_item(xymonhost, XMH_NK); hval = xmh_item(xymonhost, XMH_NKTIME); if (hval) crittime = within_sla(xmh_item(xymonhost, XMH_HOLIDAYS), hval, 0); onwaplist = xmh_item(xymonhost, XMH_WML); nopropyellowlist = xmh_item(xymonhost, XMH_NOPROPYELLOW); if (nopropyellowlist == NULL) nopropyellowlist = xmh_item(xymonhost, XMH_NOPROP); nopropredlist = xmh_item(xymonhost, XMH_NOPROPRED); noproppurplelist = xmh_item(xymonhost, XMH_NOPROPPURPLE); nopropacklist = xmh_item(xymonhost, XMH_NOPROPACK); displayname = xmh_item(xymonhost, XMH_DISPLAYNAME); comment = xmh_item(xymonhost, XMH_COMMENT); description = xmh_item(xymonhost, XMH_DESCRIPTION); hval = xmh_item(xymonhost, XMH_WARNPCT); if (hval) warnpct = atof(hval); hval = xmh_item(xymonhost, XMH_WARNSTOPS); if (hval) warnstops = atof(hval); reporttime = xmh_item(xymonhost, XMH_REPORTTIME); clientalias = xmh_item(xymonhost, XMH_CLIENTALIAS); if (xymonhost && (strcmp(xmh_item(xymonhost, XMH_HOSTNAME), clientalias) == 0)) clientalias = NULL; if (xymonhost && (strlen(pgset) > 0)) { /* Walk the clone-list and pick up the target pages for this host */ void *cwalk = xymonhost; do { hval = xmh_item_walk(cwalk); while (hval) { if (strncasecmp(hval, hosttag, strlen(hosttag)) == 0) targetpagelist[targetpagecount++] = strdup(hval+strlen(hosttag)); hval = xmh_item_walk(NULL); } cwalk = next_host(cwalk, 1); } while (cwalk && (strcmp(xmh_item(cwalk, XMH_HOSTNAME), xmh_item(xymonhost, XMH_HOSTNAME)) == 0) && (targetpagecount < MAX_TARGETPAGES_PER_HOST) ); /* * HACK: Check if the pageset tag is present at all in the host * entry. If it isn't, then drop this incarnation of the host. * * Without this, the following hosts.cfg file will have the * www.hswn.dk host listed twice on the alternate pageset: * * adminpage nyc NYC * * 127.0.0.1 localhost # bbd http://localhost/ CLIENT:osiris * 172.16.10.2 www.xymon.com # http://www.xymon.com/ ADMIN:nyc ssh noinfo * * page superdome Superdome * 172.16.10.2 www.xymon.com # noconn * */ if (strstr(inbol, hosttag) == NULL) targetpagecount = 0; } if (strlen(pgset) == 0) { /* * Default pageset generated. Put the host into * whatever group or page is current. */ if (curhost == NULL) { curhost = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist, nopropredlist, noproppurplelist, nopropacklist); if (curgroup != NULL) { curgroup->hosts = curhost; } else if (cursubparent != NULL) { cursubparent->hosts = curhost; } else if (cursubpage != NULL) { cursubpage->hosts = curhost; } else if (curpage != NULL) { curpage->hosts = curhost; } else { toppage->hosts = curhost; } } else { curhost = curhost->next = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist,nopropredlist, noproppurplelist, nopropacklist); } curhost->parent = (cursubparent ? cursubparent : (cursubpage ? cursubpage : curpage)); if (curtitle) { curhost->pretitle = curtitle; curtitle = NULL; } curhost->nonongreen = nonongreen; } else if (targetpagecount) { int pgnum; for (pgnum=0; (pgnum < targetpagecount); pgnum++) { char *targetpagename = targetpagelist[pgnum]; char savechar; int wantedgroup = 0; xymonpagelist_t *targetpage = NULL; /* Put the host into the page specified by the PGSET: tag */ p = strchr(targetpagename, ','); if (p) { savechar = *p; *p = '\0'; wantedgroup = atoi(p+1); } else { savechar = '\0'; p = targetpagename + strlen(targetpagename); } /* Find the page */ if (strcmp(targetpagename, "*") == 0) { *targetpagename = '\0'; } for (targetpage = pagelisthead; (targetpage && (strcmp(targetpagename, targetpage->pageentry->name) != 0)); targetpage = targetpage->next) ; *p = savechar; if (targetpage == NULL) { errprintf("Warning: Cannot find any target page named '%s' in set '%s' - dropping host '%s'\n", targetpagename, pgset, hostname); } else { host_t *newhost = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist,nopropredlist, noproppurplelist, nopropacklist); if (wantedgroup > 0) { group_t *gwalk; host_t *hwalk; int i; for (gwalk = targetpage->pageentry->groups, i=1; (gwalk && (i < wantedgroup)); i++,gwalk=gwalk->next) ; if (gwalk) { if (gwalk->hosts == NULL) gwalk->hosts = newhost; else { for (hwalk = gwalk->hosts; (hwalk->next); hwalk = hwalk->next) ; hwalk->next = newhost; } } else { errprintf("Warning: Cannot find group %d for host %s - dropping host\n", wantedgroup, hostname); } } else { /* Just put in on the page's hostlist */ host_t *walk; if (targetpage->pageentry->hosts == NULL) targetpage->pageentry->hosts = newhost; else { for (walk = targetpage->pageentry->hosts; (walk->next); walk = walk->next) ; walk->next = newhost; } } newhost->parent = targetpage->pageentry; if (curtitle) newhost->pretitle = curtitle; } curtitle = NULL; } } } else if (strncmp(inbol, summarytag, strlen(summarytag)) == 0) { /* summary row.column IP-ADDRESS-OF-PARENT http://xymon.com/ */ char sumname[MAX_LINE_LEN]; char receiver[MAX_LINE_LEN]; char url[MAX_LINE_LEN]; summary_t *newsum; if (sscanf(inbol, "summary %s %s %s", sumname, receiver, url) == 3) { newsum = init_summary(sumname, receiver, url); newsum->next = sumhead; sumhead = newsum; } } else if (strncmp(inbol, titletag, strlen(titletag)) == 0) { /* Save the title for the next entry */ curtitle = strdup(skipwhitespace(skipword(inbol))); } nextline: if (ineol) { *ineol = insavchar; if (*ineol != '\n') ineol = strchr(ineol, '\n'); inbol = (ineol ? ineol+1 : NULL); } else inbol = NULL; } xfree(cfgdata); return toppage; }
void do_notifylog(FILE *output, int maxcount, int maxminutes, char *fromtime, char *totime, char *pageregex, char *expageregex, char *hostregex, char *exhostregex, char *testregex, char *extestregex, char *rcptregex, char *exrcptregex) { FILE *notifylog; char notifylogfilename[PATH_MAX]; time_t firstevent = 0; time_t lastevent = getcurrenttime(NULL); notification_t *head, *walk; struct stat st; char l[MAX_LINE_LEN]; char title[200]; /* For the PCRE matching */ const char *errmsg = NULL; int errofs = 0; pcre *pageregexp = NULL; pcre *expageregexp = NULL; pcre *hostregexp = NULL; pcre *exhostregexp = NULL; pcre *testregexp = NULL; pcre *extestregexp = NULL; pcre *rcptregexp = NULL; pcre *exrcptregexp = NULL; if (maxminutes && (fromtime || totime)) { fprintf(output, "<B>Only one time interval type is allowed!</B>"); return; } if (fromtime) { firstevent = convert_time(fromtime); if(firstevent < 0) { fprintf(output,"<B>Invalid 'from' time: %s</B>", htmlquoted(fromtime)); return; } } else if (maxminutes) { firstevent = getcurrenttime(NULL) - maxminutes*60; } else { firstevent = getcurrenttime(NULL) - 86400; } if (totime) { lastevent = convert_time(totime); if (lastevent < 0) { fprintf(output,"<B>Invalid 'to' time: %s</B>", htmlquoted(totime)); return; } if (lastevent < firstevent) { fprintf(output,"<B>'to' time must be after 'from' time.</B>"); return; } } if (!maxcount) maxcount = 100; if (pageregex && *pageregex) pageregexp = pcre_compile(pageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (expageregex && *expageregex) expageregexp = pcre_compile(expageregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (hostregex && *hostregex) hostregexp = pcre_compile(hostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (exhostregex && *exhostregex) exhostregexp = pcre_compile(exhostregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (testregex && *testregex) testregexp = pcre_compile(testregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (extestregex && *extestregex) extestregexp = pcre_compile(extestregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (rcptregex && *rcptregex) rcptregexp = pcre_compile(rcptregex, PCRE_CASELESS, &errmsg, &errofs, NULL); if (exrcptregex && *exrcptregex) exrcptregexp = pcre_compile(exrcptregex, PCRE_CASELESS, &errmsg, &errofs, NULL); sprintf(notifylogfilename, "%s/notifications.log", xgetenv("XYMONSERVERLOGS")); notifylog = fopen(notifylogfilename, "r"); if (notifylog && (stat(notifylogfilename, &st) == 0)) { time_t curtime; int done = 0; /* Find a spot in the notification log file close to where the firstevent time is */ fseeko(notifylog, 0, SEEK_END); do { /* Go back maxcount*80 bytes - one entry is ~80 bytes */ if (ftello(notifylog) > maxcount*80) { unsigned int uicurtime; fseeko(notifylog, -maxcount*80, SEEK_CUR); if (fgets(l, sizeof(l), notifylog) && /* Skip to start of line */ fgets(l, sizeof(l), notifylog)) { /* Sun Jan 7 10:29:08 2007 myhost.disk (130.225.226.90) [email protected] 1168162147 100 */ sscanf(l, "%*s %*s %*u %*u:%*u:%*u %*u %*s %*s %*s %u %*d", &uicurtime); curtime = uicurtime; done = (curtime < firstevent); } else { fprintf(output, "Error reading logfile %s: %s\n", notifylogfilename, strerror(errno)); return; } } else { rewind(notifylog); done = 1; } } while (!done); } head = NULL; while (notifylog && (fgets(l, sizeof(l), notifylog))) { unsigned int etim; time_t eventtime; char hostsvc[MAX_LINE_LEN]; char recipient[MAX_LINE_LEN]; char *hostname, *svcname, *p; int itemsfound, pagematch, hostmatch, testmatch, rcptmatch; notification_t *newrec; void *eventhost; struct htnames_t *eventcolumn; int ovector[30]; itemsfound = sscanf(l, "%*s %*s %*u %*u:%*u:%*u %*u %s %*s %s %u %*d", hostsvc, recipient, &etim); eventtime = etim; if (itemsfound != 3) continue; if (eventtime < firstevent) continue; if (eventtime > lastevent) break; hostname = hostsvc; svcname = strrchr(hostsvc, '.'); if (svcname) { *svcname = '\0'; svcname++; } else svcname = ""; eventhost = hostinfo(hostname); if (!eventhost) continue; /* Dont report hosts that no longer exist */ eventcolumn = getname(svcname, 1); p = strchr(recipient, '['); if (p) *p = '\0'; if (pageregexp) { char *pagename; pagename = xmh_item_multi(eventhost, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(pageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 1; if (!pagematch) continue; if (expageregexp) { char *pagename; pagename = xmh_item_multi(eventhost, XMH_PAGEPATH); pagematch = 0; while (!pagematch && pagename) { pagematch = (pcre_exec(expageregexp, NULL, pagename, strlen(pagename), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); pagename = xmh_item_multi(NULL, XMH_PAGEPATH); } } else pagematch = 0; if (pagematch) continue; if (hostregexp) hostmatch = (pcre_exec(hostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 1; if (!hostmatch) continue; if (exhostregexp) hostmatch = (pcre_exec(exhostregexp, NULL, hostname, strlen(hostname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else hostmatch = 0; if (hostmatch) continue; if (testregexp) testmatch = (pcre_exec(testregexp, NULL, svcname, strlen(svcname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 1; if (!testmatch) continue; if (extestregexp) testmatch = (pcre_exec(extestregexp, NULL, svcname, strlen(svcname), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else testmatch = 0; if (testmatch) continue; if (rcptregexp) rcptmatch = (pcre_exec(rcptregexp, NULL, recipient, strlen(recipient), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else rcptmatch = 1; if (!rcptmatch) continue; if (exrcptregexp) rcptmatch = (pcre_exec(exrcptregexp, NULL, recipient, strlen(recipient), 0, 0, ovector, (sizeof(ovector)/sizeof(int))) >= 0); else rcptmatch = 0; if (rcptmatch) continue; newrec = (notification_t *) malloc(sizeof(notification_t)); newrec->host = eventhost; newrec->service = eventcolumn; newrec->eventtime = eventtime; newrec->recipient = strdup(recipient); newrec->next = head; head = newrec; } if (head) { char *bgcolors[2] = { "#000000", "#000066" }; int bgcolor = 0; int count; struct notification_t *lasttoshow = head; count=0; walk=head; do { count++; lasttoshow = walk; walk = walk->next; } while (walk && (count<maxcount)); if (maxminutes) { sprintf(title, "%d notifications in the past %u minutes", count, (unsigned int)((getcurrenttime(NULL) - lasttoshow->eventtime) / 60)); } else { sprintf(title, "%d notifications sent.", count); } fprintf(output, "<BR><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"Notification log\" BORDER=0>\n"); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=4><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD></TR>\n", htmlquoted(title)); fprintf(output, "<TR BGCOLOR=\"#333333\"><TH>Time</TH><TH>Host</TH><TH>Service</TH><TH>Recipient</TH></TR>\n"); for (walk=head; (walk != lasttoshow->next); walk=walk->next) { char *hostname = xmh_item(walk->host, XMH_HOSTNAME); fprintf(output, "<TR BGCOLOR=%s>\n", bgcolors[bgcolor]); bgcolor = ((bgcolor + 1) % 2); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", ctime(&walk->eventtime)); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", hostname); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", walk->service->name); fprintf(output, "<TD ALIGN=LEFT>%s</TD>\n", walk->recipient); } fprintf(output, "</TABLE>\n"); /* Clean up */ walk = head; do { struct notification_t *tmp = walk; walk = walk->next; xfree(tmp->recipient); xfree(tmp); } while (walk); } else { /* No notifications during the past maxminutes */ if (notifylog) sprintf(title, "No notifications sent in the last %d minutes", maxminutes); else strcpy(title, "No notifications logged"); fprintf(output, "<CENTER><BR>\n"); fprintf(output, "<TABLE SUMMARY=\"%s\" BORDER=0>\n", title); fprintf(output, "<TR BGCOLOR=\"#333333\">\n"); fprintf(output, "<TD ALIGN=CENTER COLSPAN=6><FONT SIZE=-1 COLOR=\"#33ebf4\">%s</FONT></TD>\n", htmlquoted(title)); fprintf(output, "</TR>\n"); fprintf(output, "</TABLE>\n"); fprintf(output, "</CENTER>\n"); } if (notifylog) fclose(notifylog); if (pageregexp) pcre_free(pageregexp); if (expageregexp) pcre_free(expageregexp); if (hostregexp) pcre_free(hostregexp); if (exhostregexp) pcre_free(exhostregexp); if (testregexp) pcre_free(testregexp); if (extestregexp) pcre_free(extestregexp); if (rcptregexp) pcre_free(rcptregexp); if (exrcptregexp) pcre_free(exrcptregexp); }
int main(int argc, char **argv) { pcre *hostptn, *exhostptn, *pageptn, *expageptn; void *hwalk; char *hostname, *pagename; hostptn = exhostptn = pageptn = expageptn = NULL; if (getenv("QUERY_STRING") == NULL) { /* Not invoked through the CGI */ if (argc < 4) { errprintf("Usage:\n%s HOSTNAME-PATTERN STARTTIME ENDTIME", argv[0]); return 1; } hostpattern = argv[1]; if (strncmp(hostpattern, "--page=", 7) == 0) { pagepattern = strchr(argv[1], '=') + 1; hostpattern = NULL; } starttimedate = argv[2]; starttimehm = "00:00:00"; endtimedate = argv[3]; endtimehm = "00:00:00"; if (argc > 4) { if (strncmp(argv[4], "--csv", 5) == 0) { char *p; outform = O_CSV; if ((p = strchr(argv[4], '=')) != NULL) csvdelim = *(p+1); } } } else { char *envarea = NULL; int argi; for (argi = 1; (argi < argc); argi++) { if (argnmatch(argv[argi], "--env=")) { char *p = strchr(argv[argi], '='); loadenv(p+1, envarea); } else if (argnmatch(argv[argi], "--area=")) { char *p = strchr(argv[argi], '='); envarea = strdup(p+1); } else if (strcmp(argv[argi], "--debug") == 0) { debug = 1; } } /* Parse CGI parameters */ parse_query(); format_rrdtime(starttime, &starttimedate, &starttimehm); format_rrdtime(endtime, &endtimedate, &endtimehm); switch (outform) { case O_XML: printf("Content-type: application/xml\n\n"); break; case O_CSV: printf("Content-type: text/csv\n\n"); break; case O_NONE: load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); printf("Content-type: %s\n\n", xgetenv("HTMLCONTENTTYPE")); showform(stdout, "perfdata", "perfdata_form", COL_BLUE, getcurrenttime(NULL), NULL, NULL); return 0; } } load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn()); if (hostpattern) hostptn = compileregex(hostpattern); if (exhostpattern) exhostptn = compileregex(exhostpattern); if (pagepattern) pageptn = compileregex(pagepattern); if (expagepattern) expageptn = compileregex(expagepattern); switch (outform) { case O_XML: printf("<?xml version='1.0' encoding='ISO-8859-1'?>\n"); printf("<datasets>\n"); break; default: break; } dbgprintf("Got hosts, it is %s\n", (first_host() == NULL) ? "empty" : "not empty"); for (hwalk = first_host(); (hwalk); hwalk = next_host(hwalk, 0)) { hostname = xmh_item(hwalk, XMH_HOSTNAME); pagename = xmh_item(hwalk, XMH_PAGEPATH); dbgprintf("Processing host %s\n", hostname); if (hostpattern && !matchregex(hostname, hostptn)) continue; if (exhostpattern && matchregex(hostname, exhostptn)) continue; if (pagepattern && !matchregex(pagename, pageptn)) continue; if (expagepattern && matchregex(pagename, expageptn)) continue; onehost(hostname, starttime, endtime); } switch (outform) { case O_XML: printf("</datasets>\n"); break; default: break; } return 0; }