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;
51 } *config_root = NULL;
54 // ----------------------------------------------------------------------------
57 static int config_value_iterator(avl *a) { if(a) {}; return 0; }
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);
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))
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;
73 avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result);
77 // ----------------------------------------------------------------------------
80 static int config_iterator(avl *a) { if(a) {}; return 0; }
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);
88 avl_tree config_root_index = {
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))
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;
101 avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result);
105 struct config_value *config_value_create(struct config *co, const char *name, const char *value)
107 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
109 struct config_value *cv = calloc(1, sizeof(struct config_value));
110 if(!cv) fatal("Cannot allocate config_value");
112 cv->name = strdup(name);
113 if(!cv->name) fatal("Cannot allocate config.name");
114 cv->hash = simple_hash(cv->name);
116 cv->value = strdup(value);
117 if(!cv->value) fatal("Cannot allocate config.value");
119 config_value_index_add(co, cv);
121 // no need for string termination, due to calloc()
123 struct config_value *cv2 = co->values;
125 while (cv2->next) cv2 = cv2->next;
128 else co->values = cv;
133 struct config *config_create(const char *section)
135 debug(D_CONFIG, "Creating section '%s'.", section);
137 struct config *co = calloc(1, sizeof(struct config));
138 if(!co) fatal("Cannot allocate config");
140 co->name = strdup(section);
141 if(!co->name) fatal("Cannot allocate config.name");
142 co->hash = simple_hash(co->name);
144 co->values_index.compar = config_value_compare;
146 config_index_add(co);
148 // no need for string termination, due to calloc()
150 struct config *co2 = config_root;
152 while (co2->next) co2 = co2->next;
155 else config_root = co;
160 struct config *config_find_section(const char *section)
162 return config_index_find(section, 0);
165 int load_config(char *filename, int overwrite_used)
168 struct config *co = NULL;
170 pthread_rwlock_wrlock(&config_rwlock);
172 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
174 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
175 FILE *fp = fopen(filename, "r");
177 error("Cannot open file '%s'", CONFIG_DIR "/" CONFIG_FILENAME);
178 pthread_rwlock_unlock(&config_rwlock);
182 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
183 buffer[CONFIG_FILE_LINE_MAX] = '\0';
188 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
193 if(*s == '[' && s[len - 1] == ']') {
198 co = config_find_section(s);
199 if(!co) co = config_create(s);
205 // line outside a section
206 error("Ignoring line %d ('%s'), it is outsize all sections.", line, s);
211 char *value = strchr(s, '=');
213 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
223 error("Ignoring line %d, name is empty.", line);
227 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
231 struct config_value *cv = config_value_index_find(co, name, 0);
233 if(!cv) cv = config_value_create(co, name, value);
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);
238 cv->value = strdup(value);
239 if(!cv->value) fatal("Cannot allocate config.value");
242 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
244 cv->flags |= CONFIG_VALUE_LOADED;
249 pthread_rwlock_unlock(&config_rwlock);
253 char *config_get(const char *section, const char *name, const char *default_value)
255 struct config_value *cv;
257 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
259 pthread_rwlock_rdlock(&config_rwlock);
261 struct config *co = config_find_section(section);
262 if(!co) co = config_create(section);
264 cv = config_value_index_find(co, name, 0);
266 cv = config_value_create(co, name, default_value);
269 cv->flags |= CONFIG_VALUE_USED;
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;
280 pthread_rwlock_unlock(&config_rwlock);
284 long long config_get_number(const char *section, const char *name, long long value)
286 char buffer[100], *s;
287 sprintf(buffer, "%lld", value);
289 s = config_get(section, name, buffer);
292 return strtoll(s, NULL, 0);
295 int config_get_boolean(const char *section, const char *name, int value)
301 s = config_get(section, name, s);
304 if(!strcmp(s, "yes")) return 1;
308 const char *config_set(const char *section, const char *name, const char *value)
310 struct config_value *cv;
312 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
314 pthread_rwlock_wrlock(&config_rwlock);
316 struct config *co = config_find_section(section);
317 if(!co) co = config_create(section);
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;
323 if(strcmp(cv->value, value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
326 cv->value = strdup(value);
327 if(!cv->value) fatal("Cannot allocate config.value");
329 pthread_rwlock_unlock(&config_rwlock);
334 long long config_set_number(const char *section, const char *name, long long value)
337 sprintf(buffer, "%lld", value);
339 config_set(section, name, buffer);
344 int config_set_boolean(const char *section, const char *name, int value)
350 config_set(section, name, s);
355 void generate_config(struct web_buffer *wb, int only_changed)
359 struct config_value *cv;
361 for(i = 0; i < 3 ;i++) {
362 web_buffer_increase(wb, 500);
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");
373 web_buffer_printf(wb, "\n\n# per plugin configuration\n");
377 web_buffer_printf(wb, "\n\n# per chart configuration\n");
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;
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;
397 if(only_changed && !changed) continue;
400 web_buffer_increase(wb, 500);
401 web_buffer_printf(wb, "\n# node '%s' is not used.", co->name);
404 web_buffer_increase(wb, CONFIG_FILE_LINE_MAX+1);
405 web_buffer_printf(wb, "\n[%s]\n", co->name);
407 for(cv = co->values; cv ; cv = cv->next) {
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);
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);