Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / agents / plugins / mk_docker_container_piggybacked
blob36cf98cae6425c1226140c2f36f6ffb026110f5e
1 #!/bin/bash
2 # +------------------------------------------------------------------+
3 # | ____ _ _ __ __ _ __ |
4 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
5 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
6 # | | |___| | | | __/ (__| < | | | | . \ |
7 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
8 # | |
9 # | Copyright Mathias Kettner 2018 mk@mathias-kettner.de |
10 # +------------------------------------------------------------------+
12 # This file is part of Check_MK.
13 # The official homepage is at http://mathias-kettner.de/check_mk.
15 # check_mk is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License as published by
17 # the Free Software Foundation in version 2. check_mk is distributed
18 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
19 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
21 # tails. You should have received a copy of the GNU General Public
22 # License along with GNU Make; see the file COPYING. If not, write
23 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 # Boston, MA 02110-1301 USA.
26 # $REMOTE is exported from check_mk_agent.linux
28 if type docker > /dev/null 2>&1; then
29 NODE_NAME=$(docker info --format "{{json .Name}}")
31 # For the container status, we want information about *all* containers
32 for CONTAINER_ID in $(docker container ls -q --all); do
33 echo "<<<<${CONTAINER_ID}>>>>"
34 docker inspect "$CONTAINER_ID" \
35 --format='{{println "<<<docker_container_status>>>"}}{{json .State}}{{println}}{{println "<<<docker_container_node_name>>>"}}{{println '"$NODE_NAME"'}}{{println "<<<docker_container_labels>>>"}}{{json .Config.Labels}}{{println}}{{println "<<<docker_container_network>>>"}}{{json .NetworkSettings}}{{println}}'
37 if [ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_ID")" = "true" ]; then
38 # Is there a regular agent available in the container? Use it!
40 # Otherwise execute the agent of the node in the context of the container.
41 # Using this approach we should always get at least basic information from
42 # the container.
43 # Once it comes to plugins and custom configuration the user needs to use
44 # a little more complex setup. Have a look at the documentation.
45 AGENT_PATH=$(docker container exec "$CONTAINER_ID" bash -c "type check_mk_agent" 2>/dev/null) || AGENT_PATH=
46 if [ -n "$AGENT_PATH" ]; then
47 docker container exec --env "REMOTE=$REMOTE" "$CONTAINER_ID" check_mk_agent
48 elif docker container exec "$CONTAINER_ID" which bash >/dev/null 2>&1; then
49 docker container exec --env MK_FROM_NODE=1 --env "REMOTE=$REMOTE" -i "$CONTAINER_ID" bash < "$0"
53 echo "<<<<>>>>"
54 done