#!/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
# example output
#
# <<<storeonce_servicesets:sep(9)>>>
# [1]
# ServiceSet ID   1
# ServiceSet Name Service Set 1
# ServiceSet Alias        SET1
# Serial Number   CZ25132LTD01
# Software Version        3.15.1-1636.1
# Product Class   HPE StoreOnce 4700 Backup
# Capacity in bytes       75952808613643
# Free Space in bytes     53819324528395
# User Data Stored in bytes       305835970141743
# Size On Disk in bytes   19180587585836
# Deduplication Ratio     15.945078260668
# ServiceSet Health Level 1
# ServiceSet Health       OK
# ServiceSet Status       Running
# Replication Health Level        1
# Replication Health      OK
# Replication Status      Running
# Overall Health Level    1
# Overall Health  OK
# Overall Status  Running
# Housekeeping Health Level       1
# Housekeeping Health     OK
# Housekeeping Status     Running
# Primary Node    hpcz25132ltd
# Secondary Node  None
# Active Node     hpcz25132ltd
#
# In newer agent outputs 'capacity' has changed:
# cloudCapacityBytes  0
# cloudDiskBytes  0
# cloudReadWriteLicensedDiskBytes 0
# cloudFreeBytes  0
# cloudUserBytes  0
# localCapacityBytes  136721392009216
# localDiskBytes  47759419043899
# localFreeBytes  85220347674624
# localUserBytes  265622218292968
# combinedCapacityBytes   136721392009216
# combinedDiskBytes   47759419043899
# combinedFreeBytes   85220347674624
# combinedUserBytes   265622218292968


def _get_item(data):
    return data["ServiceSet ID"]


def inventory_storeonce_servicesets(parsed):
    for values in parsed.values():
        yield _get_item(values), {}


def check_storeonce_servicesets(item, params, parsed):
    for values in parsed.values():
        if not item == values["ServiceSet ID"]:
            continue

        if "ServiceSet Alias" in values:
            yield 0, "Alias: %s" % values['ServiceSet Alias']
        elif "ServiceSet Name" in values:
            yield 0, "Name: %s" % values['ServiceSet Name']

        yield 0, "Overall Status: %s, Overall Health: %s" % \
                (values['Overall Status'], values['Overall Health'])

        for component in [
                'ServiceSet Health',
                'Replication Health',
                'Housekeeping Health',
        ]:
            state = translate_storeonce_status(values["%s Level" % component])
            state_readable = "%s: %s" % (component, values[component])
            if state > 0:
                yield state, state_readable


check_info['storeonce_servicesets'] = {
    'parse_function': parse_storeonce_servicesets,
    'inventory_function': inventory_storeonce_servicesets,
    'check_function': check_storeonce_servicesets,
    'service_description': 'ServiceSet %s Status',
}


def check_storeonce_servicesets_capacity(item, params, parsed):
    for values in parsed.values():
        if _get_item(values) == item:
            return check_storeonce_space(item, params, values)


check_info['storeonce_servicesets.capacity'] = {
    'inventory_function': inventory_storeonce_servicesets,
    'check_function': check_storeonce_servicesets_capacity,
    'service_description': "ServiceSet %s Capacity",
    'has_perfdata': True,
    'group': "filesystem",
}
