X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Fappconfig.c;h=ae92ad0454a16444aeff44443913e6d79c5a5dfa;hb=909e26f825bc1f6f907231761412c885331fec7e;hp=86c229372d8185046685cbc382682f143d9ce345;hpb=f7e35af6cdd5a820833bae3db41f340c175c4565;p=netdata.git diff --git a/src/appconfig.c b/src/appconfig.c old mode 100755 new mode 100644 index 86c22937..ae92ad04 --- a/src/appconfig.c +++ b/src/appconfig.c @@ -1,19 +1,7 @@ -#ifdef HAVE_CONFIG_H -#include -#endif -#include -#include -#include - -#include "avl.h" #include "common.h" -#include "appconfig.h" -#include "log.h" #define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2) -pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER; - // ---------------------------------------------------------------------------- // definitions @@ -22,464 +10,570 @@ pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER; #define CONFIG_VALUE_CHANGED 0x04 // has been changed from the loaded value #define CONFIG_VALUE_CHECKED 0x08 // has been checked if the value is different from the default -struct config_value { - avl avl; // the index - this has to be first! - - uint32_t hash; // a simple hash to speed up searching - // we first compare hashes, and only if the hashes are equal we do string comparisons +struct config_option { + avl avl; // the index - this has to be first! - char *name; - char *value; + uint8_t flags; + uint32_t hash; // a simple hash to speed up searching + // we first compare hashes, and only if the hashes are equal we do string comparisons - uint8_t flags; + char *name; + char *value; - struct config_value *next; + struct config_option *next; // config->mutex protects just this }; -struct config { - avl avl; +struct section { + avl avl; - uint32_t hash; // a simple hash to speed up searching - // we first compare hashes, and only if the hashes are equal we do string comparisons + uint32_t hash; // a simple hash to speed up searching + // we first compare hashes, and only if the hashes are equal we do string comparisons - char *name; + char *name; - struct config_value *values; - avl_tree values_index; + struct section *next; // gloabl config_mutex protects just this - struct config *next; + struct config_option *values; + avl_tree_lock values_index; - pthread_rwlock_t rwlock; -} *config_root = NULL; + netdata_mutex_t mutex; // this locks only the writers, to ensure atomic updates + // readers are protected using the rwlock in avl_tree_lock +}; +static int appconfig_section_compare(void *a, void *b); -// ---------------------------------------------------------------------------- -// config value +struct config netdata_config = { + .sections = NULL, + .mutex = NETDATA_MUTEX_INITIALIZER, + .index = { + { NULL, appconfig_section_compare }, + AVL_LOCK_INITIALIZER + } +}; -static int config_value_iterator(avl *a) { if(a) {}; return 0; } +struct config stream_config = { + .sections = NULL, + .mutex = NETDATA_MUTEX_INITIALIZER, + .index = { + { NULL, appconfig_section_compare }, + AVL_LOCK_INITIALIZER + } +}; -static int config_value_compare(void* a, void* b) { - if(((struct config_value *)a)->hash < ((struct config_value *)b)->hash) return -1; - else if(((struct config_value *)a)->hash > ((struct config_value *)b)->hash) return 1; - else return strcmp(((struct config_value *)a)->name, ((struct config_value *)b)->name); +// ---------------------------------------------------------------------------- +// locking + +static inline void appconfig_wrlock(struct config *root) { + netdata_mutex_lock(&root->mutex); } -#define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv)) -#define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv)) +static inline void appconfig_unlock(struct config *root) { + netdata_mutex_unlock(&root->mutex); +} -static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) { - struct config_value *result = NULL, tmp; - tmp.hash = (hash)?hash:simple_hash(name); - tmp.name = (char *)name; +static inline void config_section_wrlock(struct section *co) { + netdata_mutex_lock(&co->mutex); +} - avl_search(&(co->values_index), (avl *)&tmp, config_value_iterator, (avl **)&result); - return result; +static inline void config_section_unlock(struct section *co) { + netdata_mutex_unlock(&co->mutex); } + // ---------------------------------------------------------------------------- -// config +// config name-value index -static int config_iterator(avl *a) { if(a) {}; return 0; } +static int appconfig_option_compare(void *a, void *b) { + if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1; + else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1; + else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name); +} + +#define appconfig_option_index_add(co, cv) (struct config_option *)avl_insert_lock(&((co)->values_index), (avl *)(cv)) +#define appconfig_option_index_del(co, cv) (struct config_option *)avl_remove_lock(&((co)->values_index), (avl *)(cv)) + +static struct config_option *appconfig_option_index_find(struct section *co, const char *name, uint32_t hash) { + struct config_option tmp; + tmp.hash = (hash)?hash:simple_hash(name); + tmp.name = (char *)name; -static int config_compare(void* a, void* b) { - if(((struct config *)a)->hash < ((struct config *)b)->hash) return -1; - else if(((struct config *)a)->hash > ((struct config *)b)->hash) return 1; - else return strcmp(((struct config *)a)->name, ((struct config *)b)->name); + return (struct config_option *)avl_search_lock(&(co->values_index), (avl *) &tmp); } -avl_tree config_root_index = { - NULL, - config_compare, -#ifdef AVL_LOCK_WITH_MUTEX - PTHREAD_MUTEX_INITIALIZER -#else - PTHREAD_RWLOCK_INITIALIZER -#endif -}; -#define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg)) -#define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg)) +// ---------------------------------------------------------------------------- +// config sections index -static struct config *config_index_find(const char *name, uint32_t hash) { - struct config *result = NULL, tmp; - tmp.hash = (hash)?hash:simple_hash(name); - tmp.name = (char *)name; +static int appconfig_section_compare(void *a, void *b) { + if(((struct section *)a)->hash < ((struct section *)b)->hash) return -1; + else if(((struct section *)a)->hash > ((struct section *)b)->hash) return 1; + else return strcmp(((struct section *)a)->name, ((struct section *)b)->name); +} - avl_search(&config_root_index, (avl *)&tmp, config_iterator, (avl **)&result); - return result; +#define appconfig_index_add(root, cfg) (struct section *)avl_insert_lock(&root->index, (avl *)(cfg)) +#define appconfig_index_del(root, cfg) (struct section *)avl_remove_lock(&root->index, (avl *)(cfg)) + +static struct section *appconfig_index_find(struct config *root, const char *name, uint32_t hash) { + struct section tmp; + tmp.hash = (hash)?hash:simple_hash(name); + tmp.name = (char *)name; + + return (struct section *)avl_search_lock(&root->index, (avl *) &tmp); } -struct config_value *config_value_create(struct config *co, const char *name, const char *value) -{ - debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name); - struct config_value *cv = calloc(1, sizeof(struct config_value)); - if(!cv) fatal("Cannot allocate config_value"); +// ---------------------------------------------------------------------------- +// config section methods - cv->name = strdup(name); - if(!cv->name) fatal("Cannot allocate config.name"); - cv->hash = simple_hash(cv->name); +static inline struct section *appconfig_section_find(struct config *root, const char *section) { + return appconfig_index_find(root, section, 0); +} - cv->value = strdup(value); - if(!cv->value) fatal("Cannot allocate config.value"); +static inline struct section *appconfig_section_create(struct config *root, const char *section) { + debug(D_CONFIG, "Creating section '%s'.", section); - config_value_index_add(co, cv); + struct section *co = callocz(1, sizeof(struct section)); + co->name = strdupz(section); + co->hash = simple_hash(co->name); - // no need for string termination, due to calloc() + avl_init_lock(&co->values_index, appconfig_option_compare); - pthread_rwlock_wrlock(&co->rwlock); + if(unlikely(appconfig_index_add(root, co) != co)) + error("INTERNAL ERROR: indexing of section '%s', already exists.", co->name); - struct config_value *cv2 = co->values; - if(cv2) { - while (cv2->next) cv2 = cv2->next; - cv2->next = cv; - } - else co->values = cv; + appconfig_wrlock(root); + struct section *co2 = root->sections; + if(co2) { + while (co2->next) co2 = co2->next; + co2->next = co; + } + else root->sections = co; + appconfig_unlock(root); + + return co; +} - pthread_rwlock_unlock(&co->rwlock); - return cv; +// ---------------------------------------------------------------------------- +// config name-value methods + +static inline struct config_option *appconfig_value_create(struct section *co, const char *name, const char *value) { + debug(D_CONFIG, "Creating config entry for name '%s', value '%s', in section '%s'.", name, value, co->name); + + struct config_option *cv = callocz(1, sizeof(struct config_option)); + cv->name = strdupz(name); + cv->hash = simple_hash(cv->name); + cv->value = strdupz(value); + + struct config_option *found = appconfig_option_index_add(co, cv); + if(found != cv) { + error("indexing of config '%s' in section '%s': already exists - using the existing one.", cv->name, co->name); + freez(cv->value); + freez(cv->name); + freez(cv); + return found; + } + + config_section_wrlock(co); + struct config_option *cv2 = co->values; + if(cv2) { + while (cv2->next) cv2 = cv2->next; + cv2->next = cv; + } + else co->values = cv; + config_section_unlock(co); + + return cv; } -struct config *config_create(const char *section) -{ - debug(D_CONFIG, "Creating section '%s'.", section); +int appconfig_exists(struct config *root, const char *section, const char *name) { + struct config_option *cv; - struct config *co = calloc(1, sizeof(struct config)); - if(!co) fatal("Cannot allocate config"); + debug(D_CONFIG, "request to get config in section '%s', name '%s'", section, name); - co->name = strdup(section); - if(!co->name) fatal("Cannot allocate config.name"); - co->hash = simple_hash(co->name); + struct section *co = appconfig_section_find(root, section); + if(!co) return 0; - pthread_rwlock_init(&co->rwlock, NULL); - avl_init(&co->values_index, config_value_compare); + cv = appconfig_option_index_find(co, name, 0); + if(!cv) return 0; - config_index_add(co); + return 1; +} - // no need for string termination, due to calloc() +int appconfig_move(struct config *root, const char *section_old, const char *name_old, const char *section_new, const char *name_new) { + struct config_option *cv_old, *cv_new; + int ret = -1; - pthread_rwlock_wrlock(&config_rwlock); + 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); - struct config *co2 = config_root; - if(co2) { - while (co2->next) co2 = co2->next; - co2->next = co; - } - else config_root = co; + struct section *co_old = appconfig_section_find(root, section_old); + if(!co_old) return ret; - pthread_rwlock_unlock(&config_rwlock); + struct section *co_new = appconfig_section_find(root, section_new); + if(!co_new) co_new = appconfig_section_create(root, section_new); - return co; -} + config_section_wrlock(co_old); + config_section_wrlock(co_new); -struct config *config_find_section(const char *section) -{ - return config_index_find(section, 0); -} + cv_old = appconfig_option_index_find(co_old, name_old, 0); + if(!cv_old) goto cleanup; -int load_config(char *filename, int overwrite_used) -{ - int line = 0; - struct config *co = NULL; - - char buffer[CONFIG_FILE_LINE_MAX + 1], *s; - - if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME; - FILE *fp = fopen(filename, "r"); - if(!fp) { - error("Cannot open file '%s'", filename); - return 0; - } - - while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) { - buffer[CONFIG_FILE_LINE_MAX] = '\0'; - line++; - - s = trim(buffer); - if(!s) { - debug(D_CONFIG, "Ignoring line %d, it is empty.", line); - continue; - } - - int len = (int) strlen(s); - if(*s == '[' && s[len - 1] == ']') { - // new section - s[len - 1] = '\0'; - s++; - - co = config_find_section(s); - if(!co) co = config_create(s); - - continue; - } - - if(!co) { - // line outside a section - error("Ignoring line %d ('%s'), it is outsize all sections.", line, s); - continue; - } - - char *name = s; - char *value = strchr(s, '='); - if(!value) { - error("Ignoring line %d ('%s'), there is no = in it.", line, s); - continue; - } - *value = '\0'; - value++; - - name = trim(name); - value = trim(value); - - if(!name) { - error("Ignoring line %d, name is empty.", line); - continue; - } - if(!value) { - debug(D_CONFIG, "Ignoring line %d, value is empty.", line); - continue; - } - - struct config_value *cv = config_value_index_find(co, name, 0); - - if(!cv) cv = config_value_create(co, name, value); - else { - if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) { - debug(D_CONFIG, "Overwriting '%s/%s'.", line, co->name, cv->name); - free(cv->value); - cv->value = strdup(value); - if(!cv->value) fatal("Cannot allocate config.value"); - } - else - debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name); - } - cv->flags |= CONFIG_VALUE_LOADED; - } - - fclose(fp); - - return 1; + cv_new = appconfig_option_index_find(co_new, name_new, 0); + if(cv_new) goto cleanup; + + if(unlikely(appconfig_option_index_del(co_old, cv_old) != cv_old)) + error("INTERNAL ERROR: deletion of config '%s' from section '%s', deleted tge wrong config entry.", cv_old->name, co_old->name); + + if(co_old->values == cv_old) { + co_old->values = cv_old->next; + } + else { + struct config_option *t; + for(t = co_old->values; t && t->next != cv_old ;t = t->next) ; + if(!t || t->next != cv_old) + error("INTERNAL ERROR: cannot find variable '%s' in section '%s' of the config - but it should be there.", cv_old->name, co_old->name); + else + t->next = cv_old->next; + } + + freez(cv_old->name); + cv_old->name = strdupz(name_new); + cv_old->hash = simple_hash(cv_old->name); + + cv_new = cv_old; + cv_new->next = co_new->values; + co_new->values = cv_new; + + if(unlikely(appconfig_option_index_add(co_new, cv_old) != cv_old)) + error("INTERNAL ERROR: re-indexing of config '%s' in section '%s', already exists.", cv_old->name, co_new->name); + + ret = 0; + +cleanup: + config_section_unlock(co_new); + config_section_unlock(co_old); + return ret; } -char *config_get(const char *section, const char *name, const char *default_value) +char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value) { - struct config_value *cv; - - debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); - - struct config *co = config_find_section(section); - if(!co) co = config_create(section); - - cv = config_value_index_find(co, name, 0); - if(!cv) { - cv = config_value_create(co, name, default_value); - if(!cv) return NULL; - } - cv->flags |= CONFIG_VALUE_USED; - - if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) { - // this is a loaded value from the config file - // if it is different that the default, mark it - if(!(cv->flags & CONFIG_VALUE_CHECKED)) { - if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED; - cv->flags |= CONFIG_VALUE_CHECKED; - } - } - - return(cv->value); + struct config_option *cv; + + debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); + + struct section *co = appconfig_section_find(root, section); + if(!co) co = appconfig_section_create(root, section); + + cv = appconfig_option_index_find(co, name, 0); + if(!cv) { + cv = appconfig_value_create(co, name, default_value); + if(!cv) return NULL; + } + cv->flags |= CONFIG_VALUE_USED; + + if((cv->flags & CONFIG_VALUE_LOADED) || (cv->flags & CONFIG_VALUE_CHANGED)) { + // this is a loaded value from the config file + // if it is different that the default, mark it + if(!(cv->flags & CONFIG_VALUE_CHECKED)) { + if(strcmp(cv->value, default_value) != 0) cv->flags |= CONFIG_VALUE_CHANGED; + cv->flags |= CONFIG_VALUE_CHECKED; + } + } + + return(cv->value); } -long long config_get_number(const char *section, const char *name, long long value) +long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value) { - char buffer[100], *s; - sprintf(buffer, "%lld", value); + char buffer[100], *s; + sprintf(buffer, "%lld", value); - s = config_get(section, name, buffer); - if(!s) return value; + s = appconfig_get(root, section, name, buffer); + if(!s) return value; - return strtoll(s, NULL, 0); + return strtoll(s, NULL, 0); } -int config_get_boolean(const char *section, const char *name, int value) +int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value) { - char *s; - if(value) s = "yes"; - else s = "no"; + char *s; + if(value) s = "yes"; + else s = "no"; - s = config_get(section, name, s); - if(!s) return value; + s = appconfig_get(root, section, name, s); + if(!s) return value; - if(!strcmp(s, "yes")) return 1; - else return 0; + if(!strcmp(s, "yes") || !strcmp(s, "auto") || !strcmp(s, "on demand")) return 1; + return 0; } -int config_get_boolean_ondemand(const char *section, const char *name, int value) +int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value) { - char *s; + char *s; - if(value == CONFIG_ONDEMAND_ONDEMAND) - s = "on demand"; + if(value == CONFIG_BOOLEAN_AUTO) + s = "auto"; - else if(value == CONFIG_ONDEMAND_NO) - s = "no"; + else if(value == CONFIG_BOOLEAN_NO) + s = "no"; - else - s = "yes"; + else + s = "yes"; - s = config_get(section, name, s); - if(!s) return value; + s = appconfig_get(root, section, name, s); + if(!s) return value; - if(!strcmp(s, "yes")) - return CONFIG_ONDEMAND_YES; - else if(!strcmp(s, "no")) - return CONFIG_ONDEMAND_NO; - else if(!strcmp(s, "on demand")) - return CONFIG_ONDEMAND_ONDEMAND; + if(!strcmp(s, "yes")) + return CONFIG_BOOLEAN_YES; + else if(!strcmp(s, "no")) + return CONFIG_BOOLEAN_NO; + else if(!strcmp(s, "auto") || !strcmp(s, "on demand")) + return CONFIG_BOOLEAN_AUTO; - return value; + return value; } -const char *config_set_default(const char *section, const char *name, const char *value) +const char *appconfig_set_default(struct config *root, const char *section, const char *name, const char *value) { - struct config_value *cv; + struct config_option *cv; - debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value); + debug(D_CONFIG, "request to set default config in section '%s', name '%s', value '%s'", section, name, value); - struct config *co = config_find_section(section); - if(!co) return config_set(section, name, value); + struct section *co = appconfig_section_find(root, section); + if(!co) return appconfig_set(root, section, name, value); - cv = config_value_index_find(co, name, 0); - if(!cv) return config_set(section, name, value); + cv = appconfig_option_index_find(co, name, 0); + if(!cv) return appconfig_set(root, section, name, value); - cv->flags |= CONFIG_VALUE_USED; + cv->flags |= CONFIG_VALUE_USED; - if(cv->flags & CONFIG_VALUE_LOADED) - return cv->value; + if(cv->flags & CONFIG_VALUE_LOADED) + return cv->value; - if(strcmp(cv->value, value) != 0) { - cv->flags |= CONFIG_VALUE_CHANGED; + if(strcmp(cv->value, value) != 0) { + cv->flags |= CONFIG_VALUE_CHANGED; - free(cv->value); - cv->value = strdup(value); - if(!cv->value) fatal("Cannot allocate config.value"); - } + freez(cv->value); + cv->value = strdupz(value); + } - return cv->value; + return cv->value; } -const char *config_set(const char *section, const char *name, const char *value) +const char *appconfig_set(struct config *root, const char *section, const char *name, const char *value) { - struct config_value *cv; + struct config_option *cv; - debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value); + debug(D_CONFIG, "request to set config in section '%s', name '%s', value '%s'", section, name, value); - struct config *co = config_find_section(section); - if(!co) co = config_create(section); + struct section *co = appconfig_section_find(root, section); + if(!co) co = appconfig_section_create(root, section); - cv = config_value_index_find(co, name, 0); - if(!cv) cv = config_value_create(co, name, value); - cv->flags |= CONFIG_VALUE_USED; + cv = appconfig_option_index_find(co, name, 0); + if(!cv) cv = appconfig_value_create(co, name, value); + cv->flags |= CONFIG_VALUE_USED; - if(strcmp(cv->value, value) != 0) { - cv->flags |= CONFIG_VALUE_CHANGED; + if(strcmp(cv->value, value) != 0) { + cv->flags |= CONFIG_VALUE_CHANGED; - free(cv->value); - cv->value = strdup(value); - if(!cv->value) fatal("Cannot allocate config.value"); - } + freez(cv->value); + cv->value = strdupz(value); + } - return value; + return value; } -long long config_set_number(const char *section, const char *name, long long value) +long long appconfig_set_number(struct config *root, const char *section, const char *name, long long value) { - char buffer[100]; - sprintf(buffer, "%lld", value); + char buffer[100]; + sprintf(buffer, "%lld", value); - config_set(section, name, buffer); + appconfig_set(root, section, name, buffer); - return value; + return value; } -int config_set_boolean(const char *section, const char *name, int value) +int appconfig_set_boolean(struct config *root, const char *section, const char *name, int value) { - char *s; - if(value) s = "yes"; - else s = "no"; + char *s; + if(value) s = "yes"; + else s = "no"; - config_set(section, name, s); + appconfig_set(root, section, name, s); - return value; + return value; } -void generate_config(BUFFER *wb, int only_changed) + +// ---------------------------------------------------------------------------- +// config load/save + +int appconfig_load(struct config *root, char *filename, int overwrite_used) { - int i, pri; - struct config *co; - struct config_value *cv; - - for(i = 0; i < 3 ;i++) { - switch(i) { - case 0: - buffer_strcat(wb, - "# NetData Configuration\n" - "# You can uncomment and change any of the options below.\n" - "# The value shown in the commented settings, is the default value.\n" - "\n# global netdata configuration\n"); - break; - - case 1: - buffer_strcat(wb, "\n\n# per plugin configuration\n"); - break; - - case 2: - buffer_strcat(wb, "\n\n# per chart configuration\n"); - break; - } - - pthread_rwlock_wrlock(&config_rwlock); - for(co = config_root; co ; co = co->next) { - if(strcmp(co->name, "global") == 0 || strcmp(co->name, "plugins") == 0) pri = 0; - else if(strncmp(co->name, "plugin:", 7) == 0) pri = 1; - else pri = 2; - - if(i == pri) { - int used = 0; - int changed = 0; - int count = 0; - - pthread_rwlock_wrlock(&co->rwlock); - - for(cv = co->values; cv ; cv = cv->next) { - used += (cv->flags && CONFIG_VALUE_USED)?1:0; - changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0; - count++; - } - - pthread_rwlock_unlock(&co->rwlock); - - if(!count) continue; - if(only_changed && !changed) continue; - - if(!used) { - buffer_sprintf(wb, "\n# node '%s' is not used.", co->name); - } - - buffer_sprintf(wb, "\n[%s]\n", co->name); - - pthread_rwlock_wrlock(&co->rwlock); - for(cv = co->values; cv ; cv = cv->next) { - - if(used && !(cv->flags & CONFIG_VALUE_USED)) { - buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name); - } - buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value); - } - pthread_rwlock_unlock(&co->rwlock); - } - } - pthread_rwlock_unlock(&config_rwlock); - } + int line = 0; + struct section *co = NULL; + + char buffer[CONFIG_FILE_LINE_MAX + 1], *s; + + if(!filename) filename = CONFIG_DIR "/" CONFIG_FILENAME; + + debug(D_CONFIG, "Opening config file '%s'", filename); + + FILE *fp = fopen(filename, "r"); + if(!fp) { + error("Cannot open file '%s'", filename); + return 0; + } + + while(fgets(buffer, CONFIG_FILE_LINE_MAX, fp) != NULL) { + buffer[CONFIG_FILE_LINE_MAX] = '\0'; + line++; + + s = trim(buffer); + if(!s) { + debug(D_CONFIG, "Ignoring line %d, it is empty.", line); + continue; + } + + int len = (int) strlen(s); + if(*s == '[' && s[len - 1] == ']') { + // new section + s[len - 1] = '\0'; + s++; + + co = appconfig_section_find(root, s); + if(!co) co = appconfig_section_create(root, s); + + continue; + } + + if(!co) { + // line outside a section + error("Ignoring line %d ('%s'), it is outside all sections.", line, s); + continue; + } + + char *name = s; + char *value = strchr(s, '='); + if(!value) { + error("Ignoring line %d ('%s'), there is no = in it.", line, s); + continue; + } + *value = '\0'; + value++; + + name = trim(name); + value = trim(value); + + if(!name) { + error("Ignoring line %d, name is empty.", line); + continue; + } + if(!value) { + debug(D_CONFIG, "Ignoring line %d, value is empty.", line); + continue; + } + + struct config_option *cv = appconfig_option_index_find(co, name, 0); + + if(!cv) cv = appconfig_value_create(co, name, value); + else { + if(((cv->flags & CONFIG_VALUE_USED) && overwrite_used) || !(cv->flags & CONFIG_VALUE_USED)) { + debug(D_CONFIG, "Line %d, overwriting '%s/%s'.", line, co->name, cv->name); + freez(cv->value); + cv->value = strdupz(value); + } + else + debug(D_CONFIG, "Ignoring line %d, '%s/%s' is already present and used.", line, co->name, cv->name); + } + cv->flags |= CONFIG_VALUE_LOADED; + } + + fclose(fp); + + return 1; } +void appconfig_generate(struct config *root, BUFFER *wb, int only_changed) +{ + int i, pri; + struct section *co; + struct config_option *cv; + + for(i = 0; i < 3 ;i++) { + switch(i) { + case 0: + buffer_strcat(wb, + "# netdata configuration\n" + "#\n" + "# You can download the latest version of this file, using:\n" + "#\n" + "# wget -O /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n" + "# or\n" + "# curl -o /etc/netdata/netdata.conf http://localhost:19999/netdata.conf\n" + "#\n" + "# You can uncomment and change any of the options below.\n" + "# The value shown in the commented settings, is the default value.\n" + "#\n" + "\n# global netdata configuration\n"); + break; + + case 1: + buffer_strcat(wb, "\n\n# per plugin configuration\n"); + break; + + case 2: + buffer_strcat(wb, "\n\n# per chart configuration\n"); + break; + } + + appconfig_wrlock(root); + for(co = root->sections; co ; co = co->next) { + if(!strcmp(co->name, CONFIG_SECTION_GLOBAL) + || !strcmp(co->name, CONFIG_SECTION_WEB) + || !strcmp(co->name, CONFIG_SECTION_PLUGINS) + || !strcmp(co->name, CONFIG_SECTION_REGISTRY) + || !strcmp(co->name, CONFIG_SECTION_HEALTH) + || !strcmp(co->name, CONFIG_SECTION_BACKEND) + || !strcmp(co->name, CONFIG_SECTION_STREAM) + ) + pri = 0; + else if(!strncmp(co->name, "plugin:", 7)) pri = 1; + else pri = 2; + + if(i == pri) { + int used = 0; + int changed = 0; + int count = 0; + + config_section_wrlock(co); + for(cv = co->values; cv ; cv = cv->next) { + used += (cv->flags & CONFIG_VALUE_USED)?1:0; + changed += (cv->flags & CONFIG_VALUE_CHANGED)?1:0; + count++; + } + config_section_unlock(co); + + if(!count) continue; + if(only_changed && !changed) continue; + + if(!used) { + buffer_sprintf(wb, "\n# section '%s' is not used.", co->name); + } + + buffer_sprintf(wb, "\n[%s]\n", co->name); + + config_section_wrlock(co); + for(cv = co->values; cv ; cv = cv->next) { + + if(used && !(cv->flags & CONFIG_VALUE_USED)) { + buffer_sprintf(wb, "\n\t# option '%s' is not used.\n", cv->name); + } + buffer_sprintf(wb, "\t%s%s = %s\n", ((!(cv->flags & CONFIG_VALUE_CHANGED)) && (cv->flags & CONFIG_VALUE_USED))?"# ":"", cv->name, cv->value); + } + config_section_unlock(co); + } + } + appconfig_unlock(root); + } +}