#!/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.

# <<<appdynamics_web_container:sep(124)>>>
# Hans|http-8180|Error Count:0|Busy Threads:0|Current Threads In Pool:0|Request Count:0|Maximum Threads:200
# Hans|jk-8109|Error Count:0|Request Count:2


def inventory_appdynamics_web_container(info):
    for line in info:
        yield " ".join(line[0:2]), {}


def check_appdynamics_web_container(item, params, info):
    for line in info:
        if item == " ".join(line[0:2]):
            values = {}
            for metric in line[2:]:
                name, value = metric.split(":")
                values[name] = int(value)

            error_count = values.get("Error Count", None)
            request_count = values.get("Request Count", None)

            current_threads = values.get("Current Threads In Pool", None)
            busy_threads = values.get("Busy Threads", None)
            max_threads = values.get("Maximum Threads", None)

            if isinstance(params, tuple):
                warn, crit = params
            else:
                warn, crit = (None, None)

            if current_threads is not None:
                state = 0
                if crit and current_threads >= crit:
                    state = 2
                elif warn and current_threads >= warn:
                    state = 1

                thread_levels_label = ""
                if state > 0:
                    thread_levels_label = " (warn/crit at %d/%d)" % (warn, crit)

                if max_threads is not None:
                    perfdata = [("current_threads", current_threads, warn, crit, 0, max_threads)]
                    threads_percent = 100.0 * current_threads / max(1, max_threads)
                    max_info = " of %d (%.2f%%)" % (max_threads, threads_percent)
                else:
                    perfdata = [("current_threads", current_threads, warn, crit)]
                    max_info = ""
                yield state, "Current threads: %d%s%s" % (
                    current_threads,
                    max_info,
                    thread_levels_label,
                ), perfdata

                if busy_threads is not None:
                    perfdata = [("busy_threads", busy_threads)]
                    yield 0, "Busy threads: %d" % busy_threads, perfdata

            now = time.time()

            if error_count is not None:
                rate_id = "appdynamics_web_container.%s.error" % (item.lower().replace(" ", "_"))
                error_rate = get_rate(rate_id, now, error_count)
                perfdata = [("error_rate", error_rate)]
                yield 0, "Errors: %.2f/sec" % error_rate, perfdata

            if request_count is not None:
                rate_id = "appdynamics_web_container.%s.request" % (item.lower().replace(" ", "_"))
                request_rate = get_rate(rate_id, now, request_count)
                perfdata = [("request_rate", request_rate)]
                yield 0, "Requests: %.2f/sec" % request_rate, perfdata


check_info["appdynamics_web_container"] = {
    "inventory_function": inventory_appdynamics_web_container,
    "check_function": check_appdynamics_web_container,
    "service_description": "AppDynamics Web Container %s",
    "has_perfdata": True,
    "group": "jvm_threads",
}
