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

# <<<splunk_license_state>>>
# Splunk_Enterprise_Splunk_Analytics_for_Hadoop_Download_Trial 5 30 524288000 1561977130 VALID
# Splunk_Forwarder 5 30 1048576 2147483647 VALID
# Splunk_Free 3 30 524288000 2147483647 VALID

# expiration_time default is warn/crit 14d/7d

SplunkLicenseState = collections.namedtuple(  # pylint: disable=collections-namedtuple-call
    "lcs",
    [
        "max_violations",
        "window_period",
        "quota",
        "expiration_time",
        "time_to_expiration",
        "status",
    ],
)


def parse_splunk_license_state(info):
    parsed = {}

    for lcs_detail in info:
        try:
            label, max_violations, window_period, quota, expiration_time, status = lcs_detail

            time_to_expiration = float(expiration_time) - time.time()
            parsed.setdefault(label, []).append(
                SplunkLicenseState(
                    max_violations,
                    window_period,
                    get_bytes_human_readable(int(quota)),
                    get_timestamp_human_readable(int(expiration_time)),
                    time_to_expiration,
                    status,
                )
            )

        except (IndexError, ValueError):
            pass

    return parsed


factory_settings["splunk_license_state_default_levels"] = {
    "state": 2,
    "expiration_time": (14 * 24 * 60 * 60, 7 * 24 * 60 * 60),
}


@get_parsed_item_data
def check_splunk_license_state(item, params, item_data):
    data = item_data[0]
    state = 0

    if data.status == "EXPIRED":
        state = params.get("state")

    yield state, "Status: %s" % data.status

    if data.time_to_expiration > 0:
        warn, crit = params["expiration_time"]
        state = 0

        infotext = "Expiration time: %s" % data.expiration_time

        if data.time_to_expiration <= crit:
            state = 2
        elif data.time_to_expiration <= warn:
            state = 1

        if state != 0:
            infotext += " (expires in %s - Warn/Crit at %s/%s)" % (
                get_age_human_readable(data.time_to_expiration),
                get_age_human_readable(warn),
                get_age_human_readable(crit),
            )

        yield state, infotext

    yield 0, "Max violations: %s within window period of %s Days, Quota: %s" % (
        data.max_violations,
        data.window_period,
        data.quota,
    )


check_info["splunk_license_state"] = {
    "parse_function": parse_splunk_license_state,
    "check_function": check_splunk_license_state,
    "inventory_function": discover(),
    "service_description": "Splunk License %s",
    "group": "splunk_license_state",
    "default_levels_variable": "splunk_license_state_default_levels",
}
