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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.jolokia import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_jolokia_jvm_garbagecollectors(info):
    parsed = {}
    for instance, _mbean, bulk_data in parse_jolokia_json_output(info):
        for data in bulk_data.values():
            name = data.get("Name")
            if not name:
                continue
            item = "%s GC %s" % (instance, name)
            parsed.setdefault(item, {}).update(data)

    return parsed


@discover
def discover_jolokia_jvm_garbagecollectors(_item, data):
    return -1 not in (data.get("CollectionCount", -1), data.get("CollectionTime", -1))


def transform_units(params):
    """transform 1/min to 1/s and ms/min to %, pre 1.7.0 rules."""
    if "collection_time" in params:
        # new params already!
        return params

    new_params = {}
    if "CollectionTime" in params:
        ms_per_min = params["CollectionTime"]
        new_params["collection_time"] = (ms_per_min[0] / 600.0, ms_per_min[1] / 600.0)
    if "CollectionCount" in params:
        count_rate_per_min = params["CollectionCount"]
        new_params["collection_count"] = (
            count_rate_per_min[0] / 60.0,
            count_rate_per_min[1] / 60.0,
        )
    return new_params


@get_parsed_item_data
def check_jolokia_jvm_garbagecollectors(item, params, data):

    now = time.time()
    try:
        count = data["CollectionCount"]
        ctime = data["CollectionTime"]
    except KeyError:
        return

    try:
        count_rate = get_rate("%s.count" % item, now, count)
    finally:  # initalize 2nd counter!
        ctime_rate = get_rate("%s.time" % item, now, ctime)

    params = transform_units(params)

    yield check_levels(
        count_rate,
        "jvm_garbage_collection_count",
        params.get("collection_count"),
        unit="/s",
        infoname="Garbage collections",
    )

    yield check_levels(
        ctime_rate * 10.0,  # ms/s -> %
        "jvm_garbage_collection_time",
        params.get("collection_time"),
        unit="%",
        infoname="Time spent collecting garbage",
    )


check_info["jolokia_jvm_garbagecollectors"] = {
    "service_description": "JVM %s",
    "parse_function": parse_jolokia_jvm_garbagecollectors,
    "inventory_function": discover_jolokia_jvm_garbagecollectors,
    "check_function": check_jolokia_jvm_garbagecollectors,
    "group": "jvm_gc",
    "has_perfdata": True,
}
