From 0d6e3714113d50a5f197c9035cf1bd22d2259714 Mon Sep 17 00:00:00 2001 From: "Costa Tsaousis (ktsaou)" Date: Thu, 23 Mar 2017 01:00:23 +0200 Subject: [PATCH] allow all configuration options to be set from the command line; #1991 --- src/appconfig.c | 4 ++-- src/appconfig.h | 2 +- src/main.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/appconfig.c b/src/appconfig.c index 71ff4b75..ae92ad04 100644 --- a/src/appconfig.c +++ b/src/appconfig.c @@ -337,7 +337,7 @@ const char *appconfig_set_default(struct config *root, const char *section, cons { 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 section *co = appconfig_section_find(root, section); if(!co) return appconfig_set(root, section, name, value); @@ -558,7 +558,7 @@ void appconfig_generate(struct config *root, BUFFER *wb, int only_changed) if(only_changed && !changed) continue; if(!used) { - buffer_sprintf(wb, "\n# node '%s' is not used.", co->name); + buffer_sprintf(wb, "\n# section '%s' is not used.", co->name); } buffer_sprintf(wb, "\n[%s]\n", co->name); diff --git a/src/appconfig.h b/src/appconfig.h index 45cc8cfd..81db5d97 100644 --- a/src/appconfig.h +++ b/src/appconfig.h @@ -56,7 +56,7 @@ extern void appconfig_generate(struct config *root, BUFFER *wb, int only_changed #define config_get_boolean(section, name, value) appconfig_get_boolean(&netdata_config, section, name, value) #define config_get_boolean_ondemand(section, name, value) appconfig_get_boolean_ondemand(&netdata_config, section, name, value) -#define config_set(section, name, default_value) appconfig_get(&netdata_config, section, name, default_value) +#define config_set(section, name, default_value) appconfig_set(&netdata_config, section, name, default_value) #define config_set_default(section, name, value) appconfig_set_default(&netdata_config, section, name, value) #define config_set_number(section, name, value) appconfig_set_number(&netdata_config, section, name, value) #define config_set_boolean(section, name, value) appconfig_set_boolean(&netdata_config, section, name, value) diff --git a/src/main.c b/src/main.c index a72585e2..c661f759 100644 --- a/src/main.c +++ b/src/main.c @@ -625,6 +625,7 @@ int main(int argc, char **argv) { { char* stacksize_string = "stacksize="; char* debug_flags_string = "debug_flags="; + if(strcmp(optarg, "unittest") == 0) { default_rrd_update_every = 1; default_rrd_memory_mode = RRD_MEMORY_MODE_RAM; @@ -691,9 +692,36 @@ int main(int argc, char **argv) { config_set(CONFIG_SECTION_GLOBAL, "debug flags", optarg); debug_flags = strtoull(optarg, NULL, 0); } + else if(strcmp(optarg, "set") == 0) { + if(optind + 3 > argc) { + fprintf(stderr, "%s", "\nUSAGE: -W set 'section' 'key' 'value'\n\n" + " Overwrites settings of netdata.conf.\n" + "\n" + ); + exit(1); + } + const char *section = argv[optind]; + const char *key = argv[optind + 1]; + const char *value = argv[optind + 2]; + optind += 3; + + // set this one as the default + // only if it is not already set in the config file + // so the caller can use -c netdata.conf before or + // after this parameter to prevent or allow overwriting + // variables at netdata.conf + config_set_default(section, key, value); + + // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value); + } + else { + fprintf(stderr, "Unknown -W parameter '%s'\n", optarg); + help(1); + } } break; default: /* ? */ + fprintf(stderr, "Unknown parameter '%c'\n", opt); help(1); break; } -- 2.39.2