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

# Example output:
# <<<msoffice_licenses>>>
# msonline:VISIOCLIENT 11 0 10
# msonline:POWER_BI_PRO 13 0 11
# msonline:WINDOWS_STORE 1000000 0 0
# msonline:ENTERPRISEPACK 1040 1 395
# msonline:FLOW_FREE 10000 0 11
# msonline:EXCHANGESTANDARD 5 0 2
# msonline:POWER_BI_STANDARD 1000000 0 18
# msonline:EMS 1040 0 991
# msonline:RMSBASIC 1 0 0
# msonline:PROJECTPROFESSIONAL 10 0 10
# msonline:ATP_ENTERPRISE 1040 0 988


def parse_msoffice_licenses(info):
    parsed = {}

    for line in info:
        if len(line) != 4:
            continue

        try:
            parsed.setdefault(
                line[0],
                {"active": int(line[1]), "warning_units": int(line[2]), "consumed": int(line[3])},
            )
        except ValueError:
            pass

    return parsed


factory_settings["msoffice_licenses_levels"] = {
    "usage": (80.0, 90.0),
}


@get_parsed_item_data
def check_msoffice_licenses(item, params, item_data):
    lcs_active = item_data["active"]
    lcs_consumed = item_data["consumed"]

    if lcs_active:

        warn, crit = params["usage"]
        warn_abs, crit_abs = None, None
        warn_perc, crit_perc = None, None
        if isinstance(warn, float):
            warn_perc, crit_perc = warn, crit
        else:
            warn_abs, crit_abs = warn, crit

        # the agent plugin also gathers the last 3 unused licenses with no
        # active licenses. To handle this, we only output consumed licenses for
        # licenses with active ones
        yield check_levels(
            lcs_consumed,
            "licenses",
            (warn_abs, crit_abs),
            human_readable_func=int,
            infoname="Consumed licenses",
        )

        yield (
            0,
            "Active licenses: %s" % lcs_active,
            [("licenses_total", lcs_active)],
        )

        usage = lcs_consumed * 100.0 / (lcs_active)
        yield check_levels(
            usage,
            "license_percentage",
            (warn_perc, crit_perc),
            human_readable_func=get_percent_human_readable,
            infoname="Usage",
            boundaries=(0, 100),
        )

    else:
        yield 0, "No active licenses"
        return

    lcs_warning_units = item_data["warning_units"]
    if lcs_warning_units:
        yield 0, " Warning units: %s" % lcs_warning_units


check_info["msoffice_licenses"] = {
    "parse_function": parse_msoffice_licenses,
    "inventory_function": discover(),
    "check_function": check_msoffice_licenses,
    "service_description": "MS Office Licenses %s",
    "has_perfdata": True,
    "group": "msoffice_licenses",
    "default_levels_variable": "msoffice_licenses_levels",
}
