root/src/noit_check_rest.c

Revision 6033f87e442f369d5fbf40e27d9a1755e0d6ecce, 22.1 kB (checked in by gdusbabek <gdusbabek@gmail.com>, 3 years ago)

remove uneeded resolve_rtype and cleanup

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