#!/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.fireeye import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .1.3.6.1.4.1.25597.11.2.1.1.0 Good --> FE-FIREEYE-MIB::feRaidStatus.0
# .1.3.6.1.4.1.25597.11.2.1.2.0 1 --> FE-FIREEYE-MIB::feRaidIsHealthy.0
# .1.3.6.1.4.1.25597.11.2.1.3.1.2.1 0
# .1.3.6.1.4.1.25597.11.2.1.3.1.2.2 1
# .1.3.6.1.4.1.25597.11.2.1.3.1.3.1 Online
# .1.3.6.1.4.1.25597.11.2.1.3.1.3.2 Online
# .1.3.6.1.4.1.25597.11.2.1.3.1.4.1 1
# .1.3.6.1.4.1.25597.11.2.1.3.1.4.2 1

#   .--RAID----------------------------------------------------------------.
#   |                      ____      _    ___ ____                         |
#   |                     |  _ \    / \  |_ _|  _ \                        |
#   |                     | |_) |  / _ \  | || | | |                       |
#   |                     |  _ <  / ___ \ | || |_| |                       |
#   |                     |_| \_\/_/   \_\___|____/                        |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def parse_fireeye_raid(info):
    # We only discover in case of a raid system
    parsed = {}
    if len(info[1]) > 1:
        for diskname, diskstatus, diskhealth in info[1]:
            parsed.setdefault("raid", info[0][0])
            parsed.setdefault("disks", [])
            parsed["disks"].append([diskname, diskstatus, diskhealth])

    return parsed


def check_fireeye_raid(_no_item, _no_params, parsed):
    status, health = parsed["raid"]
    for text, (state, state_readable) in check_fireeye_states([(status, 'Status'),
                                                               (health, 'Health')]).items():
        yield state, "%s: %s" % (text, state_readable)


check_info["fireeye_raid"] = {
    "parse_function"      : parse_fireeye_raid,
    "inventory_function"  : lambda parsed: \
        inventory_fireeye_generic(parsed.get("raid", []), False),
    "check_function"      : check_fireeye_raid,
    "service_description" : "RAID status",
    "snmp_info"           : [(".1.3.6.1.4.1.25597.11.2.1", [
                                "1",    # FE-FIREEYE_MIB::feRaidStatus
                                "2",    # FE-FIREEYE_MIB::feRaidIsHealthy
                             ]),
                             (".1.3.6.1.4.1.25597.11.2.1.3.1", [
                                "2",    # FE-FIREEYE_MIB::fePhysicalDiskName
                                "3",    # FE-FIREEYE_MIB::fePhysicalDiskStatus
                                "4",    # FE-FIREEYE_MIB::fePhysicalDiskIsHealthy
                            ])],
    "snmp_scan_function"  : scan_fireeye,
}

#.
#   .--disks---------------------------------------------------------------.
#   |                            _ _     _                                 |
#   |                         __| (_)___| | _____                          |
#   |                        / _` | / __| |/ / __|                         |
#   |                       | (_| | \__ \   <\__ \                         |
#   |                        \__,_|_|___/_|\_\___/                         |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def check_fireeye_raid_disks(item, _no_params, parsed):
    for diskname, diskstatus, diskhealth in parsed["disks"]:
        if diskname == item:
            for text, (state, state_readable) in check_fireeye_states([(diskstatus, 'Disk status'),
                                                                       (diskhealth, 'Health')
                                                                      ]).items():
                yield state, "%s: %s" % (text, state_readable)


check_info["fireeye_raid.disks"] = {
    "inventory_function"  : lambda parsed: \
        inventory_fireeye_generic(parsed.get("disks", []), True),
    "check_function"      : check_fireeye_raid_disks,
    "service_description" : "Disk status %s",
}
