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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.ddn_s2a import *  # pylint: disable=wildcard-import,unused-wildcard-import

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.uptime import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_ddn_s2a_uptime(info):
    return {key: value[0] for key, value in parse_ddn_s2a_api_response(info).items()}


def inventory_ddn_s2a_uptime(parsed):
    return [(None, {})]


def check_ddn_s2a_uptime(_no_item, params, parsed):
    uptime_years = int(parsed["uptime_years"])
    uptime_days = int(parsed["uptime_days"])
    uptime_hours = int(parsed["uptime_hours"])
    uptime_minutes = int(parsed["uptime_minutes"])

    uptime_sec = 60 * (
        uptime_minutes + 60 * (uptime_hours + 24 * (uptime_days + 365 * uptime_years))
    )
    return check_uptime_seconds(params, uptime_sec)


check_info["ddn_s2a_uptime"] = {
    "parse_function": parse_ddn_s2a_uptime,
    "inventory_function": inventory_ddn_s2a_uptime,
    "check_function": check_ddn_s2a_uptime,
    "service_description": "DDN S2A Power-On Time",  # We don't use "Uptime" as a service description here,
    # because this value is different from the uptime value
    # supplied via SNMP.
    "has_perfdata": True,
    "group": "uptime",
}
