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

def juniper_bgp_state_create_item(oid_end):
    return re.sub("6\.1\.[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\.1\.", "", oid_end)

def inventory_juniper_bgp_state(info):
    inventory = []
    for oid_end, bgp_state, bgp_operational_state in info:
        inventory.append( (juniper_bgp_state_create_item(oid_end), None) )
    return inventory

def check_juniper_bgp_state(item, _no_params, info):
    bgp_state_map = [             "undefined",   # 0 does not exist
                                  "idle",        # 1
                                  "connect",     # 2
                                  "active",      # 3
                                  "opensent",    # 4
                                  "openconfirm", # 5
                                  "established"] # 6
    bgp_operational_state_map = [ "undefined",   # 0 does not exist
                                  "halted",      # 1
                                  "running"]     # 2
    status = 0
    for oid_end, bgp_state, bgp_operational_state in info:
        peering_partner_ip = juniper_bgp_state_create_item(oid_end)
        bgp_state = int(bgp_state)
        bgp_operational_state = int(bgp_operational_state)

        if peering_partner_ip == item:
            operational_state_error_string = ""
            state_error_string = ""

            if bgp_operational_state != 2:
                status = 1
                operational_state_error_string = "(!)"
            elif bgp_state != 6:
                status = 2
                state_error_string = "(!!)"

            return status, "Status with peer %s is %s%s, operational status: %s%s" \
                   % (peering_partner_ip, \
                   bgp_state_map[bgp_state], state_error_string, \
                   bgp_operational_state_map[bgp_operational_state], \
                   operational_state_error_string)

    return 3, "Peering partner %s not configured" % item

check_info["juniper_bgp_state"] = {
    "check_function"        : check_juniper_bgp_state,
    "inventory_function"    : inventory_juniper_bgp_state,
    "service_description"   : "BGP Status Peer %s",
    "snmp_info"             : ('.1.3.6.1.4.1.2636.5.1.1.2.1.1.1', [ OID_END, # take peering partner IP from this
                                                                    2,       # jnxBgpM2PeerState
                                                                    3 ]),    # jnxBgpM2PeerStatus
                                                                             # (like operational status)
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.2636.1.1.1.2"),
    "has_perfdata"          : False,
}
