summaryrefslogtreecommitdiffstats
path: root/collectors/cgroups.plugin/cgroup-name.sh
diff options
context:
space:
mode:
Diffstat (limited to 'collectors/cgroups.plugin/cgroup-name.sh')
-rwxr-xr-xcollectors/cgroups.plugin/cgroup-name.sh55
1 files changed, 40 insertions, 15 deletions
diff --git a/collectors/cgroups.plugin/cgroup-name.sh b/collectors/cgroups.plugin/cgroup-name.sh
index 55b02ac7..9a5812f3 100755
--- a/collectors/cgroups.plugin/cgroup-name.sh
+++ b/collectors/cgroups.plugin/cgroup-name.sh
@@ -47,11 +47,14 @@ fatal() {
function parse_docker_like_inspect_output() {
local output="${1}"
- eval "$(grep -E "^(NOMAD_NAMESPACE|NOMAD_JOB_NAME|NOMAD_TASK_NAME|NOMAD_SHORT_ALLOC_ID|CONT_NAME)=" <<<"$output")"
+ eval "$(grep -E "^(NOMAD_NAMESPACE|NOMAD_JOB_NAME|NOMAD_TASK_NAME|NOMAD_SHORT_ALLOC_ID|CONT_NAME|IMAGE_NAME)=" <<<"$output")"
if [ -n "$NOMAD_NAMESPACE" ] && [ -n "$NOMAD_JOB_NAME" ] && [ -n "$NOMAD_TASK_NAME" ] && [ -n "$NOMAD_SHORT_ALLOC_ID" ]; then
- echo "${NOMAD_NAMESPACE}-${NOMAD_JOB_NAME}-${NOMAD_TASK_NAME}-${NOMAD_SHORT_ALLOC_ID}"
+ NAME="${NOMAD_NAMESPACE}-${NOMAD_JOB_NAME}-${NOMAD_TASK_NAME}-${NOMAD_SHORT_ALLOC_ID}"
else
- echo "${CONT_NAME}" | sed 's|^/||'
+ NAME=$(echo "${CONT_NAME}" | sed 's|^/||')
+ fi
+ if [ -n "${IMAGE_NAME}" ]; then
+ LABELS="image=\"${IMAGE_NAME}\""
fi
}
@@ -59,9 +62,9 @@ function docker_like_get_name_command() {
local command="${1}"
local id="${2}"
info "Running command: ${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}' \"${id}\""
- if OUTPUT="$(${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}' "${id}")" &&
+ if OUTPUT="$(${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}{{println}}IMAGE_NAME={{ .Config.Image}}' "${id}")" &&
[ -n "$OUTPUT" ]; then
- NAME="$(parse_docker_like_inspect_output "$OUTPUT")"
+ parse_docker_like_inspect_output "$OUTPUT"
fi
return 0
}
@@ -85,8 +88,8 @@ function docker_like_get_name_api() {
info "Running API command: curl \"${host}${path}\""
JSON=$(curl -sS "${host}${path}")
fi
- if OUTPUT=$(echo "${JSON}" | jq -r '.Config.Env[],"CONT_NAME=\(.Name)"') && [ -n "$OUTPUT" ]; then
- NAME="$(parse_docker_like_inspect_output "$OUTPUT")"
+ if OUTPUT=$(echo "${JSON}" | jq -r '.Config.Env[],"CONT_NAME=\(.Name)","IMAGE_NAME=\(.Config.Image)"') && [ -n "$OUTPUT" ]; then
+ parse_docker_like_inspect_output "$OUTPUT"
fi
return 0
}
@@ -303,8 +306,14 @@ function k8s_get_kubepod_name() {
fi
fi
- url="https://$host/api/v1/pods"
- [ -n "$MY_NODE_NAME" ] && url+="?fieldSelector=spec.nodeName==$MY_NODE_NAME"
+ local url
+ if [ -n "${USE_KUBELET_FOR_PODS_METADATA}" ]; then
+ url="${KUBELET_URL:-https://localhost:10250}/pods"
+ else
+ url="https://$host/api/v1/pods"
+ [ -n "$MY_NODE_NAME" ] && url+="?fieldSelector=spec.nodeName==$MY_NODE_NAME"
+ fi
+
# FIX: check HTTP response code
if ! pods=$(curl --fail -sSk -H "$header" "$url" 2>&1); then
warning "${fn}: error on curl '${url}': ${pods}."
@@ -401,6 +410,10 @@ function k8s_get_kubepod_name() {
# jq filter nonexistent field and nonexistent label value is 'null'
if [[ $name =~ _null(_|$) ]]; then
warning "${fn}: invalid name: $name (cgroup '$id')"
+ if [ -n "${USE_KUBELET_FOR_PODS_METADATA}" ]; then
+ # local data is cached and may not contain the correct id
+ return 2
+ fi
return 1
fi
@@ -413,20 +426,25 @@ function k8s_get_name() {
local fn="${FUNCNAME[0]}"
local cgroup_path="${1}"
local id="${2}"
+ local kubepod_name=""
- NAME=$(k8s_get_kubepod_name "$cgroup_path" "$id")
+ kubepod_name=$(k8s_get_kubepod_name "$cgroup_path" "$id")
case "$?" in
0)
- NAME="k8s_${NAME}"
+ kubepod_name="k8s_${kubepod_name}"
local name labels
- name=${NAME%% *}
- labels=${NAME#* }
+ name=${kubepod_name%% *}
+ labels=${kubepod_name#* }
+
if [ "$name" != "$labels" ]; then
info "${fn}: cgroup '${id}' has chart name '${name}', labels '${labels}"
+ NAME="$name"
+ LABELS="$labels"
else
info "${fn}: cgroup '${id}' has chart name '${NAME}'"
+ NAME="$name"
fi
EXIT_CODE=$EXIT_SUCCESS
;;
@@ -512,6 +530,7 @@ EXIT_RETRY=2
EXIT_DISABLE=3
EXIT_CODE=$EXIT_SUCCESS
NAME=
+LABELS=
# -----------------------------------------------------------------------------
@@ -591,7 +610,13 @@ if [ -z "${NAME}" ]; then
[ ${#NAME} -gt 100 ] && NAME="${NAME:0:100}"
fi
-info "cgroup '${CGROUP}' is called '${NAME}'"
-echo "${NAME}"
+NAME="${NAME// /_}"
+
+info "cgroup '${CGROUP}' is called '${NAME}', labels '${LABELS}'"
+if [ -n "$LABELS" ]; then
+ echo "${NAME} ${LABELS}"
+else
+ echo "${NAME}"
+fi
exit ${EXIT_CODE}