#!/usr/bin/python
# -*- coding: utf-8 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | 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.

# <<<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',
}
