root/src/noit_conf_checks.c

Revision e88e9e38ef3322a56040d64badb9dd6c77934d47, 41.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 2 years ago)

various memory leaks related so xmlXPathCastNodeToString and xmlGetNodePath

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, 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
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <assert.h>
40 #include <libxml/parser.h>
41 #include <libxml/tree.h>
42 #include <libxml/xpath.h>
43
44 #include "noit_conf.h"
45 #include "noit_conf_private.h"
46 #include "noit_conf_checks.h"
47 #include "noit_check.h"
48 #include "noit_check_tools.h"
49 #include "noit_filters.h"
50 #include "noit_console.h"
51 #include "utils/noit_hash.h"
52 #include "utils/noit_log.h"
53
54 static void register_console_config_check_commands();
55
56 static struct _valid_attr_t {
57   const char *scope;
58   const char *name;
59   const char *xpath;
60   int checks_fixate;
61 } valid_attrs[] = {
62   { "/checks", "name", "@name", 0 },
63   { "/checks", "target", "@target", 0 },
64   { "/checks", "period", "@period", 0 },
65   { "/checks", "timeout", "@timeout", 0 },
66   { "/checks", "oncheck", "@oncheck", 0 },
67   { "/checks", "disable", "@disable", 0 },
68   { "/checks", "resolve_rtype", "@resolve_rtype", 0 },
69   { "/checks", "filterset", "@filterset", 0 },
70   { "/checks", "module", "@module", 1 },
71   { "/filtersets", "target", "@target", 0 },
72   { "/filtersets", "module", "@module", 0 },
73   { "/filtersets", "name", "@name", 0 },
74   { "/filtersets", "metric", "@metric", 0 },
75 };
76
77 void
78 noit_console_state_add_check_attrs(noit_console_state_t *state,
79                                    console_cmd_func_t f,
80                                    const char *scope) {
81   int i;
82   for(i = 0;
83       i < sizeof(valid_attrs)/sizeof(valid_attrs[0]);
84       i++) {
85     if(strcmp(valid_attrs[i].scope, scope)) continue;
86     noit_console_state_add_cmd(state,
87       NCSCMD(valid_attrs[i].name, f, NULL,
88              NULL, &valid_attrs[i]));
89   }
90 }
91 static noit_hash_table check_attrs = NOIT_HASH_EMPTY;
92
93 void noit_console_conf_checks_init() {
94   int i;
95   for(i=0;i<sizeof(valid_attrs)/sizeof(*valid_attrs);i++) {
96     noit_hash_store(&check_attrs,
97                     valid_attrs[i].name, strlen(valid_attrs[i].name),
98                     &valid_attrs[i]);
99   }
100   register_console_config_check_commands();
101 }
102
103 static int
104 noit_console_mkcheck_xpath(char *xpath, int len,
105                            noit_conf_t_userdata_t *info,
106                            const char *arg) {
107   int rv;
108   rv = noit_check_xpath(xpath, len, "/", arg);
109   if(rv == -1) return -1;
110   if(rv == 0) {
111     char *path = (!info || !strcmp(info->path, "/")) ? "" : info->path;
112     snprintf(xpath, len, "/noit%s%s%s[@uuid]",
113              path, arg ? "/" : "", arg ? arg : "");
114   }
115   return 0;
116 }
117 static void
118 nc_attr_show(noit_console_closure_t ncct, const char *name, xmlNodePtr cnode,
119              xmlNodePtr anode, const char *value) {
120   char *cpath, *apath;
121   cpath = cnode ? (char *)xmlGetNodePath(cnode) : strdup("");
122   apath = anode ? (char *)xmlGetNodePath(anode) : strdup("");
123   nc_printf(ncct, " %s: %s", name, value ? value : "[undef]");
124   if(value && cpath && apath) {
125     int clen = strlen(cpath);
126     int plen = strlen("/noit/checks/");
127     if(!strncmp(cpath, apath, clen) && apath[clen] == '/') {
128       /* we have a match, which means it isn't inherited */
129     }
130     else {
131       nc_printf(ncct, " [inherited from %s]",
132                 strlen(apath) > plen ? apath + plen : apath);
133     }
134   }
135   nc_write(ncct, "\n", 1);
136   if(cpath) free(cpath);
137   if(apath) free(apath);
138 }
139 static void
140 refresh_subchecks(noit_console_closure_t ncct,
141                   noit_conf_t_userdata_t *info) {
142   char *path;
143   char xpath[1024];
144  
145   path = info->path;
146   if(!strcmp(path, "/")) path = "";
147
148   /* The first one is just a process_checks, the second is the reload.
149    * Reload does a lot of work and there is no need to do it twice.
150    */
151   snprintf(xpath, sizeof(xpath), "/noit/%s[@uuid]", path);
152   noit_poller_process_checks(xpath);
153   snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
154   noit_poller_reload(xpath);
155 }
156 static int
157 noit_config_check_update_attrs(xmlNodePtr node, int argc, char **argv) {
158   int i, error = 0;
159   if(argc % 2) return -1;
160
161   for(i=0; i<argc; i+=2) {
162     void *vattrinfo;
163     struct _valid_attr_t *attrinfo;
164     char *attr = argv[i], *val = NULL;
165     if(!strcasecmp(argv[i], "no")) attr = argv[i+1];
166     else val = argv[i+1];
167     if(!noit_hash_retrieve(&check_attrs, attr, strlen(attr),
168                            &vattrinfo)) {
169       error = 1;
170       break;
171     }
172     attrinfo = vattrinfo;
173     /* The fixation stuff doesn't matter here, this check is brand-new */
174     xmlUnsetProp(node, (xmlChar *)attrinfo->name);
175     if(val)
176       xmlSetProp(node, (xmlChar *)attrinfo->name, (xmlChar *)val);
177     CONF_DIRTY(node);
178     noit_conf_mark_changed();
179   }
180   return error;
181 }
182
183 static int
184 noit_conf_mkcheck_under(const char *ppath, int argc, char **argv, uuid_t out) {
185   int rv = -1;
186   const char *path;
187   char xpath[1024];
188   xmlXPathContextPtr xpath_ctxt = NULL;
189   xmlXPathObjectPtr pobj = NULL;
190   xmlNodePtr node = NULL, newnode;
191
192   /* attr val [or] no attr (sets of two) */
193   if(argc % 2) goto out;
194
195   noit_conf_xml_xpath(NULL, &xpath_ctxt);
196   path = strcmp(ppath, "/") ? ppath : "";
197   snprintf(xpath, sizeof(xpath), "/noit%s", path);
198   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
199   if(!pobj || pobj->type != XPATH_NODESET ||
200      xmlXPathNodeSetGetLength(pobj->nodesetval) != 1) {
201     goto out;
202   }
203   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
204   if((newnode = xmlNewChild(node, NULL, (xmlChar *)"check", NULL)) != NULL) {
205     char outstr[37];
206     uuid_generate(out);
207     uuid_unparse_lower(out, outstr);
208     xmlSetProp(newnode, (xmlChar *)"uuid", (xmlChar *)outstr);
209     xmlSetProp(newnode, (xmlChar *)"disable", (xmlChar *)"true");
210
211     /* No risk of running off the end (we checked this above) */
212     if(noit_config_check_update_attrs(newnode, argc, argv)) {
213       /* Something went wrong, remove the node */
214       xmlUnlinkNode(newnode);
215     }
216     else {
217       CONF_DIRTY(newnode);
218       noit_conf_mark_changed();
219       rv = 0;
220     }
221   }
222  out:
223   if(pobj) xmlXPathFreeObject(pobj);
224   return rv;
225 }
226
227 static int
228 noit_console_check(noit_console_closure_t ncct,
229                    int argc, char **argv,
230                    noit_console_state_t *state, void *closure) {
231   int cnt;
232   noit_conf_t_userdata_t *info;
233   char xpath[1024], newuuid_str[37];
234   char *uuid_conf = NULL, *wanted;
235   uuid_t checkid;
236   xmlXPathContextPtr xpath_ctxt = NULL;
237   xmlXPathObjectPtr pobj = NULL;
238   xmlNodePtr node = NULL;
239   noit_boolean creating_new = noit_false;
240
241   if(closure) {
242     char *fake_argv[1] = { ".." };
243     noit_console_state_pop(ncct, 0, argv, NULL, NULL);
244     noit_console_config_cd(ncct, 1, fake_argv, NULL, NULL);
245   }
246
247   noit_conf_xml_xpath(NULL, &xpath_ctxt);
248   if(argc < 1) {
249     nc_printf(ncct, "requires at least one argument\n");
250     return -1;
251   }
252   if(argc % 2 == 0) {
253     nc_printf(ncct, "wrong number of arguments\n");
254     return -1;
255   }
256
257   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
258   wanted = strcmp(argv[0], "new") ? argv[0] : NULL;
259   if(info && !wanted) {
260     /* We are creating a new node */
261     uuid_t out;
262     creating_new = noit_true;
263     if(strncmp(info->path, "/checks/", strlen("/checks/")) &&
264        strcmp(info->path, "/checks")) {
265       nc_printf(ncct, "New checks must be under /checks/\n");
266       return -1;
267     }
268     if(noit_conf_mkcheck_under(info->path, argc - 1, argv + 1, out)) {
269       nc_printf(ncct, "Error creating new check\n");
270       return -1;
271     }
272     newuuid_str[0] = '\0';
273     uuid_unparse_lower(out, newuuid_str);
274     wanted = newuuid_str;
275   }
276   /* We many not be in conf-t mode -- that's fine */
277   if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, wanted)) {
278     nc_printf(ncct, "could not find check '%s'\n", wanted);
279     return -1;
280   }
281
282   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
283   if(!pobj || pobj->type != XPATH_NODESET ||
284      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
285     nc_printf(ncct, "no checks found for '%s'\n", wanted);
286     goto out;
287   }
288   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
289   if(info && cnt != 1) {
290     nc_printf(ncct, "Ambiguous check specified\n");
291     goto out;
292   }
293   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
294   uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
295   if(!uuid_conf || uuid_parse(uuid_conf, checkid)) {
296     nc_printf(ncct, "%s has invalid or missing UUID!\n",
297               (char *)xmlGetNodePath(node) + strlen("/noit"));
298     goto out;
299   }
300   if(argc > 1 && !creating_new)
301     if(noit_config_check_update_attrs(node, argc - 1, argv + 1))
302       nc_printf(ncct, "Partially successful, error setting some attributes\n");
303
304   if(info) {
305     char *xmlpath;
306     if(info->path) free(info->path);
307     xmlpath = (char *)xmlGetNodePath(node);
308     info->path = strdup(xmlpath + strlen("/noit"));
309     free(xmlpath);
310     uuid_copy(info->current_check, checkid);
311     if(argc > 1) refresh_subchecks(ncct, info);
312     if(state) {
313       noit_console_state_push_state(ncct, state);
314       noit_console_state_init(ncct);
315     }
316     goto out;
317   }
318  out:
319   if(uuid_conf) free(uuid_conf);
320   if(pobj) xmlXPathFreeObject(pobj);
321   return 0;
322 }
323 static int
324 noit_console_watch_check(noit_console_closure_t ncct,
325                          int argc, char **argv,
326                          noit_console_state_t *state, void *closure) {
327   int i, cnt;
328   int adding = (int)(vpsized_int)closure;
329   int period = 0;
330   char xpath[1024];
331   xmlXPathObjectPtr pobj = NULL;
332   xmlXPathContextPtr xpath_ctxt = NULL;
333
334   noit_conf_xml_xpath(NULL, &xpath_ctxt);
335   if(argc < 1 || argc > 2) {
336     nc_printf(ncct, "requires one or two arguments\n");
337     return -1;
338   }
339   /* An alternate period */
340   if(argc == 2) period = atoi(argv[1]);
341
342   if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), NULL,
343                                 argc ? argv[0] : NULL)) {
344     nc_printf(ncct, "ERROR: could not find check '%s'\n", argv[0]);
345     return -1;
346   }
347
348   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
349   if(!pobj || pobj->type != XPATH_NODESET ||
350      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
351     nc_printf(ncct, "no checks found\n");
352     goto out;
353   }
354   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
355   for(i=0; i<cnt; i++) {
356     uuid_t checkid;
357     noit_check_t *check;
358     xmlNodePtr node;
359     char *uuid_conf;
360
361     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
362     uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
363     if(!uuid_conf || uuid_parse(uuid_conf, checkid)) {
364       nc_printf(ncct, "%s has invalid or missing UUID!\n",
365                 (char *)xmlGetNodePath(node) + strlen("/noit"));
366       continue;
367     }
368     if(period == 0) {
369       check = noit_poller_lookup(checkid);
370       if(!check) continue;
371       if(adding) noit_check_transient_add_feed(check, ncct->feed_path);
372       else noit_check_transient_remove_feed(check, ncct->feed_path);
373     }
374     else {
375       if(adding) {
376         check = noit_check_watch(checkid, period);
377         /* This check must be watched from the console */
378         noit_check_transient_add_feed(check, ncct->feed_path);
379         /* Note the check */
380         noit_check_log_check(check);
381         /* kick it off, if it isn't running already */
382         if(!NOIT_CHECK_LIVE(check)) noit_check_activate(check);
383       }
384       else {
385         check = noit_check_get_watch(checkid, period);
386         if(check) noit_check_transient_remove_feed(check, ncct->feed_path);
387       }
388     }
389   }
390  out:
391   if(pobj) xmlXPathFreeObject(pobj);
392   return 0;
393 }
394 static int
395 _qsort_string_compare(const void *i1, const void *i2) {
396         const char *s1 = ((const char **)i1)[0];
397         const char *s2 = ((const char **)i2)[0];
398         return strcasecmp(s1, s2);
399 }
400 static int
401 noit_console_show_check(noit_console_closure_t ncct,
402                         int argc, char **argv,
403                         noit_console_state_t *state, void *closure) {
404   int i, cnt;
405   noit_conf_t_userdata_t *info;
406   char xpath[1024];
407   xmlXPathObjectPtr pobj = NULL;
408   xmlXPathContextPtr xpath_ctxt = NULL;
409
410   noit_conf_xml_xpath(NULL, &xpath_ctxt);
411   if(argc > 1) {
412     nc_printf(ncct, "requires zero or one arguments\n");
413     return -1;
414   }
415
416   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
417   /* We many not be in conf-t mode -- that's fine */
418   if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info,
419                                 argc ? argv[0] : NULL)) {
420     nc_printf(ncct, "could not find check '%s'\n", argv[0]);
421     return -1;
422   }
423
424   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
425   if(!pobj || pobj->type != XPATH_NODESET ||
426      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
427     nc_printf(ncct, "no checks found\n");
428     goto out;
429   }
430   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
431   if(info && cnt != 1) {
432     nc_printf(ncct, "Ambiguous check specified\n");
433     goto out;
434   }
435   for(i=0; i<cnt; i++) {
436     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
437     const char *k;
438     int klen;
439     void *data;
440     uuid_t checkid;
441     noit_check_t *check;
442     noit_hash_table *config;
443     xmlNodePtr node, anode, mnode = NULL;
444     char *uuid_conf;
445     char *module, *value;
446
447     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
448     uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
449     if(!uuid_conf || uuid_parse(uuid_conf, checkid)) {
450       nc_printf(ncct, "%s has invalid or missing UUID!\n",
451                 (char *)xmlGetNodePath(node) + strlen("/noit"));
452       continue;
453     }
454     nc_printf(ncct, "==== %s ====\n", uuid_conf);
455     free(uuid_conf);
456
457 #define MYATTR(a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b))
458 #define INHERIT(a,n,b) \
459   _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b))
460 #define SHOW_ATTR(a) do { \
461   anode = NULL; \
462   value = NULL; \
463   INHERIT(a, anode, value); \
464   nc_attr_show(ncct, #a, node, anode, value); \
465   if(value != NULL) free(value); \
466 } while(0)
467
468     if(!INHERIT(module, mnode, module)) module = NULL;
469     if(MYATTR(name, anode, value)) {
470       nc_printf(ncct, " name: %s\n", value);
471       free(value);
472     }
473     else
474       nc_printf(ncct, " name: %s [from module]\n", module ? module : "[undef]");
475     nc_attr_show(ncct, "module", node, mnode, module);
476     if(module) free(module);
477     SHOW_ATTR(target);
478     SHOW_ATTR(resolve_rtype);
479     SHOW_ATTR(period);
480     SHOW_ATTR(timeout);
481     SHOW_ATTR(oncheck);
482     SHOW_ATTR(filterset);
483     SHOW_ATTR(disable);
484     /* Print out all the config settings */
485     config = noit_conf_get_hash(node, "config");
486     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
487       nc_printf(ncct, " config::%s: %s\n", k, (const char *)data);
488     }
489     noit_hash_destroy(config, free, free);
490     free(config);
491
492     check = noit_poller_lookup(checkid);
493     if(!check) {
494       nc_printf(ncct, " ERROR: not in running system\n");
495     }
496     else {
497       int idx = 0;
498       nc_printf(ncct, " target_ip: %s\n", check->target_ip);
499       nc_printf(ncct, " currently: ");
500       if(NOIT_CHECK_RUNNING(check)) nc_printf(ncct, "%srunning", idx++?",":"");
501       if(NOIT_CHECK_KILLED(check)) nc_printf(ncct, "%skilled", idx++?",":"");
502       if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":"");
503       if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":"");
504       if(!idx) nc_printf(ncct, "idle");
505       nc_write(ncct, "\n", 1);
506       if (check->fire_event != NULL) {
507         struct timeval now, diff;
508         gettimeofday(&now, NULL);
509         sub_timeval(check->fire_event->whence, now, &diff);
510         nc_printf(ncct, " next run: %0.3f seconds\n",
511                 diff.tv_sec + (diff.tv_usec / 1000000.0));
512       }
513       else {
514         nc_printf(ncct, " next run: unscheduled\n");
515       }
516
517       if(check->stats.current.whence.tv_sec == 0) {
518         nc_printf(ncct, " last run: never\n");
519       }
520       else {
521         stats_t *c = &check->stats.current;
522         int mcount=0;
523         const char **sorted_keys;
524         char buff[256];
525         struct timeval now, diff;
526         noit_boolean filtered;
527
528         gettimeofday(&now, NULL);
529         sub_timeval(now, c->whence, &diff);
530         nc_printf(ncct, " last run: %0.3f seconds ago\n",
531                   diff.tv_sec + (diff.tv_usec / 1000000.0));
532         nc_printf(ncct, " availability/state: %s/%s\n",
533                   noit_check_available_string(c->available),
534                   noit_check_state_string(c->state));
535         nc_printf(ncct, " status: %s\n", c->status ? c->status : "[[null]]");
536         nc_printf(ncct, " metrics:\n");
537         memset(&iter, 0, sizeof(iter));
538         sorted_keys = alloca(c->metrics.size * sizeof(*sorted_keys));
539         while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) {
540           if(sorted_keys) sorted_keys[mcount++] = k;
541           else {
542             noit_stats_snprint_metric(buff, sizeof(buff), (metric_t *)data);
543             filtered = !noit_apply_filterset(check->filterset, check, (metric_t *)data);
544             nc_printf(ncct, "  %c%s\n", filtered ? '*' : ' ', buff);
545           }
546         }
547         if(sorted_keys) {
548           int j;
549           qsort(sorted_keys, mcount, sizeof(*sorted_keys),
550                 _qsort_string_compare);
551           for(j=0;j<mcount;j++) {
552             if(noit_hash_retrieve(&c->metrics,
553                                   sorted_keys[j], strlen(sorted_keys[j]),
554                                   &data)) {
555               noit_stats_snprint_metric(buff, sizeof(buff), (metric_t *)data);
556               filtered = !noit_apply_filterset(check->filterset, check, (metric_t *)data);
557               nc_printf(ncct, "  %c%s\n", filtered ? '*' : ' ', buff);
558             }
559           }
560         }
561       }
562     }
563   }
564  out:
565   if(pobj) xmlXPathFreeObject(pobj);
566   return 0;
567 }
568 static int
569 noit_console_config_nocheck(noit_console_closure_t ncct,
570                             int argc, char **argv,
571                             noit_console_state_t *state, void *closure) {
572   int i, cnt;
573   const char *err = "internal error";
574   noit_conf_t_userdata_t *info;
575   xmlXPathObjectPtr pobj = NULL;
576   xmlXPathContextPtr xpath_ctxt = NULL;
577   char xpath[1024];
578   uuid_t checkid;
579
580   noit_conf_xml_xpath(NULL, &xpath_ctxt);
581   if(argc < 1) {
582     nc_printf(ncct, "requires one argument\n");
583     return -1;
584   }
585
586   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
587   if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argv[0])) {
588     nc_printf(ncct, "could not find check '%s'\n", argv[0]);
589     return -1;
590   }
591   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
592   if(!pobj || pobj->type != XPATH_NODESET ||
593      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
594     err = "no checks found";
595     goto bad;
596   }
597   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
598   for(i=0; i<cnt; i++) {
599     xmlNodePtr node;
600     char *uuid_conf;
601     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
602     uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid");
603     if(!uuid_conf || uuid_parse(uuid_conf, checkid)) {
604       nc_printf(ncct, "%s has invalid or missing UUID!\n",
605                 (char *)xmlGetNodePath(node) + strlen("/noit"));
606     }
607     else {
608       if(argc > 1) {
609         int j;
610         for(j=1;j<argc;j++)
611           xmlUnsetProp(node, (xmlChar *)argv[j]);
612         CONF_DIRTY(node);
613       } else {
614         nc_printf(ncct, "descheduling %s\n", uuid_conf);
615         noit_poller_deschedule(checkid);
616         CONF_REMOVE(node);
617         xmlUnlinkNode(node);
618       }
619       noit_conf_mark_changed();
620     }
621   }
622   if(argc > 1) {
623     noit_poller_process_checks(xpath);
624     noit_poller_reload(xpath);
625   }
626   nc_printf(ncct, "rebuilding causal map...\n");
627   noit_poller_make_causal_map();
628   if(pobj) xmlXPathFreeObject(pobj);
629   return 0;
630  bad:
631   if(pobj) xmlXPathFreeObject(pobj);
632   nc_printf(ncct, "%s\n", err);
633   return -1;
634 }
635 static int
636 noit_console_config_show(noit_console_closure_t ncct,
637                          int argc, char **argv,
638                          noit_console_state_t *state, void *closure) {
639   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
640   const char *k;
641   int klen;
642   void *data;
643   int i, cnt, titled = 0, cliplen = 0;
644   const char *path = "", *basepath = NULL;
645   char xpath[1024];
646   noit_conf_t_userdata_t *info = NULL;
647   noit_hash_table *config;
648   xmlXPathObjectPtr pobj = NULL;
649   xmlXPathContextPtr xpath_ctxt = NULL, current_ctxt;
650   xmlDocPtr master_config = NULL;
651   xmlNodePtr node = NULL;
652
653   noit_conf_xml_xpath(&master_config, &xpath_ctxt);
654   if(argc > 1) {
655     nc_printf(ncct, "too many arguments\n");
656     return -1;
657   }
658
659   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
660   if(info && info->path) path = basepath = info->path;
661   if(!info && argc == 0) {
662     nc_printf(ncct, "argument required when not in configuration mode\n");
663     return -1;
664   }
665
666   if(argc == 1) path = argv[0];
667   if(!basepath) basepath = path;
668
669   /* { / } is a special case */
670   if(!strcmp(basepath, "/")) basepath = "";
671   if(!strcmp(path, "/")) path = "";
672
673   if(!master_config) {
674     nc_printf(ncct, "no config\n");
675     return -1;
676   }
677
678   /* { / } is the only path that will end with a /
679    * in XPath { / / * } means something _entirely different than { / * }
680    * Ever notice how it is hard to describe xpath in C comments?
681    */
682   /* We don't want to show the root node */
683   cliplen = strlen("/noit/");
684
685   /* If we are in configuration mode
686    * and we are without an argument or the argument is absolute,
687    * clip the current path off */
688   if(info && (argc == 0 || path[0] != '/')) cliplen += strlen(basepath);
689   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
690     snprintf(xpath, sizeof(xpath), "/noit%s/@*", path);
691   else
692     snprintf(xpath, sizeof(xpath), "/noit%s/%s/@*", basepath, path);
693
694   current_ctxt = xpath_ctxt;
695   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
696   if(!pobj || pobj->type != XPATH_NODESET) {
697     nc_printf(ncct, "no such object\n");
698     goto bad;
699   }
700   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
701   titled = 0;
702   for(i=0; i<cnt; i++) {
703     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
704     if(!strcmp((char *)node->name, "check")) continue;
705     if(node->children && node->children == xmlGetLastChild(node) &&
706       xmlNodeIsText(node->children)) {
707       char *node_str, *xmlpath;
708       node_str = (char *)xmlXPathCastNodeToString(node->children);
709       xmlpath = (char *)xmlGetNodePath(node);
710       if(!titled++) nc_printf(ncct, "== Section Settings ==\n");
711       nc_printf(ncct, "%s: %s\n", xmlpath + cliplen, node_str);
712       free(xmlpath);
713       free(node_str);
714     }
715   }
716   xmlXPathFreeObject(pobj);
717
718   /* Print out all the config settings */
719   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
720     snprintf(xpath, sizeof(xpath), "/noit%s", path);
721   else
722     snprintf(xpath, sizeof(xpath), "/noit%s/%s", basepath, path);
723   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
724   if(!pobj || pobj->type != XPATH_NODESET) {
725     nc_printf(ncct, "no such object\n");
726     goto bad;
727   }
728   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
729   if(cnt > 0) {
730     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
731     titled = 0;
732     config = noit_conf_get_hash(node, "config");
733     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
734       if(!titled++) nc_printf(ncct, "== Section [Aggregated] Config ==\n");
735       nc_printf(ncct, "config::%s: %s\n", k, (const char *)data);
736     }
737     noit_hash_destroy(config, free, free);
738     free(config);
739   }
740   xmlXPathFreeObject(pobj);
741
742   /* _shorten string_ turning last { / @ * } to { / * } */
743   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
744     snprintf(xpath, sizeof(xpath), "/noit%s/*", path);
745   else
746     snprintf(xpath, sizeof(xpath), "/noit%s/%s/*", basepath, path);
747   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
748   if(!pobj || pobj->type != XPATH_NODESET) {
749     nc_printf(ncct, "no such object\n");
750     goto bad;
751   }
752   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
753   titled = 0;
754   for(i=0; i<cnt; i++) {
755     char *xmlpath;
756     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
757     if(!strcmp((char *)node->name, "check")) continue;
758     if(!strcmp((char *)node->name, "filterset")) continue;
759     xmlpath = (char *)xmlGetNodePath(node);
760     if(strcmp(xmlpath + cliplen, "config")) {
761       if(!(node->children && node->children == xmlGetLastChild(node) &&
762            xmlNodeIsText(node->children))) {
763         if(!titled++) nc_printf(ncct, "== Subsections ==\n");
764         nc_printf(ncct, "%s\n", xmlpath + cliplen);
765       }
766     }
767     free(xmlpath);
768   }
769
770   titled = 0;
771   for(i=0; i<cnt; i++) {
772     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
773     if(!strcmp((char *)node->name, "filterset")) {
774       xmlAttr *attr;
775       char *filter_name = NULL;
776       for(attr=node->properties; attr; attr = attr->next) {
777         if(!strcmp((char *)attr->name, "name"))
778           filter_name = (char *)xmlXPathCastNodeToString(attr->children);
779       }
780       if(filter_name) {
781         nc_printf(ncct, "filterset[@name=\"%s\"]\n", filter_name);
782         xmlFree(filter_name);
783       }
784       else nc_printf(ncct, "fitlerset\n");
785     }
786     else if(!strcmp((char *)node->name, "check")) {
787       int busted = 1;
788       xmlAttr *attr;
789       char *uuid_str = NULL;
790       uuid_t checkid;
791
792       if(!titled++) nc_printf(ncct, "== Checks ==\n");
793
794       for(attr=node->properties; attr; attr = attr->next) {
795         if(!strcmp((char *)attr->name, "uuid")) {
796           uuid_str = (char *)xmlXPathCastNodeToString(attr->children);
797           break;
798         }
799       }
800       nc_printf(ncct, "check[@uuid=\"%s\"] ", uuid_str ? uuid_str : "undefined");
801       if(uuid_str && uuid_parse(uuid_str, checkid) == 0) {
802         noit_check_t *check;
803         check = noit_poller_lookup(checkid);
804         if(check) {
805           busted = 0;
806           nc_printf(ncct, "%s`%s`%s", check->target, check->module, check->name);
807         }
808       }
809       if(uuid_str) free(uuid_str);
810       if(busted) nc_printf(ncct, "[check not in running system]");
811       nc_write(ncct, "\n", 1);
812     }
813   }
814   xmlXPathFreeObject(pobj);
815   return 0;
816  bad:
817   if(pobj) xmlXPathFreeObject(pobj);
818   return -1;
819 }
820
821 static char *
822 conf_t_check_prompt(EditLine *el) {
823   noit_console_closure_t ncct;
824   noit_conf_t_userdata_t *info;
825   noit_check_t *check;
826   static char *tl = "noit(conf)# ";
827   static char *pfmt = "noit(conf:%s%s%s)# ";
828
829   el_get(el, EL_USERDATA, (void *)&ncct);
830   if(!ncct) return tl;
831   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
832   if(!info) return tl;
833
834   check = noit_poller_lookup(info->current_check);
835   if(check &&
836      check->target && check->target[0] &&
837      check->name && check->name[0])
838     snprintf(info->prompt, sizeof(info->prompt),
839              pfmt, check->target, "`", check->name);
840   else {
841     char uuid_str[37];
842     uuid_unparse_lower(info->current_check, uuid_str);
843     snprintf(info->prompt, sizeof(info->prompt), pfmt, "[", uuid_str, "]");
844   }
845   return info->prompt;
846 }
847 static int
848 noit_conf_checks_reload(noit_console_closure_t ncct,
849                         int argc, char **argv,
850                         noit_console_state_t *state, void *closure) {
851   if(noit_conf_reload(ncct, argc, argv, state, closure)) return -1;
852   noit_poller_reload(NULL);
853   return 0;
854 }
855
856 static int
857 validate_attr_set_scope(noit_conf_t_userdata_t *info,
858                         struct _valid_attr_t *attrinfo) {
859   int len;
860   len = strlen(attrinfo->scope);
861   if(strncmp(info->path, attrinfo->scope, len) ||
862      (info->path[len] != '\0' && info->path[len] != '/')) {
863     return -1;
864   }
865   return 0;
866 }
867 static int
868 replace_config(noit_console_closure_t ncct,
869                noit_conf_t_userdata_t *info, const char *name,
870                const char *value) {
871   int i, cnt, rv = -1, active = 0;
872   xmlXPathObjectPtr pobj = NULL;
873   xmlXPathContextPtr xpath_ctxt = NULL;
874   xmlNodePtr node, confignode;
875   char xpath[1024], *path;
876
877   path = info->path;
878   if(!strcmp(path, "/")) path = "";
879
880   noit_conf_xml_xpath(NULL, &xpath_ctxt);
881
882   /* Only if checks will fixate this attribute shall we check for
883    * child <check> nodes.
884    * NOTE: this return nothing and "seems" okay if we are _in_
885    *       a <check> node.  That case is handled below.
886    */
887   snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
888   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
889   if(!pobj || pobj->type != XPATH_NODESET) goto out;
890   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
891   for(i=0; i<cnt; i++) {
892     uuid_t checkid;
893     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
894     if(noit_conf_get_uuid(node, "@uuid", checkid)) {
895       noit_check_t *check;
896       check = noit_poller_lookup(checkid);
897       if(check && NOIT_CHECK_LIVE(check)) active++;
898     }
899   }
900   if(pobj) xmlXPathFreeObject(pobj);
901
902 #ifdef UNSAFE_RECONFIG
903   snprintf(xpath, sizeof(xpath), "/noit/%s", path);
904   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
905   if(!pobj || pobj->type != XPATH_NODESET) goto out;
906   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
907   if(cnt != 1) {
908     nc_printf(ncct, "Internal error: context node disappeared\n");
909     goto out;
910   }
911   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
912   if(strcmp((const char *)node->name, "check")) {
913     uuid_t checkid;
914     /* Detect if  we are actually a <check> node and attempting to
915      * change something we shouldn't.
916      * This is the counterpart noted above.
917      */
918     if(noit_conf_get_uuid(node, "@uuid", checkid)) {
919       noit_check_t *check;
920       check = noit_poller_lookup(checkid);
921       if(NOIT_CHECK_LIVE(check)) active++;
922     }
923   }
924   if(active) {
925     nc_printf(ncct, "Cannot set '%s', it would effect %d live check(s)\n",
926               name, active);
927     goto out;
928   }
929   if(pobj) xmlXPathFreeObject(pobj);
930 #endif
931
932   /* Here we want to remove /noit/path/config/name */
933   snprintf(xpath, sizeof(xpath), "/noit/%s/config/%s", path, name);
934   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
935   if(!pobj || pobj->type != XPATH_NODESET) goto out;
936   if(xmlXPathNodeSetGetLength(pobj->nodesetval) > 0) {
937     xmlNodePtr toremove;
938     toremove = xmlXPathNodeSetItem(pobj->nodesetval, 0);
939     CONF_REMOVE(toremove);
940     xmlUnlinkNode(toremove);
941   }
942   /* TODO: if there are no more children of config, remove config? */
943   if(value) {
944     if(pobj) xmlXPathFreeObject(pobj);
945     /* He we create config if needed and place a child node under it */
946     snprintf(xpath, sizeof(xpath), "/noit/%s/config", path);
947     pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
948     if(!pobj || pobj->type != XPATH_NODESET) goto out;
949     if(xmlXPathNodeSetGetLength(pobj->nodesetval) == 0) {
950       if(pobj) xmlXPathFreeObject(pobj);
951       snprintf(xpath, sizeof(xpath), "/noit/%s", path);
952       pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
953       if(!pobj || pobj->type != XPATH_NODESET) goto out;
954       if(xmlXPathNodeSetGetLength(pobj->nodesetval) != 1) {
955         nc_printf(ncct, "Node disappeared from under you!\n");
956         goto out;
957       }
958       confignode = xmlNewChild(xmlXPathNodeSetItem(pobj->nodesetval, 0),
959                                NULL, (xmlChar *)"config", NULL);
960       if(confignode == NULL) {
961         nc_printf(ncct, "Error creating config child node.\n");
962         goto out;
963       }
964     }
965     else confignode = xmlXPathNodeSetItem(pobj->nodesetval, 0);
966
967     assert(confignode);
968     /* Now we create a child */
969     xmlNewChild(confignode, NULL, (xmlChar *)name, (xmlChar *)value);
970     CONF_DIRTY(confignode);
971   }
972   noit_conf_mark_changed();
973   rv = 0;
974  out:
975   if(pobj) xmlXPathFreeObject(pobj);
976   return rv;
977 }
978 static int
979 replace_attr(noit_console_closure_t ncct,
980              noit_conf_t_userdata_t *info, struct _valid_attr_t *attrinfo,
981              const char *value) {
982   int i, cnt, rv = -1, active = 0;
983   xmlXPathObjectPtr pobj = NULL;
984   xmlXPathContextPtr xpath_ctxt = NULL;
985   xmlNodePtr node;
986   char xpath[1024], *path;
987
988   path = info->path;
989   if(!strcmp(path, "/")) path = "";
990
991   noit_conf_xml_xpath(NULL, &xpath_ctxt);
992   if(attrinfo->checks_fixate) {
993     /* Only if checks will fixate this attribute shall we check for
994      * child <check> nodes.
995      * NOTE: this return nothing and "seems" okay if we are _in_
996      *       a <check> node.  That case is handled below.
997      */
998     snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
999     pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
1000     if(!pobj || pobj->type != XPATH_NODESET) goto out;
1001     cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
1002     for(i=0; i<cnt; i++) {
1003       uuid_t checkid;
1004       node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
1005       if(noit_conf_get_uuid(node, "@uuid", checkid)) {
1006         noit_check_t *check;
1007         check = noit_poller_lookup(checkid);
1008         if(check && NOIT_CHECK_LIVE(check)) active++;
1009       }
1010     }
1011     if(pobj) xmlXPathFreeObject(pobj);
1012   }
1013   snprintf(xpath, sizeof(xpath), "/noit/%s", path);
1014   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
1015   if(!pobj || pobj->type != XPATH_NODESET) goto out;
1016   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
1017   if(cnt != 1) {
1018     nc_printf(ncct, "Internal error: context node disappeared\n");
1019     goto out;
1020   }
1021   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
1022   if(attrinfo->checks_fixate &&
1023      !strcmp((const char *)node->name, "check")) {
1024     uuid_t checkid;
1025     /* Detect if  we are actually a <check> node and attempting to
1026      * change something we shouldn't.
1027      * This is the counterpart noted above.
1028      */
1029     if(noit_conf_get_uuid(node, "@uuid", checkid)) {
1030       noit_check_t *check;
1031       check = noit_poller_lookup(checkid);
1032       if(check && NOIT_CHECK_LIVE(check)) active++;
1033     }
1034   }
1035   if(active) {
1036     nc_printf(ncct, "Cannot set '%s', it would effect %d live check(s)\n",
1037               attrinfo->name, active);
1038     goto out;
1039   }
1040   xmlUnsetProp(node, (xmlChar *)attrinfo->name);
1041   if(value)
1042     xmlSetProp(node, (xmlChar *)attrinfo->name, (xmlChar *)value);
1043   CONF_DIRTY(node);
1044   noit_conf_mark_changed();
1045   rv = 0;
1046  out:
1047   if(pobj) xmlXPathFreeObject(pobj);
1048   return rv;
1049 }
1050 int
1051 noit_conf_check_set_attr(noit_console_closure_t ncct,
1052                          int argc, char **argv,
1053                          noit_console_state_t *state, void *closure) {
1054   struct _valid_attr_t *attrinfo = closure;
1055   noit_conf_t_userdata_t *info;
1056
1057   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
1058   if(!info || validate_attr_set_scope(info, attrinfo)) {
1059     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
1060               attrinfo->name, attrinfo->scope);
1061     return -1;
1062   }
1063
1064   if(argc != 1) {
1065     nc_printf(ncct, "set requires exactly one value\n");
1066     return -1;
1067   }
1068   /* Okay, we have an attribute and it should be set/replaced on the
1069    * current path.
1070    */
1071   if(replace_attr(ncct, info, attrinfo, argv[0])) {
1072     return -1;
1073   }
1074
1075   /* So, we updated an attribute, so we need to reload all checks
1076    * that are descendent-or-self of this node.
1077    */
1078   if(!strncmp(info->path, "/checks", strlen("/checks")))
1079     refresh_subchecks(ncct, info);
1080   if(!strncmp(info->path, "/filtersets", strlen("/filtersets")))
1081     noit_refresh_filtersets(ncct, info);
1082   return 0;
1083 }
1084
1085 int
1086 noit_conf_check_unset_attr(noit_console_closure_t ncct,
1087                            int argc, char **argv,
1088                            noit_console_state_t *state, void *closure) {
1089   struct _valid_attr_t *attrinfo = closure;
1090   noit_conf_t_userdata_t *info;
1091
1092   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
1093   if(!info || validate_attr_set_scope(info, attrinfo)) {
1094     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
1095               attrinfo->name, attrinfo->scope);
1096     return -1;
1097   }
1098
1099   if(argc != 0) {
1100     nc_printf(ncct, "no arguments allowed to this command.\n");
1101     return -1;
1102   }
1103   /* Okay, we have an attribute and it should be set/replaced on the
1104    * current path.
1105    */
1106   if(replace_attr(ncct, info, attrinfo, NULL)) {
1107     return -1;
1108   }
1109
1110   /* So, we updated an attribute, so we need to reload all checks
1111    * that are descendent-or-self of this node.
1112    */
1113   if(!strncmp(info->path, "/checks", strlen("/checks")))
1114     refresh_subchecks(ncct, info);
1115   if(!strncmp(info->path, "/filterset", strlen("/filterest")))
1116     noit_refresh_filtersets(ncct, info);
1117   return 0;
1118 }
1119
1120 int
1121 noit_console_config_setconfig(noit_console_closure_t ncct,
1122                                 int argc, char **argv,
1123                                 noit_console_state_t *state, void *closure) {
1124   noit_conf_t_userdata_t *info;
1125
1126   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
1127
1128   if(argc != 2) {
1129     nc_printf(ncct, "two arguments required.\n");
1130     return -1;
1131   }
1132   /* Okay, we have an child name and it should be culled from
1133    * current path/config.
1134    */
1135   if(replace_config(ncct, info, argv[0], argv[1])) {
1136     return -1;
1137   }
1138
1139   /* So, we updated an attribute, so we need to reload all checks
1140    * that are descendent-or-self of this node.
1141    */
1142   refresh_subchecks(ncct, info);
1143   return 0;
1144 }
1145
1146 int
1147 noit_console_config_unsetconfig(noit_console_closure_t ncct,
1148                                 int argc, char **argv,
1149                                 noit_console_state_t *state, void *closure) {
1150   noit_conf_t_userdata_t *info;
1151
1152   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
1153
1154   if(argc != 1) {
1155     nc_printf(ncct, "one argument required.\n");
1156     return -1;
1157   }
1158   /* Okay, we have an child name and it should be culled from
1159    * current path/config.
1160    */
1161   if(replace_config(ncct, info, argv[0], NULL)) {
1162     return -1;
1163   }
1164
1165   /* So, we updated an attribute, so we need to reload all checks
1166    * that are descendent-or-self of this node.
1167    */
1168   refresh_subchecks(ncct, info);
1169   return 0;
1170 }
1171
1172
1173 #define NEW_STATE(a) (a) = noit_console_state_alloc()
1174 #define ADD_CMD(a,cmd,func,ac,ss,c) \
1175   noit_console_state_add_cmd((a), \
1176     NCSCMD(cmd, func, ac, ss, c))
1177 #define DELEGATE_CMD(a,cmd,ac,ss) \
1178   noit_console_state_add_cmd((a), \
1179     NCSCMD(cmd, noit_console_state_delegate, ac, ss, NULL))
1180
1181 static
1182 void register_console_config_check_commands() {
1183   cmd_info_t *showcmd, *nocmd, *confcmd, *conftcmd, *conftnocmd, *lscmd;
1184   noit_console_state_t *tl, *_conf_t_check_state, *_unset_state,
1185                        *_attr_state, *_uattr_state;
1186
1187   tl = noit_console_state_initial();
1188   showcmd = noit_console_state_get_cmd(tl, "show");
1189   nocmd = noit_console_state_get_cmd(tl, "no");
1190   confcmd = noit_console_state_get_cmd(tl, "configure");
1191   conftcmd = noit_console_state_get_cmd(confcmd->dstate, "terminal");
1192   conftnocmd = noit_console_state_get_cmd(conftcmd->dstate, "no");
1193   lscmd = noit_console_state_get_cmd(conftcmd->dstate, "ls");
1194   lscmd->func = noit_console_config_show;
1195   /* attribute <attrname> <value> */
1196   NEW_STATE(_attr_state);
1197   noit_console_state_add_check_attrs(_attr_state, noit_conf_check_set_attr,
1198                                      "/checks");
1199  
1200   /* no attribute <attrname> <value> */
1201   NEW_STATE(_uattr_state);
1202   noit_console_state_add_check_attrs(_uattr_state, noit_conf_check_unset_attr,
1203                                      "/checks");
1204   NEW_STATE(_unset_state);
1205   DELEGATE_CMD(_unset_state, "attribute",
1206                noit_console_opt_delegate, _uattr_state);
1207   ADD_CMD(_unset_state, "config",
1208           noit_console_config_unsetconfig, NULL, NULL, NULL);
1209
1210   DELEGATE_CMD(conftnocmd->dstate, "attribute",
1211                noit_console_opt_delegate, _uattr_state);
1212   ADD_CMD(conftnocmd->dstate, "config",
1213           noit_console_config_unsetconfig, NULL, NULL, NULL);
1214   ADD_CMD(conftnocmd->dstate, "check",
1215           noit_console_config_nocheck, NULL, NULL, NULL);
1216  
1217   NEW_STATE(_conf_t_check_state);
1218   _conf_t_check_state->console_prompt_function = conf_t_check_prompt;
1219   DELEGATE_CMD(_conf_t_check_state, "attribute",
1220                noit_console_opt_delegate, _attr_state);
1221   DELEGATE_CMD(_conf_t_check_state, "no",
1222                noit_console_opt_delegate, _unset_state);
1223   ADD_CMD(_conf_t_check_state, "config",
1224           noit_console_config_setconfig, NULL, NULL, NULL);
1225   ADD_CMD(_conf_t_check_state, "status",
1226           noit_console_show_check, NULL, NULL, NULL);
1227   ADD_CMD(_conf_t_check_state, "exit",
1228           noit_console_config_cd, NULL, NULL, "..");
1229   ADD_CMD(_conf_t_check_state, "check",
1230           noit_console_check, noit_console_conf_check_opts,
1231           _conf_t_check_state, "..");
1232
1233   ADD_CMD(conftcmd->dstate, "config",
1234           noit_console_config_setconfig, NULL, NULL, NULL);
1235   ADD_CMD(conftcmd->dstate, "check",
1236           noit_console_check, noit_console_conf_check_opts,
1237           _conf_t_check_state, NULL);
1238
1239   ADD_CMD(showcmd->dstate, "check",
1240           noit_console_show_check, noit_console_check_opts, NULL, NULL);
1241
1242   ADD_CMD(tl, "watch",
1243           noit_console_watch_check, noit_console_check_opts, NULL, (void *)1);
1244
1245   ADD_CMD(nocmd->dstate, "watch",
1246           noit_console_watch_check, noit_console_check_opts, NULL, (void *)0);
1247
1248   DELEGATE_CMD(conftcmd->dstate, "attribute",
1249                noit_console_opt_delegate, _attr_state);
1250
1251   ADD_CMD(tl, "reload", noit_conf_checks_reload, NULL, NULL, NULL);
1252 }
1253
Note: See TracBrowser for help on using the browser.