]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
improved configuration and fixes for proxy mode
[netdata.git] / src / appconfig.c
1 #include "common.h"
2
3 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
4
5 // ----------------------------------------------------------------------------
6 // definitions
7
8 #define CONFIG_VALUE_LOADED  0x01 // has been loaded from the config
9 #define CONFIG_VALUE_USED    0x02 // has been accessed from the program
10 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
11 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
12
13 struct config_option {
14     avl avl;                // the index - this has to be first!
15
16     uint8_t flags;
17     uint32_t hash;          // a simple hash to speed up searching
18                             // we first compare hashes, and only if the hashes are equal we do string comparisons
19
20     char *name;
21     char *value;
22
23     struct config_option *next; // config->mutex protects just this
24 };
25
26 struct section {
27     avl avl;
28
29     uint32_t hash;          // a simple hash to speed up searching
30                             // we first compare hashes, and only if the hashes are equal we do string comparisons
31
32     char *name;
33
34     struct section *next;    // gloabl config_mutex protects just this
35
36     struct config_option *values;
37     avl_tree_lock values_index;
38
39     pthread_mutex_t mutex;  // this locks only the writers, to ensure atomic updates
40                             // readers are protected using the rwlock in avl_tree_lock
41 };
42
43 static int appconfig_section_compare(void *a, void *b);
44
45 struct config netdata_config = {
46         .sections = NULL,
47         .mutex = PTHREAD_MUTEX_INITIALIZER,
48         .index = {
49             { NULL, appconfig_section_compare },
50             AVL_LOCK_INITIALIZER
51         }
52 };
53
54 struct config stream_config = {
55         .sections = NULL,
56         .mutex = PTHREAD_MUTEX_INITIALIZER,
57         .index = {
58                 { NULL, appconfig_section_compare },
59                 AVL_LOCK_INITIALIZER
60         }
61 };
62
63 // ----------------------------------------------------------------------------
64 // locking
65
66 static inline void appconfig_wrlock(struct config *root) {
67     pthread_mutex_lock(&root->mutex);
68 }
69
70 static inline void appconfig_unlock(struct config *root) {
71     pthread_mutex_unlock(&root->mutex);
72 }
73
74 static inline void config_section_wrlock(struct section *co) {
75     pthread_mutex_lock(&co->mutex);
76 }
77
78 static inline void config_section_unlock(struct section *co) {
79     pthread_mutex_unlock(&co->mutex);
80 }
81
82
83 // ----------------------------------------------------------------------------
84 // config name-value index
85
86 static int appconfig_option_compare(void *a, void *b) {
87     if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1;
88     else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1;
89     else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name);
90 }
91
92 #define appconfig_option_index_add(co, cv) (struct config_option *)avl_insert_lock(&((co)->values_index), (avl *)(cv))
93 #define appconfig_option_index_del(co, cv) (struct config_option *)avl_remove_lock(&((co)->values_index), (avl *)(cv))
94
95 static struct config_option *appconfig_option_index_find(struct section *co, const char *name, uint32_t hash) {
96     struct config_option tmp;
97     tmp.hash = (hash)?hash:simple_hash(name);
98     tmp.name = (char *)name;
99
100     return (struct config_option *)avl_search_lock(&(co->values_index), (avl *) &tmp);
101 }
102
103
104 // ----------------------------------------------------------------------------
105 // config sections index
106
107 static int appconfig_section_compare(void *a, void *b) {
108     if(((struct section *)a)->hash < ((struct section *)b)->hash) return -1;
109     else if(((struct section *)a)->hash > ((struct section *)b)->hash) return 1;
110     else return strcmp(((struct section *)a)->name, ((struct section *)b)->name);
111 }
112
113 #define appconfig_index_add(root, cfg) (struct section *)avl_insert_lock(&root->index, (avl *)(cfg))
114 #define appconfig_index_del(root, cfg) (struct section *)avl_remove_lock(&root->index, (avl *)(cfg))
115
116 static struct section *appconfig_index_find(struct config *root, const char *name, uint32_t hash) {
117     struct section tmp;
118     tmp.hash = (hash)?hash:simple_hash(name);
119     tmp.name = (char *)name;
120
121     return (struct section *)avl_search_lock(&root->index, (avl *) &tmp);
122 }
123
124
125 // ----------------------------------------------------------------------------
126 // config section methods
127
128 static inline struct section *appconfig_section_find(struct config *root, const char *section) {
129     return appconfig_index_find(root, section, 0);
130 }
131
132 static inline struct section *appconfig_section_create(struct config *root, const char *section) {
133     debug(D_CONFIG, "Creating section '%s'.", section);
134
135     struct section *co = callocz(1, sizeof(struct section));
136     co->name = strdupz(section);
137     co->hash = simple_hash(co->name);
138
139     avl_init_lock(&co->values_index, appconfig_option_compare);
140
141     if(unlikely(appconfig_index_add(root, co) != co))
142         error("INTERNAL ERROR: indexing of section '%s', already exists.", co->name);
143
144     appconfig_wrlock(root);
145     struct section *co2 = root->sections;
146     if(co2) {
147         while (co2->next) co2 = co2->next;
148         co2->next = co;
149     }
150     else root->sections = co;
151     appconfig_unlock(root);
152
153     return co;
154 }
155
156
157 // ----------------------------------------------------------------------------
158 // config name-value methods
159
160 static inline struct config_option *appconfig_value_create(struct section *co, const char *name, const char *value)
161 {
162     debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
163
164     struct config_option *cv = callocz(1, sizeof(struct config_option));
165     cv->name = strdupz(name);
166     cv->hash = simple_hash(cv->name);
167     cv->value = strdupz(value);
168
169     if(unlikely(appconfig_option_index_add(co, cv) != cv))
170         error("INTERNAL ERROR: indexing of config '%s' in section '%s': already exists.", cv->name, co->name);
171
172     config_section_wrlock(co);
173     struct config_option *cv2 = co->values;
174     if(cv2) {
175         while (cv2->next) cv2 = cv2->next;
176         cv2->next = cv;
177     }
178     else co->values = cv;
179     config_section_unlock(co);
180
181     return cv;
182 }
183
184 int appconfig_exists(struct config *root, const char *section, const char *name) {
185     struct config_option *cv;
186
187     debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
188
189     struct section *co = appconfig_section_find(root, section);
190     if(!co) return 0;
191
192     cv = appconfig_option_index_find(co, name, 0);
193     if(!cv) return 0;
194
195     return 1;
196 }
197
198 int appconfig_move(struct config *root, const char *section_old, const char *name_old, const char *section_new, const char *name_new) {
199     struct config_option *cv_old, *cv_new;
200     int ret = -1;
201
202     debug(D_CONFIG, "request to rename config in section '%s', old name '%s', to section '%s', new name '%s'", section_old, name_old, section_new, name_new);
203
204     struct section *co_old = appconfig_section_find(root, section_old);
205     if(!co_old) return -1;
206
207     struct section *co_new = appconfig_section_find(root, section_new);
208     if(!co_new) co_new = appconfig_section_create(root, section_new);
209
210     config_section_wrlock(co_old);
211     config_section_wrlock(co_new);
212
213     cv_old = appconfig_option_index_find(co_old, name_old, 0);
214     if(!cv_old) goto cleanup;
215
216     cv_new = appconfig_option_index_find(co_new, name_new, 0);
217     if(cv_new) goto cleanup;
218
219     if(unlikely(appconfig_option_index_del(co_old, cv_old) != cv_old))
220         error("INTERNAL ERROR: deletion of config '%s' from section '%s', deleted tge wrong config entry.", cv_old->name, co_old->name);
221
222     if(co_old->values == cv_old) {
223         co_old->values = cv_old->next;
224     }
225     else {
226         struct config_option *t;
227         for(t = co_old->values; t && t->next != cv_old ;t = t->next) ;
228         if(!t || t->next != cv_old)
229             error("INTERNAL ERROR: cannot find variable '%s' in section '%s' of the config - but it should be there.", cv_old->name, co_old->name);
230         else
231             t->next = cv_old->next;
232     }
233
234     freez(cv_old->name);
235     cv_old->name = strdupz(name_new);
236     cv_old->hash = simple_hash(cv_old->name);
237
238     cv_new->next = co_new->values;
239     co_new->values = cv_new;
240
241     if(unlikely(appconfig_option_index_add(co_new, cv_old) != cv_old))
242         error("INTERNAL ERROR: indexing of config '%s' in section '%s', already exists.", cv_old->name, co_new->name);
243
244     ret = 0;
245
246 cleanup:
247     config_section_unlock(co_new);
248     config_section_unlock(co_old);
249     return ret;
250 }
251
252 char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value)
253 {
254     struct config_option *cv;
255
256     debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
257
258     struct section *co = appconfig_section_find(root, section);
259     if(!co) co = appconfig_section_create(root, section);
260
261     cv = appconfig_option_index_find(co, name, 0);
262     if(!cv) {
263         cv = appconfig_value_create(co, name, default_value);
264         if(!cv) return NULL;
265     }
266     cv->flags |= CONFIG_VALUE_USED;
267
268     if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
269         // this is a loaded value from the config file
270         // if it is different that the default, mark it
271         if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
272             if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
273             cv->flags |= CONFIG_VALUE_CHECKED;
274         }
275     }
276
277     return(cv->value);
278 }
279
280 long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value)
281 {
282     char buffer[100], *s;
283     sprintf(buffer, "%lld", value);
284
285     s = appconfig_get(root, section, name, buffer);
286     if(!s) return value;
287
288     return strtoll(s, NULL, 0);
289 }
290
291 int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value)
292 {
293     char *s;
294     if(value) s = "yes";
295     else s = "no";
296
297     s = appconfig_get(root, section, name, s);
298     if(!s) return value;
299
300     if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
301     return 0;
302 }
303
304 int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value)
305 {
306     char *s;
307
308     if(value == CONFIG_BOOLEAN_AUTO)
309         s = "auto";
310
311     else if(value == CONFIG_BOOLEAN_NO)
312         s = "no";
313
314     else
315         s = "yes";
316
317     s = appconfig_get(root, section, name, s);
318     if(!s) return value;
319
320     if(!strcmp(s, "yes"))
321         return CONFIG_BOOLEAN_YES;
322     else if(!strcmp(s, "no"))
323         return CONFIG_BOOLEAN_NO;
324     else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
325         return CONFIG_BOOLEAN_AUTO;
326
327     return value;
328 }
329
330 const char *appconfig_set_default(struct config *root, const char *section, const char *name, const char *value)
331 {
332     struct config_option *cv;
333
334     debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
335
336     struct section *co = appconfig_section_find(root, section);
337     if(!co) return appconfig_set(root, section, name, value);
338
339     cv = appconfig_option_index_find(co, name, 0);
340     if(!cv) return appconfig_set(root, section, name, value);
341
342     cv->flags |= CONFIG_VALUE_USED;
343
344     if(cv->flags & CONFIG_VALUE_LOADED)
345         return cv->value;
346
347     if(strcmp(cv->value, value) != 0) {
348         cv->flags |= CONFIG_VALUE_CHANGED;
349
350         freez(cv->value);
351         cv->value = strdupz(value);
352     }
353
354     return cv->value;
355 }
356
357 const char *appconfig_set(struct config *root, const char *section, const char *name, const char *value)
358 {
359     struct config_option *cv;
360
361     debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
362
363     struct section *co = appconfig_section_find(root, section);
364     if(!co) co = appconfig_section_create(root, section);
365
366     cv = appconfig_option_index_find(co, name, 0);
367     if(!cv) cv = appconfig_value_create(co, name, value);
368     cv->flags |= CONFIG_VALUE_USED;
369
370     if(strcmp(cv->value, value) != 0) {
371         cv->flags |= CONFIG_VALUE_CHANGED;
372
373         freez(cv->value);
374         cv->value = strdupz(value);
375     }
376
377     return value;
378 }
379
380 long long appconfig_set_number(struct config *root, const char *section, const char *name, long long value)
381 {
382     char buffer[100];
383     sprintf(buffer, "%lld", value);
384
385     appconfig_set(root, section, name, buffer);
386
387     return value;
388 }
389
390 int appconfig_set_boolean(struct config *root, const char *section, const char *name, int value)
391 {
392     char *s;
393     if(value) s = "yes";
394     else s = "no";
395
396     appconfig_set(root, section, name, s);
397
398     return value;
399 }
400
401
402 // ----------------------------------------------------------------------------
403 // config load/save
404
405 int appconfig_load(struct config *root, char *filename, int overwrite_used)
406 {
407     int line = 0;
408     struct section *co = NULL;
409
410     char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
411
412     if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
413
414     debug(D_CONFIG, "Opening config file '%s'", filename);
415
416     FILE *fp = fopen(filename, "r");
417     if(!fp) {
418         error("Cannot open file '%s'", filename);
419         return 0;
420     }
421
422     while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
423         buffer[CONFIG_FILE_LINE_MAX] = '\0';
424         line++;
425
426         s = trim(buffer);
427         if(!s) {
428             debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
429             continue;
430         }
431
432         int len = (int) strlen(s);
433         if(*s == '[' && s[len - 1] == ']') {
434             // new section
435             s[len - 1] = '\0';
436             s++;
437
438             co = appconfig_section_find(root, s);
439             if(!co) co = appconfig_section_create(root, s);
440
441             continue;
442         }
443
444         if(!co) {
445             // line outside a section
446             error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
447             continue;
448         }
449
450         char *name = s;
451         char *value = strchr(s, '=');
452         if(!value) {
453             error("Ignoring line %d ('%s'), there is no = in it.", line, s);
454             continue;
455         }
456         *value = '\0';
457         value++;
458
459         name = trim(name);
460         value = trim(value);
461
462         if(!name) {
463             error("Ignoring line %d, name is empty.", line);
464             continue;
465         }
466         if(!value) {
467             debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
468             continue;
469         }
470
471         struct config_option *cv = appconfig_option_index_find(co, name, 0);
472
473         if(!cv) cv = appconfig_value_create(co, name, value);
474         else {
475             if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
476                 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
477                 freez(cv->value);
478                 cv->value = strdupz(value);
479             }
480             else
481                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
482         }
483         cv->flags |= CONFIG_VALUE_LOADED;
484     }
485
486     fclose(fp);
487
488     return 1;
489 }
490
491 void appconfig_generate(struct config *root, BUFFER *wb, int only_changed)
492 {
493     int i, pri;
494     struct section *co;
495     struct config_option *cv;
496
497     for(i = 0; i < 3 ;i++) {
498         switch(i) {
499             case 0:
500                 buffer_strcat(wb,
501                     "# netdata configuration\n"
502                     "#\n"
503                     "# You can download the latest version of this file, using:\n"
504                     "#\n"
505                     "#  wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
506                     "# or\n"
507                     "#  curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
508                     "#\n"
509                     "# You can uncomment and change any of the options below.\n"
510                     "# The value shown in the commented settings, is the default value.\n"
511                     "#\n"
512                     "\n# global netdata configuration\n");
513                 break;
514
515             case 1:
516                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
517                 break;
518
519             case 2:
520                 buffer_strcat(wb, "\n\n# per chart configuration\n");
521                 break;
522         }
523
524         appconfig_wrlock(root);
525         for(co = root->sections; co ; co = co->next) {
526             if(!strcmp(co->name, CONFIG_SECTION_GLOBAL)
527                || !strcmp(co->name, CONFIG_SECTION_API)
528                || !strcmp(co->name, CONFIG_SECTION_PLUGINS)
529                || !strcmp(co->name, CONFIG_SECTION_REGISTRY)
530                || !strcmp(co->name, CONFIG_SECTION_HEALTH)
531                || !strcmp(co->name, CONFIG_SECTION_BACKEND)
532                || !strcmp(co->name, CONFIG_SECTION_STREAM)
533                     )
534                 pri = 0;
535             else if(!strncmp(co->name, "plugin:", 7)) pri = 1;
536             else pri = 2;
537
538             if(i == pri) {
539                 int used = 0;
540                 int changed = 0;
541                 int count = 0;
542
543                 config_section_wrlock(co);
544                 for(cv = co->values; cv ; cv = cv->next) {
545                     used += (cv->flags & CONFIG_VALUE_USED)?1:0;
546                     changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
547                     count++;
548                 }
549                 config_section_unlock(co);
550
551                 if(!count) continue;
552                 if(only_changed && !changed) continue;
553
554                 if(!used) {
555                     buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
556                 }
557
558                 buffer_sprintf(wb, "\n[%s]\n", co->name);
559
560                 config_section_wrlock(co);
561                 for(cv = co->values; cv ; cv = cv->next) {
562
563                     if(used && !(cv->flags & CONFIG_VALUE_USED)) {
564                         buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
565                     }
566                     buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
567                 }
568                 config_section_unlock(co);
569             }
570         }
571         appconfig_unlock(root);
572     }
573 }