1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*----------------------------------------------------------------------------*/
/* Xymon status-log viewer CGI. */
/* */
/* This CGI provides an XML interface to the xymondboard status. Intended for */
/* use by external user interfaces, e.g. smartphones. */
/* */
/* Copyright (C) 2011 Henrik Storner <henrik@storner.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
static char rcsid[] = "$Id: svcstatus.c 6765 2011-10-13 11:55:08Z storner $";
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "libxymon.h"
static char *queryfilter = NULL;
static char *boardcmd = "xymondboard";
static char *fieldlist = "fields=hostname,testname,color,lastchange,logtime,cookie,acktime,ackmsg,disabletime,dismsg,line1";
static char *colorlist = "color=red,yellow,purple";
static void errormsg(char *msg)
{
fprintf(stderr,
"Refresh: 30\nContent-type: %s\n\n<html><head><title>Invalid request</title></head>\n<body>%s</body></html>\n",
xgetenv("HTMLCONTENTTYPE"), msg);
}
static int parse_query(void)
{
cgidata_t *cgidata = cgi_request();
cgidata_t *cwalk;
cwalk = cgidata;
while (cwalk) {
if (strcasecmp(cwalk->name, "filter") == 0) {
queryfilter = strdup(cwalk->value);
}
cwalk = cwalk->next;
}
if (!queryfilter) queryfilter = "";
/* See if the query includes a color filter - this overrides our default */
if ((strncmp(queryfilter, "color=", 6) == 0) || (strstr(queryfilter, " color=") != NULL)) colorlist = "";
return 0;
}
char *extractline(char *ptn, char **src)
{
char *pos = strstr(*src, ptn);
char *eoln;
if (pos == NULL) return NULL;
eoln = strchr(pos, '\n');
if (eoln) *eoln = '\0';
if (pos == *src)
*src = eoln+1;
else
*(pos-1) = '\0';
if (pos) pos += strlen(ptn);
return pos;
}
int main(int argc, char **argv)
{
int argi;
char *criticalconfig = NULL;
char *envarea = NULL;
char *accessfn = NULL;
char *userid = getenv("REMOTE_USER");
FILE *output = stdout;
SBUF_DEFINE(xymondreq);
sendreturn_t *sres;
int xymondresult;
char *log, *bol, *eoln, *endkey;
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 (strcmp(argv[argi], "--hobbit") == 0) {
boardcmd = "hobbitdboard";
}
else if (argnmatch(argv[argi], "--critical=")) {
char *p = strchr(argv[argi], '=');
criticalconfig = strdup(p+1);
}
else if (argnmatch(argv[argi], "--access=")) {
char *p = strchr(argv[argi], '=');
accessfn = strdup(p+1);
}
}
/* Setup the query for xymond */
parse_query();
SBUF_MALLOC(xymondreq, strlen(boardcmd) + strlen(fieldlist) + strlen(colorlist) + strlen(queryfilter) + 5);
snprintf(xymondreq, xymondreq_buflen, "%s %s %s %s", boardcmd, fieldlist, colorlist, queryfilter);
/* Get the current status */
sres = newsendreturnbuf(1, NULL);
xymondresult = sendmessage(xymondreq, NULL, XYMON_TIMEOUT, sres);
if (xymondresult != XYMONSEND_OK) {
SBUF_DEFINE(errtxt);
SBUF_MALLOC(errtxt, 1024 + MAX_HTMLQUOTE_FACTOR*strlen(xymondreq));
snprintf(errtxt, errtxt_buflen, "Status not available: Req=%s, result=%d\n", htmlquoted(xymondreq), xymondresult);
errormsg(errtxt);
return 1;
}
else {
log = getsendreturnstr(sres, 1);
}
freesendreturnbuf(sres);
/* Load the host data (for access control) */
if (accessfn) {
load_hostnames(xgetenv("HOSTSCFG"), NULL, get_fqdn());
load_web_access_config(accessfn);
}
/* Load the critical config */
if (criticalconfig) load_critconfig(criticalconfig);
fprintf(output, "Content-type: text/xml\n\n");
fprintf(output, "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
fprintf(output, "<StatusBoard>\n");
/* Step through the status board, one line at a time */
bol = log;
while (bol && *bol) {
int useit = 1;
char *hostname, *testname, *color, *txt, *lastchange, *logtime, *cookie, *acktime, *ackmsg, *distime, *dismsg;
eoln = strchr(bol, '\n'); if (eoln) *eoln = '\0';
if (criticalconfig) {
critconf_t *cfg;
/* The key for looking up items in the critical config is "hostname|testname", which we already have */
endkey = strchr(bol, '|'); if (endkey) endkey = strchr(endkey+1, '|');
*endkey = '\0';
cfg = get_critconfig(bol, CRITCONF_TIMEFILTER, NULL);
*endkey = '|';
if (!cfg) useit = 0;
}
if (useit) {
hostname = gettok(bol, "|");
testname = (hostname ? gettok(NULL, "|") : NULL);
if (accessfn) useit = web_access_allowed(userid, hostname, testname, WEB_ACCESS_VIEW);
}
if (useit) {
color = (testname ? gettok(NULL, "|") : NULL);
lastchange = (color ? gettok(NULL, "|") : NULL);
logtime = (lastchange ? gettok(NULL, "|") : NULL);
cookie = (logtime ? gettok(NULL, "|") : NULL);
acktime = (cookie ? gettok(NULL, "|") : NULL);
ackmsg = (acktime ? gettok(NULL, "|") : NULL);
distime = (ackmsg ? gettok(NULL, "|") : NULL);
dismsg = (distime ? gettok(NULL, "|") : NULL);
txt = (dismsg ? gettok(NULL, "|") : NULL);
if (txt) {
/* We have all data */
fprintf(output, "<ServerStatus>\n");
fprintf(output, " <Servername>%s</Servername>\n", hostname);
fprintf(output, " <Type>%s</Type>\n", testname);
fprintf(output, " <Status>%s</Status>\n", color);
fprintf(output, " <LastChange>%s</LastChange>\n", lastchange);
fprintf(output, " <LogTime>%s</LogTime>\n", logtime);
fprintf(output, " <Cookie>%s</Cookie>\n", cookie);
if (atoi(acktime) != 0) {
char *ackedby;
nldecode(ackmsg);
ackedby = extractline("Acked by: ", &ackmsg);
fprintf(output, " <AckTime>%s</AckTime>\n", acktime);
fprintf(output, " <AckText><![CDATA[%s]]></AckText>\n", ackmsg);
if (ackedby) fprintf(output, " <AckedBy><![CDATA[%s]]></AckedBy>\n", ackedby);
}
if (atoi(distime) != 0) {
char *disabledby;
nldecode(dismsg);
disabledby = extractline("Disabled by: ", &dismsg);
if (strncmp(dismsg, "Reason: ", 8) == 0) dismsg += 8;
fprintf(output, " <DisableTime>%s</DisableTime>\n", distime);
fprintf(output, " <DisableText><![CDATA[%s]]></DisableText>\n", dismsg);
if (disabledby) fprintf(output, " <DisabledBy><![CDATA[%s]]></DisabledBy>\n", disabledby);
}
fprintf(output, " <MessageSummary><![CDATA[%s]]></MessageSummary>\n", txt);
fprintf(output, " <DetailURL><![CDATA[%s]]></DetailURL>\n", hostsvcurl(hostname, testname, 0));
fprintf(output, "</ServerStatus>\n");
}
}
if (eoln) {
*eoln = '\n';
bol = eoln+1;
}
else
bol = NULL;
}
fprintf(output, "</StatusBoard>\n");
xfree(log);
return 0;
}
|