#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | 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.


def parse_docker_node_info(info):
    version = docker_get_version(info)  # pylint: disable=undefined-variable
    if version is None:
        return parse_legacy_docker_node_info(info)  # pylint: disable=undefined-variable

    if len(info) < 2:
        return {}

    loaded = {}
    for line in info[1:]:
        loaded.update(docker_json_get_obj(line) or {})  # pylint: disable=undefined-variable
    return loaded


def discover_docker_node_info(info):
    if info:
        yield HostLabel(u"cmk/docker_object", u"node")
        yield (None, {})


@append_deprecation_warning  # pylint: disable=undefined-variable
def check_docker_node_info(_no_item, _no_params, parsed):
    if "Name" in parsed:
        yield 0, 'Daemon running on host %s' % parsed["Name"]
    for state, key in enumerate(("Warning", "Critical", "Unknown"), 1):
        if key in parsed:
            yield state, parsed[key]


check_info['docker_node_info'] = {
    'includes': ['docker.include', 'legacy_docker.include'],
    'parse_function': parse_docker_node_info,
    'inventory_function': discover_docker_node_info,
    'check_function': check_docker_node_info,
    'service_description': 'Docker node info',
}


@append_deprecation_warning  # pylint: disable=undefined-variable
def check_docker_node_containers(_no_item, params, parsed):

    for title, key, levels_prefix in (
        ("containers", "Containers", ''),
        ("running", "ContainersRunning", 'running_'),
        ("paused", "ContainersPaused", 'paused_'),
        ("stopped", "ContainersStopped", 'stopped_'),
    ):

        count = parsed.get(key)
        if count is None:
            yield 3, "%s: count not present in agent output" % title.title()
            continue

        levels = params.get('%supper_levels' % levels_prefix, (None, None))
        levels_lower = params.get('%slower_levels' % levels_prefix, (None, None))
        yield check_levels(count,
                           title,
                           levels + levels_lower,
                           human_readable_func=lambda x: "%d" % x,
                           infoname=title.title())


check_info["docker_node_info.containers"] = {
    "inventory_function": discover_single,
    "check_function": check_docker_node_containers,
    "service_description": "Docker containers",
    "has_perfdata": True,
    "group": "docker_node_containers",
    'includes': ['docker.include', 'legacy_docker.include'],
}
