#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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-
# tails. 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.


def inventory_dell_eql_storage(info):
    for line in info:
        yield line[0], {}


def check_dell_eql_storage(item, _no_params, info):
    for name, desc, health_state, raid_state, total_storage, \
            repl_storage, snap_storage, used_storage in info:
        if name == item:
            yield 0, desc

            # Health Status:
            health_states = {
                    "0" :  "Unknown",
                    "1" :  "Normal",
                    "2" :  "Warning",
                    "3" :  "Critical",
            }
            if health_state == "1":
                state = 0
            elif health_state in [ "2", "0" ]:
                state = 1
            else:
                state = 2
            yield state, "Health State: %s" % health_states[health_state]

            # Raid Status
            raid_states = {
                    "1" : "Ok",
                    "2" : "Degraded",
                    "3" : "Verifying",
                    "4" : "Reconstructing",
                    "5" : "Failed",
                    "6" : "CatastrophicLoss",
                    "7" : "Expanding",
                    "8" : "Mirroring",
            }

            if raid_state == "1":
                state = 0
            elif raid_state in [ "3", "4", "7", "8"]:
                state = 1
            else:
                state = 2
            yield state, "Raid State: %s" % raid_states[raid_state]

            # Storage
            total_bytes = int(total_storage) * 1048576
            used_bytes  = int(used_storage) * 1048576
            repl_bytes  = int(repl_storage) * 1048576
            snap_bytes  = int(snap_storage) * 1048576
            perfdata = [("fs_used", used_bytes),("fs_size", total_bytes)]
            yield 0, "Used: %s/%s (Snapshots: %s, Replication: %s)" % \
                    (get_bytes_human_readable(used_bytes), \
                     get_bytes_human_readable(total_bytes),\
                     get_bytes_human_readable(snap_bytes),\
                     get_bytes_human_readable(repl_bytes)),\
                     perfdata



check_info["dell_eql_storage"] = {
    "check_function"        : check_dell_eql_storage,
    "inventory_function"    : inventory_dell_eql_storage,
    "service_description"   : "Storage %s",
    "has_perfdata"          : True,
    "snmp_scan_function"    : lambda oid: "EQL-SUP" in oid(".1.3.6.1.2.1.1.1.0") or \
                                          oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.12740.17"),
    "snmp_info"             : (".1.3.6.1.4.1.12740.2.1", [
                                            "1.1.9.1",  #eqlMemberName
                                            "1.1.7.1",  #eqlMemberDescription
                                            "5.1.1.1",  #eqlMemberHealthStatus
                                            "13.1.1.1", #eqlMemberRaidStatus
                                            "10.1.1.1", #eqlMemberTotalStorage
                                            "10.1.4.1", #eqlMemberReplStorage
                                            "10.1.3.1", #eqlMemberSnapStorage
                                            "10.1.2.1", #eqlMemberUsedStorage
                                        ]),
}
