#!/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.filerdisks 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.ibm_svc import *  # pylint: disable=wildcard-import,unused-wildcard-import

# Agent output:
# <<<ibm_svc_disk:sep(58)>>>
# 0:online::member:sas_hdd:558.4GB:7:V7BRZ_mdisk08:4:1:24::
# 1:online::member:sas_hdd:558.4GB:7:V7BRZ_mdisk08:3:1:23::
# 2:online::member:sas_hdd:558.4GB:7:V7BRZ_mdisk08:2:1:22::
# 3:online::member:sas_hdd:558.4GB:7:V7BRZ_mdisk08:1:1:21::
# 4:online::member:sas_hdd:558.4GB:7:V7BRZ_mdisk08:0:1:20::
# 5:online::member:sas_hdd:558.4GB:8:V7BRZ_mdisk09:4:5:4::
# 6:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:6:1:18::
# 7:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:5:1:17::
# 8:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:4:1:16::
# 9:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:3:1:15::
# 10:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:2:1:14::
# 11:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:1:1:13::
# 12:online::member:sas_hdd:558.4GB:1:V7BRZ_mdisk02:0:1:12::
# 13:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:6:1:10::
# 14:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:7:1:11::
# 15:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:5:1:9::
# 16:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:3:1:7::
# 17:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:4:1:8::
# 18:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:2:1:6::
# 19:online::member:sas_hdd:558.4GB:16:V7BRZ_mdisk19:1:1:5::

# newer versions report an additional column
# 0:online::member:sas_hdd:558.4GB:7:V7RZ_mdisk8:4:1:24:::inactive
# 1:online::member:sas_hdd:558.4GB:7:V7RZ_mdisk8:3:1:23:::inactive

factory_settings["filer_disks_default_levels"] = FILER_DISKS_CHECK_DEFAULT_PARAMETERS


def parse_ibm_svc_disks(info):
    dflt_header = [
        "id",
        "status",
        "error_sequence_number",
        "use",
        "tech_type",
        "capacity",
        "mdisk_id",
        "mdisk_name",
        "member_id",
        "enclosure_id",
        "slot_id",
        "auto_manage",
        "drive_class_id",
    ]
    parsed = []
    for rows in parse_ibm_svc_with_header(info, dflt_header).values():
        parsed.extend(rows)
    return parsed


def check_ibm_svc_disks(_no_item, params, parsed):
    disks = []
    for data in parsed:
        status = data["status"]
        use = data["use"]
        capacity = data["capacity"]

        disk = {}
        disk["identifier"] = "Enclosure: %s, Slot: %s, Type: %s" % (
            data["enclosure_id"],
            data["slot_id"],
            data["tech_type"],
        )

        if capacity.endswith("GB"):
            disk["capacity"] = float(capacity[:-2]) * 1024 * 1024 * 1024
        elif capacity.endswith("TB"):
            disk["capacity"] = float(capacity[:-2]) * 1024 * 1024 * 1024 * 1024
        elif capacity.endswith("PB"):
            disk["capacity"] = float(capacity[:-2]) * 1024 * 1024 * 1024 * 1024 * 1024

        # Failure State can be found here, also spare is a state here
        disk["state"] = use
        if status == "offline" and use != "failed":
            disk["state"] = "offline"

        disk["type"] = ""  # We dont have a type

        disks.append(disk)

    return check_filer_disks(disks, params)


check_info["ibm_svc_disks"] = {
    "parse_function": parse_ibm_svc_disks,
    "check_function": check_ibm_svc_disks,
    "inventory_function": discover_single,
    "service_description": "Disk Summary",
    "group": "netapp_disks",
    "default_levels_variable": "filer_disks_default_levels",
    "has_perfdata": True,
}
