@cryptotaxi247 / netdata-1 / commits / e96e66a95

use netdata to get configuration options from netdata.conf

Costa Tsaousis (ktsaou) committed Apr 30, 2017 at 19:09 UTC e96e66a953c32d8ecec7f3f6d23b4c88e7451b9a
2 files changed +47 -25
netdata-installer.sh
+20 -25
@@ -630,51 +630,46 @@ progress "Read installation options from netdata.conf"
630
631 # function to extract values from the config file
632 config_option() {
633 - local key="${1}" value="${2}" line=
633 + local section="${1}" key="${2}" value="${3}" line=
634
635 if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
636 then
637 - line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
638 - [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
637 + "${NETDATA_PREFIX}/usr/sbin/netdata" \
638 + -c "${NETDATA_PREFIX}/etc/netdata/netdata.conf" \
639 + -W get "${section}" "${key}" "${value}" || \
640 + echo "${value}"
641 + # line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
642 + # [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
643 + else
644 + echo "${value}"
645 fi
640 -
641 - echo "${value}"
646 }
647
648 # the user netdata will run as
649 if [ "${UID}" = "0" ]
650 then
647 - NETDATA_USER="$( config_option "run as user" "netdata" )"
651 + NETDATA_USER="$( config_option "global" "run as user" "netdata" )"
652 else
653 NETDATA_USER="${USER}"
654 fi
655
656 # the owners of the web files
653 -NETDATA_WEB_USER="$( config_option "web files owner" "${NETDATA_USER}" )"
654 -NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
657 +NETDATA_WEB_USER="$( config_option "web" "web files owner" "${NETDATA_USER}" )"
658 +NETDATA_WEB_GROUP="$( config_option "web" "web files group" "${NETDATA_WEB_USER}" )"
659
660 # debug flags
657 -NETDATA_DEBUG="$( config_option "debug flags" 0 )"
661 +NETDATA_DEBUG="$( config_option "global" "debug flags" 0 )"
662
663 # port
664 defport=19999
661 -NETDATA_PORT="$( config_option "default port" ${defport} )"
662 -NETDATA_PORT2="$( config_option "port" ${defport} )"
663 -
664 -if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
665 -then
666 - if [ "${NETDATA_PORT2}" != "${defport}" ]
667 - then
668 - NETDATA_PORT="${NETDATA_PORT2}"
669 - fi
670 -fi
665 +NETDATA_PORT="$( config_option "web" "default port" ${defport} )"
666
667 # directories
673 -NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
674 -NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
675 -NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
676 -NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
677 -NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
668 +NETDATA_LIB_DIR="$( config_option "global" "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
669 +NETDATA_CACHE_DIR="$( config_option "global" "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
670 +NETDATA_WEB_DIR="$( config_option "global" "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
671 +NETDATA_LOG_DIR="$( config_option "global" "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
672 +NETDATA_CONF_DIR="$( config_option "global" "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
673 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
674
675
@@ -1257,7 +1252,7 @@ REINSTALL
1252 echo >&2 "Update script generated : ${TPUT_GREEN}${TPUT_BOLD}./netdata-updater.sh${TPUT_RESET}"
1253 echo >&2
1254 echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} can work from cron. It will trigger an email from cron"
1260 - echo >&2 "only if it fails (it does not print anything if it can update netdata).${TPUT_RESET}"
1255 + echo >&2 "only if it fails (it does not print anything when it can update netdata).${TPUT_RESET}"
1256 if [ "${UID}" -eq "0" ]
1257 then
1258 if [ -d "/etc/cron.daily" -a ! -f "/etc/cron.daily/netdata-updater.sh" ]
src/main.c
+27
@@ -729,6 +729,33 @@ int main(int argc, char **argv) {
729
730 // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value);
731 }
732 + else if(strcmp(optarg, "get") == 0) {
733 + if(optind + 3 > argc) {
734 + fprintf(stderr, "%s", "\nUSAGE: -W get 'section' 'key' 'value'\n\n"
735 + " Prints settings of netdata.conf.\n"
736 + "\n"
737 + " These options interact with: -c netdata.conf\n"
738 + " -c netdata.conf has to be given before -W get.\n"
739 + "\n"
740 + );
741 + exit(1);
742 + }
743 +
744 + if(!config_loaded) {
745 + fprintf(stderr, "warning: no configuration file has been loaded. Use -c CONFIG_FILE, before -W get. Using default config.\n");
746 + config_load(NULL, 0);
747 + }
748 +
749 + backwards_compatible_config();
750 + get_netdata_configured_variables();
751 +
752 + const char *section = argv[optind];
753 + const char *key = argv[optind + 1];
754 + const char *def = argv[optind + 2];
755 + const char *value = config_get(section, key, def);
756 + printf("%s\n", value);
757 + exit(0);
758 + }
759 else {
760 fprintf(stderr, "Unknown -W parameter '%s'\n", optarg);
761 help(1);