| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2009, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include <assert.h> |
|---|
| 35 |
#include <errno.h> |
|---|
| 36 |
#include <libxml/parser.h> |
|---|
| 37 |
#include <libxml/tree.h> |
|---|
| 38 |
#include <libxml/xpath.h> |
|---|
| 39 |
#include "noit_listener.h" |
|---|
| 40 |
#include "noit_http.h" |
|---|
| 41 |
#include "noit_rest.h" |
|---|
| 42 |
#include "noit_check.h" |
|---|
| 43 |
#include "noit_check_tools.h" |
|---|
| 44 |
#include "noit_conf.h" |
|---|
| 45 |
#include "noit_conf_private.h" |
|---|
| 46 |
|
|---|
| 47 |
#define FAIL(a) do { error = (a); goto error; } while(0) |
|---|
| 48 |
|
|---|
| 49 |
#define NODE_CONTENT(parent, k, v) do { \ |
|---|
| 50 |
xmlNodePtr tmp; \ |
|---|
| 51 |
if(v) { \ |
|---|
| 52 |
tmp = xmlNewNode(NULL, (xmlChar *)(k)); \ |
|---|
| 53 |
xmlNodeAddContent(tmp, (xmlChar *)(v)); \ |
|---|
| 54 |
xmlAddChild(parent, tmp); \ |
|---|
| 55 |
} \ |
|---|
| 56 |
} while(0) |
|---|
| 57 |
|
|---|
| 58 |
xmlNodePtr |
|---|
| 59 |
noit_check_state_as_xml(noit_check_t *check) { |
|---|
| 60 |
xmlNodePtr state, tmp, metrics; |
|---|
| 61 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 62 |
const char *k; |
|---|
| 63 |
int klen; |
|---|
| 64 |
void *data; |
|---|
| 65 |
stats_t *c = &check->stats.current; |
|---|
| 66 |
|
|---|
| 67 |
state = xmlNewNode(NULL, (xmlChar *)"state"); |
|---|
| 68 |
NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false"); |
|---|
| 69 |
NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false"); |
|---|
| 70 |
NODE_CONTENT(state, "configured", |
|---|
| 71 |
NOIT_CHECK_CONFIGURED(check)?"true":"false"); |
|---|
| 72 |
NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false"); |
|---|
| 73 |
xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run"))); |
|---|
| 74 |
if(check->stats.current.whence.tv_sec) { |
|---|
| 75 |
struct timeval f = check->stats.current.whence; |
|---|
| 76 |
struct timeval n; |
|---|
| 77 |
char timestr[20]; |
|---|
| 78 |
gettimeofday(&n, NULL); |
|---|
| 79 |
snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| 80 |
n.tv_sec + (n.tv_usec / 1000000.0)); |
|---|
| 81 |
xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr); |
|---|
| 82 |
snprintf(timestr, sizeof(timestr), "%0.3f", |
|---|
| 83 |
f.tv_sec + (f.tv_usec / 1000000.0)); |
|---|
| 84 |
xmlNodeAddContent(tmp, (xmlChar *)timestr); |
|---|
| 85 |
} |
|---|
| 86 |
if(c->available) { /* truth here means the check has been run */ |
|---|
| 87 |
char buff[20]; |
|---|
| 88 |
snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0); |
|---|
| 89 |
NODE_CONTENT(state, "runtime", buff); |
|---|
| 90 |
} |
|---|
| 91 |
NODE_CONTENT(state, "availability", |
|---|
| 92 |
noit_check_available_string(c->available)); |
|---|
| 93 |
NODE_CONTENT(state, "state", noit_check_state_string(c->state)); |
|---|
| 94 |
NODE_CONTENT(state, "status", c->status ? c->status : ""); |
|---|
| 95 |
memset(&iter, 0, sizeof(iter)); |
|---|
| 96 |
xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics"))); |
|---|
| 97 |
while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { |
|---|
| 98 |
char buff[256]; |
|---|
| 99 |
metric_t *m = (metric_t *)data; |
|---|
| 100 |
xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)m->metric_name))); |
|---|
| 101 |
buff[0] = m->metric_type; buff[1] = '\0'; |
|---|
| 102 |
xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff); |
|---|
| 103 |
if(m->metric_value.s) { |
|---|
| 104 |
int rv; |
|---|
| 105 |
rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m); |
|---|
| 106 |
if(rv < 0) |
|---|
| 107 |
xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type"); |
|---|
| 108 |
else |
|---|
| 109 |
xmlNodeAddContent(tmp, (xmlChar *)buff); |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
return state; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
static int |
|---|
| 116 |
rest_show_check(noit_http_rest_closure_t *restc, |
|---|
| 117 |
int npats, char **pats) { |
|---|
| 118 |
noit_http_session_ctx *ctx = restc->http_ctx; |
|---|
| 119 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 120 |
xmlXPathContextPtr xpath_ctxt = NULL; |
|---|
| 121 |
xmlDocPtr doc = NULL; |
|---|
| 122 |
xmlNodePtr node, root, attr, config, state, tmp, anode; |
|---|
| 123 |
uuid_t checkid; |
|---|
| 124 |
noit_check_t *check; |
|---|
| 125 |
char xpath[1024], *uuid_conf, *module, *value; |
|---|
| 126 |
int rv, cnt, error_code = 500; |
|---|
| 127 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 128 |
const char *k; |
|---|
| 129 |
int klen; |
|---|
| 130 |
void *data; |
|---|
| 131 |
noit_hash_table *configh; |
|---|
| 132 |
|
|---|
| 133 |
if(npats != 2) goto error; |
|---|
| 134 |
|
|---|
| 135 |
rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); |
|---|
| 136 |
if(rv == 0) goto not_found; |
|---|
| 137 |
if(rv < 0) goto error; |
|---|
| 138 |
|
|---|
| 139 |
noit_conf_xml_xpath(NULL, &xpath_ctxt); |
|---|
| 140 |
pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); |
|---|
| 141 |
if(!pobj || pobj->type != XPATH_NODESET || |
|---|
| 142 |
xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found; |
|---|
| 143 |
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 144 |
if(cnt != 1) goto error; |
|---|
| 145 |
|
|---|
| 146 |
node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); |
|---|
| 147 |
uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); |
|---|
| 148 |
if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; |
|---|
| 149 |
|
|---|
| 150 |
doc = xmlNewDoc((xmlChar *)"1.0"); |
|---|
| 151 |
root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); |
|---|
| 152 |
xmlDocSetRootElement(doc, root); |
|---|
| 153 |
|
|---|
| 154 |
#define MYATTR(node,a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b)) |
|---|
| 155 |
#define INHERIT(node,a,n,b) \ |
|---|
| 156 |
_noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) |
|---|
| 157 |
#define SHOW_ATTR(parent, node, a) do { \ |
|---|
| 158 |
xmlNodePtr anode = NULL; \ |
|---|
| 159 |
char *value = NULL; \ |
|---|
| 160 |
INHERIT(node, a, anode, value); \ |
|---|
| 161 |
if(value != NULL) { \ |
|---|
| 162 |
int clen, plen;\ |
|---|
| 163 |
const char *cpath, *apath; \ |
|---|
| 164 |
xmlNodePtr child; \ |
|---|
| 165 |
cpath = node ? (char *)xmlGetNodePath(node) : ""; \ |
|---|
| 166 |
apath = anode ? (char *)xmlGetNodePath(anode) : ""; \ |
|---|
| 167 |
clen = strlen(cpath); \ |
|---|
| 168 |
plen = strlen("/noit/checks"); \ |
|---|
| 169 |
child = xmlNewNode(NULL, (xmlChar *)#a); \ |
|---|
| 170 |
xmlNodeAddContent(child, (xmlChar *)value); \ |
|---|
| 171 |
if(!strncmp(cpath, apath, clen) && apath[clen] == '/') { \ |
|---|
| 172 |
} \ |
|---|
| 173 |
else { \ |
|---|
| 174 |
xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)apath+plen); \ |
|---|
| 175 |
} \ |
|---|
| 176 |
xmlAddChild(parent, child); \ |
|---|
| 177 |
} \ |
|---|
| 178 |
} while(0) |
|---|
| 179 |
|
|---|
| 180 |
attr = xmlNewNode(NULL, (xmlChar *)"attributes"); |
|---|
| 181 |
xmlAddChild(root, attr); |
|---|
| 182 |
|
|---|
| 183 |
SHOW_ATTR(attr,node,uuid); |
|---|
| 184 |
|
|---|
| 185 |
/* Name is odd, it falls back transparently to module */ |
|---|
| 186 |
if(!INHERIT(node, module, tmp, module)) module = NULL; |
|---|
| 187 |
xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name"))); |
|---|
| 188 |
if(MYATTR(node, name, anode, value)) |
|---|
| 189 |
xmlNodeAddContent(tmp, (xmlChar *)value); |
|---|
| 190 |
else if(module) |
|---|
| 191 |
xmlNodeAddContent(tmp, (xmlChar *)module); |
|---|
| 192 |
|
|---|
| 193 |
SHOW_ATTR(attr,node,module); |
|---|
| 194 |
SHOW_ATTR(attr,node,target); |
|---|
| 195 |
SHOW_ATTR(attr,node,period); |
|---|
| 196 |
SHOW_ATTR(attr,node,timeout); |
|---|
| 197 |
SHOW_ATTR(attr,node,oncheck); |
|---|
| 198 |
SHOW_ATTR(attr,node,filterset); |
|---|
| 199 |
SHOW_ATTR(attr,node,disable); |
|---|
| 200 |
|
|---|
| 201 |
/* Add the config */ |
|---|
| 202 |
config = xmlNewNode(NULL, (xmlChar *)"config"); |
|---|
| 203 |
configh = noit_conf_get_hash(node, "config"); |
|---|
| 204 |
while(noit_hash_next(configh, &iter, &k, &klen, &data)) |
|---|
| 205 |
NODE_CONTENT(config, k, data); |
|---|
| 206 |
noit_hash_destroy(configh, free, free); |
|---|
| 207 |
free(configh); |
|---|
| 208 |
xmlAddChild(root, config); |
|---|
| 209 |
|
|---|
| 210 |
/* Add the state */ |
|---|
| 211 |
check = noit_poller_lookup(checkid); |
|---|
| 212 |
if(!check) { |
|---|
| 213 |
state = xmlNewNode(NULL, (xmlChar *)"state"); |
|---|
| 214 |
xmlSetProp(state, (xmlChar *)"error", (xmlChar *)"true"); |
|---|
| 215 |
} |
|---|
| 216 |
else |
|---|
| 217 |
state = noit_check_state_as_xml(check); |
|---|
| 218 |
xmlAddChild(root, state); |
|---|
| 219 |
noit_http_response_ok(ctx, "text/xml"); |
|---|
| 220 |
noit_http_response_xml(ctx, doc); |
|---|
| 221 |
noit_http_response_end(ctx); |
|---|
| 222 |
goto cleanup; |
|---|
| 223 |
|
|---|
| 224 |
not_found: |
|---|
| 225 |
noit_http_response_not_found(ctx, "text/html"); |
|---|
| 226 |
noit_http_response_end(ctx); |
|---|
| 227 |
goto cleanup; |
|---|
| 228 |
|
|---|
| 229 |
error: |
|---|
| 230 |
noit_http_response_standard(ctx, error_code, "ERROR", "text/html"); |
|---|
| 231 |
noit_http_response_end(ctx); |
|---|
| 232 |
goto cleanup; |
|---|
| 233 |
|
|---|
| 234 |
cleanup: |
|---|
| 235 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 236 |
if(doc) xmlFreeDoc(doc); |
|---|
| 237 |
return 0; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
int |
|---|
| 241 |
noit_validate_check_rest_post(xmlDocPtr doc, xmlNodePtr *a, xmlNodePtr *c, |
|---|
| 242 |
const char **error) { |
|---|
| 243 |
xmlNodePtr root, tl, an; |
|---|
| 244 |
int name=0, module=0, target=0, period=0, timeout=0, filterset=0; |
|---|
| 245 |
*a = *c = NULL; |
|---|
| 246 |
root = xmlDocGetRootElement(doc); |
|---|
| 247 |
if(!root || strcmp((char *)root->name, "check")) return 0; |
|---|
| 248 |
for(tl = root->children; tl; tl = tl->next) { |
|---|
| 249 |
if(!strcmp((char *)tl->name, "attributes")) { |
|---|
| 250 |
*a = tl; |
|---|
| 251 |
for(an = tl->children; an; an = an->next) { |
|---|
| 252 |
#define CHECK_N_SET(a) if(!strcmp((char *)an->name, #a)) |
|---|
| 253 |
CHECK_N_SET(name) { |
|---|
| 254 |
xmlChar *tmp; |
|---|
| 255 |
pcre *valid_name = noit_conf_get_valid_name_checker(); |
|---|
| 256 |
int ovector[30], valid; |
|---|
| 257 |
tmp = xmlNodeGetContent(an); |
|---|
| 258 |
valid = (pcre_exec(valid_name, NULL, |
|---|
| 259 |
(char *)tmp, strlen((char *)tmp), 0, 0, |
|---|
| 260 |
ovector, sizeof(ovector)/sizeof(*ovector)) > 0); |
|---|
| 261 |
xmlFree(tmp); |
|---|
| 262 |
if(!valid) { *error = "invalid name"; return 0; } |
|---|
| 263 |
name = 1; |
|---|
| 264 |
} |
|---|
| 265 |
else CHECK_N_SET(module) module = 1; /* This is validated by called */ |
|---|
| 266 |
else CHECK_N_SET(target) { |
|---|
| 267 |
int valid; |
|---|
| 268 |
xmlChar *tmp; |
|---|
| 269 |
tmp = xmlNodeGetContent(an); |
|---|
| 270 |
valid = noit_check_is_valid_target((char *)tmp); |
|---|
| 271 |
xmlFree(tmp); |
|---|
| 272 |
if(!valid) { *error = "invalid target"; return 0; } |
|---|
| 273 |
target = 1; |
|---|
| 274 |
} |
|---|
| 275 |
else CHECK_N_SET(period) { |
|---|
| 276 |
int pint; |
|---|
| 277 |
xmlChar *tmp; |
|---|
| 278 |
tmp = xmlNodeGetContent(an); |
|---|
| 279 |
pint = noit_conf_string_to_int((char *)tmp); |
|---|
| 280 |
xmlFree(tmp); |
|---|
| 281 |
if(pint < 5000 || pint > 300000) { |
|---|
| 282 |
*error = "invalid period"; |
|---|
| 283 |
return 0; |
|---|
| 284 |
} |
|---|
| 285 |
period = 1; |
|---|
| 286 |
} |
|---|
| 287 |
else CHECK_N_SET(timeout) { |
|---|
| 288 |
int pint; |
|---|
| 289 |
xmlChar *tmp; |
|---|
| 290 |
tmp = xmlNodeGetContent(an); |
|---|
| 291 |
pint = noit_conf_string_to_int((char *)tmp); |
|---|
| 292 |
xmlFree(tmp); |
|---|
| 293 |
if(pint < 0 || pint > 300000) { |
|---|
| 294 |
*error = "invalid timeout"; |
|---|
| 295 |
return 0; |
|---|
| 296 |
} |
|---|
| 297 |
timeout = 1; |
|---|
| 298 |
} |
|---|
| 299 |
else CHECK_N_SET(filterset) filterset = 1; |
|---|
| 300 |
else CHECK_N_SET(disable) { /* not required */ |
|---|
| 301 |
int valid; |
|---|
| 302 |
xmlChar *tmp; |
|---|
| 303 |
tmp = xmlNodeGetContent(an); |
|---|
| 304 |
valid = (!strcasecmp((char *)tmp, "true") || |
|---|
| 305 |
!strcasecmp((char *)tmp, "on") || |
|---|
| 306 |
!strcasecmp((char *)tmp, "false") || |
|---|
| 307 |
!strcasecmp((char *)tmp, "off")); |
|---|
| 308 |
xmlFree(tmp); |
|---|
| 309 |
if(!valid) { *error = "bad disable parameter"; return 0; } |
|---|
| 310 |
target = 1; |
|---|
| 311 |
} |
|---|
| 312 |
else return 0; |
|---|
| 313 |
} |
|---|
| 314 |
} |
|---|
| 315 |
else if(!strcmp((char *)tl->name, "config")) { |
|---|
| 316 |
*c = tl; |
|---|
| 317 |
/* Noop, anything goes */ |
|---|
| 318 |
} |
|---|
| 319 |
else return 0; |
|---|
| 320 |
} |
|---|
| 321 |
if(name && module && target && period && timeout && filterset) return 1; |
|---|
| 322 |
*error = "insufficient information"; |
|---|
| 323 |
return 0; |
|---|
| 324 |
} |
|---|
| 325 |
static void |
|---|
| 326 |
configure_xml_check(xmlNodePtr check, xmlNodePtr a, xmlNodePtr c) { |
|---|
| 327 |
xmlNodePtr n, config, oldconfig; |
|---|
| 328 |
for(n = a->children; n; n = n->next) { |
|---|
| 329 |
#define ATTR2PROP(attr) do { \ |
|---|
| 330 |
if(!strcmp((char *)n->name, #attr)) { \ |
|---|
| 331 |
xmlChar *v = xmlNodeGetContent(n); \ |
|---|
| 332 |
if(v) xmlSetProp(check, n->name, v); \ |
|---|
| 333 |
else xmlUnsetProp(check, n->name); \ |
|---|
| 334 |
if(v) xmlFree(v); \ |
|---|
| 335 |
} \ |
|---|
| 336 |
} while(0) |
|---|
| 337 |
ATTR2PROP(name); |
|---|
| 338 |
ATTR2PROP(target); |
|---|
| 339 |
ATTR2PROP(module); |
|---|
| 340 |
ATTR2PROP(period); |
|---|
| 341 |
ATTR2PROP(timeout); |
|---|
| 342 |
ATTR2PROP(disable); |
|---|
| 343 |
ATTR2PROP(filterset); |
|---|
| 344 |
} |
|---|
| 345 |
for(oldconfig = check->children; oldconfig; oldconfig = oldconfig->next) |
|---|
| 346 |
if(!strcmp((char *)oldconfig->name, "config")) break; |
|---|
| 347 |
config = xmlNewNode(NULL, (xmlChar *)"config"); |
|---|
| 348 |
if(c) { |
|---|
| 349 |
xmlAttrPtr inherit; |
|---|
| 350 |
if((inherit = xmlHasProp(c, (xmlChar *)"inherit")) != NULL && |
|---|
| 351 |
inherit->children && inherit->children->content) |
|---|
| 352 |
xmlSetProp(config, (xmlChar *)"inherit", inherit->children->content); |
|---|
| 353 |
for(n = c->children; n; n = n->next) { |
|---|
| 354 |
xmlChar *v = xmlNodeGetContent(n); |
|---|
| 355 |
xmlNodePtr co = xmlNewNode(NULL, n->name); |
|---|
| 356 |
xmlNodeAddContent(co, v); |
|---|
| 357 |
xmlFree(v); |
|---|
| 358 |
xmlAddChild(config, co); |
|---|
| 359 |
} |
|---|
| 360 |
} |
|---|
| 361 |
if(oldconfig) { |
|---|
| 362 |
xmlReplaceNode(oldconfig, config); |
|---|
| 363 |
xmlFreeNode(oldconfig); |
|---|
| 364 |
} |
|---|
| 365 |
else xmlAddChild(check, config); |
|---|
| 366 |
} |
|---|
| 367 |
static xmlNodePtr |
|---|
| 368 |
make_conf_path(char *path) { |
|---|
| 369 |
xmlNodePtr start, tmp; |
|---|
| 370 |
char fullpath[1024], *tok, *brk; |
|---|
| 371 |
if(!path || strlen(path) < 1) return NULL; |
|---|
| 372 |
snprintf(fullpath, sizeof(fullpath), "%s", path+1); |
|---|
| 373 |
fullpath[strlen(fullpath)-1] = '\0'; |
|---|
| 374 |
start = noit_conf_get_section(NULL, "/noit/checks"); |
|---|
| 375 |
if(!start) return NULL; |
|---|
| 376 |
for (tok = strtok_r(fullpath, "/", &brk); |
|---|
| 377 |
tok; |
|---|
| 378 |
tok = strtok_r(NULL, "/", &brk)) { |
|---|
| 379 |
if(!xmlValidateNameValue((xmlChar *)tok)) return NULL; |
|---|
| 380 |
if(!strcmp(tok, "check")) return NULL; /* These two paths */ |
|---|
| 381 |
if(!strcmp(tok, "config")) return NULL; /* are off limits. */ |
|---|
| 382 |
for (tmp = start->children; tmp; tmp = tmp->next) { |
|---|
| 383 |
if(!strcmp((char *)tmp->name, tok)) break; |
|---|
| 384 |
} |
|---|
| 385 |
if(!tmp) { |
|---|
| 386 |
tmp = xmlNewNode(NULL, (xmlChar *)tok); |
|---|
| 387 |
xmlAddChild(start, tmp); |
|---|
| 388 |
} |
|---|
| 389 |
start = tmp; |
|---|
| 390 |
} |
|---|
| 391 |
return start; |
|---|
| 392 |
} |
|---|
| 393 |
static int |
|---|
| 394 |
rest_delete_check(noit_http_rest_closure_t *restc, |
|---|
| 395 |
int npats, char **pats) { |
|---|
| 396 |
noit_http_session_ctx *ctx = restc->http_ctx; |
|---|
| 397 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 398 |
xmlXPathContextPtr xpath_ctxt = NULL; |
|---|
| 399 |
xmlNodePtr node; |
|---|
| 400 |
uuid_t checkid; |
|---|
| 401 |
noit_check_t *check; |
|---|
| 402 |
const char *error; |
|---|
| 403 |
char xpath[1024], *uuid_conf; |
|---|
| 404 |
int rv, cnt, error_code = 500; |
|---|
| 405 |
noit_boolean exists = noit_false; |
|---|
| 406 |
|
|---|
| 407 |
if(npats != 2) goto error; |
|---|
| 408 |
|
|---|
| 409 |
if(uuid_parse(pats[1], checkid)) goto error; |
|---|
| 410 |
check = noit_poller_lookup(checkid); |
|---|
| 411 |
if(check) |
|---|
| 412 |
exists = noit_true; |
|---|
| 413 |
|
|---|
| 414 |
rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); |
|---|
| 415 |
if(rv == 0) FAIL("uuid not valid"); |
|---|
| 416 |
if(rv < 0) FAIL("Tricky McTrickster... No"); |
|---|
| 417 |
|
|---|
| 418 |
noit_conf_xml_xpath(NULL, &xpath_ctxt); |
|---|
| 419 |
pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); |
|---|
| 420 |
if(!pobj || pobj->type != XPATH_NODESET || |
|---|
| 421 |
xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { |
|---|
| 422 |
if(exists) { error_code = 403; FAIL("uuid not yours"); } |
|---|
| 423 |
goto not_found; |
|---|
| 424 |
} |
|---|
| 425 |
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 426 |
if(cnt != 1) FAIL("internal error, |checkid| > 1"); |
|---|
| 427 |
node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); |
|---|
| 428 |
uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); |
|---|
| 429 |
if(!uuid_conf || strcasecmp(uuid_conf, pats[1])) |
|---|
| 430 |
FAIL("internal error uuid"); |
|---|
| 431 |
|
|---|
| 432 |
/* delete this here */ |
|---|
| 433 |
noit_poller_deschedule(check->checkid); |
|---|
| 434 |
xmlUnlinkNode(node); |
|---|
| 435 |
xmlFreeNode(node); |
|---|
| 436 |
if(noit_conf_write_file(NULL) != 0) |
|---|
| 437 |
noitL(noit_error, "local config write failed\n"); |
|---|
| 438 |
noit_conf_mark_changed(); |
|---|
| 439 |
noit_http_response_ok(ctx, "text/html"); |
|---|
| 440 |
noit_http_response_end(ctx); |
|---|
| 441 |
goto cleanup; |
|---|
| 442 |
|
|---|
| 443 |
not_found: |
|---|
| 444 |
noit_http_response_not_found(ctx, "text/html"); |
|---|
| 445 |
noit_http_response_end(ctx); |
|---|
| 446 |
goto cleanup; |
|---|
| 447 |
|
|---|
| 448 |
error: |
|---|
| 449 |
noit_http_response_standard(ctx, error_code, "ERROR", "text/html"); |
|---|
| 450 |
noit_http_response_end(ctx); |
|---|
| 451 |
goto cleanup; |
|---|
| 452 |
|
|---|
| 453 |
cleanup: |
|---|
| 454 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 455 |
return 0; |
|---|
| 456 |
} |
|---|
| 457 |
|
|---|
| 458 |
static int |
|---|
| 459 |
rest_set_check(noit_http_rest_closure_t *restc, |
|---|
| 460 |
int npats, char **pats) { |
|---|
| 461 |
noit_http_session_ctx *ctx = restc->http_ctx; |
|---|
| 462 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 463 |
xmlXPathContextPtr xpath_ctxt = NULL; |
|---|
| 464 |
xmlDocPtr doc = NULL, indoc = NULL; |
|---|
| 465 |
xmlNodePtr node, root, attr, config, parent; |
|---|
| 466 |
uuid_t checkid; |
|---|
| 467 |
noit_check_t *check; |
|---|
| 468 |
char xpath[1024], *uuid_conf; |
|---|
| 469 |
int rv, cnt, error_code = 500, complete = 0, mask = 0; |
|---|
| 470 |
const char *error = "internal error"; |
|---|
| 471 |
noit_boolean exists = noit_false; |
|---|
| 472 |
|
|---|
| 473 |
if(npats != 2) goto error; |
|---|
| 474 |
|
|---|
| 475 |
indoc = rest_get_xml_upload(restc, &mask, &complete); |
|---|
| 476 |
if(!complete) return mask; |
|---|
| 477 |
if(indoc == NULL) FAIL("xml parse error"); |
|---|
| 478 |
if(!noit_validate_check_rest_post(indoc, &attr, &config, &error)) goto error; |
|---|
| 479 |
|
|---|
| 480 |
if(uuid_parse(pats[1], checkid)) goto error; |
|---|
| 481 |
check = noit_poller_lookup(checkid); |
|---|
| 482 |
if(check) |
|---|
| 483 |
exists = noit_true; |
|---|
| 484 |
|
|---|
| 485 |
rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); |
|---|
| 486 |
if(rv == 0) FAIL("uuid not valid"); |
|---|
| 487 |
if(rv < 0) FAIL("Tricky McTrickster... No"); |
|---|
| 488 |
|
|---|
| 489 |
noit_conf_xml_xpath(NULL, &xpath_ctxt); |
|---|
| 490 |
pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); |
|---|
| 491 |
if(!pobj || pobj->type != XPATH_NODESET || |
|---|
| 492 |
xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { |
|---|
| 493 |
if(exists) { error_code = 403; FAIL("uuid not yours"); } |
|---|
| 494 |
else { |
|---|
| 495 |
char *target = NULL, *name = NULL, *module = NULL; |
|---|
| 496 |
noit_module_t *m; |
|---|
| 497 |
xmlNodePtr newcheck, a; |
|---|
| 498 |
/* make sure this isn't a dup */ |
|---|
| 499 |
for(a = attr->children; a; a = a->next) { |
|---|
| 500 |
if(!strcmp((char *)a->name, "target")) |
|---|
| 501 |
target = (char *)xmlNodeGetContent(a); |
|---|
| 502 |
if(!strcmp((char *)a->name, "name")) |
|---|
| 503 |
name = (char *)xmlNodeGetContent(a); |
|---|
| 504 |
if(!strcmp((char *)a->name, "module")) |
|---|
| 505 |
module = (char *)xmlNodeGetContent(a); |
|---|
| 506 |
} |
|---|
| 507 |
exists = (noit_poller_lookup_by_name(target, name) != NULL); |
|---|
| 508 |
m = noit_module_lookup(module); |
|---|
| 509 |
xmlFree(target); |
|---|
| 510 |
xmlFree(name); |
|---|
| 511 |
xmlFree(module); |
|---|
| 512 |
if(exists) FAIL("target`name already registered"); |
|---|
| 513 |
if(!m) FAIL("module does not exist"); |
|---|
| 514 |
/* create a check here */ |
|---|
| 515 |
newcheck = xmlNewNode(NULL, (xmlChar *)"check"); |
|---|
| 516 |
xmlSetProp(newcheck, (xmlChar *)"uuid", (xmlChar *)pats[1]); |
|---|
| 517 |
configure_xml_check(newcheck, attr, config); |
|---|
| 518 |
parent = make_conf_path(pats[0]); |
|---|
| 519 |
if(!parent) FAIL("invalid path"); |
|---|
| 520 |
xmlAddChild(parent, newcheck); |
|---|
| 521 |
} |
|---|
| 522 |
} |
|---|
| 523 |
if(exists) { |
|---|
| 524 |
int module_change; |
|---|
| 525 |
char *target = NULL, *name = NULL, *module = NULL; |
|---|
| 526 |
xmlNodePtr a; |
|---|
| 527 |
noit_check_t *ocheck; |
|---|
| 528 |
|
|---|
| 529 |
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 530 |
if(cnt != 1) FAIL("internal error, |checkid| > 1"); |
|---|
| 531 |
node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); |
|---|
| 532 |
uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); |
|---|
| 533 |
if(!uuid_conf || strcasecmp(uuid_conf, pats[1])) |
|---|
| 534 |
FAIL("internal error uuid"); |
|---|
| 535 |
|
|---|
| 536 |
/* make sure this isn't a dup */ |
|---|
| 537 |
for(a = attr->children; a; a = a->next) { |
|---|
| 538 |
if(!strcmp((char *)a->name, "target")) |
|---|
| 539 |
target = (char *)xmlNodeGetContent(a); |
|---|
| 540 |
if(!strcmp((char *)a->name, "name")) |
|---|
| 541 |
name = (char *)xmlNodeGetContent(a); |
|---|
| 542 |
if(!strcmp((char *)a->name, "module")) |
|---|
| 543 |
module = (char *)xmlNodeGetContent(a); |
|---|
| 544 |
} |
|---|
| 545 |
ocheck = noit_poller_lookup_by_name(target, name); |
|---|
| 546 |
module_change = strcmp(check->module, module); |
|---|
| 547 |
xmlFree(target); |
|---|
| 548 |
xmlFree(name); |
|---|
| 549 |
xmlFree(module); |
|---|
| 550 |
if(ocheck && ocheck != check) FAIL("new target`name would collide"); |
|---|
| 551 |
if(module_change) FAIL("cannot change module"); |
|---|
| 552 |
configure_xml_check(node, attr, config); |
|---|
| 553 |
parent = make_conf_path(pats[0]); |
|---|
| 554 |
if(!parent) FAIL("invalid path"); |
|---|
| 555 |
xmlUnlinkNode(node); |
|---|
| 556 |
xmlAddChild(parent, node); |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
if(noit_conf_write_file(NULL) != 0) |
|---|
| 560 |
noitL(noit_error, "local config write failed\n"); |
|---|
| 561 |
noit_conf_mark_changed(); |
|---|
| 562 |
noit_poller_reload(xpath); |
|---|
| 563 |
if(restc->call_closure_free) restc->call_closure_free(restc->call_closure); |
|---|
| 564 |
restc->call_closure_free = NULL; |
|---|
| 565 |
restc->call_closure = NULL; |
|---|
| 566 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 567 |
if(doc) xmlFreeDoc(doc); |
|---|
| 568 |
restc->fastpath = rest_show_check; |
|---|
| 569 |
return restc->fastpath(restc, restc->nparams, restc->params); |
|---|
| 570 |
|
|---|
| 571 |
error: |
|---|
| 572 |
noit_http_response_standard(ctx, error_code, "ERROR", "text/html"); |
|---|
| 573 |
doc = xmlNewDoc((xmlChar *)"1.0"); |
|---|
| 574 |
root = xmlNewDocNode(doc, NULL, (xmlChar *)"error", NULL); |
|---|
| 575 |
xmlDocSetRootElement(doc, root); |
|---|
| 576 |
xmlNodeAddContent(root, (xmlChar *)error); |
|---|
| 577 |
noit_http_response_xml(ctx, doc); |
|---|
| 578 |
noit_http_response_end(ctx); |
|---|
| 579 |
goto cleanup; |
|---|
| 580 |
|
|---|
| 581 |
cleanup: |
|---|
| 582 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 583 |
if(doc) xmlFreeDoc(doc); |
|---|
| 584 |
return 0; |
|---|
| 585 |
} |
|---|
| 586 |
|
|---|
| 587 |
void |
|---|
| 588 |
noit_check_rest_init() { |
|---|
| 589 |
assert(noit_http_rest_register_auth( |
|---|
| 590 |
"GET", "/checks/", "^show(/.*)(?<=/)(" UUID_REGEX ")$", |
|---|
| 591 |
rest_show_check, noit_http_rest_client_cert_auth |
|---|
| 592 |
) == 0); |
|---|
| 593 |
assert(noit_http_rest_register_auth( |
|---|
| 594 |
"PUT", "/checks/", "^set(/.*)(?<=/)(" UUID_REGEX ")$", |
|---|
| 595 |
rest_set_check, noit_http_rest_client_cert_auth |
|---|
| 596 |
) == 0); |
|---|
| 597 |
assert(noit_http_rest_register_auth( |
|---|
| 598 |
"DELETE", "/checks/", "^delete(/.*)(?<=/)(" UUID_REGEX ")$", |
|---|
| 599 |
rest_delete_check, noit_http_rest_client_cert_auth |
|---|
| 600 |
) == 0); |
|---|
| 601 |
} |
|---|
| 602 |
|
|---|