diff options
author | Andrew Moss <1043609+amoss@users.noreply.github.com> | 2019-12-16 15:12:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-16 15:12:00 +0100 |
commit | c8c72f18a6a8fd09d3b6284e49525396b24e8395 (patch) | |
tree | 5b9aeaea7d72e1d1029d45f67c0a5f130ecc2f80 /daemon | |
parent | c4bb3d2642ab34e6aca912b22e55aed52f84e974 (diff) |
Labels issues (#7515)
Initial work on host labels from the dedicated branch. Includes work for issues #7096, #7400, #7411, #7369, #7410, #7458, #7459, #7412 and #7408 by @vlvkobal, @thiagoftsm, @cakrit and @amoss.
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/Makefile.am | 2 | ||||
-rw-r--r-- | daemon/commands.c | 26 | ||||
-rw-r--r-- | daemon/commands.h | 1 | ||||
-rw-r--r-- | daemon/common.h | 2 | ||||
-rw-r--r-- | daemon/get-kubernetes-labels.sh.in | 18 | ||||
-rw-r--r-- | daemon/main.c | 9 | ||||
-rwxr-xr-x | daemon/system-info.sh | 2 |
7 files changed, 55 insertions, 5 deletions
diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 6383b559ed..d3102f695e 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -13,9 +13,11 @@ dist_noinst_DATA = \ README.md \ config/README.md \ anonymous-statistics.sh.in \ + get-kubernetes-labels.sh.in \ $(NULL) dist_plugins_SCRIPTS = \ anonymous-statistics.sh \ system-info.sh \ + get-kubernetes-labels.sh \ $(NULL) diff --git a/daemon/commands.c b/daemon/commands.c index 6847e9f0be..5eb22a6693 100644 --- a/daemon/commands.c +++ b/daemon/commands.c @@ -40,6 +40,7 @@ static cmd_status_t cmd_save_database_execute(char *args, char **message); static cmd_status_t cmd_reopen_logs_execute(char *args, char **message); static cmd_status_t cmd_exit_execute(char *args, char **message); static cmd_status_t cmd_fatal_execute(char *args, char **message); +static cmd_status_t cmd_reload_labels_execute(char *args, char **message); static command_info_t command_info_array[] = { {"help", cmd_help_execute, CMD_TYPE_HIGH_PRIORITY}, // show help menu @@ -48,6 +49,7 @@ static command_info_t command_info_array[] = { {"reopen-logs", cmd_reopen_logs_execute, CMD_TYPE_ORTHOGONAL}, // Close and reopen log files {"shutdown-agent", cmd_exit_execute, CMD_TYPE_EXCLUSIVE}, // exit cleanly {"fatal-agent", cmd_fatal_execute, CMD_TYPE_HIGH_PRIORITY}, // exit with fatal error + {"reload-labels", cmd_reload_labels_execute, CMD_TYPE_ORTHOGONAL}, // reload the labels }; /* Mutexes for commands of type CMD_TYPE_ORTHOGONAL */ @@ -97,6 +99,8 @@ static cmd_status_t cmd_help_execute(char *args, char **message) " Show this help menu.\n" "reload-health\n" " Reload health configuration.\n" + "reload-labels\n" + " Reload all labels.\n" "save-database\n" " Save internal DB to disk for memory mode save.\n" "reopen-logs\n" @@ -172,6 +176,28 @@ static cmd_status_t cmd_fatal_execute(char *args, char **message) return CMD_STATUS_SUCCESS; } +static cmd_status_t cmd_reload_labels_execute(char *args, char **message) +{ + (void)args; + info("COMMAND: reloading host labels."); + reload_host_labels(); + + BUFFER *wb = buffer_create(10); + + netdata_rwlock_rdlock(&localhost->labels_rwlock); + struct label *l=localhost->labels; + while (l != NULL) { + buffer_sprintf(wb,"Label [source id=%s]: \"%s\" -> \"%s\"\n", translate_label_source(l->label_source), l->key, l->value); + l = l->next; + } + netdata_rwlock_unlock(&localhost->labels_rwlock); + + (*message)=strdupz(buffer_tostring(wb)); + buffer_free(wb); + + return CMD_STATUS_SUCCESS; +} + static void cmd_lock_exclusive(unsigned index) { (void)index; diff --git a/daemon/commands.h b/daemon/commands.h index 55cd6455d6..6de4084217 100644 --- a/daemon/commands.h +++ b/daemon/commands.h @@ -19,6 +19,7 @@ typedef enum cmd { CMD_REOPEN_LOGS, CMD_EXIT, CMD_FATAL, + CMD_RELOAD_LABELS, CMD_TOTAL_COMMANDS } cmd_t; diff --git a/daemon/common.h b/daemon/common.h index c04c8ad04f..5202a53082 100644 --- a/daemon/common.h +++ b/daemon/common.h @@ -8,7 +8,7 @@ // ---------------------------------------------------------------------------- // shortcuts for the default netdata configuration -#define config_load(filename, overwrite_used) appconfig_load(&netdata_config, filename, overwrite_used) +#define config_load(filename, overwrite_used, section) appconfig_load(&netdata_config, filename, overwrite_used, section) #define config_get(section, name, default_value) appconfig_get(&netdata_config, section, name, default_value) #define config_get_number(section, name, value) appconfig_get_number(&netdata_config, section, name, value) #define config_get_float(section, name, value) appconfig_get_float(&netdata_config, section, name, value) diff --git a/daemon/get-kubernetes-labels.sh.in b/daemon/get-kubernetes-labels.sh.in new file mode 100644 index 0000000000..805d027b86 --- /dev/null +++ b/daemon/get-kubernetes-labels.sh.in @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +# Checks if netdata is running in a kubernetes pod and fetches that pod's labels + +if [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ] && [ -n "${MY_POD_NAMESPACE}" ] && [ -n "${MY_POD_NAME}" ]; then + if command -v jq >/dev/null 2>&1; then + KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)" + URL="https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$MY_POD_NAMESPACE/pods/$MY_POD_NAME" + curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "$URL" | + jq -r '.metadata.labels' | grep ':' | tr -d '," ' + exit 0 + else + echo "jq command not available. Please install jq to get host labels for kubernetes pods." + exit 1 + fi +else + exit 0 +fi diff --git a/daemon/main.c b/daemon/main.c index f8c6d84d45..ece8feb447 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -703,20 +703,20 @@ static int load_netdata_conf(char *filename, char overwrite_used) { int ret = 0; if(filename && *filename) { - ret = config_load(filename, overwrite_used); + ret = config_load(filename, overwrite_used, NULL); if(!ret) error("CONFIG: cannot load config file '%s'.", filename); } else { filename = strdupz_path_subpath(netdata_configured_user_config_dir, "netdata.conf"); - ret = config_load(filename, overwrite_used); + ret = config_load(filename, overwrite_used, NULL); if(!ret) { info("CONFIG: cannot load user config '%s'. Will try the stock version.", filename); freez(filename); filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "netdata.conf"); - ret = config_load(filename, overwrite_used); + ret = config_load(filename, overwrite_used, NULL); if(!ret) info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename); } @@ -1271,6 +1271,9 @@ int main(int argc, char **argv) { error_log_limit_reset(); + // Load host labels + reload_host_labels(); + // ------------------------------------------------------------------------ // spawn the threads diff --git a/daemon/system-info.sh b/daemon/system-info.sh index 6fab741fde..7b091a9e54 100755 --- a/daemon/system-info.sh +++ b/daemon/system-info.sh @@ -107,7 +107,7 @@ if [ "${CONTAINER}" = "unknown" ]; then fi fi -echo "NETDATA_SYSTEM_OS_NAME=\"${NAME}\"" +echo "NETDATA_SYSTEM_OS_NAME=${NAME}" echo "NETDATA_SYSTEM_OS_ID=${ID}" echo "NETDATA_SYSTEM_OS_ID_LIKE=${ID_LIKE}" echo "NETDATA_SYSTEM_OS_VERSION=${VERSION}" |