]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
Merge remote-tracking branch 'upstream/master' into health
[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) avl_insert_lock(&((co)->values_index), (avl *)(cv))
76 #define config_value_index_del(co, cv) 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) avl_insert_lock(&config_root_index, (avl *)(cfg))
102 #define config_index_del(cfg) 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         config_index_add(co);
131
132         config_global_write_lock();
133         struct config *co2 = config_root;
134         if(co2) {
135                 while (co2->next) co2 = co2->next;
136                 co2->next = co;
137         }
138         else config_root = co;
139         config_global_unlock();
140
141         return co;
142 }
143
144
145 // ----------------------------------------------------------------------------
146 // config name-value methods
147
148 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
149 {
150         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
151
152         struct config_value *cv = callocz(1, sizeof(struct config_value));
153         cv->name = strdupz(name);
154         cv->hash = simple_hash(cv->name);
155         cv->value = strdupz(value);
156
157         config_value_index_add(co, cv);
158
159         config_section_write_lock(co);
160         struct config_value *cv2 = co->values;
161         if(cv2) {
162                 while (cv2->next) cv2 = cv2->next;
163                 cv2->next = cv;
164         }
165         else co->values = cv;
166         config_section_unlock(co);
167
168         return cv;
169 }
170
171 int config_exists(const char *section, const char *name) {
172         struct config_value *cv;
173
174         debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
175
176         struct config *co = config_section_find(section);
177         if(!co) return 0;
178
179         cv = config_value_index_find(co, name, 0);
180         if(!cv) return 0;
181
182         return 1;
183 }
184
185 int config_rename(const char *section, const char *old, const char *new) {
186         struct config_value *cv, *cv2;
187
188         debug(D_CONFIG, "request to rename config in section '%s', old name '%s', new name '%s'", section, old, new);
189
190         struct config *co = config_section_find(section);
191         if(!co) return -1;
192
193         config_section_write_lock(co);
194
195         cv = config_value_index_find(co, old, 0);
196         if(!cv) goto cleanup;
197
198         cv2 = config_value_index_find(co, new, 0);
199         if(cv2) goto cleanup;
200
201         config_value_index_del(co, cv);
202
203         freez(cv->name);
204         cv->name = strdupz(new);
205
206         cv->hash = simple_hash(cv->name);
207
208         config_value_index_add(co, cv);
209         config_section_unlock(co);
210
211         return 0;
212
213 cleanup:
214         config_section_unlock(co);
215         return -1;
216 }
217
218 char *config_get(const char *section, const char *name, const char *default_value)
219 {
220         struct config_value *cv;
221
222         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
223
224         struct config *co = config_section_find(section);
225         if(!co) co = config_section_create(section);
226
227         cv = config_value_index_find(co, name, 0);
228         if(!cv) {
229                 cv = config_value_create(co, name, default_value);
230                 if(!cv) return NULL;
231         }
232         cv->flags |= CONFIG_VALUE_USED;
233
234         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
235                 // this is a loaded value from the config file
236                 // if it is different that the default, mark it
237                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
238                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
239                         cv->flags |= CONFIG_VALUE_CHECKED;
240                 }
241         }
242
243         return(cv->value);
244 }
245
246 long long config_get_number(const char *section, const char *name, long long value)
247 {
248         char buffer[100], *s;
249         sprintf(buffer, "%lld", value);
250
251         s = config_get(section, name, buffer);
252         if(!s) return value;
253
254         return strtoll(s, NULL, 0);
255 }
256
257 int config_get_boolean(const char *section, const char *name, int value)
258 {
259         char *s;
260         if(value) s = "yes";
261         else s = "no";
262
263         s = config_get(section, name, s);
264         if(!s) return value;
265
266         if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
267         return 0;
268 }
269
270 int config_get_boolean_ondemand(const char *section, const char *name, int value)
271 {
272         char *s;
273
274         if(value == CONFIG_ONDEMAND_ONDEMAND)
275                 s = "auto";
276
277         else if(value == CONFIG_ONDEMAND_NO)
278                 s = "no";
279
280         else
281                 s = "yes";
282
283         s = config_get(section, name, s);
284         if(!s) return value;
285
286         if(!strcmp(s, "yes"))
287                 return CONFIG_ONDEMAND_YES;
288         else if(!strcmp(s, "no"))
289                 return CONFIG_ONDEMAND_NO;
290         else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
291                 return CONFIG_ONDEMAND_ONDEMAND;
292
293         return value;
294 }
295
296 const char *config_set_default(const char *section, const char *name, const char *value)
297 {
298         struct config_value *cv;
299
300         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
301
302         struct config *co = config_section_find(section);
303         if(!co) return config_set(section, name, value);
304
305         cv = config_value_index_find(co, name, 0);
306         if(!cv) return config_set(section, name, value);
307
308         cv->flags |= CONFIG_VALUE_USED;
309
310         if(cv->flags & CONFIG_VALUE_LOADED)
311                 return cv->value;
312
313         if(strcmp(cv->value, value) != 0) {
314                 cv->flags |= CONFIG_VALUE_CHANGED;
315
316                 freez(cv->value);
317                 cv->value = strdupz(value);
318         }
319
320         return cv->value;
321 }
322
323 const char *config_set(const char *section, const char *name, const char *value)
324 {
325         struct config_value *cv;
326
327         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
328
329         struct config *co = config_section_find(section);
330         if(!co) co = config_section_create(section);
331
332         cv = config_value_index_find(co, name, 0);
333         if(!cv) cv = config_value_create(co, name, value);
334         cv->flags |= CONFIG_VALUE_USED;
335
336         if(strcmp(cv->value, value) != 0) {
337                 cv->flags |= CONFIG_VALUE_CHANGED;
338
339                 freez(cv->value);
340                 cv->value = strdupz(value);
341         }
342
343         return value;
344 }
345
346 long long config_set_number(const char *section, const char *name, long long value)
347 {
348         char buffer[100];
349         sprintf(buffer, "%lld", value);
350
351         config_set(section, name, buffer);
352
353         return value;
354 }
355
356 int config_set_boolean(const char *section, const char *name, int value)
357 {
358         char *s;
359         if(value) s = "yes";
360         else s = "no";
361
362         config_set(section, name, s);
363
364         return value;
365 }
366
367
368 // ----------------------------------------------------------------------------
369 // config load/save
370
371 int load_config(char *filename, int overwrite_used)
372 {
373         int line = 0;
374         struct config *co = NULL;
375
376         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
377
378         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
379         FILE *fp = fopen(filename, "r");
380         if(!fp) {
381                 error("Cannot open file '%s'", filename);
382                 return 0;
383         }
384
385         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
386                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
387                 line++;
388
389                 s = trim(buffer);
390                 if(!s) {
391                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
392                         continue;
393                 }
394
395                 int len = (int) strlen(s);
396                 if(*s == '[' && s[len - 1] == ']') {
397                         // new section
398                         s[len - 1] = '\0';
399                         s++;
400
401                         co = config_section_find(s);
402                         if(!co) co = config_section_create(s);
403
404                         continue;
405                 }
406
407                 if(!co) {
408                         // line outside a section
409                         error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
410                         continue;
411                 }
412
413                 char *name = s;
414                 char *value = strchr(s, '=');
415                 if(!value) {
416                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
417                         continue;
418                 }
419                 *value = '\0';
420                 value++;
421
422                 name = trim(name);
423                 value = trim(value);
424
425                 if(!name) {
426                         error("Ignoring line %d, name is empty.", line);
427                         continue;
428                 }
429                 if(!value) {
430                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
431                         continue;
432                 }
433
434                 struct config_value *cv = config_value_index_find(co, name, 0);
435
436                 if(!cv) cv = config_value_create(co, name, value);
437                 else {
438                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
439                                 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
440                                 freez(cv->value);
441                                 cv->value = strdupz(value);
442                         }
443                         else
444                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
445                 }
446                 cv->flags |= CONFIG_VALUE_LOADED;
447         }
448
449         fclose(fp);
450
451         return 1;
452 }
453
454 void generate_config(BUFFER *wb, int only_changed)
455 {
456         int i, pri;
457         struct config *co;
458         struct config_value *cv;
459
460         for(i = 0; i < 3 ;i++) {
461                 switch(i) {
462                         case 0:
463                                 buffer_strcat(wb,
464                                         "# NetData Configuration\n"
465                                         "# You can uncomment and change any of the options below.\n"
466                                         "# The value shown in the commented settings, is the default value.\n"
467                                         "\n# global netdata configuration\n");
468                                 break;
469
470                         case 1:
471                                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
472                                 break;
473
474                         case 2:
475                                 buffer_strcat(wb, "\n\n# per chart configuration\n");
476                                 break;
477                 }
478
479                 config_global_write_lock();
480                 for(co = config_root; co ; co = co->next) {
481                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
482                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
483                         else pri = 2;
484
485                         if(i == pri) {
486                                 int used = 0;
487                                 int changed = 0;
488                                 int count = 0;
489
490                                 config_section_write_lock(co);
491                                 for(cv = co->values; cv ; cv = cv->next) {
492                                         used += (cv->flags & CONFIG_VALUE_USED)?1:0;
493                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
494                                         count++;
495                                 }
496                                 config_section_unlock(co);
497
498                                 if(!count) continue;
499                                 if(only_changed && !changed) continue;
500
501                                 if(!used) {
502                                         buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
503                                 }
504
505                                 buffer_sprintf(wb, "\n[%s]\n", co->name);
506
507                                 config_section_write_lock(co);
508                                 for(cv = co->values; cv ; cv = cv->next) {
509
510                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
511                                                 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
512                                         }
513                                         buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
514                                 }
515                                 config_section_unlock(co);
516                         }
517                 }
518                 config_global_unlock();
519         }
520 }