3 #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
5 // ----------------------------------------------------------------------------
8 #define CONFIG_VALUE_LOADED 0x01 // has been loaded from the config
9 #define CONFIG_VALUE_USED 0x02 // has been accessed from the program
10 #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value
11 #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default
13 struct config_option {
14 avl avl; // the index - this has to be first!
17 uint32_t hash; // a simple hash to speed up searching
18 // we first compare hashes, and only if the hashes are equal we do string comparisons
23 struct config_option *next; // config->mutex protects just this
29 uint32_t hash; // a simple hash to speed up searching
30 // we first compare hashes, and only if the hashes are equal we do string comparisons
34 struct section *next; // gloabl config_mutex protects just this
36 struct config_option *values;
37 avl_tree_lock values_index;
39 pthread_mutex_t mutex; // this locks only the writers, to ensure atomic updates
40 // readers are protected using the rwlock in avl_tree_lock
43 static int appconfig_section_compare(void *a, void *b);
45 struct config netdata_config = {
47 .mutex = PTHREAD_MUTEX_INITIALIZER,
49 { NULL, appconfig_section_compare },
54 struct config stream_config = {
56 .mutex = PTHREAD_MUTEX_INITIALIZER,
58 { NULL, appconfig_section_compare },
63 // ----------------------------------------------------------------------------
66 static inline void appconfig_wrlock(struct config *root) {
67 pthread_mutex_lock(&root->mutex);
70 static inline void appconfig_unlock(struct config *root) {
71 pthread_mutex_unlock(&root->mutex);
74 static inline void config_section_wrlock(struct section *co) {
75 pthread_mutex_lock(&co->mutex);
78 static inline void config_section_unlock(struct section *co) {
79 pthread_mutex_unlock(&co->mutex);
83 // ----------------------------------------------------------------------------
84 // config name-value index
86 static int appconfig_option_compare(void *a, void *b) {
87 if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1;
88 else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1;
89 else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name);
92 #define appconfig_option_index_add(co, cv) (struct config_option *)avl_insert_lock(&((co)->values_index), (avl *)(cv))
93 #define appconfig_option_index_del(co, cv) (struct config_option *)avl_remove_lock(&((co)->values_index), (avl *)(cv))
95 static struct config_option *appconfig_option_index_find(struct section *co, const char *name, uint32_t hash) {
96 struct config_option tmp;
97 tmp.hash = (hash)?hash:simple_hash(name);
98 tmp.name = (char *)name;
100 return (struct config_option *)avl_search_lock(&(co->values_index), (avl *) &tmp);
104 // ----------------------------------------------------------------------------
105 // config sections index
107 static int appconfig_section_compare(void *a, void *b) {
108 if(((struct section *)a)->hash < ((struct section *)b)->hash) return -1;
109 else if(((struct section *)a)->hash > ((struct section *)b)->hash) return 1;
110 else return strcmp(((struct section *)a)->name, ((struct section *)b)->name);
113 #define appconfig_index_add(root, cfg) (struct section *)avl_insert_lock(&root->index, (avl *)(cfg))
114 #define appconfig_index_del(root, cfg) (struct section *)avl_remove_lock(&root->index, (avl *)(cfg))
116 static struct section *appconfig_index_find(struct config *root, const char *name, uint32_t hash) {
118 tmp.hash = (hash)?hash:simple_hash(name);
119 tmp.name = (char *)name;
121 return (struct section *)avl_search_lock(&root->index, (avl *) &tmp);
125 // ----------------------------------------------------------------------------
126 // config section methods
128 static inline struct section *appconfig_section_find(struct config *root, const char *section) {
129 return appconfig_index_find(root, section, 0);
132 static inline struct section *appconfig_section_create(struct config *root, const char *section) {
133 debug(D_CONFIG, "Creating section '%s'.", section);
135 struct section *co = callocz(1, sizeof(struct section));
136 co->name = strdupz(section);
137 co->hash = simple_hash(co->name);
139 avl_init_lock(&co->values_index, appconfig_option_compare);
141 if(unlikely(appconfig_index_add(root, co) != co))
142 error("INTERNAL ERROR: indexing of section '%s', already exists.", co->name);
144 appconfig_wrlock(root);
145 struct section *co2 = root->sections;
147 while (co2->next) co2 = co2->next;
150 else root->sections = co;
151 appconfig_unlock(root);
157 // ----------------------------------------------------------------------------
158 // config name-value methods
160 static inline struct config_option *appconfig_value_create(struct section *co, const char *name, const char *value)
162 debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name);
164 struct config_option *cv = callocz(1, sizeof(struct config_option));
165 cv->name = strdupz(name);
166 cv->hash = simple_hash(cv->name);
167 cv->value = strdupz(value);
169 if(unlikely(appconfig_option_index_add(co, cv) != cv))
170 error("INTERNAL ERROR: indexing of config '%s' in section '%s': already exists.", cv->name, co->name);
172 config_section_wrlock(co);
173 struct config_option *cv2 = co->values;
175 while (cv2->next) cv2 = cv2->next;
178 else co->values = cv;
179 config_section_unlock(co);
184 int appconfig_exists(struct config *root, const char *section, const char *name) {
185 struct config_option *cv;
187 debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name);
189 struct section *co = appconfig_section_find(root, section);
192 cv = appconfig_option_index_find(co, name, 0);
198 int appconfig_move(struct config *root, const char *section_old, const char *name_old, const char *section_new, const char *name_new) {
199 struct config_option *cv_old, *cv_new;
202 debug(D_CONFIG, "request to rename config in section '%s', old name '%s', to section '%s', new name '%s'", section_old, name_old, section_new, name_new);
204 struct section *co_old = appconfig_section_find(root, section_old);
205 if(!co_old) return ret;
207 struct section *co_new = appconfig_section_find(root, section_new);
208 if(!co_new) co_new = appconfig_section_create(root, section_new);
210 config_section_wrlock(co_old);
211 config_section_wrlock(co_new);
213 cv_old = appconfig_option_index_find(co_old, name_old, 0);
214 if(!cv_old) goto cleanup;
216 cv_new = appconfig_option_index_find(co_new, name_new, 0);
217 if(cv_new) goto cleanup;
219 if(unlikely(appconfig_option_index_del(co_old, cv_old) != cv_old))
220 error("INTERNAL ERROR: deletion of config '%s' from section '%s', deleted tge wrong config entry.", cv_old->name, co_old->name);
222 if(co_old->values == cv_old) {
223 co_old->values = cv_old->next;
226 struct config_option *t;
227 for(t = co_old->values; t && t->next != cv_old ;t = t->next) ;
228 if(!t || t->next != cv_old)
229 error("INTERNAL ERROR: cannot find variable '%s' in section '%s' of the config - but it should be there.", cv_old->name, co_old->name);
231 t->next = cv_old->next;
235 cv_old->name = strdupz(name_new);
236 cv_old->hash = simple_hash(cv_old->name);
239 cv_new->next = co_new->values;
240 co_new->values = cv_new;
242 if(unlikely(appconfig_option_index_add(co_new, cv_old) != cv_old))
243 error("INTERNAL ERROR: indexing of config '%s' in section '%s', already exists.", cv_old->name, co_new->name);
248 config_section_unlock(co_new);
249 config_section_unlock(co_old);
253 char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value)
255 struct config_option *cv;
257 debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value);
259 struct section *co = appconfig_section_find(root, section);
260 if(!co) co = appconfig_section_create(root, section);
262 cv = appconfig_option_index_find(co, name, 0);
264 cv = appconfig_value_create(co, name, default_value);
267 cv->flags |= CONFIG_VALUE_USED;
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;
281 long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value)
283 char buffer[100], *s;
284 sprintf(buffer, "%lld", value);
286 s = appconfig_get(root, section, name, buffer);
289 return strtoll(s, NULL, 0);
292 int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value)
298 s = appconfig_get(root, section, name, s);
301 if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1;
305 int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value)
309 if(value == CONFIG_BOOLEAN_AUTO)
312 else if(value == CONFIG_BOOLEAN_NO)
318 s = appconfig_get(root, section, name, s);
321 if(!strcmp(s, "yes"))
322 return CONFIG_BOOLEAN_YES;
323 else if(!strcmp(s, "no"))
324 return CONFIG_BOOLEAN_NO;
325 else if(!strcmp(s, "auto") || !strcmp(s, "on demand"))
326 return CONFIG_BOOLEAN_AUTO;
331 const char *appconfig_set_default(struct config *root, const char *section, const char *name, const char *value)
333 struct config_option *cv;
335 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
337 struct section *co = appconfig_section_find(root, section);
338 if(!co) return appconfig_set(root, section, name, value);
340 cv = appconfig_option_index_find(co, name, 0);
341 if(!cv) return appconfig_set(root, section, name, value);
343 cv->flags |= CONFIG_VALUE_USED;
345 if(cv->flags & CONFIG_VALUE_LOADED)
348 if(strcmp(cv->value, value) != 0) {
349 cv->flags |= CONFIG_VALUE_CHANGED;
352 cv->value = strdupz(value);
358 const char *appconfig_set(struct config *root, const char *section, const char *name, const char *value)
360 struct config_option *cv;
362 debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value);
364 struct section *co = appconfig_section_find(root, section);
365 if(!co) co = appconfig_section_create(root, section);
367 cv = appconfig_option_index_find(co, name, 0);
368 if(!cv) cv = appconfig_value_create(co, name, value);
369 cv->flags |= CONFIG_VALUE_USED;
371 if(strcmp(cv->value, value) != 0) {
372 cv->flags |= CONFIG_VALUE_CHANGED;
375 cv->value = strdupz(value);
381 long long appconfig_set_number(struct config *root, const char *section, const char *name, long long value)
384 sprintf(buffer, "%lld", value);
386 appconfig_set(root, section, name, buffer);
391 int appconfig_set_boolean(struct config *root, const char *section, const char *name, int value)
397 appconfig_set(root, section, name, s);
403 // ----------------------------------------------------------------------------
406 int appconfig_load(struct config *root, char *filename, int overwrite_used)
409 struct section *co = NULL;
411 char buffer[CONFIG_FILE_LINE_MAX + 1], *s;
413 if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME;
415 debug(D_CONFIG, "Opening config file '%s'", filename);
417 FILE *fp = fopen(filename, "r");
419 error("Cannot open file '%s'", filename);
423 while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) {
424 buffer[CONFIG_FILE_LINE_MAX] = '\0';
429 debug(D_CONFIG, "Ignoring line %d, it is empty.", line);
433 int len = (int) strlen(s);
434 if(*s == '[' && s[len - 1] == ']') {
439 co = appconfig_section_find(root, s);
440 if(!co) co = appconfig_section_create(root, s);
446 // line outside a section
447 error("Ignoring line %d ('%s'), it is outside all sections.", line, s);
452 char *value = strchr(s, '=');
454 error("Ignoring line %d ('%s'), there is no = in it.", line, s);
464 error("Ignoring line %d, name is empty.", line);
468 debug(D_CONFIG, "Ignoring line %d, value is empty.", line);
472 struct config_option *cv = appconfig_option_index_find(co, name, 0);
474 if(!cv) cv = appconfig_value_create(co, name, value);
476 if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) {
477 debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name);
479 cv->value = strdupz(value);
482 debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name);
484 cv->flags |= CONFIG_VALUE_LOADED;
492 void appconfig_generate(struct config *root, BUFFER *wb, int only_changed)
496 struct config_option *cv;
498 for(i = 0; i < 3 ;i++) {
502 "# netdata configuration\n"
504 "# You can download the latest version of this file, using:\n"
506 "# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
508 "# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n"
510 "# You can uncomment and change any of the options below.\n"
511 "# The value shown in the commented settings, is the default value.\n"
513 "\n# global netdata configuration\n");
517 buffer_strcat(wb, "\n\n# per plugin configuration\n");
521 buffer_strcat(wb, "\n\n# per chart configuration\n");
525 appconfig_wrlock(root);
526 for(co = root->sections; co ; co = co->next) {
527 if(!strcmp(co->name, CONFIG_SECTION_GLOBAL)
528 || !strcmp(co->name, CONFIG_SECTION_API)
529 || !strcmp(co->name, CONFIG_SECTION_PLUGINS)
530 || !strcmp(co->name, CONFIG_SECTION_REGISTRY)
531 || !strcmp(co->name, CONFIG_SECTION_HEALTH)
532 || !strcmp(co->name, CONFIG_SECTION_BACKEND)
533 || !strcmp(co->name, CONFIG_SECTION_STREAM)
536 else if(!strncmp(co->name, "plugin:", 7)) pri = 1;
544 config_section_wrlock(co);
545 for(cv = co->values; cv ; cv = cv->next) {
546 used += (cv->flags & CONFIG_VALUE_USED)?1:0;
547 changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0;
550 config_section_unlock(co);
553 if(only_changed && !changed) continue;
556 buffer_sprintf(wb, "\n# node '%s' is not used.", co->name);
559 buffer_sprintf(wb, "\n[%s]\n", co->name);
561 config_section_wrlock(co);
562 for(cv = co->values; cv ; cv = cv->next) {
564 if(used && !(cv->flags & CONFIG_VALUE_USED)) {
565 buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name);
567 buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value);
569 config_section_unlock(co);
572 appconfig_unlock(root);