-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_juniper_bgp.py
85 lines (67 loc) · 2.6 KB
/
check_juniper_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
#!/usr/bin/env python
#
# -*- coding: utf-8 -*-
#
# Autors: David Hannequin <david.hannequin@fullsave.com>,
# Date: 2015-03-31
# URL: http://www.fullsave.com
#
# Plugins to check BGP IPv4 and IPv6 state.
#
# Shinken plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
#
# Requires: Python >= 2.7
# Requires: Python Paramiko
import os
import re
import argparse
import paramiko
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--hostname', default='', help='ip or hostname for BGP router')
parser.add_argument('-U', '--username', default='nagios', help='ssh user for BGP router')
parser.add_argument('-K', '--key', default='~/.ssh/id_dsa', help='ssh private key for BGP routeur')
parser.add_argument('-P', '--peer', default='', help='bgp peer ipv4 or ipv6')
args = parser.parse_args()
hostname = args.hostname
username = args.username
key = args.key
peer = args.peer
cmd = 'show bgp neighbor ' + peer
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username, key_filename=key)
stdin, stdout, stderr = ssh.exec_command(cmd)
data = stdout.readlines()
if not data:
print ('UNKNOWN - State is UNKNOWN')
raise SystemExit(3)
ssh.close()
as_juniper = data[1]
as_number = as_juniper.split();
as_number = str(as_number[1])
as_jstate = data[2]
as_jstate = as_jstate.split();
as_jstate = str(as_jstate[3])
if as_jstate in 'Established':
print ('OK - %s state is %s' % (as_number, as_jstate.lower()))
raise SystemExit(0)
elif as_jstate in 'Connect' or as_jstate in 'Active' or as_jstate in 'OpenConfirm' or as_jstate in 'OpenSent':
print ('WARNING - %s state is %s' % (as_number, as_jstate.lower()))
raise SystemExit(1)
else:
print ('CRITICAL - %s state is %s' % (as_number, as_jstate.lower()))
raise SystemExit(2)
if __name__ == "__main__":
main()