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