From e2e20dad1fc29dcd6bd76b6dc50c80045d0a2956 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 26 Jul 2019 00:38:31 -0700 Subject: Safer container names (#6441) * Allow building without pushing This enables easier local testing * Refactor fetching Docker container names to be safer Fixes #5680 * Wrap shell variable with quotes And change spaces to tabs * Make cgroup-name quieter * Make DOCKER_USR overridable * Update documentation to explain safe usage * Remove recommended image for docker socket proxy * Add capability to pass in a privileged GID * Fix some documentation typos * Update documentation to remove socket reference and clean up wording --- collectors/cgroups.plugin/cgroup-name.sh.in | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'collectors/cgroups.plugin') diff --git a/collectors/cgroups.plugin/cgroup-name.sh.in b/collectors/cgroups.plugin/cgroup-name.sh.in index 48f523885b..784c06042d 100755 --- a/collectors/cgroups.plugin/cgroup-name.sh.in +++ b/collectors/cgroups.plugin/cgroup-name.sh.in @@ -53,18 +53,25 @@ function docker_get_name_classic() { } function docker_get_name_api() { - local id="${1}" - if [ ! -S "${DOCKER_HOST}" ]; then - warning "Can't find ${DOCKER_HOST}" + local path="/containers/${1}/json" + if [ -z "${DOCKER_HOST}" ]; then + warning "No DOCKER_HOST is set" return 1 fi if ! command -v jq >/dev/null 2>&1; then warning "Can't find jq command line tool. jq is required for netdata to retrieve docker container name using ${DOCKER_HOST} API, falling back to docker ps" return 1 fi - - info "Running API command: /containers/${id}/json" - JSON=$(echo -e "GET /containers/${id}/json HTTP/1.0\\r\\n" | nc -U "${DOCKER_HOST}" | grep '^{.*') + if [ -S "${DOCKER_HOST}" ]; then + info "Running API command: curl --unix-socket ${DOCKER_HOST} http://localhost${path}" + JSON=$(curl -sS --unix-socket "${DOCKER_HOST}" "http://localhost${path}") + elif [ "${DOCKER_HOST}" == "/var/run/docker.sock" ]; then + warning "Docker socket was not found at ${DOCKER_HOST}" + return 1 + else + info "Running API command: curl ${DOCKER_HOST}${path}" + JSON=$(curl -sS "${DOCKER_HOST}${path}") + fi NAME=$(echo "$JSON" | jq -r .Name,.Config.Hostname | grep -v null | head -n1 | sed 's|^/||') return 0 } -- cgit v1.2.3