root/src/noit_conf_checks.c

Revision 8a26fbab8eeb5faefa109ddedfc52a6e3d4af181, 43.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 days ago)

Add a sequency number to checks.

If 'seq' attribute exists, enforce that it monotonously increases.

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