#!/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.df 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.size_trend 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.storeonce 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
# example output:
#
# <<<storeonce_clusterinfo:sep(9)>>>
# Appliance Name  HPCT15732RTD
# Network Name    10.0.0.240
# Serial Number   CT15732RTD
# Software Version        3.15.1-1636.1
# Product Class   HPE StoreOnce 4700 Backup
# Total Capacity  75952.808613643
# Free Space      53819.324528395
# User Data Stored        305835.97014174
# Size On Disk    19180.587585836
# Total Capacity (bytes)  75952808613643
# Free Space (bytes)      53819324528395
# User Data Stored (bytes)        305835970141743
# Size On Disk (bytes)    19180587585836
# Dedupe Ratio    15.945078260667367
# Cluster Health Level    1
# Cluster Health  OK
# Cluster Status  Running
# Replication Health Level        1
# Replication Health      OK
# Replication Status      Running
# Uptime Seconds  4305030
# sysContact      None
# sysLocation     None
# isMixedCluster  false

#.
#   .--general-------------------------------------------------------------.
#   |                                                  _                   |
#   |                   __ _  ___ _ __   ___ _ __ __ _| |                  |
#   |                  / _` |/ _ \ '_ \ / _ \ '__/ _` | |                  |
#   |                 | (_| |  __/ | | |  __/ | | (_| | |                  |
#   |                  \__, |\___|_| |_|\___|_|  \__,_|_|                  |
#   |                  |___/                                               |
#   +----------------------------------------------------------------------+
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_storeonce_clusterinfo(parsed):
    if "Product Class" in parsed:
        return [(parsed['Product Class'], None)]


def check_storeonce_clusterinfo(item, _no_params, parsed):
    return 0, "Name: %s, Serial Number: %s, Version: %s" % \
                    (parsed['Appliance Name'],
                     parsed['Serial Number'],
                     parsed['Software Version'])


check_info['storeonce_clusterinfo'] = {
    'parse_function': parse_storeonce_clusterinfo,
    'inventory_function': inventory_storeonce_clusterinfo,
    'check_function': check_storeonce_clusterinfo,
    'service_description': '%s',
}

#.
#   .--cluster-------------------------------------------------------------.
#   |                         _           _                                |
#   |                     ___| |_   _ ___| |_ ___ _ __                     |
#   |                    / __| | | | / __| __/ _ \ '__|                    |
#   |                   | (__| | |_| \__ \ ||  __/ |                       |
#   |                    \___|_|\__,_|___/\__\___|_|                       |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_storeonce_clusterinfo_cluster(parsed):
    if "Cluster Health" in parsed:
        return [(None, {})]


def check_storeonce_clusterinfo_cluster(item, params, parsed):
    yield 0, "Cluster Status: %s, Replication Status: %s" % \
            (parsed['Cluster Status'], parsed['Replication Status'])

    # Check state of components
    for component in ['Cluster Health', 'Replication Health']:
        state = translate_storeonce_status(parsed['%s Level' % component])
        state_readable = "%s: %s" % (component, parsed[component])
        if state > 0:
            yield state, state_readable


check_info['storeonce_clusterinfo.cluster'] = {
    'inventory_function': inventory_storeonce_clusterinfo_cluster,
    'check_function': check_storeonce_clusterinfo_cluster,
    'service_description': 'Appliance Status',
}

#.
#   .--cluster space-------------------------------------------------------.
#   |           _           _                                              |
#   |       ___| |_   _ ___| |_ ___ _ __   ___ _ __   __ _  ___ ___        |
#   |      / __| | | | / __| __/ _ \ '__| / __| '_ \ / _` |/ __/ _ \       |
#   |     | (__| | |_| \__ \ ||  __/ |    \__ \ |_) | (_| | (_|  __/       |
#   |      \___|_|\__,_|___/\__\___|_|    |___/ .__/ \__,_|\___\___|       |
#   |                                         |_|                          |
#   +----------------------------------------------------------------------+
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_storeonce_clusterinfo_space(parsed):
    return [('Total Capacity', {})]


check_info['storeonce_clusterinfo.space'] = {
    'inventory_function': inventory_storeonce_clusterinfo_space,
    'check_function': check_storeonce_space,
    'service_description': "%s",
    'has_perfdata': True,
    'group': "filesystem",
    'default_levels_variable': "filesystem_Default_levels",
}

#.
#   .--uptime--------------------------------------------------------------.
#   |                              _   _                                   |
#   |                  _   _ _ __ | |_(_)_ __ ___   ___                    |
#   |                 | | | | '_ \| __| | '_ ` _ \ / _ \                   |
#   |                 | |_| | |_) | |_| | | | | | |  __/                   |
#   |                  \__,_| .__/ \__|_|_| |_| |_|\___|                   |
#   |                       |_|                                            |
#   +----------------------------------------------------------------------+
#   |                                                                      |
#   '----------------------------------------------------------------------'


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


def check_storeonce_clusterinfo_uptime(item, params, parsed):
    uptime = float(parsed['Uptime Seconds'])
    return check_uptime_seconds(params, uptime)


check_info['storeonce_clusterinfo.uptime'] = {
    'inventory_function': inventory_storeonce_clusterinfo_uptime,
    'check_function': check_storeonce_clusterinfo_uptime,
    'service_description': "Uptime",
    'has_perfdata': True,
    'group': "uptime",
}
