]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
dashboard does not show past CLEAR notifications; fixes #1035
[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
380     debug(D_CONFIG, "Opening config file '%s'", filename);
381
382     FILE *fp = fopen(filename, "r");
383     if(!fp) {
384         error("Cannot open file '%s'", filename);
385         return 0;
386     }
387
388     while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
389         buffer[CONFIG_FILE_LINE_MAX] = '\0';
390         line++;
391
392         s = trim(buffer);
393         if(!s) {
394             debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
395             continue;
396         }
397
398         int len = (int) strlen(s);
399         if(*s == '[' && s[len - 1] == ']') {
400             // new section
401             s[len - 1] = '\0';
402             s++;
403
404             co = config_section_find(s);
405             if(!co) co = config_section_create(s);
406
407             continue;
408         }
409
410         if(!co) {
411             // line outside a section
412             error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
413             continue;
414         }
415
416         char *name = s;
417         char *value = strchr(s, '=');
418         if(!value) {
419             error("Ignoring line %d ('%s'), there is no = in it.", line, s);
420             continue;
421         }
422         *value = '\0';
423         value++;
424
425         name = trim(name);
426         value = trim(value);
427
428         if(!name) {
429             error("Ignoring line %d, name is empty.", line);
430             continue;
431         }
432         if(!value) {
433             debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
434             continue;
435         }
436
437         struct config_value *cv = config_value_index_find(co, name, 0);
438
439         if(!cv) cv = config_value_create(co, name, value);
440         else {
441             if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
442                 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
443                 freez(cv->value);
444                 cv->value = strdupz(value);
445             }
446             else
447                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
448         }
449         cv->flags |= CONFIG_VALUE_LOADED;
450     }
451
452     fclose(fp);
453
454     return 1;
455 }
456
457 void generate_config(BUFFER *wb, int only_changed)
458 {
459     int i, pri;
460     struct config *co;
461     struct config_value *cv;
462
463     for(i = 0; i < 3 ;i++) {
464         switch(i) {
465             case 0:
466                 buffer_strcat(wb,
467                     "# netdata configuration\n"
468                     "#\n"
469                     "# You can download the latest version of this file, using:\n"
470                     "#\n"
471                     "#  wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
472                     "# or\n"
473                     "#  curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
474                     "#\n"
475                     "# You can uncomment and change any of the options below.\n"
476                     "# The value shown in the commented settings, is the default value.\n"
477                     "#\n"
478                     "\n# global netdata configuration\n");
479                 break;
480
481             case 1:
482                 buffer_strcat(wb, "\n\n# per plugin configuration\n");
483                 break;
484
485             case 2:
486                 buffer_strcat(wb, "\n\n# per chart configuration\n");
487                 break;
488         }
489
490         config_global_write_lock();
491         for(co = config_root; co ; co = co->next) {
492             if(!strcmp(co->name, "global") ||
493                     !strcmp(co->name, "plugins") ||
494                     !strcmp(co->name, "registry") ||
495                     !strcmp(co->name, "health"))
496                 pri = 0;
497             else if(!strncmp(co->name, "plugin:", 7)) pri = 1;
498             else pri = 2;
499
500             if(i == pri) {
501                 int used = 0;
502                 int changed = 0;
503                 int count = 0;
504
505                 config_section_write_lock(co);
506                 for(cv = co->values; cv ; cv = cv->next) {
507                     used += (cv->flags & CONFIG_VALUE_USED)?1:0;
508                     changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
509                     count++;
510                 }
511                 config_section_unlock(co);
512
513                 if(!count) continue;
514                 if(only_changed && !changed) continue;
515
516                 if(!used) {
517                     buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
518                 }
519
520                 buffer_sprintf(wb, "\n[%s]\n", co->name);
521
522                 config_section_write_lock(co);
523                 for(cv = co->values; cv ; cv = cv->next) {
524
525                     if(used && !(cv->flags & CONFIG_VALUE_USED)) {
526                         buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
527                     }
528                     buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
529                 }
530                 config_section_unlock(co);
531             }
532         }
533         config_global_unlock();
534     }
535 }