root/src/noit_check_rest.c

Revision c6c11c3827cd812cc0f9cc92cf6de8140b859416, 23.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

Support generic module configuration with using XML namespaces and
allow modules to register themselves as as interested in such for
all checks.

  • Property mode set to 100644
Line 
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 #include "noit_filters.h"
47
48 #define FAIL(a) do { error = (a); goto error; } while(0)
49
50 #define NS_NODE_CONTENT(parent, ns, k, v) do { \
51   xmlNodePtr tmp; \
52   if(v) { \
53     tmp = xmlNewNode(ns, (xmlChar *)(k)); \
54     xmlNodeAddContent(tmp, (xmlChar *)(v)); \
55     xmlAddChild(parent, tmp); \
56   } \
57 } while(0)
58 #define NODE_CONTENT(parent, k, v) NS_NODE_CONTENT(parent, NULL, k, v)
59
60 xmlNodePtr
61 noit_check_state_as_xml(noit_check_t *check) {
62   xmlNodePtr state, tmp, metrics;
63   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
64   const char *k;
65   int klen;
66   void *data;
67   stats_t *c = &check->stats.current;
68
69   state = xmlNewNode(NULL, (xmlChar *)"state");
70   NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false");
71   NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false");
72   NODE_CONTENT(state, "configured",
73                NOIT_CHECK_CONFIGURED(check)?"true":"false");
74   NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false");
75   NODE_CONTENT(state, "target_ip", check->target_ip);
76   xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run")));
77   if(check->stats.current.whence.tv_sec) {
78     struct timeval f = check->stats.current.whence;
79     struct timeval n;
80     char timestr[20];
81     gettimeofday(&n, NULL);
82     snprintf(timestr, sizeof(timestr), "%0.3f",
83              n.tv_sec + (n.tv_usec / 1000000.0));
84     xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr);
85     snprintf(timestr, sizeof(timestr), "%0.3f",
86              f.tv_sec + (f.tv_usec / 1000000.0));
87     xmlNodeAddContent(tmp, (xmlChar *)timestr);
88   }
89   if(c->available) { /* truth here means the check has been run */
90     char buff[20], *compiler_warning;
91     snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0);
92     compiler_warning = buff;
93     NODE_CONTENT(state, "runtime", compiler_warning);
94   }
95   NODE_CONTENT(state, "availability",
96                noit_check_available_string(c->available));
97   NODE_CONTENT(state, "state", noit_check_state_string(c->state));
98   NODE_CONTENT(state, "status", c->status ? c->status : "");
99   memset(&iter, 0, sizeof(iter));
100   xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics")));
101   while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) {
102     char buff[256];
103     metric_t *m = (metric_t *)data;
104     xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)"metric")));
105     xmlSetProp(tmp, (xmlChar *)"name", (xmlChar *)m->metric_name);
106     buff[0] = m->metric_type; buff[1] = '\0';
107     xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff);
108     if(m->metric_value.s) {
109       int rv;
110       rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m);
111       if(rv < 0)
112         xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type");
113       else
114         xmlNodeAddContent(tmp, (xmlChar *)buff);
115     }
116   }
117   return state;
118 }
119
120 static int
121 rest_show_check(noit_http_rest_closure_t *restc,
122                 int npats, char **pats) {
123   noit_http_session_ctx *ctx = restc->http_ctx;
124   xmlXPathObjectPtr pobj = NULL;
125   xmlXPathContextPtr xpath_ctxt = NULL;
126   xmlDocPtr doc = NULL;
127   xmlNodePtr node, root, attr, config, state, tmp, anode;
128   uuid_t checkid;
129   noit_check_t *check;
130   char xpath[1024], *uuid_conf, *module, *value;
131   int rv, mod, mod_cnt, cnt, error_code = 500;
132   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
133   const char *k;
134   int klen;
135   void *data;
136   noit_hash_table *configh;
137
138   if(npats != 2) goto error;
139
140   rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]);
141   if(rv == 0) goto not_found;
142   if(rv < 0) goto error;
143
144   noit_conf_xml_xpath(NULL, &xpath_ctxt);
145   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
146   if(!pobj || pobj->type != XPATH_NODESET ||
147      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found;
148   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
149   if(cnt != 1) goto error;
150
151   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
152   uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
153   if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error;
154
155   doc = xmlNewDoc((xmlChar *)"1.0");
156   root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL);
157   xmlDocSetRootElement(doc, root);
158
159 #define MYATTR(node,a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b))
160 #define INHERIT(node,a,n,b) \
161   _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b))
162 #define SHOW_ATTR(parent, node, a) do { \
163   xmlNodePtr anode = NULL; \
164   char *value = NULL; \
165   INHERIT(node, a, anode, value); \
166   if(value != NULL) { \
167     int clen, plen;\
168     const char *cpath, *apath; \
169     xmlNodePtr child; \
170     cpath = node ? (char *)xmlGetNodePath(node) : ""; \
171     apath = anode ? (char *)xmlGetNodePath(anode) : ""; \
172     clen = strlen(cpath); \
173     plen = strlen("/noit/checks"); \
174     child = xmlNewNode(NULL, (xmlChar *)#a); \
175     xmlNodeAddContent(child, (xmlChar *)value); \
176     if(!strncmp(cpath, apath, clen) && apath[clen] == '/') { \
177     } \
178     else { \
179       xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)apath+plen); \
180     } \
181     xmlAddChild(parent, child); \
182   } \
183 } while(0)
184
185   attr = xmlNewNode(NULL, (xmlChar *)"attributes");
186   xmlAddChild(root, attr);
187
188   SHOW_ATTR(attr,node,uuid);
189
190   /* Name is odd, it falls back transparently to module */
191   if(!INHERIT(node, module, tmp, module)) module = NULL;
192   xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name")));
193   if(MYATTR(node, name, anode, value))
194     xmlNodeAddContent(tmp, (xmlChar *)value);
195   else if(module)
196     xmlNodeAddContent(tmp, (xmlChar *)module);
197
198   SHOW_ATTR(attr,node,module);
199   SHOW_ATTR(attr,node,target);
200   SHOW_ATTR(attr,node,resolve_rtype);
201   SHOW_ATTR(attr,node,period);
202   SHOW_ATTR(attr,node,timeout);
203   SHOW_ATTR(attr,node,oncheck);
204   SHOW_ATTR(attr,node,filterset);
205   SHOW_ATTR(attr,node,disable);
206
207   /* Add the config */
208   config = xmlNewNode(NULL, (xmlChar *)"config");
209   configh = noit_conf_get_hash(node, "config");
210   while(noit_hash_next(configh, &iter, &k, &klen, &data))
211     NODE_CONTENT(config, k, data);
212   noit_hash_destroy(configh, free, free);
213   free(configh);
214   mod_cnt = noit_check_registered_module_cnt();
215   for(mod=0; mod<mod_cnt; mod++) {
216     xmlNsPtr ns;
217     const char *nsname;
218     char buff[256];
219     nsname = noit_check_registered_module(mod);
220     snprintf(buff, sizeof(buff), "noit://module/%s", nsname);
221     ns = xmlNewNs(config, (xmlChar *)buff, (xmlChar *)nsname);
222     if(NULL != (configh = noit_conf_get_namespaced_hash(node, "config", nsname))) {
223       while(noit_hash_next(configh, &iter, &k, &klen, &data))
224         NS_NODE_CONTENT(config, ns, k, data);
225       noit_hash_destroy(configh, free, free);
226       free(configh);
227     }
228   }
229   xmlAddChild(root, config);
230
231   /* Add the state */
232   check = noit_poller_lookup(checkid);
233   if(!check) {
234     state = xmlNewNode(NULL, (xmlChar *)"state");
235     xmlSetProp(state, (xmlChar *)"error", (xmlChar *)"true");
236   }
237   else
238     state = noit_check_state_as_xml(check);
239   xmlAddChild(root, state);
240   noit_http_response_ok(ctx, "text/xml");
241   noit_http_response_xml(ctx, doc);
242   noit_http_response_end(ctx);
243   goto cleanup;
244
245  not_found:
246   noit_http_response_not_found(ctx, "text/html");
247   noit_http_response_end(ctx);
248   goto cleanup;
249
250  error:
251   noit_http_response_standard(ctx, error_code, "ERROR", "text/html");
252   noit_http_response_end(ctx);
253   goto cleanup;
254
255  cleanup:
256   if(pobj) xmlXPathFreeObject(pobj);
257   if(doc) xmlFreeDoc(doc);
258   return 0;
259 }
260
261 int
262 noit_validate_check_rest_post(xmlDocPtr doc, xmlNodePtr *a, xmlNodePtr *c,
263                               const char **error) {
264   xmlNodePtr root, tl, an;
265   int name=0, module=0, target=0, period=0, timeout=0, filterset=0;
266   *a = *c = NULL;
267   root = xmlDocGetRootElement(doc);
268   if(!root || strcmp((char *)root->name, "check")) return 0;
269   for(tl = root->children; tl; tl = tl->next) {
270     if(!strcmp((char *)tl->name, "attributes")) {
271       *a = tl;
272       for(an = tl->children; an; an = an->next) {
273 #define CHECK_N_SET(a) if(!strcmp((char *)an->name, #a))
274         CHECK_N_SET(name) {
275           xmlChar *tmp;
276           pcre *valid_name = noit_conf_get_valid_name_checker();
277           int ovector[30], valid;
278           tmp = xmlNodeGetContent(an);
279           valid = (pcre_exec(valid_name, NULL,
280                              (char *)tmp, strlen((char *)tmp), 0, 0,
281                              ovector, sizeof(ovector)/sizeof(*ovector)) > 0);
282           xmlFree(tmp);
283           if(!valid) { *error = "invalid name"; return 0; }
284           name = 1;
285         }
286         else CHECK_N_SET(module) module = 1; /* This is validated by called */
287         else CHECK_N_SET(target) {
288           noit_boolean should_resolve;
289           int valid;
290           xmlChar *tmp;
291           tmp = xmlNodeGetContent(an);
292           valid = noit_check_is_valid_target((char *)tmp);
293           xmlFree(tmp);
294           if(noit_conf_get_boolean(NULL, "//checks/@resolve_targets",
295                                    &should_resolve) &&
296              should_resolve == noit_false &&
297              !valid) {
298             *error = "invalid target";
299             return 0;
300           }
301           target = 1;
302         }
303         else CHECK_N_SET(resolve_rtype) {
304           xmlChar *tmp;
305           noit_boolean invalid;
306           tmp = xmlNodeGetContent(an);
307           invalid = strcmp((char *)tmp, PREFER_IPV4) &&
308                     strcmp((char *)tmp, PREFER_IPV6) &&
309                     strcmp((char *)tmp, FORCE_IPV4) &&
310                     strcmp((char *)tmp, FORCE_IPV6);
311           xmlFree(tmp);
312           if(invalid) {
313             *error = "invalid reslove_rtype";
314             return 0;
315           }
316         }
317         else CHECK_N_SET(period) {
318           int pint;
319           xmlChar *tmp;
320           tmp = xmlNodeGetContent(an);
321           pint = noit_conf_string_to_int((char *)tmp);
322           xmlFree(tmp);
323           if(pint < 1000 || pint > 300000) {
324             *error = "invalid period";
325             return 0;
326           }
327           period = 1;
328         }
329         else CHECK_N_SET(timeout) {
330           int pint;
331           xmlChar *tmp;
332           tmp = xmlNodeGetContent(an);
333           pint = noit_conf_string_to_int((char *)tmp);
334           xmlFree(tmp);
335           if(pint < 0 || pint > 300000) {
336             *error = "invalid timeout";
337             return 0;
338           }
339           timeout = 1;
340         }
341         else CHECK_N_SET(filterset) {
342           xmlChar *tmp;
343           tmp = xmlNodeGetContent(an);
344           if(!noit_filter_exists((char *)tmp)) {
345             *error = "filterset does not exist";
346             return 0;
347           }
348           filterset = 1;
349         }
350         else CHECK_N_SET(disable) { /* not required */
351           int valid;
352           xmlChar *tmp;
353           tmp = xmlNodeGetContent(an);
354           valid = (!strcasecmp((char *)tmp, "true") ||
355                    !strcasecmp((char *)tmp, "on") ||
356                    !strcasecmp((char *)tmp, "false") ||
357                    !strcasecmp((char *)tmp, "off"));
358           xmlFree(tmp);
359           if(!valid) { *error = "bad disable parameter"; return 0; }
360           target = 1;
361         }
362         else return 0;
363       }
364     }
365     else if(!strcmp((char *)tl->name, "config")) {
366       *c = tl;
367       /* Noop, anything goes */
368     }
369     else return 0;
370   }
371   if(name && module && target && period && timeout && filterset) return 1;
372   *error = "insufficient information";
373   return 0;
374 }
375 static void
376 configure_xml_check(xmlNodePtr check, xmlNodePtr a, xmlNodePtr c) {
377   xmlNodePtr n, config, oldconfig;
378   for(n = a->children; n; n = n->next) {
379 #define ATTR2PROP(attr) do { \
380   if(!strcmp((char *)n->name, #attr)) { \
381     xmlChar *v = xmlNodeGetContent(n); \
382     if(v) xmlSetProp(check, n->name, v); \
383     else xmlUnsetProp(check, n->name); \
384     if(v) xmlFree(v); \
385   } \
386 } while(0)
387     ATTR2PROP(name);
388     ATTR2PROP(target);
389     ATTR2PROP(resolve_rtype);
390     ATTR2PROP(module);
391     ATTR2PROP(period);
392     ATTR2PROP(timeout);
393     ATTR2PROP(disable);
394     ATTR2PROP(filterset);
395   }
396   for(oldconfig = check->children; oldconfig; oldconfig = oldconfig->next)
397     if(!strcmp((char *)oldconfig->name, "config")) break;
398   config = xmlNewNode(NULL, (xmlChar *)"config");
399   if(c) {
400     xmlAttrPtr inherit;
401     if((inherit = xmlHasProp(c, (xmlChar *)"inherit")) != NULL &&
402         inherit->children && inherit->children->content)
403       xmlSetProp(config, (xmlChar *)"inherit", inherit->children->content);
404     for(n = c->children; n; n = n->next) {
405       xmlNsPtr targetns = NULL;
406       xmlChar *v = xmlNodeGetContent(n);
407       if(n->ns) {
408         targetns = xmlSearchNs(check->doc, config, n->ns->prefix);
409         if(!targetns) targetns = xmlNewNs(config, n->ns->href, n->ns->prefix);
410       }
411       xmlNodePtr co = xmlNewNode(targetns, n->name);
412       xmlNodeAddContent(co, v);
413       xmlFree(v);
414       xmlAddChild(config, co);
415     }
416   }
417   if(oldconfig) {
418     xmlReplaceNode(oldconfig, config);
419     xmlFreeNode(oldconfig);
420   }
421   else xmlAddChild(check, config);
422 }
423 static xmlNodePtr
424 make_conf_path(char *path) {
425   xmlNodePtr start, tmp;
426   char fullpath[1024], *tok, *brk;
427   if(!path || strlen(path) < 1) return NULL;
428   snprintf(fullpath, sizeof(fullpath), "%s", path+1);
429   fullpath[strlen(fullpath)-1] = '\0';
430   start = noit_conf_get_section(NULL, "/noit/checks");
431   if(!start) return NULL;
432   for (tok = strtok_r(fullpath, "/", &brk);
433        tok;
434        tok = strtok_r(NULL, "/", &brk)) {
435     if(!xmlValidateNameValue((xmlChar *)tok)) return NULL;
436     if(!strcmp(tok, "check")) return NULL;  /* These two paths */
437     if(!strcmp(tok, "config")) return NULL; /* are off limits. */
438     for (tmp = start->children; tmp; tmp = tmp->next) {
439       if(!strcmp((char *)tmp->name, tok)) break;
440     }
441     if(!tmp) {
442       tmp = xmlNewNode(NULL, (xmlChar *)tok);
443       xmlAddChild(start, tmp);
444     }
445     start = tmp;
446   }
447   return start;
448 }
449 static int
450 rest_delete_check(noit_http_rest_closure_t *restc,
451                   int npats, char **pats) {
452   noit_http_session_ctx *ctx = restc->http_ctx;
453   xmlXPathObjectPtr pobj = NULL;
454   xmlXPathContextPtr xpath_ctxt = NULL;
455   xmlNodePtr node;
456   uuid_t checkid;
457   noit_check_t *check;
458   const char *error;
459   char xpath[1024], *uuid_conf;
460   int rv, cnt, error_code = 500;
461   noit_boolean exists = noit_false;
462
463   if(npats != 2) goto error;
464
465   if(uuid_parse(pats[1], checkid)) goto error;
466   check = noit_poller_lookup(checkid);
467   if(check)
468     exists = noit_true;
469
470   rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]);
471   if(rv == 0) FAIL("uuid not valid");
472   if(rv < 0) FAIL("Tricky McTrickster... No");
473
474   noit_conf_xml_xpath(NULL, &xpath_ctxt);
475   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
476   if(!pobj || pobj->type != XPATH_NODESET ||
477      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
478     if(exists) { error_code = 403; FAIL("uuid not yours"); }
479     goto not_found;
480   }
481   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
482   if(cnt != 1) FAIL("internal error, |checkid| > 1");
483   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
484   uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
485   if(!uuid_conf || strcasecmp(uuid_conf, pats[1]))
486     FAIL("internal error uuid");
487
488   /* delete this here */
489   noit_poller_deschedule(check->checkid);
490   xmlUnlinkNode(node);
491   xmlFreeNode(node);
492   if(noit_conf_write_file(NULL) != 0)
493     noitL(noit_error, "local config write failed\n");
494   noit_conf_mark_changed();
495   noit_http_response_ok(ctx, "text/html");
496   noit_http_response_end(ctx);
497   goto cleanup;
498
499  not_found:
500   noit_http_response_not_found(ctx, "text/html");
501   noit_http_response_end(ctx);
502   goto cleanup;
503
504  error:
505   noit_http_response_standard(ctx, error_code, "ERROR", "text/html");
506   noit_http_response_end(ctx);
507   goto cleanup;
508
509  cleanup:
510   if(pobj) xmlXPathFreeObject(pobj);
511   (void)error;
512   return 0;
513 }
514
515 static int
516 rest_set_check(noit_http_rest_closure_t *restc,
517                int npats, char **pats) {
518   noit_http_session_ctx *ctx = restc->http_ctx;
519   xmlXPathObjectPtr pobj = NULL;
520   xmlXPathContextPtr xpath_ctxt = NULL;
521   xmlDocPtr doc = NULL, indoc = NULL;
522   xmlNodePtr node, root, attr, config, parent;
523   uuid_t checkid;
524   noit_check_t *check;
525   char xpath[1024], *uuid_conf;
526   int rv, cnt, error_code = 500, complete = 0, mask = 0;
527   const char *error = "internal error";
528   noit_boolean exists = noit_false;
529
530   if(npats != 2) goto error;
531
532   indoc = rest_get_xml_upload(restc, &mask, &complete);
533   if(!complete) return mask;
534   if(indoc == NULL) FAIL("xml parse error");
535   if(!noit_validate_check_rest_post(indoc, &attr, &config, &error)) goto error;
536
537   if(uuid_parse(pats[1], checkid)) goto error;
538   check = noit_poller_lookup(checkid);
539   if(check)
540     exists = noit_true;
541
542   rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]);
543   if(rv == 0) FAIL("uuid not valid");
544   if(rv < 0) FAIL("Tricky McTrickster... No");
545
546   noit_conf_xml_xpath(NULL, &xpath_ctxt);
547   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
548   if(!pobj || pobj->type != XPATH_NODESET ||
549      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
550     if(exists) { error_code = 403; FAIL("uuid not yours"); }
551     else {
552       char *target = NULL, *name = NULL, *module = NULL;
553       noit_module_t *m;
554       xmlNodePtr newcheck, a;
555       /* make sure this isn't a dup */
556       for(a = attr->children; a; a = a->next) {
557         if(!strcmp((char *)a->name, "target"))
558           target = (char *)xmlNodeGetContent(a);
559         if(!strcmp((char *)a->name, "name"))
560           name = (char *)xmlNodeGetContent(a);
561         if(!strcmp((char *)a->name, "module"))
562           module = (char *)xmlNodeGetContent(a);
563       }
564       exists = (noit_poller_lookup_by_name(target, name) != NULL);
565       m = noit_module_lookup(module);
566       xmlFree(target);
567       xmlFree(name);
568       xmlFree(module);
569       if(exists) FAIL("target`name already registered");
570       if(!m) FAIL("module does not exist");
571       /* create a check here */
572       newcheck = xmlNewNode(NULL, (xmlChar *)"check");
573       xmlSetProp(newcheck, (xmlChar *)"uuid", (xmlChar *)pats[1]);
574       configure_xml_check(newcheck, attr, config);
575       parent = make_conf_path(pats[0]);
576       if(!parent) FAIL("invalid path");
577       xmlAddChild(parent, newcheck);
578     }
579   }
580   if(exists) {
581     int module_change;
582     char *target = NULL, *name = NULL, *module = NULL;
583     xmlNodePtr a;
584     noit_check_t *ocheck;
585
586     cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
587     if(cnt != 1) FAIL("internal error, |checkid| > 1");
588     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
589     uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
590     if(!uuid_conf || strcasecmp(uuid_conf, pats[1]))
591       FAIL("internal error uuid");
592
593     /* make sure this isn't a dup */
594     for(a = attr->children; a; a = a->next) {
595       if(!strcmp((char *)a->name, "target"))
596         target = (char *)xmlNodeGetContent(a);
597       if(!strcmp((char *)a->name, "name"))
598         name = (char *)xmlNodeGetContent(a);
599       if(!strcmp((char *)a->name, "module"))
600         module = (char *)xmlNodeGetContent(a);
601     }
602     ocheck = noit_poller_lookup_by_name(target, name);
603     module_change = strcmp(check->module, module);
604     xmlFree(target);
605     xmlFree(name);
606     xmlFree(module);
607     if(ocheck && ocheck != check) FAIL("new target`name would collide");
608     if(module_change) FAIL("cannot change module");
609     configure_xml_check(node, attr, config);
610     parent = make_conf_path(pats[0]);
611     if(!parent) FAIL("invalid path");
612     xmlUnlinkNode(node);
613     xmlAddChild(parent, node);
614   }
615
616   if(noit_conf_write_file(NULL) != 0)
617     noitL(noit_error, "local config write failed\n");
618   noit_conf_mark_changed();
619   noit_poller_reload(xpath);
620   if(restc->call_closure_free) restc->call_closure_free(restc->call_closure);
621   restc->call_closure_free = NULL;
622   restc->call_closure = NULL;
623   if(pobj) xmlXPathFreeObject(pobj);
624   if(doc) xmlFreeDoc(doc);
625   restc->fastpath = rest_show_check;
626   return restc->fastpath(restc, restc->nparams, restc->params);
627
628  error:
629   noit_http_response_standard(ctx, error_code, "ERROR", "text/html");
630   doc = xmlNewDoc((xmlChar *)"1.0");
631   root = xmlNewDocNode(doc, NULL, (xmlChar *)"error", NULL);
632   xmlDocSetRootElement(doc, root);
633   xmlNodeAddContent(root, (xmlChar *)error);
634   noit_http_response_xml(ctx, doc);
635   noit_http_response_end(ctx);
636   goto cleanup;
637
638  cleanup:
639   if(pobj) xmlXPathFreeObject(pobj);
640   if(doc) xmlFreeDoc(doc);
641   return 0;
642 }
643
644 static int
645 rest_show_config(noit_http_rest_closure_t *restc,
646                  int npats, char **pats) {
647   noit_http_session_ctx *ctx = restc->http_ctx;
648   xmlDocPtr doc = NULL;
649   xmlNodePtr node, root;
650   char xpath[1024];
651
652   snprintf(xpath, sizeof(xpath), "/noit%s", pats ? pats[0] : "");
653   node = noit_conf_get_section(NULL, xpath);
654
655   if(!node) {
656     noit_http_response_not_found(ctx, "text/xml");
657     noit_http_response_end(ctx);
658   }
659   else {
660     doc = xmlNewDoc((xmlChar *)"1.0");
661     root = xmlCopyNode(node, 1);
662     xmlDocSetRootElement(doc, root);
663     noit_http_response_ok(ctx, "text/xml");
664     noit_http_response_xml(ctx, doc);
665     noit_http_response_end(ctx);
666   }
667
668   if(doc) xmlFreeDoc(doc);
669
670   return 0;
671 }
672
673 void
674 noit_check_rest_init() {
675   assert(noit_http_rest_register_auth(
676     "GET", "/", "^config(/.*)?$",
677     rest_show_config, noit_http_rest_client_cert_auth
678   ) == 0);
679   assert(noit_http_rest_register_auth(
680     "GET", "/checks/", "^show(/.*)(?<=/)(" UUID_REGEX ")$",
681     rest_show_check, noit_http_rest_client_cert_auth
682   ) == 0);
683   assert(noit_http_rest_register_auth(
684     "PUT", "/checks/", "^set(/.*)(?<=/)(" UUID_REGEX ")$",
685     rest_set_check, noit_http_rest_client_cert_auth
686   ) == 0);
687   assert(noit_http_rest_register_auth(
688     "DELETE", "/checks/", "^delete(/.*)(?<=/)(" UUID_REGEX ")$",
689     rest_delete_check, noit_http_rest_client_cert_auth
690   ) == 0);
691 }
692
Note: See TracBrowser for help on using the browser.