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