#!/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.

# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.2.4  "Failover LAN Interface"
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.2.6  "Primary unit (this device)"
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.2.7  "Secondary unit"
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.3.4  2
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.3.6  9     < These two values flip during
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.3.7  10    < failover
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.4.4  "LAN_FO GigabitEthernet0/0.777"
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.4.6  "Active unit"
# .1.3.6.1.4.1.9.9.147.1.2.1.1.1.4.7  "Standby unit"

# [['Failover LAN Interface', '2', 'LAN_FO GigabitEthernet0/0.777'], ['Primary unit', '9', 'Active unit'], ['Secondary unit (this device)', '10', 'Standby unit']]

def inventory_cisco_asa_failover(info):
    for name, state, state_txt in info:
        if name.lower() == "failover lan interface" and state == "3":
            # clustering is disabled
            return None

    for deviceentry in info[-2:]:
        if "this device" in deviceentry[0] and not "Failover Off" in deviceentry[2]:
            return [ (None, None) ]


def check_cisco_asa_failover(_no_item, _no_params, info):
    asa_state_names = {
        1   : "other",
        2   : "up",
        3   : "down",
        4   : "error",
        5   : "overTemp",
        6   : "busy",
        7   : "noMedia",
        8   : "backup",
        9   : "active",
        10  : "standby",
    }

    for unit_type, (descr, device_state, state_txt), \
        (other_descr, other_device_state, other_state_txt) in [ ("primary", info[-2], info[-1]),
                                                                ("secondary", info[-1], info[-2]) ]:

        if "this device" in descr:
            state, details = 0, None
            device_state, other_device_state = int(device_state), int(other_device_state)

            if (unit_type == "primary" and device_state == 9) \
                or (unit_type == "secondary" and device_state == 10):
                state = 0
            elif device_state in [ 9, 10 ] and other_device_state in [ 9, 10 ]:
                state = 0
                details = "The cluster is in failover state (Secondary device is active)"
            elif device_state == 4:
                state = 2
                details = "The device reports an error state"
            elif device_state == 9 and other_device_state == 4:
                state = 1
                details = "The other device reports an error state"
            else:
                state = 1
                details = "Unhandled state \"%s\" reported" % asa_state_names.get(device_state, device_state)

            output = "Device is the %s" % state_txt
            if details:
                output += " (%s)" % details

            return state, output


check_info["cisco_asa_failover"]  = {
    "check_function"     : check_cisco_asa_failover,
    "inventory_function" : inventory_cisco_asa_failover,
    "service_description": "Cluster Status",
    "has_perfdata"       : False,
    "snmp_scan_function" : lambda oid: oid(".1.3.6.1.2.1.1.1.0").lower().startswith("cisco adaptive security") \
                                       or "cisco pix security" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "snmp_info"          : (".1.3.6.1.4.1.9.9.147.1.2.1.1.1", [
         "2", # CISCO-FIREWALL-MIB::cfwHardwareInformation
         "3", # CISCO-FIREWALL-MIB::cfwHardwareStatusValue
         "4", # CISCO-FIREWALL-MIB::cfwHardwareStatusDetail
        ]),
}
