root/src/noit_conf.c

Revision ab3215d849aeac6f677f2f106be98aad4dc8bd50, 26.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

support creating new sections

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