int loadstatus(int maxprio, time_t maxage, int mincolor, int wantacked) { char *board, *bol, *eol; time_t now; /* * We leak memory by dup'ing this and not freeing it. * But we cannot free it, because the tree holding the data * for later printing contains pointers into this string buffer. */ board = strdup(boardmaster); now = getcurrenttime(NULL); treecount++; if (treecount == 1) { rbstate = malloc(sizeof(void *)); hostsonpage = malloc(sizeof(void *)); } else { rbstate = realloc(rbstate, (treecount) * sizeof(void *)); hostsonpage = realloc(hostsonpage, (treecount) * sizeof(void *)); } rbstate[treecount-1] = xtreeNew(strcasecmp); hostsonpage[treecount-1] = xtreeNew(strcasecmp); bol = board; while (bol && (*bol)) { char *endkey; eol = strchr(bol, '\n'); if (eol) *eol = '\0'; /* Find the config entry */ endkey = strchr(bol, '|'); if (endkey) endkey = strchr(endkey+1, '|'); if (endkey) { critconf_t *cfg; char *ackstr, *ackrtimestr, *ackvtimestr, *acklevelstr, *ackbystr, *ackmsgstr; *endkey = '\0'; cfg = get_critconfig(bol, CRITCONF_TIMEFILTER, NULL); *endkey = '|'; if (cfg) { hstatus_t *newitem = (hstatus_t *)calloc(1, sizeof(hstatus_t)); newitem->config = cfg; newitem->hostname = gettok(bol, "|"); newitem->testname = gettok(NULL, "|"); newitem->color = parse_color(gettok(NULL, "|")); newitem->lastchange = atoi(gettok(NULL, "|")); newitem->logtime = atoi(gettok(NULL, "|")); newitem->validtime = atoi(gettok(NULL, "|")); ackstr = gettok(NULL, "|"); ackrtimestr = ackvtimestr = acklevelstr = ackbystr = ackmsgstr = NULL; if (ackstr) { nldecode(ackstr); ackrtimestr = strtok(ackstr, ":"); if (ackrtimestr) ackvtimestr = strtok(NULL, ":"); if (ackvtimestr) acklevelstr = strtok(NULL, ":"); if (acklevelstr) ackbystr = strtok(NULL, ":"); if (ackbystr) ackmsgstr = strtok(NULL, ":"); } if ( (hostinfo(newitem->hostname) == NULL) || ((newitem->config->priority > maxprio) && (newitem->config->priority != 99)) || ((now - newitem->lastchange) > maxage) || (newitem->color < mincolor) || (ackmsgstr && !wantacked) ) { xfree(newitem); } else { if (ackvtimestr && ackbystr && ackmsgstr) { newitem->acktime = atoi(ackvtimestr); newitem->ackedby = strdup(ackbystr); newitem->ackmsg = strdup(ackmsgstr); } newitem->key = (char *)malloc(strlen(newitem->hostname) + strlen(newitem->testname) + 2); sprintf(newitem->key, "%s|%s", newitem->hostname, newitem->testname); xtreeAdd(rbstate[treecount-1], newitem->key, newitem); } } } bol = (eol ? (eol+1) : NULL); } return 0; }
int loadstatus(int maxprio, time_t maxage, int mincolor, int wantacked) { int xymondresult; char *board = NULL; char *bol, *eol; time_t now; char msg[1024]; int i; sendreturn_t *sres; sprintf(msg, "xymondboard acklevel=%d fields=hostname,testname,color,lastchange,logtime,validtime,acklist color=%s", critacklevel,colorname(mincolor)); for (i=mincolor+1; (i < COL_COUNT); i++) sprintf(msg+strlen(msg), ",%s", colorname(i)); sres = newsendreturnbuf(1, NULL); xymondresult = sendmessage(msg, NULL, XYMON_TIMEOUT, sres); if (xymondresult != XYMONSEND_OK) { freesendreturnbuf(sres); errormsg("Unable to fetch current status\n"); return 1; } else { board = getsendreturnstr(sres, 1); freesendreturnbuf(sres); } now = getcurrenttime(NULL); rbstate = rbtNew(name_compare); bol = board; while (bol && (*bol)) { char *endkey; RbtStatus status; eol = strchr(bol, '\n'); if (eol) *eol = '\0'; /* Find the config entry */ endkey = strchr(bol, '|'); if (endkey) endkey = strchr(endkey+1, '|'); if (endkey) { critconf_t *cfg; char *ackstr, *ackrtimestr, *ackvtimestr, *acklevelstr, *ackbystr, *ackmsgstr; *endkey = '\0'; cfg = get_critconfig(bol, CRITCONF_TIMEFILTER, NULL); *endkey = '|'; if (cfg) { hstatus_t *newitem = (hstatus_t *)calloc(1, sizeof(hstatus_t)); newitem->config = cfg; newitem->hostname = gettok(bol, "|"); newitem->testname = gettok(NULL, "|"); newitem->color = parse_color(gettok(NULL, "|")); newitem->lastchange = atoi(gettok(NULL, "|")); newitem->logtime = atoi(gettok(NULL, "|")); newitem->validtime = atoi(gettok(NULL, "|")); ackstr = gettok(NULL, "|"); ackrtimestr = ackvtimestr = acklevelstr = ackbystr = ackmsgstr = NULL; if (ackstr) { nldecode(ackstr); ackrtimestr = strtok(ackstr, ":"); if (ackrtimestr) ackvtimestr = strtok(NULL, ":"); if (ackvtimestr) acklevelstr = strtok(NULL, ":"); if (acklevelstr) ackbystr = strtok(NULL, ":"); if (ackbystr) ackmsgstr = strtok(NULL, ":"); } if ( (hostinfo(newitem->hostname) == NULL) || (newitem->config->priority > maxprio) || ((now - newitem->lastchange) > maxage) || (newitem->color < mincolor) || (ackmsgstr && !wantacked) ) { xfree(newitem); } else { if (ackvtimestr && ackbystr && ackmsgstr) { newitem->acktime = atoi(ackvtimestr); newitem->ackedby = strdup(ackbystr); newitem->ackmsg = strdup(ackmsgstr); } newitem->key = (char *)malloc(strlen(newitem->hostname) + strlen(newitem->testname) + 2); sprintf(newitem->key, "%s|%s", newitem->hostname, newitem->testname); status = rbtInsert(rbstate, newitem->key, newitem); } } } bol = (eol ? (eol+1) : NULL); } return 0; }