10 #include "appconfig.h"
13 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
15 pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
17 // ----------------------------------------------------------------------------
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
26 avl avl; // the index - this has to be first!
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
36 struct config_value *next;
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
47 struct config_value *values;
48 avl_tree values_index;
52 pthread_rwlock_t rwlock;
53 } *config_root = NULL;
56 // ----------------------------------------------------------------------------
59 static int config_value_iterator(avl *a) { if(a) {}; return 0; }
61 static int config_value_compare(void* a, void* b) {
62 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
63 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
64 else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
67 #define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv))
68 #define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv))
70 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
71 struct config_value *result = NULL, tmp;
72 tmp.hash = (hash)?hash:simple_hash(name);
73 tmp.name = (char *)name;
75 avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
79 // ----------------------------------------------------------------------------
82 static int config_iterator(avl *a) { if(a) {}; return 0; }
84 static int config_compare(void* a, void* b) {
85 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
86 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
87 else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
90 avl_tree config_root_index = {
93 #ifdef AVL_LOCK_WITH_MUTEX
94 PTHREAD_MUTEX_INITIALIZER
96 PTHREAD_RWLOCK_INITIALIZER
100 #define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
101 #define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
103 static struct config *config_index_find(const char *name, uint32_t hash) {
104 struct config *result = NULL, tmp;
105 tmp.hash = (hash)?hash:simple_hash(name);
106 tmp.name = (char *)name;
108 avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
112 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
114 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
116 struct config_value *cv = calloc(1, sizeof(struct config_value));
117 if(!cv) fatal("Cannot allocate config_value");
119 cv->name = strdup(name);
120 if(!cv->name) fatal("Cannot allocate config.name");
121 cv->hash = simple_hash(cv->name);
123 cv->value = strdup(value);
124 if(!cv->value) fatal("Cannot allocate config.value");
126 config_value_index_add(co, cv);
128 // no need for string termination, due to calloc()
130 pthread_rwlock_wrlock(&co->rwlock);
132 struct config_value *cv2 = co->values;
134 while (cv2->next) cv2 = cv2->next;
137 else co->values = cv;
139 pthread_rwlock_unlock(&co->rwlock);
144 struct config *config_create(const char *section)
146 debug(D_CONFIG, "Creating section '%s'.", section);
148 struct config *co = calloc(1, sizeof(struct config));
149 if(!co) fatal("Cannot allocate config");
151 co->name = strdup(section);
152 if(!co->name) fatal("Cannot allocate config.name");
153 co->hash = simple_hash(co->name);
155 pthread_rwlock_init(&co->rwlock, NULL);
156 avl_init(&co->values_index, config_value_compare);
158 config_index_add(co);
160 // no need for string termination, due to calloc()
162 pthread_rwlock_wrlock(&config_rwlock);
164 struct config *co2 = config_root;
166 while (co2->next) co2 = co2->next;
169 else config_root = co;
171 pthread_rwlock_unlock(&config_rwlock);
176 struct config *config_find_section(const char *section)
178 return config_index_find(section, 0);
181 int load_config(char *filename, int overwrite_used)
184 struct config *co = NULL;
186 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
188 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
189 FILE *fp = fopen(filename, "r");
191 error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME);
195 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
196 buffer[CONFIG_FILE_LINE_MAX] = '\0';
201 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
206 if(*s == '[' && s[len - 1] == ']') {
211 co = config_find_section(s);
212 if(!co) co = config_create(s);
218 // line outside a section
219 error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
224 char *value = strchr(s, '=');
226 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
236 error("Ignoring line %d, name is empty.", line);
240 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
244 struct config_value *cv = config_value_index_find(co, name, 0);
246 if(!cv) cv = config_value_create(co, name, value);
248 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
249 debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name);
251 cv->value = strdup(value);
252 if(!cv->value) fatal("Cannot allocate config.value");
255 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
257 cv->flags |= CONFIG_VALUE_LOADED;
265 char *config_get(const char *section, const char *name, const char *default_value)
267 struct config_value *cv;
269 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
271 struct config *co = config_find_section(section);
272 if(!co) co = config_create(section);
274 cv = config_value_index_find(co, name, 0);
276 cv = config_value_create(co, name, default_value);
279 cv->flags |= CONFIG_VALUE_USED;
281 if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
282 // this is a loaded value from the config file
283 // if it is different that the default, mark it
284 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
285 if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
286 cv->flags |= CONFIG_VALUE_CHECKED;
293 long long config_get_number(const char *section, const char *name, long long value)
295 char buffer[100], *s;
296 sprintf(buffer, "%lld", value);
298 s = config_get(section, name, buffer);
301 return strtoll(s, NULL, 0);
304 int config_get_boolean(const char *section, const char *name, int value)
310 s = config_get(section, name, s);
313 if(!strcmp(s, "yes")) return 1;
317 int config_get_boolean_ondemand(const char *section, const char *name, int value)
321 if(value == CONFIG_ONDEMAND_ONDEMAND)
324 else if(value == CONFIG_ONDEMAND_NO)
330 s = config_get(section, name, s);
333 if(!strcmp(s, "yes"))
334 return CONFIG_ONDEMAND_YES;
335 else if(!strcmp(s, "no"))
336 return CONFIG_ONDEMAND_NO;
337 else if(!strcmp(s, "on demand"))
338 return CONFIG_ONDEMAND_ONDEMAND;
343 const char *config_set(const char *section, const char *name, const char *value)
345 struct config_value *cv;
347 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
349 struct config *co = config_find_section(section);
350 if(!co) co = config_create(section);
352 cv = config_value_index_find(co, name, 0);
353 if(!cv) cv = config_value_create(co, name, value);
354 cv->flags |= CONFIG_VALUE_USED;
356 if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
359 cv->value = strdup(value);
360 if(!cv->value) fatal("Cannot allocate config.value");
365 long long config_set_number(const char *section, const char *name, long long value)
368 sprintf(buffer, "%lld", value);
370 config_set(section, name, buffer);
375 int config_set_boolean(const char *section, const char *name, int value)
381 config_set(section, name, s);
386 void generate_config(BUFFER *wb, int only_changed)
390 struct config_value *cv;
392 for(i = 0; i < 3 ;i++) {
396 "# NetData Configuration\n"
397 "# You can uncomment and change any of the options below.\n"
398 "# The value shown in the commented settings, is the default value.\n"
399 "\n# global netdata configuration\n");
403 buffer_strcat(wb, "\n\n# per plugin configuration\n");
407 buffer_strcat(wb, "\n\n# per chart configuration\n");
411 pthread_rwlock_wrlock(&config_rwlock);
412 for(co = config_root; co ; co = co->next) {
413 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0;
414 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
422 pthread_rwlock_wrlock(&co->rwlock);
424 for(cv = co->values; cv ; cv = cv->next) {
425 used += (cv->flags && CONFIG_VALUE_USED)?1:0;
426 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
430 pthread_rwlock_unlock(&co->rwlock);
433 if(only_changed && !changed) continue;
436 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
439 buffer_sprintf(wb, "\n[%s]\n", co->name);
441 pthread_rwlock_wrlock(&co->rwlock);
442 for(cv = co->values; cv ; cv = cv->next) {
444 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
445 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
447 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
449 pthread_rwlock_unlock(&co->rwlock);
452 pthread_rwlock_unlock(&config_rwlock);