#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
import collections


def inventory_k8s_pod_container(parsed):
    if parsed:
        yield (None, {})


def check_k8s_pod_container(_no_item, params, parsed):
    total = len(parsed)
    ready = sum(container['ready'] for container in parsed.values())
    state = collections.defaultdict(int)
    exit_code = 0
    for container in parsed.values():
        state[container["state"]] += 1
        exit_code = max(exit_code, 0 if int(container["state_exit_code"]) == 0 else 2)
    perfdata = [
        ('docker_all_containers', total, None, None, 0, total),
        ('ready_containers', ready, None, None, 0, total),
    ]
    yield 0, "Ready: %d/%d" % (ready, total), perfdata
    yield 0, "Running: %d" % state["running"]
    yield 0, "Waiting: %d" % state["waiting"]
    yield 0 if not exit_code else 2, "Terminated: %d" % state["terminated"]


check_info['k8s_pod_container'] = {
    # section is already migrated
    'inventory_function': inventory_k8s_pod_container,
    'check_function': check_k8s_pod_container,
    'service_description': 'Container',
}
