-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_int_status_cisco.py
155 lines (134 loc) · 5.47 KB
/
check_int_status_cisco.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
__author__ = 'Devannes Rémi'
__license__ = 'GNU General Public License v3.0'
__version__ = '0.1'
__maintainer__ = 'Devannes Rémi'
__email__ = '[email protected]'
__status__ = 'In development'
import subprocess
import sys
"""
Return codes defined by Nagios in the API
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/pluginapi.html
"""
STATUS_OK = 0
STATUS_WARNING = 1
STATUS_CRITICAL = 2
STATUS_UNKNOWN = 3
USERNAME = 'username'
AUTH_PASSWORD = 'a_password'
PRIVACY_PASSWORD = 'p_password'
def sendSNMP(ip_dest, oid):
"""Send SNMP request by using snmpwalk
"""
output = subprocess.run(['snmpwalk',
'-v', '3',
'-c', 'private',
'-u', USERNAME,
'-a', 'SHA',
'-A', AUTH_PASSWORD,
'-x', 'AES',
'-X', PRIVACY_PASSWORD,
'-l', 'authPriv',
ip_dest,
oid],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True)
if not output.stderr == '':
print("Not receiving any SNMP answer")
sys.exit(STATUS_CRITICAL)
else:
return output.stdout
def getInterfaces(ip_dest):
"""Get interfaces with their indexes in an array of tuple
format (id, intName)
http://oidref.com/1.3.6.1.2.1.2.2.1.1
http://cric.grenoble.cnrs.fr/Administrateurs/Outils/MIBS/?oid=1.3.6.1.2.1.2.2.1.1
"""
interfaces_with_indexes = []
oid = '1.3.6.1.2.1.2.2.1.2' #OID for interface and name
snmp_output = sendSNMP(ip_dest, oid)
liste = snmp_output.split('\n')
liste.remove('')
for i in liste:
temp = i.split(' ')
interfaces_with_indexes.append((temp[0].split('.')[1], temp[-1]))
return interfaces_with_indexes
def checkExistingInterface(interfaces, intName):
"""Check if the entered interface name exists
return his id if she exists
else it print """
for i in interfaces:
if i[1] == intName:
return i[0]
return None
def checkStatusInt(interfaceId, ip_dest):
""" Take an interface ID and check administrative status and operational
status of a single interface by sending SNMP request to the server
return both state in a tuple (admin_status, oper_status)
http://oidref.com/1.3.6.1.2.1.2.2.1.8
http://cric.grenoble.cnrs.fr/Administrateurs/Outils/MIBS/?oid=1.3.6.1.2.1.2.2.1.8
"""
#Adding the interface ID to specify it
oid_admin = '1.3.6.1.2.1.2.2.1.7.' + interfaceId
oid_oper = '1.3.6.1.2.1.2.2.1.8.' + interfaceId
"""Using split on only keep the state of the interface, answer format is
IF-MIB::ifOperStatus.15101 = INTEGER: down(2)
IF-MIB::ifAdminStatus.15101 = INTEGER: down(2)
"""
admin_status = sendSNMP(ip_dest, oid_admin).split(' ')[-1]
oper_status = sendSNMP(ip_dest, oid_oper).split(' ')[-1]
return (admin_status, oper_status)
def main():
#Check if all arguments are passed
if len(sys.argv) < 2:
print("Missing 2 arguments in the command !")
sys.exit(STATUS_UNKNOWN)
elif len(sys.argv) < 3:
print("Missing 1 argument in the command !")
sys.exit(STATUS_UNKNOWN)
else:
ip_dest = sys.argv[1]
intName = sys.argv[2]
interfaces_list = getInterfaces(ip_dest)
interface = checkExistingInterface(interfaces_list, intName)
#Check if an interface has this name
if interface is None:
print("The interface " + intName + " doesn't exist, set exact name, case sensitive")
sys.exit(STATUS_CRITICAL)
admin_status, oper_status = checkStatusInt(interface, ip_dest)
print(repr(admin_status))
print(repr(oper_status))
if admin_status == 'down(2)':
print("Interface administrativement down")
sys.exit(STATUS_WARNING)
elif admin_status == 'testing(3)':
print("Interface en etat de test")
sys.exit(STATUS_CRITICAL)
elif admin_status == 'up(1)':
if oper_status == 'up(1)':
print("Interface " + intName + " is up !")
sys.exit(STATUS_OK)
elif oper_status == 'down(2)':
print("Interface " + intName + " is down !")
sys.exit(STATUS_CRITICAL)
elif oper_status == 'testing(3)':
print("Interface " + intName + " is in testing")
sys.exit(STATUS_CRITICAL)
elif oper_status == 'unknown(4)':
print("Interface " + intName + " is in unknown state")
sys.exit(STATUS_CRITICAL)
elif oper_status == 'dormant(5)':
print("Interface " + intName + " is waiting for external actions")
sys.exit(STATUS_CRITICAL)
elif oper_status == 'notPresent(6)':
print("Interface " + intName + " is not present")
sys.exit(STATUS_CRITICAL)
else:
print("Interface " + intName + " has an unknown issue")
sys.exit(STATUS_CRITICAL)
if __name__ == "__main__":
# execute only if run as a script
main()