#!/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.
#
# Example Output:
# .1.3.6.1.4.1.5951.4.1.1.23.3.0  1
# .1.3.6.1.4.1.5951.4.1.1.23.23.0  1
# .1.3.6.1.4.1.5951.4.1.1.23.24.0  3

netscaler_ha_cur_states = {
    0: ("unknown", 1),
    1: ("initializing", 1), # Indicates that the node is in the process of becoming part of the high
                            # availability configuration.
    2: ("down", 2), # undocumented
    3: ("functional", 0), # Indicates that the node is accessible and can function as either
                          # a primary or secondary node.
    4: ("some HA monitored interfaces failed", 2), # Indicates that one of the high availability monitored
                                                   # interfaces has failed because of a card or link failure.
                                                   # This state triggers a failover.
    5: ("monitorFail", 1),  # undocumented
    6: ("monitorOK", 1),    # undocumented
    7: ("all HA monitored interfaces failed", 2), # Indicates that all the interfaces of the node are
                                                  # unusable because the interfaces on which high
                                                  # availability monitoring is enabled are not connected
                                                  # or are manually disabled. This state triggers a failover.
    8: ("configured to listening mode (dumb)", 1), # Indicates that the node is in listening mode. It does not
                                                   # participate in high availability transitions or transfer
                                                   # configuration from the peer node. This is a configured
                                                   # value, not a statistic.
    9: ("HA status manually disabled", 1), # Indicates that the high availability status of the node has been
                                           # manually disabled. Synchronization and propagation cannot take
                                           # place between the peer nodes.
   10: ("SSL card failed", 2), # Indicates that the SSL card has failed. This state triggers a failover.
   11: ("route monitor has failed", 2), # Indicates that the route monitor has failed. This state triggers
                                        # a failover.
}

netscaler_ha_peer_mode = {
    0: ("standalone", 0),
    1: ("primary", 0),
    2: ("secondary", 0),
    3: ("unknown", 1),
}

def inventory_netscaler_ha(info):
    if info:
        return [ (None, None) ]

def check_netscaler_ha(_no_item, _no_params, info):
    if info:
        state = 0
        peer_state, cur_status, cur_state = map(int, info[0])
        if cur_status == 0:
            infotext = "System not setup for HA"
        else:
            infotext = "State: %s, Neighbour: %s" % (
                                                netscaler_ha_cur_states[cur_state][0],
                                                netscaler_ha_peer_mode[peer_state][0],
                                               )
            state = max(netscaler_ha_cur_states[cur_state][1], netscaler_ha_peer_mode[peer_state][1])

        return state, infotext

check_info["netscaler_ha"] = {
    "check_function"          : check_netscaler_ha,
    "inventory_function"      : inventory_netscaler_ha,
    "service_description"     : "HA Node Status",
    "snmp_info"               : (".1.3.6.1.4.1.5951.4.1.1.23", [ # nsHighAvailabilityGroup
                                            3, # haPeerState
                                           23, # haCurStatus
                                           24, # haCurState
                                        ]),
    "snmp_scan_function"      : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.5951.1"),
}
