summaryrefslogtreecommitdiffstats
path: root/plugins.d/cgroup-name.sh
blob: a1e3abe081ad2825fb2edb5321f1c9eb4a2a38db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash

# netdata
# real-time performance and health monitoring, done right!
# (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
# GPL v3+
#
# Script to find a better name for cgroups
#

export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
export LC_ALL=C

# -----------------------------------------------------------------------------

PROGRAM_NAME="$(basename "${0}")"

logdate() {
    date "+%Y-%m-%d %H:%M:%S"
}

log() {
    local status="${1}"
    shift

    echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"

}

warning() {
    log WARNING "${@}"
}

error() {
    log ERROR "${@}"
}

info() {
    log INFO "${@}"
}

fatal() {
    log FATAL "${@}"
    exit 1
}

debug=0
debug() {
    [ $debug -eq 1 ] && log DEBUG "${@}"
}

# -----------------------------------------------------------------------------

NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
CONFIG="${NETDATA_CONFIG_DIR}/cgroups-names.conf"
CGROUP="${1}"
NAME=

# -----------------------------------------------------------------------------

if [ -z "${CGROUP}" ]
    then
    fatal "called without a cgroup name. Nothing to do."
fi

if [ -f "${CONFIG}" ]
    then
    NAME="$(grep "^${CGROUP} " "${CONFIG}" | sed "s/[[:space:]]\+/ /g" | cut -d ' ' -f 2)"
    if [ -z "${NAME}" ]
        then
        info "cannot find cgroup '${CGROUP}' in '${CONFIG}'."
    fi
#else
#   info "configuration file '${CONFIG}' is not available."
fi

function get_name_classic {
    local DOCKERID="$1"
    info "Running command: docker ps --filter=id=\"${DOCKERID}\" --format=\"{{.Names}}\""
    NAME="$( docker ps --filter=id="${DOCKERID}" --format="{{.Names}}" )"
    return 0
}

function get_name_api {
    local DOCKERID="$1"
    if [ ! -S "/var/run/docker.sock" ]
        then
        warning "Can't find /var/run/docker.sock"
        return 1
    fi
    info "Running API command: /containers/${DOCKERID}/json"
    JSON=$(echo -e "GET /containers/${DOCKERID}/json HTTP/1.0\r\n" | nc -U /var/run/docker.sock | egrep '^{.*')
    NAME=$(echo $JSON | jq -r .Name,.Config.Hostname | grep -v null | head -n1 | sed 's|^/||')
    return 0
}

if [ -z "${NAME}" ]
    then
    if [[ "${CGROUP}" =~ ^.*docker[-_/\.][a-fA-F0-9]+[-_\.]?.*$ ]]
        then
        # docker containers

        DOCKERID="$( echo "${CGROUP}" | sed "s|^.*docker[-_/]\([a-fA-F0-9]\+\)[-_\.]\?.*$|\1|" )"
        # echo "DOCKERID=${DOCKERID}"

        if [ ! -z "${DOCKERID}" -a \( ${#DOCKERID} -eq 64 -o ${#DOCKERID} -eq 12 \) ]
            then
            if hash docker 2>/dev/null
                then
                get_name_classic $DOCKERID
            else
                get_name_api $DOCKERID || get_name_classic $DOCKERID
            fi
            if [ -z "${NAME}" ]
                then
                warning "cannot find the name of docker container '${DOCKERID}'"
                NAME="${DOCKERID:0:12}"
            else
                info "docker container '${DOCKERID}' is named '${NAME}'"
            fi
        fi
    elif [[ "${CGROUP}" =~ machine.slice_machine.*-qemu ]]
        then
        # libvirtd / qemu virtual machines

        NAME="$(echo ${CGROUP} | sed 's/machine.slice_machine.*-qemu//; s/\/x2d//; s/\/x2d/\-/g; s/\.scope//g')"
    fi

    [ -z "${NAME}" ] && NAME="${CGROUP}"
    [ ${#NAME} -gt 100 ] && NAME="${NAME:0:100}"
fi

info "cgroup '${CGROUP}' is called '${NAME}'"
echo "${NAME}"