#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


# .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,
    "includes"            : [ "fireeye.include" ]
}


#.
#   .--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",
    "includes"            : [ "fireeye.include" ]
}
