3 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
5 pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER;
7 // ----------------------------------------------------------------------------
10 #define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config
11 #define CONFIG_VALUE_USED 0x02 // has been accessed from the program
12 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
13 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
16 avl avl; // the index - this has to be first!
19 uint32_t hash; // a simple hash to speed up searching
20 // we first compare hashes, and only if the hashes are equal we do string comparisons
25 struct config_value *next; // config->mutex protects just this
31 uint32_t hash; // a simple hash to speed up searching
32 // we first compare hashes, and only if the hashes are equal we do string comparisons
36 struct config *next; // gloabl config_mutex protects just this
38 struct config_value *values;
39 avl_tree_lock values_index;
41 pthread_mutex_t mutex; // this locks only the writers, to ensure atomic updates
42 // readers are protected using the rwlock in avl_tree_lock
43 } *config_root = NULL;
46 // ----------------------------------------------------------------------------
49 static inline void config_global_write_lock(void) {
50 pthread_mutex_lock(&config_mutex);
53 static inline void config_global_unlock(void) {
54 pthread_mutex_unlock(&config_mutex);
57 static inline void config_section_write_lock(struct config *co) {
58 pthread_mutex_lock(&co->mutex);
61 static inline void config_section_unlock(struct config *co) {
62 pthread_mutex_unlock(&co->mutex);
66 // ----------------------------------------------------------------------------
67 // config name-value index
69 static int config_value_compare(void* a, void* b) {
70 if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1;
71 else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1;
72 else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name);
75 #define config_value_index_add(co, cv) avl_insert_lock(&((co)->values_index), (avl *)(cv))
76 #define config_value_index_del(co, cv) avl_remove_lock(&((co)->values_index), (avl *)(cv))
78 static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
79 struct config_value tmp;
80 tmp.hash = (hash)?hash:simple_hash(name);
81 tmp.name = (char *)name;
83 return (struct config_value *)avl_search_lock(&(co->values_index), (avl *) &tmp);
87 // ----------------------------------------------------------------------------
88 // config sections index
90 static int config_compare(void* a, void* b) {
91 if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1;
92 else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1;
93 else return strcmp(((struct config *)a)->name, ((struct config *)b)->name);
96 avl_tree_lock config_root_index = {
97 { NULL, config_compare },
101 #define config_index_add(cfg) avl_insert_lock(&config_root_index, (avl *)(cfg))
102 #define config_index_del(cfg) avl_remove_lock(&config_root_index, (avl *)(cfg))
104 static struct config *config_index_find(const char *name, uint32_t hash) {
106 tmp.hash = (hash)?hash:simple_hash(name);
107 tmp.name = (char *)name;
109 return (struct config *)avl_search_lock(&config_root_index, (avl *) &tmp);
113 // ----------------------------------------------------------------------------
114 // config section methods
116 static inline struct config *config_section_find(const char *section) {
117 return config_index_find(section, 0);
120 static inline struct config *config_section_create(const char *section)
122 debug(D_CONFIG, "Creating section '%s'.", section);
124 struct config *co = calloc(1, sizeof(struct config));
125 if(!co) fatal("Cannot allocate config");
127 co->name = strdup(section);
128 if(!co->name) fatal("Cannot allocate config.name");
129 co->hash = simple_hash(co->name);
131 avl_init_lock(&co->values_index, config_value_compare);
133 config_index_add(co);
135 config_global_write_lock();
136 struct config *co2 = config_root;
138 while (co2->next) co2 = co2->next;
141 else config_root = co;
142 config_global_unlock();
148 // ----------------------------------------------------------------------------
149 // config name-value methods
151 static inline struct config_value *config_value_create(struct config *co, const char *name, const char *value)
153 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
155 struct config_value *cv = calloc(1, sizeof(struct config_value));
156 if(!cv) fatal("Cannot allocate config_value");
158 cv->name = strdup(name);
159 if(!cv->name) fatal("Cannot allocate config.name");
160 cv->hash = simple_hash(cv->name);
162 cv->value = strdup(value);
163 if(!cv->value) fatal("Cannot allocate config.value");
165 config_value_index_add(co, cv);
167 config_section_write_lock(co);
168 struct config_value *cv2 = co->values;
170 while (cv2->next) cv2 = cv2->next;
173 else co->values = cv;
174 config_section_unlock(co);
179 int config_exists(const char *section, const char *name) {
180 struct config_value *cv;
182 debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
184 struct config *co = config_section_find(section);
187 cv = config_value_index_find(co, name, 0);
193 int config_rename(const char *section, const char *old, const char *new) {
194 struct config_value *cv, *cv2;
196 debug(D_CONFIG, "request to rename config in section '%s', old name '%s', new name '%s'", section, old, new);
198 struct config *co = config_section_find(section);
201 config_section_write_lock(co);
203 cv = config_value_index_find(co, old, 0);
204 if(!cv) goto cleanup;
206 cv2 = config_value_index_find(co, new, 0);
207 if(cv2) goto cleanup;
209 config_value_index_del(co, cv);
212 cv->name = strdup(new);
213 if(!cv->name) fatal("Cannot allocate memory for config_rename()");
215 cv->hash = simple_hash(cv->name);
217 config_value_index_add(co, cv);
218 config_section_unlock(co);
223 config_section_unlock(co);
227 char *config_get(const char *section, const char *name, const char *default_value)
229 struct config_value *cv;
231 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
233 struct config *co = config_section_find(section);
234 if(!co) co = config_section_create(section);
236 cv = config_value_index_find(co, name, 0);
238 cv = config_value_create(co, name, default_value);
241 cv->flags |= CONFIG_VALUE_USED;
243 if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) {
244 // this is a loaded value from the config file
245 // if it is different that the default, mark it
246 if(!(cv->flags & CONFIG_VALUE_CHECKED)) {
247 if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED;
248 cv->flags |= CONFIG_VALUE_CHECKED;
255 long long config_get_number(const char *section, const char *name, long long value)
257 char buffer[100], *s;
258 sprintf(buffer, "%lld", value);
260 s = config_get(section, name, buffer);
263 return strtoll(s, NULL, 0);
266 int config_get_boolean(const char *section, const char *name, int value)
272 s = config_get(section, name, s);
275 if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
279 int config_get_boolean_ondemand(const char *section, const char *name, int value)
283 if(value == CONFIG_ONDEMAND_ONDEMAND)
286 else if(value == CONFIG_ONDEMAND_NO)
292 s = config_get(section, name, s);
295 if(!strcmp(s, "yes"))
296 return CONFIG_ONDEMAND_YES;
297 else if(!strcmp(s, "no"))
298 return CONFIG_ONDEMAND_NO;
299 else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
300 return CONFIG_ONDEMAND_ONDEMAND;
305 const char *config_set_default(const char *section, const char *name, const char *value)
307 struct config_value *cv;
309 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
311 struct config *co = config_section_find(section);
312 if(!co) return config_set(section, name, value);
314 cv = config_value_index_find(co, name, 0);
315 if(!cv) return config_set(section, name, value);
317 cv->flags |= CONFIG_VALUE_USED;
319 if(cv->flags & CONFIG_VALUE_LOADED)
322 if(strcmp(cv->value, value) != 0) {
323 cv->flags |= CONFIG_VALUE_CHANGED;
326 cv->value = strdup(value);
327 if(!cv->value) fatal("Cannot allocate config.value");
333 const char *config_set(const char *section, const char *name, const char *value)
335 struct config_value *cv;
337 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
339 struct config *co = config_section_find(section);
340 if(!co) co = config_section_create(section);
342 cv = config_value_index_find(co, name, 0);
343 if(!cv) cv = config_value_create(co, name, value);
344 cv->flags |= CONFIG_VALUE_USED;
346 if(strcmp(cv->value, value) != 0) {
347 cv->flags |= CONFIG_VALUE_CHANGED;
350 cv->value = strdup(value);
351 if(!cv->value) fatal("Cannot allocate config.value");
357 long long config_set_number(const char *section, const char *name, long long value)
360 sprintf(buffer, "%lld", value);
362 config_set(section, name, buffer);
367 int config_set_boolean(const char *section, const char *name, int value)
373 config_set(section, name, s);
379 // ----------------------------------------------------------------------------
382 int load_config(char *filename, int overwrite_used)
385 struct config *co = NULL;
387 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
389 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
390 FILE *fp = fopen(filename, "r");
392 error("Cannot open file '%s'", filename);
396 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
397 buffer[CONFIG_FILE_LINE_MAX] = '\0';
402 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
406 int len = (int) strlen(s);
407 if(*s == '[' && s[len - 1] == ']') {
412 co = config_section_find(s);
413 if(!co) co = config_section_create(s);
419 // line outside a section
420 error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
425 char *value = strchr(s, '=');
427 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
437 error("Ignoring line %d, name is empty.", line);
441 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
445 struct config_value *cv = config_value_index_find(co, name, 0);
447 if(!cv) cv = config_value_create(co, name, value);
449 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
450 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
452 cv->value = strdup(value);
453 if(!cv->value) fatal("Cannot allocate config.value");
456 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
458 cv->flags |= CONFIG_VALUE_LOADED;
466 void generate_config(BUFFER *wb, int only_changed)
470 struct config_value *cv;
472 for(i = 0; i < 3 ;i++) {
476 "# NetData Configuration\n"
477 "# You can uncomment and change any of the options below.\n"
478 "# The value shown in the commented settings, is the default value.\n"
479 "\n# global netdata configuration\n");
483 buffer_strcat(wb, "\n\n# per plugin configuration\n");
487 buffer_strcat(wb, "\n\n# per chart configuration\n");
491 config_global_write_lock();
492 for(co = config_root; co ; co = co->next) {
493 if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0 || strcmp(co->name, "registry") == 0) pri = 0;
494 else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1;
502 config_section_write_lock(co);
503 for(cv = co->values; cv ; cv = cv->next) {
504 used += (cv->flags & CONFIG_VALUE_USED)?1:0;
505 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
508 config_section_unlock(co);
511 if(only_changed && !changed) continue;
514 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
517 buffer_sprintf(wb, "\n[%s]\n", co->name);
519 config_section_write_lock(co);
520 for(cv = co->values; cv ; cv = cv->next) {
522 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
523 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
525 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
527 config_section_unlock(co);
530 config_global_unlock();