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