root/src/noit_conf.c

Revision 7a1324aa50711b4d7f5d81f7e901081a150cf9fe, 24.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 10 years ago)

online editing of attributes

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <assert.h>
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #include <libxml/xpath.h>
16
17 #include "noit_conf.h"
18 #include "noit_check.h"
19 #include "noit_console.h"
20 #include "utils/noit_hash.h"
21 #include "utils/noit_log.h"
22
23 /* tmp hash impl, replace this with something nice */
24 static noit_hash_table _tmp_config = NOIT_HASH_EMPTY;
25 static xmlDocPtr master_config = NULL;
26 static char master_config_file[PATH_MAX] = "";
27 static xmlXPathContextPtr xpath_ctxt = NULL;
28
29 static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY;
30 static struct {
31   const char *key;
32   const char *val;
33 } config_info[] = {
34   /*
35    * These are compile-time fallbacks to be used in the event
36    * that the current running config does not have values for
37    * these config paths.
38    *
39    * PLEASE: keep them alphabetically sorted.
40    */
41   { "/noit/modules/directory", MODULES_DIR },
42
43   { NULL, NULL }
44 };
45
46 static void register_console_config_commands();
47
48 void noit_conf_init() {
49   int i;
50   for(i = 0; config_info[i].key != NULL; i++) {
51     noit_hash_store(&_compiled_fallback,
52                     strdup(config_info[i].key), strlen(config_info[i].key),
53                     (void *)strdup(config_info[i].val));
54   }
55   xmlInitParser();
56   xmlXPathInit();
57   register_console_config_commands();
58 }
59
60 int noit_conf_load(const char *path) {
61   xmlDocPtr new_config;
62   new_config = xmlParseFile(path);
63   if(new_config) {
64     if(master_config) xmlFreeDoc(master_config);
65     if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt);
66
67     master_config = new_config;
68     xpath_ctxt = xmlXPathNewContext(master_config);
69     realpath(path, master_config_file);
70     return 0;
71   }
72   return -1;
73 }
74 int noit_conf_save(const char *path) {
75   return -1;
76 }
77
78 noit_hash_table *noit_conf_get_hash(noit_conf_section_t section,
79                                     const char *path) {
80   int i, cnt;
81   noit_hash_table *table = NULL;
82   xmlXPathObjectPtr pobj = NULL;
83   xmlXPathContextPtr current_ctxt;
84   xmlNodePtr current_node = (xmlNodePtr)section;
85   xmlNodePtr node;
86
87   current_ctxt = xpath_ctxt;
88   if(current_node) {
89     current_ctxt = xmlXPathNewContext(master_config);
90     current_ctxt->node = current_node;
91   }
92   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
93   if(!pobj) goto out;
94   if(pobj->type != XPATH_NODESET) goto out;
95   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
96   table = calloc(1, sizeof(*table));
97   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
98   for(i=0; i<cnt; i++) {
99     char *value;
100     node = xmlXPathNodeSetItem(pobj->nodesetval, i);
101     value = (char *)xmlXPathCastNodeToString(node);
102     noit_hash_replace(table,
103                       strdup((char *)node->name), strlen((char *)node->name),
104                       strdup(value), free, free);
105   }
106  out:
107   if(pobj) xmlXPathFreeObject(pobj);
108   if(current_ctxt && current_ctxt != xpath_ctxt)
109     xmlXPathFreeContext(current_ctxt);
110   return table;
111 }
112 noit_conf_section_t noit_conf_get_section(noit_conf_section_t section,
113                                           const char *path) {
114   noit_conf_section_t subsection = NULL;
115   xmlXPathObjectPtr pobj = NULL;
116   xmlXPathContextPtr current_ctxt;
117   xmlNodePtr current_node = (xmlNodePtr)section;
118
119   current_ctxt = xpath_ctxt;
120   if(current_node) {
121     current_ctxt = xmlXPathNewContext(master_config);
122     current_ctxt->node = current_node;
123   }
124   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
125   if(!pobj) goto out;
126   if(pobj->type != XPATH_NODESET) goto out;
127   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
128   subsection = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
129  out:
130   if(pobj) xmlXPathFreeObject(pobj);
131   if(current_ctxt && current_ctxt != xpath_ctxt)
132     xmlXPathFreeContext(current_ctxt);
133   return subsection;
134 }
135 noit_conf_section_t *noit_conf_get_sections(noit_conf_section_t section,
136                                             const char *path,
137                                             int *cnt) {
138   int i;
139   noit_conf_section_t *sections = NULL;
140   xmlXPathObjectPtr pobj = NULL;
141   xmlXPathContextPtr current_ctxt;
142   xmlNodePtr current_node = (xmlNodePtr)section;
143
144   *cnt = 0;
145   current_ctxt = xpath_ctxt;
146   if(current_node) {
147     current_ctxt = xmlXPathNewContext(master_config);
148     current_ctxt->node = current_node;
149   }
150   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
151   if(!pobj) goto out;
152   if(pobj->type != XPATH_NODESET) goto out;
153   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
154   *cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
155   sections = calloc(*cnt, sizeof(*sections));
156   for(i=0; i<*cnt; i++)
157     sections[i] = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
158  out:
159   if(pobj) xmlXPathFreeObject(pobj);
160   if(current_ctxt && current_ctxt != xpath_ctxt)
161     xmlXPathFreeContext(current_ctxt);
162   return sections;
163 }
164 int _noit_conf_get_string(noit_conf_section_t section,
165                           const char *path, char **value) {
166   char *str;
167   int i;
168   xmlXPathObjectPtr pobj;
169   xmlXPathContextPtr current_ctxt;
170   xmlNodePtr current_node = (xmlNodePtr)section;
171
172   current_ctxt = xpath_ctxt;
173   if(current_node) {
174     current_ctxt = xmlXPathNewContext(master_config);
175     current_ctxt->node = current_node;
176   }
177   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
178   if(pobj) {
179     xmlNodePtr node;
180     switch(pobj->type) {
181       case XPATH_NODESET:
182         if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) return 0;
183         i = xmlXPathNodeSetGetLength(pobj->nodesetval);
184         node = xmlXPathNodeSetItem(pobj->nodesetval, i-1);
185         *value = (char *)xmlXPathCastNodeToString(node);
186         break;
187       default:
188         *value = (char *)xmlXPathCastToString(pobj);
189     }
190     goto found;
191   }
192   if(noit_hash_retrieve(&_compiled_fallback,
193                         path, strlen(path), (void **)&str)) {
194     *value = str;
195     goto found;
196   }
197   return 0;
198  found:
199   if(current_ctxt && current_ctxt != xpath_ctxt)
200     xmlXPathFreeContext(current_ctxt);
201   return 1;
202 }
203 int noit_conf_get_string(noit_conf_section_t section,
204                          const char *path, char **value) {
205   char *str;
206   if(_noit_conf_get_string(section,path,&str)) {
207     *value = strdup(str);
208     return 1;
209   }
210   return 0;
211 }
212 int noit_conf_get_stringbuf(noit_conf_section_t section,
213                             const char *path, char *buf, int len) {
214   char *str;
215   if(_noit_conf_get_string(section,path,&str)) {
216     strlcpy(buf, str, len);
217     return 1;
218   }
219   return 0;
220 }
221 int noit_conf_set_string(noit_conf_section_t section,
222                          const char *path, const char *value) {
223   noit_hash_replace(&_tmp_config,
224                     strdup(path), strlen(path), (void *)strdup(value),
225                     free, free);
226   return 1;
227 }
228 int noit_conf_get_int(noit_conf_section_t section,
229                       const char *path, int *value) {
230   char *str;
231   long longval;
232   if(noit_conf_get_string(section,path,&str)) {
233     int base = 10;
234     if(str[0] == '0') {
235       if(str[1] == 'x') base = 16;
236       else base = 8;
237     }
238     longval = strtol(str, NULL, base);
239     free(str);
240     *value = (int)longval;
241     return 1;
242   }
243   return 0;
244 }
245 int noit_conf_set_int(noit_conf_section_t section,
246                       const char *path, int value) {
247   char buffer[32];
248   snprintf(buffer, 32, "%d", value);
249   return noit_conf_set_string(section,path,buffer);
250 }
251 int noit_conf_get_float(noit_conf_section_t section,
252                         const char *path, float *value) {
253   char *str;
254   if(noit_conf_get_string(section,path,&str)) {
255     *value = atof(str);
256     free(str);
257     return 1;
258   }
259   return 0;
260 }
261 int noit_conf_set_float(noit_conf_section_t section,
262                         const char *path, float value) {
263   char buffer[32];
264   snprintf(buffer, 32, "%f", value);
265   return noit_conf_set_string(section,path,buffer);
266 }
267 int noit_conf_get_boolean(noit_conf_section_t section,
268                           const char *path, noit_conf_boolean *value) {
269   char *str;
270   if(noit_conf_get_string(section,path,&str)) {
271     if(!strcasecmp(str, "true")) *value = noit_true;
272     else *value = noit_false;
273     free(str);
274     return 1;
275   }
276   return 0;
277 }
278 int noit_conf_set_boolean(noit_conf_section_t section,
279                           const char *path, noit_conf_boolean value) {
280   if(value == noit_true)
281     return noit_conf_set_string(section,path,"true");
282   return noit_conf_set_string(section,path,"false");
283 }
284
285 static void
286 conf_t_userdata_free(void *data) {
287   noit_conf_t_userdata_t *info = data;
288   if(info) {
289     if(info->path) free(info->path);
290     free(info);
291   }
292 }
293 static int
294 noit_console_state_conf_terminal(noit_console_closure_t ncct,
295                                  int argc, char **argv,
296                                  noit_console_state_t *state, void *closure) {
297   noit_conf_t_userdata_t *info;
298   if(argc) {
299     nc_printf(ncct, "extra arguments not expected.\n");
300     return -1;
301   }
302   info = calloc(1, sizeof(*info));
303   info->path = strdup("/");
304   noit_console_userdata_set(ncct, NOIT_CONF_T_USERDATA, info,
305                             conf_t_userdata_free);
306   noit_console_state_push_state(ncct, state);
307   noit_console_state_init(ncct);
308   return 0;
309 }
310
311 static int
312 noit_console_config_cd(noit_console_closure_t ncct,
313                        int argc, char **argv,
314                        noit_console_state_t *state, void *closure) {
315   const char *err = "internal error";
316   char *path, xpath[1024];
317   noit_conf_t_userdata_t *info;
318   xmlXPathObjectPtr pobj = NULL;
319   xmlXPathContextPtr current_ctxt;
320   xmlNodePtr node = NULL;
321
322   if(argc != 1) {
323     nc_printf(ncct, "requires one argument\n");
324     return -1;
325   }
326   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
327   if(argv[0][0] == '/')
328     snprintf(xpath, sizeof(xpath), "/noit%s", argv[0]);
329   else {
330     snprintf(xpath, sizeof(xpath), "/noit%s/%s", info->path, argv[0]);
331   }
332   if(xpath[strlen(xpath)-1] == '/') xpath[strlen(xpath)-1] = '\0';
333
334   current_ctxt = xpath_ctxt;
335   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
336   if(!pobj || pobj->type != XPATH_NODESET ||
337      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
338     err = "no such section";
339     goto bad;
340   }
341   if(xmlXPathNodeSetGetLength(pobj->nodesetval) > 1) {
342     err = "ambiguous section";
343     goto bad;
344   }
345
346   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
347   path = (char *)xmlGetNodePath(node);
348   if(strncmp(path, "/noit/", strlen("/noit/")) && strcmp(path, "/noit")) {
349     err = "new path outside out tree";
350     goto bad;
351   }
352   free(info->path);
353   if(!strcmp(path, "/noit"))
354     info->path = strdup("/");
355   else
356     info->path = strdup((char *)xmlGetNodePath(node) + strlen("/noit"));
357   if(pobj) xmlXPathFreeObject(pobj);
358   return 0;
359  bad:
360   if(pobj) xmlXPathFreeObject(pobj);
361   nc_printf(ncct, "%s\n", err);
362   return -1;
363 }
364 static int
365 noit_console_config_show(noit_console_closure_t ncct,
366                          int argc, char **argv,
367                          noit_console_state_t *state, void *closure) {
368   int i, cnt, titled = 0, cliplen = 0;
369   const char *path, *basepath = NULL;
370   char xpath[1024];
371   noit_conf_t_userdata_t *info = NULL;
372   xmlXPathObjectPtr pobj = NULL;
373   xmlXPathContextPtr current_ctxt;
374   xmlNodePtr node;
375
376   if(argc > 1) {
377     nc_printf(ncct, "too many arguments\n");
378     return -1;
379   }
380
381   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
382   if(info) path = basepath = info->path;
383   if(!info && argc == 0) {
384     nc_printf(ncct, "argument required when not in configuration mode\n");
385     return -1;
386   }
387
388   if(argc == 1) path = argv[0];
389   if(!basepath) basepath = path;
390
391   /* { / } is a special case */
392   if(!strcmp(basepath, "/")) basepath = "";
393   if(!strcmp(path, "/")) path = "";
394
395   if(!master_config) {
396     nc_printf(ncct, "no config\n");
397     return -1;
398   }
399
400   /* { / } is the only path that will end with a /
401    * in XPath { / / * } means something _entirely different than { / * }
402    * Ever notice how it is hard to describe xpath in C comments?
403    */
404   /* We don't want to show the root node */
405   cliplen = strlen("/noit/");
406
407   /* If we are in configuration mode
408    * and we are without an argument or the argument is absolute,
409    * clip the current path off */
410   if(info && (argc == 0 || path[0] != '/')) cliplen += strlen(basepath);
411   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
412     snprintf(xpath, sizeof(xpath), "/noit%s/@*", path);
413   else
414     snprintf(xpath, sizeof(xpath), "/noit%s/%s/@*", basepath, path);
415
416   current_ctxt = xpath_ctxt;
417   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
418   if(!pobj || pobj->type != XPATH_NODESET) {
419     nc_printf(ncct, "no such object\n");
420     goto bad;
421   }
422   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
423   titled = 0;
424   for(i=0; i<cnt; i++) {
425     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
426     if(!strcmp((char *)node->name, "check")) continue;
427     if(node->children && node->children == xmlGetLastChild(node) &&
428       xmlNodeIsText(node->children)) {
429       if(!titled++) nc_printf(ncct, "== Section Settings ==\n");
430       nc_printf(ncct, "%s: %s\n", xmlGetNodePath(node) + cliplen,
431                 xmlXPathCastNodeToString(node->children));
432     }
433   }
434   xmlXPathFreeObject(pobj);
435
436   /* _shorten string_ turning last { / @ * } to { / * } */
437   strlcpy(xpath + strlen(xpath) - 2, "*", 2);
438 nc_printf(ncct, "Looking up path '%s'\n", xpath);
439   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
440   if(!pobj || pobj->type != XPATH_NODESET) {
441     nc_printf(ncct, "no such object\n");
442     goto bad;
443   }
444   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
445   titled = 0;
446   for(i=0; i<cnt; i++) {
447     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
448     if(!strcmp((char *)node->name, "check")) continue;
449     if(!(node->children && node->children == xmlGetLastChild(node) &&
450          xmlNodeIsText(node->children))) {
451       if(!titled++) nc_printf(ncct, "== Subsections ==\n");
452       nc_printf(ncct, "%s\n", xmlGetNodePath(node) + cliplen);
453     }
454   }
455
456   titled = 0;
457   for(i=0; i<cnt; i++) {
458     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
459     if(!strcmp((char *)node->name, "check")) {
460       int busted = 1;
461       xmlAttr *attr;
462       char *uuid_str = "undefined";;
463
464       if(!titled++) nc_printf(ncct, "== Checks ==\n");
465
466       for(attr=node->properties; attr; attr = attr->next) {
467         if(!strcmp((char *)attr->name, "uuid"))
468           uuid_str = (char *)xmlXPathCastNodeToString(attr->children);
469       }
470       if(uuid_str) {
471         uuid_t checkid;
472         nc_printf(ncct, "check[@uuid=\"%s\"] ", uuid_str);
473         if(uuid_parse(uuid_str, checkid) == 0) {
474           noit_check_t *check;
475           check = noit_poller_lookup(checkid);
476           if(check) {
477             busted = 0;
478             nc_printf(ncct, "%s`%s", check->target, check->name);
479           }
480         }
481       }
482       else
483         nc_printf(ncct, "%s ", xmlGetNodePath(node) + cliplen);
484       if(busted) nc_printf(ncct, "[check not in running system]");
485       nc_write(ncct, "\n", 1);
486     }
487   }
488   xmlXPathFreeObject(pobj);
489   return 0;
490  bad:
491   if(pobj) xmlXPathFreeObject(pobj);
492   return -1;
493 }
494
495 static char *
496 conf_t_prompt(EditLine *el) {
497   noit_console_closure_t ncct;
498   noit_conf_t_userdata_t *info;
499   static char *tl = "noit(conf)# ";
500   static char *pfmt = "noit(conf:%s%s)# ";
501   int path_len, max_len;
502
503   el_get(el, EL_USERDATA, (void *)&ncct);
504   if(!ncct) return tl;
505   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
506   if(!info) return tl;
507
508   path_len = strlen(info->path);
509   max_len = strlen(pfmt) - 4 /* %s%s */ - 1 /* \0 */;
510   if(path_len > max_len)
511     snprintf(info->prompt, sizeof(info->prompt),
512              pfmt, "...", info->path + max_len - 3 /* ... */);
513   else
514     snprintf(info->prompt, sizeof(info->prompt), pfmt, "", info->path);
515   return info->prompt;
516 }
517
518 static int
519 noit_console_write_xml(void *vncct, const char *buffer, int len) {
520   noit_console_closure_t ncct = vncct;
521   return nc_write(ncct, buffer, len);
522 }
523 static int
524 noit_console_close_xml(void *vncct) {
525   return 0;
526 }
527 static int
528 noit_conf_write_terminal(noit_console_closure_t ncct,
529                          int argc, char **argv,
530                          noit_console_state_t *state, void *closure) {
531   xmlOutputBufferPtr out;
532   xmlCharEncodingHandlerPtr enc;
533   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
534   out = xmlOutputBufferCreateIO(noit_console_write_xml,
535                                 noit_console_close_xml,
536                                 ncct, enc);
537   xmlSaveFileTo(out, master_config, "utf8");
538   return 0;
539 }
540 static int
541 noit_conf_write_file(noit_console_closure_t ncct,
542                      int argc, char **argv,
543                      noit_console_state_t *state, void *closure) {
544   int fd, len;
545   char master_file_tmp[PATH_MAX];
546   xmlOutputBufferPtr out;
547   xmlCharEncodingHandlerPtr enc;
548
549   snprintf(master_file_tmp, sizeof(master_file_tmp),
550            "%s.tmp", master_config_file);
551   unlink(master_file_tmp);
552   fd = open(master_file_tmp, O_CREAT|O_EXCL|O_WRONLY, 0640);
553   if(fd < 0) {
554     nc_printf(ncct, "Failed to open tmp file: %s\n", strerror(errno));
555     return -1;
556   }
557   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
558   out = xmlOutputBufferCreateFd(fd, enc);
559   if(!out) {
560     close(fd);
561     unlink(master_file_tmp);
562     nc_printf(ncct, "internal error: OutputBufferCreate failed\n");
563     return -1;
564   }
565   len = xmlSaveFileTo(out, master_config, "utf8");
566   close(fd);
567   if(len <= 0) {
568     nc_printf(ncct, "internal error: writing to tmp file failed.\n");
569     return -1;
570   }
571   if(rename(master_file_tmp, master_config_file) != 0) {
572     nc_printf(ncct, "Failed to replace file: %s\n", strerror(errno));
573     return -1;
574   }
575   nc_printf(ncct, "%d bytes written.\n", len);
576   return 0;
577 }
578
579 static struct _valid_attr_t {
580   const char *scope;
581   const char *name;
582   const char *xpath;
583   int checks_fixate;
584 } valid_attrs[] = {
585   { "/checks", "name", "@name", 0 },
586   { "/checks", "target", "@target", 0 },
587   { "/checks", "period", "@period", 0 },
588   { "/checks", "timeout", "@timeout", 0 },
589   { "/checks", "oncheck", "@oncheck", 0 },
590   { "/checks", "module", "@module", 1 },
591 };
592
593 void
594 noit_console_state_add_check_attrs(noit_console_state_t *state,
595                                    console_cmd_func_t f) {
596   int i;
597   for(i = 0;
598       i < sizeof(valid_attrs)/sizeof(valid_attrs[0]);
599       i++) {
600     noit_console_state_add_cmd(state,
601       NCSCMD(valid_attrs[i].name, f,
602              NULL, &valid_attrs[i]));
603   }
604 }
605
606 static int
607 validate_attr_set_scope(noit_conf_t_userdata_t *info,
608                         struct _valid_attr_t *attrinfo) {
609   int len;
610   len = strlen(attrinfo->scope);
611   if(strncmp(info->path, attrinfo->scope, len) ||
612      (info->path[len] != '\0' && info->path[len] != '/')) {
613     return -1;
614   }
615   return 0;
616 }
617 static int
618 replace_attr(noit_console_closure_t ncct,
619              noit_conf_t_userdata_t *info, struct _valid_attr_t *attrinfo,
620              const char *value) {
621   int cnt, rv = -1;
622   xmlXPathObjectPtr pobj = NULL;
623   xmlNodePtr node;
624   char xpath[1024], *path;
625
626   path = info->path;
627   if(!strcmp(path, "/")) path = "";
628
629   if(attrinfo->checks_fixate) {
630     /* Only if checks will fixate this attribute shall we check for
631      * child <check> nodes.
632      * NOTE: this return nothing and "seems" okay if we are _in_
633      *       a <check> node.  That case is handled below.
634      */
635     snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
636     pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
637     if(pobj && pobj->type == XPATH_NODESET &&
638        !xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
639       nc_printf(ncct, "Cannot set '%s', it would effect live checks\n",
640                 attrinfo->name);
641       goto out;
642     }
643     if(pobj) xmlXPathFreeObject(pobj);
644   }
645   snprintf(xpath, sizeof(xpath), "/noit/%s", path);
646   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
647   if(!pobj || pobj->type != XPATH_NODESET) goto out;
648   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
649   if(cnt != 1) {
650     nc_printf(ncct, "Internal error: context node disappeared\n");
651     goto out;
652   }
653   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
654   if(attrinfo->checks_fixate &&
655      !strcmp((const char *)node->name, "check")) {
656     /* Detect if  we are actually a <check> node and attempting to
657      * change something we shouldn't.
658      * This is the counterpart noted above.
659      */
660     nc_printf(ncct, "Cannot set '%s', it would effect live checks\n");
661     goto out;
662   }
663   xmlUnsetProp(node, (xmlChar *)attrinfo->name);
664   if(value)
665     xmlSetProp(node, (xmlChar *)attrinfo->name, (xmlChar *)value);
666   rv = 0;
667  out:
668   if(pobj) xmlXPathFreeObject(pobj);
669   return rv;
670 }
671 static void
672 refresh_subchecks(noit_console_closure_t ncct,
673                   noit_conf_t_userdata_t *info) {
674   char *path;
675   char xpath[1024];
676  
677   path = info->path;
678   if(!strcmp(path, "/")) path = "";
679
680   snprintf(xpath, sizeof(xpath), "/noit/%s[@uuid]", path);
681   noit_poller_process_checks(xpath);
682   snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
683   noit_poller_process_checks(xpath);
684 }
685 int
686 noit_conf_check_set_attr(noit_console_closure_t ncct,
687                          int argc, char **argv,
688                          noit_console_state_t *state, void *closure) {
689   struct _valid_attr_t *attrinfo = closure;
690   noit_conf_t_userdata_t *info;
691
692   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
693   if(!info || validate_attr_set_scope(info, attrinfo)) {
694     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
695               attrinfo->name, attrinfo->scope);
696     return -1;
697   }
698
699   if(argc != 1) {
700     nc_printf(ncct, "set requires exactly one value\n");
701     return -1;
702   }
703   /* Okay, we have an attribute and it should be set/replaced on the
704    * current path.
705    */
706   if(replace_attr(ncct, info, attrinfo, argv[0])) {
707     return -1;
708   }
709
710   /* So, we updated an attribute, so we need to reload all checks
711    * that are descendent-or-self of this node.
712    */
713   refresh_subchecks(ncct, info);
714   return 0;
715 }
716
717 int
718 noit_conf_check_unset_attr(noit_console_closure_t ncct,
719                            int argc, char **argv,
720                            noit_console_state_t *state, void *closure) {
721   struct _valid_attr_t *attrinfo = closure;
722   noit_conf_t_userdata_t *info;
723
724   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
725   if(!info || validate_attr_set_scope(info, attrinfo)) {
726     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
727               attrinfo->name, attrinfo->scope);
728     return -1;
729   }
730
731   if(argc != 0) {
732     nc_printf(ncct, "no arguments allowed to this command.\n");
733     return -1;
734   }
735   /* Okay, we have an attribute and it should be set/replaced on the
736    * current path.
737    */
738   if(replace_attr(ncct, info, attrinfo, NULL)) {
739     return -1;
740   }
741
742   /* So, we updated an attribute, so we need to reload all checks
743    * that are descendent-or-self of this node.
744    */
745   refresh_subchecks(ncct, info);
746   return 0;
747 }
748
749 #define NEW_STATE(a) (a) = calloc(1, sizeof(*(a)))
750 #define ADD_CMD(a,cmd,func,ss,c) \
751   noit_console_state_add_cmd((a), \
752     NCSCMD(cmd, func, ss, c))
753 #define DELEGATE_CMD(a,cmd,ss) \
754   noit_console_state_add_cmd((a), \
755     NCSCMD(cmd, noit_console_state_delegate, ss, NULL))
756
757 static
758 void register_console_config_commands() {
759   noit_console_state_t *tl, *_conf_state, *_conf_t_state,
760                        *_write_state, *_attr_state,
761                        *_unset_state, *_uattr_state;
762
763   tl = noit_console_state_initial();
764
765   /* write <terimal|memory|file> */
766   NEW_STATE(_write_state);
767   ADD_CMD(_write_state, "terminal", noit_conf_write_terminal, NULL, NULL);
768   ADD_CMD(_write_state, "file", noit_conf_write_file, NULL, NULL);
769   /* write memory?  It's to a file, but I like router syntax */
770   ADD_CMD(_write_state, "memory", noit_conf_write_file, NULL, NULL);
771
772   /* attribute <attrname> <value> */
773   NEW_STATE(_attr_state);
774   noit_console_state_add_check_attrs(_attr_state, noit_conf_check_set_attr);
775  
776   /* no attribute <attrname> <value> */
777   NEW_STATE(_uattr_state);
778   noit_console_state_add_check_attrs(_uattr_state, noit_conf_check_unset_attr);
779   NEW_STATE(_unset_state);
780   DELEGATE_CMD(_unset_state, "attribute", _uattr_state);
781  
782   NEW_STATE(_conf_t_state);
783   _conf_t_state->console_prompt_function = conf_t_prompt;
784   noit_console_state_add_cmd(_conf_t_state, &console_command_exit);
785   ADD_CMD(_conf_t_state, "ls", noit_console_config_show, NULL, NULL);
786   ADD_CMD(_conf_t_state, "cd", noit_console_config_cd, NULL, NULL);
787   DELEGATE_CMD(_conf_t_state, "write", _write_state);
788   DELEGATE_CMD(_conf_t_state, "attribute", _attr_state);
789   DELEGATE_CMD(_conf_t_state, "no", _unset_state);
790
791   NEW_STATE(_conf_state);
792   ADD_CMD(_conf_state, "terminal", noit_console_state_conf_terminal, _conf_t_state, NULL);
793
794   ADD_CMD(tl, "configure", noit_console_state_delegate, _conf_state, NULL);
795   ADD_CMD(tl, "write", noit_console_state_delegate, _write_state, NULL);
796 }
Note: See TracBrowser for help on using the browser.