Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / juniper_bgp_state
blob7af961fd996b03246377169c53859d6477a695d1
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
28 def _clean_v4_address(chunks):
29 return "%d.%d.%d.%d" % tuple(chunks)
32 def _clean_v6_address(chunks):
33 ichunks = iter('%02x' % int(i) for i in chunks)
34 blocks = ("%s%s" % pair for pair in zip(ichunks, ichunks))
35 stripped_blocks = ('0' if b == '0000' else b.lstrip('0') for b in blocks)
36 v6_string = ':'.join(b for b in stripped_blocks)
38 # replace longest series of zeros
39 for length in range(8, 0, -1):
40 subs = ':'.join('0' * length)
41 if subs in v6_string:
42 shortened = v6_string.replace(subs, '', 1)
43 if not shortened or shortened.startswith(':'):
44 shortened = ':' + shortened
45 if shortened.endswith(':'):
46 shortened += ':'
47 return '[%s]' % shortened
48 return v6_string
51 def juniper_bgp_state_create_item(peering_entry):
52 try:
53 if len(peering_entry) == 4:
54 return _clean_v4_address(peering_entry)
55 elif len(peering_entry) == 16:
56 return _clean_v6_address(peering_entry)
57 except (ValueError, IndexError):
58 pass
59 return ' '.join('%02X' % int(i) for i in peering_entry) # that's what has been in the data
62 def parse_juniper_bgp_state(info):
63 bgp_state_map = {
64 "0": "undefined", # 0 does not exist
65 "1": "idle", # 1
66 "2": "connect", # 2
67 "3": "active", # 3
68 "4": "opensent", # 4
69 "5": "openconfirm", # 5
70 "6": "established", # 6
72 bgp_operational_state_map = {
73 "0": "undefined", # 0 does not exist
74 "1": "halted", # 1
75 "2": "running" # 2
77 parsed = {}
78 for state, operational_state, peering_entry in info:
79 item = juniper_bgp_state_create_item(peering_entry)
80 state_txt = bgp_state_map.get(state.strip(), "undefined")
81 operational_txt = bgp_operational_state_map.get(operational_state.strip(), "undefined")
83 parsed[item] = {
84 "state": state_txt,
85 "operational_state": operational_txt,
87 return parsed
90 @get_parsed_item_data
91 def check_juniper_bgp_state(item, _no_params, data):
92 state = data.get('state', "undefined")
93 operational_state = data.get('operational_state', "undefined")
95 status = {
96 "established": 0,
97 "undefined": 3,
98 }.get(state, 2)
99 # if we're halted, being un-established is fine
100 yield status if operational_state == "running" else 0, \
101 "Status with peer %s is %s" % (item, state)
103 op_status = {
104 "running": 0,
105 "undefined": 3,
106 }.get(operational_state, 1)
107 yield op_status, "operational status: %s" % operational_state
110 check_info["juniper_bgp_state"] = {
111 "parse_function": parse_juniper_bgp_state,
112 "check_function": check_juniper_bgp_state,
113 "inventory_function": discover(),
114 "service_description": "BGP Status Peer %s",
115 "snmp_info": (
116 '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1',
118 2, # jnxBgpM2PeerState
119 3, # jnxBgpM2PeerStatus (like operational status)
120 BINARY(11), # jnxBgpM2PeerRemoteAddr
122 "snmp_scan_function":
123 lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.2636.1.1.1"),