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

# <<<win_license>>>
#
# Name: Windows(R) 7, Enterprise edition
# Description: Windows Operating System - Windows(R) 7, TIMEBASED_EVAL channel
# Partial Product Key: JCDDG
# License Status: Initial grace period
# Time remaining: 11820 minute(s) (8 day(s))

factory_settings["windows_license_default_levels"] = {
    "status": ["Licensed", "Initial grace period"],
    "expiration_time": (14 * 24 * 60 * 60, 7 * 24 * 60 * 60),
}


def parse_win_license(info):
    parsed = {}
    for line in info:
        if len(line) == 0:
            continue

        if line[0] == "License":
            parsed["License"] = " ".join(line[2:])

        # Depending on Windows version this variable reads
        # Time remaining or Volume activation expiration or is not present
        if line[0] in ["Time", "Volume"]:
            expiration = " ".join(line).split(":")[1].strip()
            parsed["expiration"] = expiration
            time_left_re = regex(r"(\d+) minute")
            time_left = int(time_left_re.search(expiration).group(1)) * 60
            parsed["expiration_time"] = time_left
    return parsed


def inventory_win_license(parsed):
    if "License" in parsed:
        return [(None, {})]


def check_win_license(_item, params, parsed):
    sw_license = parsed.get("License", None)

    if not sw_license:
        return

    message = "Software is %s" % sw_license

    license_state = 0 if sw_license in params["status"] else 2

    if license_state:
        message += " Required: " + " ".join(params["status"])

    yield license_state, message

    time_left = parsed.get("expiration_time", None)

    if not time_left:
        return

    time_message = "License will expire in %s" % get_age_human_readable(time_left)

    warn, crit = params["expiration_time"]

    time_state = 0

    if time_left < crit:
        time_state = 2
    elif time_left < warn:
        time_state = 1

    if time_state:
        time_message += " (warn/crit at %s/%s)" % tuple(map(get_age_human_readable, (warn, crit)))

    yield time_state, time_message


check_info["win_license"] = {
    "service_description": "Windows License",
    "parse_function": parse_win_license,
    "inventory_function": inventory_win_license,
    "check_function": check_win_license,
    "group": "win_license",
    "default_levels_variable": "windows_license_default_levels",
}
