root/src/noit_conf.c

Revision 666f3b3ec6db879445b66943258a56a195e0af41, 24.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 11 years ago)

no cd into checks and fix prompt len

  • 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   if(!strcmp((char *)node->name, "check")) {
348     err = "can't cd into a check, use 'check' instead";
349     goto bad;
350   }
351   path = (char *)xmlGetNodePath(node);
352   if(strncmp(path, "/noit/", strlen("/noit/")) && strcmp(path, "/noit")) {
353     err = "new path outside out tree";
354     goto bad;
355   }
356   free(info->path);
357   if(!strcmp(path, "/noit"))
358     info->path = strdup("/");
359   else
360     info->path = strdup((char *)xmlGetNodePath(node) + strlen("/noit"));
361   if(pobj) xmlXPathFreeObject(pobj);
362   return 0;
363  bad:
364   if(pobj) xmlXPathFreeObject(pobj);
365   nc_printf(ncct, "%s\n", err);
366   return -1;
367 }
368 static int
369 noit_console_config_show(noit_console_closure_t ncct,
370                          int argc, char **argv,
371                          noit_console_state_t *state, void *closure) {
372   int i, cnt, titled = 0, cliplen = 0;
373   const char *path, *basepath = NULL;
374   char xpath[1024];
375   noit_conf_t_userdata_t *info = NULL;
376   xmlXPathObjectPtr pobj = NULL;
377   xmlXPathContextPtr current_ctxt;
378   xmlNodePtr node;
379
380   if(argc > 1) {
381     nc_printf(ncct, "too many arguments\n");
382     return -1;
383   }
384
385   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
386   if(info) path = basepath = info->path;
387   if(!info && argc == 0) {
388     nc_printf(ncct, "argument required when not in configuration mode\n");
389     return -1;
390   }
391
392   if(argc == 1) path = argv[0];
393   if(!basepath) basepath = path;
394
395   /* { / } is a special case */
396   if(!strcmp(basepath, "/")) basepath = "";
397   if(!strcmp(path, "/")) path = "";
398
399   if(!master_config) {
400     nc_printf(ncct, "no config\n");
401     return -1;
402   }
403
404   /* { / } is the only path that will end with a /
405    * in XPath { / / * } means something _entirely different than { / * }
406    * Ever notice how it is hard to describe xpath in C comments?
407    */
408   /* We don't want to show the root node */
409   cliplen = strlen("/noit/");
410
411   /* If we are in configuration mode
412    * and we are without an argument or the argument is absolute,
413    * clip the current path off */
414   if(info && (argc == 0 || path[0] != '/')) cliplen += strlen(basepath);
415   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
416     snprintf(xpath, sizeof(xpath), "/noit%s/@*", path);
417   else
418     snprintf(xpath, sizeof(xpath), "/noit%s/%s/@*", basepath, path);
419
420   current_ctxt = xpath_ctxt;
421   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
422   if(!pobj || pobj->type != XPATH_NODESET) {
423     nc_printf(ncct, "no such object\n");
424     goto bad;
425   }
426   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
427   titled = 0;
428   for(i=0; i<cnt; i++) {
429     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
430     if(!strcmp((char *)node->name, "check")) continue;
431     if(node->children && node->children == xmlGetLastChild(node) &&
432       xmlNodeIsText(node->children)) {
433       if(!titled++) nc_printf(ncct, "== Section Settings ==\n");
434       nc_printf(ncct, "%s: %s\n", xmlGetNodePath(node) + cliplen,
435                 xmlXPathCastNodeToString(node->children));
436     }
437   }
438   xmlXPathFreeObject(pobj);
439
440   /* _shorten string_ turning last { / @ * } to { / * } */
441   strlcpy(xpath + strlen(xpath) - 2, "*", 2);
442 nc_printf(ncct, "Looking up path '%s'\n", xpath);
443   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
444   if(!pobj || pobj->type != XPATH_NODESET) {
445     nc_printf(ncct, "no such object\n");
446     goto bad;
447   }
448   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
449   titled = 0;
450   for(i=0; i<cnt; i++) {
451     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
452     if(!strcmp((char *)node->name, "check")) continue;
453     if(!(node->children && node->children == xmlGetLastChild(node) &&
454          xmlNodeIsText(node->children))) {
455       if(!titled++) nc_printf(ncct, "== Subsections ==\n");
456       nc_printf(ncct, "%s\n", xmlGetNodePath(node) + cliplen);
457     }
458   }
459
460   titled = 0;
461   for(i=0; i<cnt; i++) {
462     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
463     if(!strcmp((char *)node->name, "check")) {
464       int busted = 1;
465       xmlAttr *attr;
466       char *uuid_str = "undefined";;
467
468       if(!titled++) nc_printf(ncct, "== Checks ==\n");
469
470       for(attr=node->properties; attr; attr = attr->next) {
471         if(!strcmp((char *)attr->name, "uuid"))
472           uuid_str = (char *)xmlXPathCastNodeToString(attr->children);
473       }
474       if(uuid_str) {
475         uuid_t checkid;
476         nc_printf(ncct, "check[@uuid=\"%s\"] ", uuid_str);
477         if(uuid_parse(uuid_str, checkid) == 0) {
478           noit_check_t *check;
479           check = noit_poller_lookup(checkid);
480           if(check) {
481             busted = 0;
482             nc_printf(ncct, "%s`%s", check->target, check->name);
483           }
484         }
485       }
486       else
487         nc_printf(ncct, "%s ", xmlGetNodePath(node) + cliplen);
488       if(busted) nc_printf(ncct, "[check not in running system]");
489       nc_write(ncct, "\n", 1);
490     }
491   }
492   xmlXPathFreeObject(pobj);
493   return 0;
494  bad:
495   if(pobj) xmlXPathFreeObject(pobj);
496   return -1;
497 }
498
499 static char *
500 conf_t_prompt(EditLine *el) {
501   noit_console_closure_t ncct;
502   noit_conf_t_userdata_t *info;
503   static char *tl = "noit(conf)# ";
504   static char *pfmt = "noit(conf:%s%s)# ";
505   int path_len, max_len;
506
507   el_get(el, EL_USERDATA, (void *)&ncct);
508   if(!ncct) return tl;
509   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
510   if(!info) return tl;
511
512   path_len = strlen(info->path);
513   max_len = sizeof(info->prompt) - (strlen(pfmt) - 4 /* %s%s */) - 1 /* \0 */;
514   if(path_len > max_len)
515     snprintf(info->prompt, sizeof(info->prompt),
516              pfmt, "...", info->path + max_len - 3 /* ... */);
517   else
518     snprintf(info->prompt, sizeof(info->prompt), pfmt, "", info->path);
519   return info->prompt;
520 }
521
522 static int
523 noit_console_write_xml(void *vncct, const char *buffer, int len) {
524   noit_console_closure_t ncct = vncct;
525   return nc_write(ncct, buffer, len);
526 }
527 static int
528 noit_console_close_xml(void *vncct) {
529   return 0;
530 }
531 static int
532 noit_conf_write_terminal(noit_console_closure_t ncct,
533                          int argc, char **argv,
534                          noit_console_state_t *state, void *closure) {
535   xmlOutputBufferPtr out;
536   xmlCharEncodingHandlerPtr enc;
537   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
538   out = xmlOutputBufferCreateIO(noit_console_write_xml,
539                                 noit_console_close_xml,
540                                 ncct, enc);
541   xmlSaveFileTo(out, master_config, "utf8");
542   return 0;
543 }
544 static int
545 noit_conf_write_file(noit_console_closure_t ncct,
546                      int argc, char **argv,
547                      noit_console_state_t *state, void *closure) {
548   int fd, len;
549   char master_file_tmp[PATH_MAX];
550   xmlOutputBufferPtr out;
551   xmlCharEncodingHandlerPtr enc;
552
553   snprintf(master_file_tmp, sizeof(master_file_tmp),
554            "%s.tmp", master_config_file);
555   unlink(master_file_tmp);
556   fd = open(master_file_tmp, O_CREAT|O_EXCL|O_WRONLY, 0640);
557   if(fd < 0) {
558     nc_printf(ncct, "Failed to open tmp file: %s\n", strerror(errno));
559     return -1;
560   }
561   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
562   out = xmlOutputBufferCreateFd(fd, enc);
563   if(!out) {
564     close(fd);
565     unlink(master_file_tmp);
566     nc_printf(ncct, "internal error: OutputBufferCreate failed\n");
567     return -1;
568   }
569   len = xmlSaveFileTo(out, master_config, "utf8");
570   close(fd);
571   if(len <= 0) {
572     nc_printf(ncct, "internal error: writing to tmp file failed.\n");
573     return -1;
574   }
575   if(rename(master_file_tmp, master_config_file) != 0) {
576     nc_printf(ncct, "Failed to replace file: %s\n", strerror(errno));
577     return -1;
578   }
579   nc_printf(ncct, "%d bytes written.\n", len);
580   return 0;
581 }
582
583 static struct _valid_attr_t {
584   const char *scope;
585   const char *name;
586   const char *xpath;
587   int checks_fixate;
588 } valid_attrs[] = {
589   { "/checks", "name", "@name", 0 },
590   { "/checks", "target", "@target", 0 },
591   { "/checks", "period", "@period", 0 },
592   { "/checks", "timeout", "@timeout", 0 },
593   { "/checks", "oncheck", "@oncheck", 0 },
594   { "/checks", "module", "@module", 1 },
595 };
596
597 void
598 noit_console_state_add_check_attrs(noit_console_state_t *state,
599                                    console_cmd_func_t f) {
600   int i;
601   for(i = 0;
602       i < sizeof(valid_attrs)/sizeof(valid_attrs[0]);
603       i++) {
604     noit_console_state_add_cmd(state,
605       NCSCMD(valid_attrs[i].name, f,
606              NULL, &valid_attrs[i]));
607   }
608 }
609
610 static int
611 validate_attr_set_scope(noit_conf_t_userdata_t *info,
612                         struct _valid_attr_t *attrinfo) {
613   int len;
614   len = strlen(attrinfo->scope);
615   if(strncmp(info->path, attrinfo->scope, len) ||
616      (info->path[len] != '\0' && info->path[len] != '/')) {
617     return -1;
618   }
619   return 0;
620 }
621 static int
622 replace_attr(noit_console_closure_t ncct,
623              noit_conf_t_userdata_t *info, struct _valid_attr_t *attrinfo,
624              const char *value) {
625   int cnt, rv = -1;
626   xmlXPathObjectPtr pobj = NULL;
627   xmlNodePtr node;
628   char xpath[1024], *path;
629
630   path = info->path;
631   if(!strcmp(path, "/")) path = "";
632
633   if(attrinfo->checks_fixate) {
634     /* Only if checks will fixate this attribute shall we check for
635      * child <check> nodes.
636      * NOTE: this return nothing and "seems" okay if we are _in_
637      *       a <check> node.  That case is handled below.
638      */
639     snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
640     pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
641     if(pobj && pobj->type == XPATH_NODESET &&
642        !xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
643       nc_printf(ncct, "Cannot set '%s', it would effect live checks\n",
644                 attrinfo->name);
645       goto out;
646     }
647     if(pobj) xmlXPathFreeObject(pobj);
648   }
649   snprintf(xpath, sizeof(xpath), "/noit/%s", path);
650   pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt);
651   if(!pobj || pobj->type != XPATH_NODESET) goto out;
652   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
653   if(cnt != 1) {
654     nc_printf(ncct, "Internal error: context node disappeared\n");
655     goto out;
656   }
657   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
658   if(attrinfo->checks_fixate &&
659      !strcmp((const char *)node->name, "check")) {
660     /* Detect if  we are actually a <check> node and attempting to
661      * change something we shouldn't.
662      * This is the counterpart noted above.
663      */
664     nc_printf(ncct, "Cannot set '%s', it would effect live checks\n");
665     goto out;
666   }
667   xmlUnsetProp(node, (xmlChar *)attrinfo->name);
668   if(value)
669     xmlSetProp(node, (xmlChar *)attrinfo->name, (xmlChar *)value);
670   rv = 0;
671  out:
672   if(pobj) xmlXPathFreeObject(pobj);
673   return rv;
674 }
675 static void
676 refresh_subchecks(noit_console_closure_t ncct,
677                   noit_conf_t_userdata_t *info) {
678   char *path;
679   char xpath[1024];
680  
681   path = info->path;
682   if(!strcmp(path, "/")) path = "";
683
684   snprintf(xpath, sizeof(xpath), "/noit/%s[@uuid]", path);
685   noit_poller_process_checks(xpath);
686   snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path);
687   noit_poller_process_checks(xpath);
688 }
689 int
690 noit_conf_check_set_attr(noit_console_closure_t ncct,
691                          int argc, char **argv,
692                          noit_console_state_t *state, void *closure) {
693   struct _valid_attr_t *attrinfo = closure;
694   noit_conf_t_userdata_t *info;
695
696   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
697   if(!info || validate_attr_set_scope(info, attrinfo)) {
698     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
699               attrinfo->name, attrinfo->scope);
700     return -1;
701   }
702
703   if(argc != 1) {
704     nc_printf(ncct, "set requires exactly one value\n");
705     return -1;
706   }
707   /* Okay, we have an attribute and it should be set/replaced on the
708    * current path.
709    */
710   if(replace_attr(ncct, info, attrinfo, argv[0])) {
711     return -1;
712   }
713
714   /* So, we updated an attribute, so we need to reload all checks
715    * that are descendent-or-self of this node.
716    */
717   refresh_subchecks(ncct, info);
718   return 0;
719 }
720
721 int
722 noit_conf_check_unset_attr(noit_console_closure_t ncct,
723                            int argc, char **argv,
724                            noit_console_state_t *state, void *closure) {
725   struct _valid_attr_t *attrinfo = closure;
726   noit_conf_t_userdata_t *info;
727
728   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
729   if(!info || validate_attr_set_scope(info, attrinfo)) {
730     nc_printf(ncct, "'%s' attribute only valid in %s scope\n",
731               attrinfo->name, attrinfo->scope);
732     return -1;
733   }
734
735   if(argc != 0) {
736     nc_printf(ncct, "no arguments allowed to this command.\n");
737     return -1;
738   }
739   /* Okay, we have an attribute and it should be set/replaced on the
740    * current path.
741    */
742   if(replace_attr(ncct, info, attrinfo, NULL)) {
743     return -1;
744   }
745
746   /* So, we updated an attribute, so we need to reload all checks
747    * that are descendent-or-self of this node.
748    */
749   refresh_subchecks(ncct, info);
750   return 0;
751 }
752
753 #define NEW_STATE(a) (a) = calloc(1, sizeof(*(a)))
754 #define ADD_CMD(a,cmd,func,ss,c) \
755   noit_console_state_add_cmd((a), \
756     NCSCMD(cmd, func, ss, c))
757 #define DELEGATE_CMD(a,cmd,ss) \
758   noit_console_state_add_cmd((a), \
759     NCSCMD(cmd, noit_console_state_delegate, ss, NULL))
760
761 static
762 void register_console_config_commands() {
763   noit_console_state_t *tl, *_conf_state, *_conf_t_state,
764                        *_write_state, *_attr_state,
765                        *_unset_state, *_uattr_state;
766
767   tl = noit_console_state_initial();
768
769   /* write <terimal|memory|file> */
770   NEW_STATE(_write_state);
771   ADD_CMD(_write_state, "terminal", noit_conf_write_terminal, NULL, NULL);
772   ADD_CMD(_write_state, "file", noit_conf_write_file, NULL, NULL);
773   /* write memory?  It's to a file, but I like router syntax */
774   ADD_CMD(_write_state, "memory", noit_conf_write_file, NULL, NULL);
775
776   /* attribute <attrname> <value> */
777   NEW_STATE(_attr_state);
778   noit_console_state_add_check_attrs(_attr_state, noit_conf_check_set_attr);
779  
780   /* no attribute <attrname> <value> */
781   NEW_STATE(_uattr_state);
782   noit_console_state_add_check_attrs(_uattr_state, noit_conf_check_unset_attr);
783   NEW_STATE(_unset_state);
784   DELEGATE_CMD(_unset_state, "attribute", _uattr_state);
785  
786   NEW_STATE(_conf_t_state);
787   _conf_t_state->console_prompt_function = conf_t_prompt;
788   noit_console_state_add_cmd(_conf_t_state, &console_command_exit);
789   ADD_CMD(_conf_t_state, "ls", noit_console_config_show, NULL, NULL);
790   ADD_CMD(_conf_t_state, "cd", noit_console_config_cd, NULL, NULL);
791   DELEGATE_CMD(_conf_t_state, "write", _write_state);
792   DELEGATE_CMD(_conf_t_state, "attribute", _attr_state);
793   DELEGATE_CMD(_conf_t_state, "no", _unset_state);
794
795   NEW_STATE(_conf_state);
796   ADD_CMD(_conf_state, "terminal", noit_console_state_conf_terminal, _conf_t_state, NULL);
797
798   ADD_CMD(tl, "configure", noit_console_state_delegate, _conf_state, NULL);
799   ADD_CMD(tl, "write", noit_console_state_delegate, _write_state, NULL);
800 }
Note: See TracBrowser for help on using the browser.