]> arthur.barton.de Git - netdata.git/blob - src/appconfig.c
build: migrate to autotools
[netdata.git] / src / appconfig.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <pthread.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "avl.h"
9 #include "common.h"
10 #include "appconfig.h"
11 #include "log.h"
12
13 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
14
15 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
16
17 // ----------------------------------------------------------------------------
18 // definitions
19
20 #define CONFIG_VALUE_LOADED  0x01 // has been loaded from the config
21 #define CONFIG_VALUE_USED    0x02 // has been accessed from the program
22 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
23 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
24
25 struct config_value {
26         avl avl;                                // the index - this has to be first!
27
28         uint32_t hash;                  // a simple hash to speed up searching
29                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
30
31         char *name;
32         char *value;
33
34         uint8_t flags;
35
36         struct config_value *next;
37 };
38
39 struct config {
40         avl avl;
41
42         uint32_t hash;                  // a simple hash to speed up searching
43                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
44
45         char *name;
46
47         struct config_value *values;
48         avl_tree values_index;
49
50         struct config *next;
51 } *config_root = NULL;
52
53
54 // ----------------------------------------------------------------------------
55 // config value
56
57 static int config_value_iterator(avl *a) { if(a) {}; return 0; }
58
59 static int config_value_compare(void* a, void* b) {
60         if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
61         else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
62         else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
63 }
64
65 #define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv))
66 #define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv))
67
68 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
69         struct config_value *result = NULL, tmp;
70         tmp.hash = (hash)?hash:simple_hash(name);
71         tmp.name = (char *)name;
72
73         avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
74         return result;
75 }
76
77 // ----------------------------------------------------------------------------
78 // config
79
80 static int config_iterator(avl *a) { if(a) {}; return 0; }
81
82 static int config_compare(void* a, void* b) {
83         if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
84         else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
85         else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
86 }
87
88 avl_tree config_root_index = {
89                 NULL,
90                 config_compare
91 };
92
93 #define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
94 #define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
95
96 static struct config *config_index_find(const char *name, uint32_t hash) {
97         struct config *result = NULL, tmp;
98         tmp.hash = (hash)?hash:simple_hash(name);
99         tmp.name = (char *)name;
100
101         avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
102         return result;
103 }
104
105 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
106 {
107         debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
108
109         struct config_value *cv = calloc(1, sizeof(struct config_value));
110         if(!cv) fatal("Cannot allocate config_value");
111
112         cv->name = strdup(name);
113         if(!cv->name) fatal("Cannot allocate config.name");
114         cv->hash = simple_hash(cv->name);
115
116         cv->value = strdup(value);
117         if(!cv->value) fatal("Cannot allocate config.value");
118
119         config_value_index_add(co, cv);
120
121         // no need for string termination, due to calloc()
122
123         struct config_value *cv2 = co->values;
124         if(cv2) {
125                 while (cv2->next) cv2 = cv2->next;
126                 cv2->next = cv;
127         }
128         else co->values = cv;
129
130         return cv;
131 }
132
133 struct config *config_create(const char *section)
134 {
135         debug(D_CONFIG, "Creating section '%s'.", section);
136
137         struct config *co = calloc(1, sizeof(struct config));
138         if(!co) fatal("Cannot allocate config");
139
140         co->name = strdup(section);
141         if(!co->name) fatal("Cannot allocate config.name");
142         co->hash = simple_hash(co->name);
143
144         co->values_index.compar = config_value_compare;
145
146         config_index_add(co);
147
148         // no need for string termination, due to calloc()
149
150         struct config *co2 = config_root;
151         if(co2) {
152                 while (co2->next) co2 = co2->next;
153                 co2->next = co;
154         }
155         else config_root = co;
156
157         return co;
158 }
159
160 struct config *config_find_section(const char *section)
161 {
162         return config_index_find(section, 0);
163 }
164
165 int load_config(char *filename, int overwrite_used)
166 {
167         int line = 0;
168         struct config *co = NULL;
169
170         pthread_rwlock_wrlock(&config_rwlock);
171
172         char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
173
174         if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
175         FILE *fp = fopen(filename, "r");
176         if(!fp) {
177                 error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME);
178                 pthread_rwlock_unlock(&config_rwlock);
179                 return 0;
180         }
181
182         while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
183                 buffer[CONFIG_FILE_LINE_MAX] = '\0';
184                 line++;
185
186                 s = trim(buffer);
187                 if(!s) {
188                         debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
189                         continue;
190                 }
191
192                 int len = strlen(s);
193                 if(*s == '[' && s[len - 1] == ']') {
194                         // new section
195                         s[len - 1] = '\0';
196                         s++;
197
198                         co = config_find_section(s);
199                         if(!co) co = config_create(s);
200
201                         continue;
202                 }
203
204                 if(!co) {
205                         // line outside a section
206                         error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
207                         continue;
208                 }
209
210                 char *name = s;
211                 char *value = strchr(s, '=');
212                 if(!value) {
213                         error("Ignoring line %d ('%s'), there is no = in it.", line, s);
214                         continue;
215                 }
216                 *value = '\0';
217                 value++;
218
219                 name = trim(name);
220                 value = trim(value);
221
222                 if(!name) {
223                         error("Ignoring line %d, name is empty.", line);
224                         continue;
225                 }
226                 if(!value) {
227                         debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
228                         continue;
229                 }
230
231                 struct config_value *cv = config_value_index_find(co, name, 0);
232
233                 if(!cv) cv = config_value_create(co, name, value);
234                 else {
235                         if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
236                                 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
237                                 free(cv->value);
238                                 cv->value = strdup(value);
239                                 if(!cv->value) fatal("Cannot allocate config.value");
240                         }
241                         else
242                                 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
243                 }
244                 cv->flags |= CONFIG_VALUE_LOADED;
245         }
246
247         fclose(fp);
248
249         pthread_rwlock_unlock(&config_rwlock);
250         return 1;
251 }
252
253 char *config_get(const char *section, const char *name, const char *default_value)
254 {
255         struct config_value *cv;
256
257         debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
258
259         pthread_rwlock_rdlock(&config_rwlock);
260
261         struct config *co = config_find_section(section);
262         if(!co) co = config_create(section);
263
264         cv = config_value_index_find(co, name, 0);
265         if(!cv) {
266                 cv = config_value_create(co, name, default_value);
267                 if(!cv) return NULL;
268         }
269         cv->flags |= CONFIG_VALUE_USED;
270
271         if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
272                 // this is a loaded value from the config file
273                 // if it is different that the default, mark it
274                 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
275                         if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
276                         cv->flags |= CONFIG_VALUE_CHECKED;
277                 }
278         }
279
280         pthread_rwlock_unlock(&config_rwlock);
281         return(cv->value);
282 }
283
284 long long config_get_number(const char *section, const char *name, long long value)
285 {
286         char buffer[100], *s;
287         sprintf(buffer, "%lld", value);
288
289         s = config_get(section, name, buffer);
290         if(!s) return 0;
291
292         return strtoll(s, NULL, 0);
293 }
294
295 int config_get_boolean(const char *section, const char *name, int value)
296 {
297         char *s;
298         if(value) s = "yes";
299         else s = "no";
300
301         s = config_get(section, name, s);
302         if(!s) return 0;
303
304         if(!strcmp(s, "yes")) return 1;
305         else return 0;
306 }
307
308 const char *config_set(const char *section, const char *name, const char *value)
309 {
310         struct config_value *cv;
311
312         debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
313
314         pthread_rwlock_wrlock(&config_rwlock);
315
316         struct config *co = config_find_section(section);
317         if(!co) co = config_create(section);
318
319         cv = config_value_index_find(co, name, 0);
320         if(!cv) cv = config_value_create(co, name, value);
321         cv->flags |= CONFIG_VALUE_USED;
322
323         if(strcmp(cv->value, value) != 0) 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         pthread_rwlock_unlock(&config_rwlock);
330
331         return value;
332 }
333
334 long long config_set_number(const char *section, const char *name, long long value)
335 {
336         char buffer[100];
337         sprintf(buffer, "%lld", value);
338
339         config_set(section, name, buffer);
340
341         return value;
342 }
343
344 int config_set_boolean(const char *section, const char *name, int value)
345 {
346         char *s;
347         if(value) s = "yes";
348         else s = "no";
349
350         config_set(section, name, s);
351
352         return value;
353 }
354
355 void generate_config(struct web_buffer *wb, int only_changed)
356 {
357         int i, pri;
358         struct config *co;
359         struct config_value *cv;
360
361         for(i = 0; i < 3 ;i++) {
362                 web_buffer_increase(wb, 500);
363                 switch(i) {
364                         case 0:
365                                 web_buffer_printf(wb, 
366                                         "# NetData Configuration\n"
367                                         "# You can uncomment and change any of the options below.\n"
368                                         "# The value shown in the commented settings, is the default value.\n"
369                                         "\n# global netdata configuration\n");
370                                 break;
371
372                         case 1:
373                                 web_buffer_printf(wb, "\n\n# per plugin configuration\n");
374                                 break;
375
376                         case 2:
377                                 web_buffer_printf(wb, "\n\n# per chart configuration\n");
378                                 break;
379                 }
380
381                 for(co = config_root; co ; co = co->next) {
382                         if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
383                         else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
384                         else pri = 2;
385
386                         if(i == pri) {
387                                 int used = 0;
388                                 int changed = 0;
389                                 int count = 0;
390                                 for(cv = co->values; cv ; cv = cv->next) {
391                                         used += (cv->flags && CONFIG_VALUE_USED)?1:0;
392                                         changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
393                                         count++;
394                                 }
395
396                                 if(!count) continue;
397                                 if(only_changed && !changed) continue;
398
399                                 if(!used) {
400                                         web_buffer_increase(wb, 500);
401                                         web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
402                                 }
403
404                                 web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1);
405                                 web_buffer_printf(wb, "\n[%s]\n", co->name);
406
407                                 for(cv = co->values; cv ; cv = cv->next) {
408
409                                         if(used && !(cv->flags & CONFIG_VALUE_USED)) {
410                                                 web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
411                                                 web_buffer_printf(wb, "\n\t# option '%s' is not used.\n", cv->name);
412                                         }
413                                         web_buffer_increase(wb, CONFIG_FILE_LINE_MAX + 1);
414                                         web_buffer_printf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
415                                 }
416                         }
417                 }
418         }
419 }
420