diff options
Diffstat (limited to 'collectors/cgroups.plugin/cgroup-name.sh.in')
-rwxr-xr-x | collectors/cgroups.plugin/cgroup-name.sh.in | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/collectors/cgroups.plugin/cgroup-name.sh.in b/collectors/cgroups.plugin/cgroup-name.sh.in index c470870b53..97e030852e 100755 --- a/collectors/cgroups.plugin/cgroup-name.sh.in +++ b/collectors/cgroups.plugin/cgroup-name.sh.in @@ -64,6 +64,28 @@ function docker_get_name_api() { return 0 } +function k8s_get_name() { + # Take the last part of the delimited path identifier (expecting either _ or / as a delimiter). + local id="${1##*_}" + if [ "${id}" == "${1}" ]; then + id="${1##*/}" + fi + KUBE_TOKEN="$(</var/run/secrets/kubernetes.io/serviceaccount/token)" + NAME="$( +curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" "https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/pods" | +jq -r '.items[] | "k8s_\(.metadata.namespace)_\(.metadata.name)_\(.metadata.uid)_\(.status.containerStatuses[0].name) \(.status.containerStatuses[0].containerID)"' | +grep "$id" | +cut -d' ' -f1 +)" + if [ -z "${NAME}" ]; then + warning "cannot find the name of k8s pod with containerID '${id}'. Setting name to ${id} and disabling it" + NAME="${id}" + NAME_NOT_FOUND=3 + else + info "k8s containerID '${id}' has chart name (namespace_podname_poduid_containername) '${NAME}'" + fi +} + function docker_get_name() { local id="${1}" if hash docker 2>/dev/null; then @@ -73,7 +95,7 @@ function docker_get_name() { fi if [ -z "${NAME}" ]; then warning "cannot find the name of docker container '${id}'" - NAME_NOT_FOUND=1 + NAME_NOT_FOUND=2 NAME="${id:0:12}" else info "docker container '${id}' is named '${NAME}'" @@ -89,6 +111,7 @@ function docker_validate_id() { fi } + # ----------------------------------------------------------------------------- [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@" @@ -118,25 +141,22 @@ for CONFIG in "${NETDATA_USER_CONFIG_DIR}/cgroups-names.conf" "${NETDATA_STOCK_C fi done +if [ -z "${NAME}" ] && [ -n "${KUBERNETES_SERVICE_HOST}" ] && [ -n "${KUBERNETES_PORT_443_TCP_PORT}" ] && [[ ${CGROUP} =~ ^.*kubepods.* ]]; then + k8s_get_name "${CGROUP}" +fi + if [ -z "${NAME}" ]; then if [[ ${CGROUP} =~ ^.*docker[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then # docker containers #shellcheck disable=SC1117 DOCKERID="$(echo "${CGROUP}" | sed "s|^.*docker[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|")" docker_validate_id "${DOCKERID}" - elif [[ ${CGROUP} =~ ^.*ecs[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]; then # ECS #shellcheck disable=SC1117 DOCKERID="$(echo "${CGROUP}" | sed "s|^.*ecs[-_/].*[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|")" docker_validate_id "${DOCKERID}" - elif [[ ${CGROUP} =~ ^.*kubepods[_/].*[_/]pod[a-fA-F0-9-]+[_/][a-fA-F0-9]+$ ]]; then - # kubernetes - #shellcheck disable=SC1117 - DOCKERID="$(echo "${CGROUP}" | sed "s|^.*kubepods[_/].*[_/]pod[a-fA-F0-9-]\+[_/]\([a-fA-F0-9]\+\)$|\1|")" - docker_validate_id "${DOCKERID}" - elif [[ ${CGROUP} =~ machine.slice[_/].*\.service ]]; then # systemd-nspawn NAME="$(echo "${CGROUP}" | sed 's/.*machine.slice[_\/]\(.*\)\.service/\1/g')" @@ -177,8 +197,5 @@ fi info "cgroup '${CGROUP}' is called '${NAME}'" echo "${NAME}" -if [ "${NAME_NOT_FOUND}" -eq 1 ]; then - exit 2 -else - exit 0 -fi +exit ${NAME_NOT_FOUND} + |