#!/bin/bash
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2018             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# $REMOTE is exported from check_mk_agent.linux

if type docker > /dev/null 2>&1; then
    NODE_NAME=$(docker info --format "{{json .Name}}")

    # For the container status, we want information about *all* containers
    for CONTAINER_ID in $(docker container ls -q --all); do
        echo "<<<<${CONTAINER_ID}>>>>"
        docker inspect "$CONTAINER_ID" \
            --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}}'

        if [ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_ID")" = "true" ]; then
            # Is there a regular agent available in the container? Use it!
            #
            # Otherwise execute the agent of the node in the context of the container.
            # Using this approach we should always get at least basic information from
            # the container.
            # Once it comes to plugins and custom configuration the user needs to use
            # a little more complex setup. Have a look at the documentation.
            AGENT_PATH=$(docker container exec "$CONTAINER_ID" bash -c "type check_mk_agent" 2>/dev/null) || AGENT_PATH=
            if [ -n "$AGENT_PATH" ]; then
                docker container exec --env "REMOTE=$REMOTE" "$CONTAINER_ID" check_mk_agent
            elif docker container exec "$CONTAINER_ID" which bash >/dev/null 2>&1; then
                docker container exec --env MK_FROM_NODE=1 --env "REMOTE=$REMOTE" -i "$CONTAINER_ID" bash < "$(which check_mk_agent)"
            fi
        fi

        echo "<<<<>>>>"
    done
fi
