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