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