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





# netappFiler(1)
# netappClusteredFiler(3)
#                sysStat(2) cf(3)     cfSettings(1)
#                                     cfState(2)
#                                     cfCannotTakeoverCause(3)
#                                     cfPartnerStatus(4)
#                                     cfPartnerName(6)
#                                     cfInterconnectStatus(8)
# SNMPv2-SMI::enterprises.789.1.2.3.1.0 = INTEGER: 2
# SNMPv2-SMI::enterprises.789.1.2.3.2.0 = INTEGER: 2
# SNMPv2-SMI::enterprises.789.1.2.3.3.0 = INTEGER: 1
# SNMPv2-SMI::enterprises.789.1.2.3.4.0 = INTEGER: 2
# SNMPv2-SMI::enterprises.789.1.2.3.6.0 = STRING: "ZMUCFB"
# SNMPv2-SMI::enterprises.789.1.2.3.8.0 = INTEGER: 4



def inventory_netapp_cluster(info):
    inventory = []
    if info:
        cfSettings, cfState, cfCannotTakeoverCause, cfPartnerStatus, cfPartnerName, cfInterconnectStatus = info[0]
        # only inventorizes clusters that dont have takeover disabled.
        if int(cfSettings) not in [1, 3]:
            # Include the cluster partner name in inventory (value added data)
            inventory.append ((cfPartnerName, None))
        return inventory

def check_netapp_cluster(item, _no_params, info):

    cfSettings, cfState, cfCannotTakeoverCause, cfPartnerStatus, cfPartnerName, cfInterconnectStatus = info[0]


    # first handle all critical states.
    # "dead" and "thisNodeDead"
    if   cfState == "1" or cfSettings == "5":
        return (2, "Node is declared dead by cluster")
    elif cfPartnerStatus in [1, 3]:
        return (2, "Partner Status is dead or maybeDown")
    elif cfInterconnectStatus == "2":
        return (2, "Cluster Interconnect failure")


    # then handle warnings.
    elif cfSettings in [3, 4] or cfState == "3":
        return (1, "Cluster takeover is disabled")
    elif cfInterconnectStatus == "partialFailure":
        return (1, "Cluster interconnect partially failed")

    # if the partner name has changed, we'd like to issue a warning
    if cfPartnerName != item:
        return (1, "Partner Name %s instead of %s") % (cfPartnerName, item)

    # OK - Cluster enabled, Cluster can takeover and the partner is OK and the
    # infiniband interconnect is working.
    if cfSettings == "2" and cfState == "2" \
        and cfCannotTakeoverCause == "1" and cfPartnerStatus == "2" \
        and cfInterconnectStatus == "4":
        return (0, "Cluster Status is OK")

    # if we reach here, we hit an unknown case.
    return (3, "Got unhandled information")

check_info["netapp_cluster"] = {
    'check_function':          check_netapp_cluster,
    'inventory_function':      inventory_netapp_cluster,
    'service_description':     'metrocluster_w_%s',
    'snmp_info':               ('.1.3.6.1.4.1.789.1.2.3', [
                                    '1', # cfSettings
                                    '2', # cfState
                                    '3', # cfCannotTakeoverCause
                                    '4', # cfPartnerStatus
                                    '6', # cfPartnerName
                                    '8', # cfInterconnectStatus
                               ]),
    'snmp_scan_function':      \
    # Run inventory only on Data Ontap OS with cluster enabled
     lambda oid: "netapp release" in oid(".1.3.6.1.2.1.1.1.0").lower() and \
                 oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.789"),
}
