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

#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

factory_settings["msoffice_licenses_levels"] = {
    "usage": (80, 90),
}


def inventory_msoffice_licenses(info):
    for line in info:
        if len(line) == 4:
            yield line[0], {}


def check_msoffice_licenses(item, params, info):
    # will be refactored in 1.7
    for line in info:
        if len(line) != 4:
            continue

        name, active, warning_units, consumed = line
        if name != item:
            continue

        try:
            active = int(active)
            warning_units = int(warning_units)
            consumed = int(consumed)
        except ValueError:
            yield 3, "Invalid data in agent output"
            return

        if active:
            usage = consumed * 100.0 / (active)

            state = 0
            infotext = "Consumed: %s of %s (%.2f%%)" % (consumed, active, usage)
            warn, crit = params["usage"]
            if usage >= crit:
                state = 2
            elif usage >= warn:
                state = 1
            if state:
                infotext += " (warn/crit at %s%%/%s%%)" % (warn, crit)

            yield (state, infotext, [("licenses", consumed), ("licenses_total", active),
                                     ("license_percentage", usage, warn, crit, 0, 100)])
        else:
            yield 0, "No active licenses"
            return

        if warning_units:
            yield 0, " Warning units: %s" % warning_units


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