-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_bgp.py
executable file
·93 lines (78 loc) · 2.66 KB
/
check_bgp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
#
# @descr Checks BGP status on Cisco IOS devices
#
# @author Johan Hedberg <[email protected]>
#
import argparse
import ipaddress
import sys
from lib.cnh_nm import STATE_OK, STATE_CRIT, STATE_WARN
from lib.cnh_nm import my_snmp_walk, snmp_oid_decode_ip, snmpresult_to_dict
from lib.cnh_nm import trigger_not_ok, check_if_ok
# Argument parsing
parser = argparse.ArgumentParser(description='Check iBGP session status')
parser.add_argument('-C', metavar='<community>', required=True,
help='SNMP Community')
parser.add_argument('-H', metavar='<host>', required=True,
help='Host to check')
parser.add_argument('-p', metavar='<peer>', required=True,
help='Peer to check')
args = parser.parse_args()
# Expand IPv6
orig_args_p = args.p
if ':' in args.p:
addr = ipaddress.ip_address(unicode(args.p))
args.p = addr.exploded.lower()
oids = [
'CISCO-BGP4-MIB::cbgpPeer2AdminStatus',
'CISCO-BGP4-MIB::cbgpPeer2State',
'CISCO-BGP4-MIB::cbgpPeer2LastErrorTxt',
'CISCO-BGP4-MIB::cbgpPeer2RemoteAs'
]
# Get all BGP peers
rawdata = my_snmp_walk(args, oids)
data = snmpresult_to_dict(rawdata)
# Now loop over data, and check the states
status = STATE_OK
statusstr = ''
peer_found = False
remote_as = ""
for index, peer in data.iteritems():
peer_ip = snmp_oid_decode_ip(index)
if peer_ip != args.p:
continue
peer_found = True
admin_state = peer['cbgpPeer2AdminStatus'].value
bgp_state = peer['cbgpPeer2State'].value
last_error = peer['cbgpPeer2LastErrorTxt'].value
remote_as = peer['cbgpPeer2RemoteAs'].value
if not last_error.strip():
last_error = 'None'
admin_state = int(str(admin_state))
if admin_state == 1: # Down
status, statusstr = trigger_not_ok(
status,
statusstr,
STATE_WARN,
"{}(AS{}) admin down".format(orig_args_p, remote_as))
continue
bgp_state = int(str(bgp_state))
if bgp_state in [0, 1, 2, 3, 4, 5]: # none/idle/connect/active/opensent/openconfirm
status, statusstr = trigger_not_ok(
status,
statusstr,
STATE_CRIT,
"{}(AS{}) BGP session down".format(orig_args_p, remote_as))
continue
statusstr = last_error
if not peer_found:
status, statusstr = trigger_not_ok(
status,
statusstr,
STATE_CRIT,
"BGP session for peer {} not found!".format(orig_args_p))
# All checks completed, exiting with the relevant message
check_if_ok(status, statusstr)
print "OK: BGP session with {}(AS{}) established, last error: {}".format(orig_args_p, remote_as, statusstr)
sys.exit(STATE_OK)