-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinterface_down_since.py
More file actions
130 lines (116 loc) · 4.94 KB
/
Copy pathinterface_down_since.py
File metadata and controls
130 lines (116 loc) · 4.94 KB
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
#!/usr/bin/env python
'''
This script collects SNMP parameters from Aruba Networks Provision switches to determine downtime of a port.
When script is executed you can provide following parameters:
-c for SNMP COMMUNITY name (v3 is not included yet)
-p for SNMP port default will be 161
-i for ip range for e.g. 192.168.56-57.1 will generate IP's 192.168.56.1, 192.168.57.1
Thanks to Kirk Byers for simplfy snmp within Python with snmp_helper module
https://github.com/ktbyers/pynet/blob/master/snmp/snmp_helper.py
'''
__author__ = "Dobias van Ingen"
__license__ = "MIT"
__version__ = "0.2"
__maintainer__ = "Dobias van Ingen"
__email__ = "dobias.vaningen@gmail.com"
from snmp_helper import snmp_get_oid, snmp_extract
from datetime import timedelta
import argparse
import textwrap
def collect_snmp(community, port, hosts, verbose):
DEBUG= False
# Set all SNMP OID to collect
OID_name = '1.3.6.1.2.1.1.5.0'
OID_descr = '1.3.6.1.2.1.1.1.0'
SYS_uptime = '1.3.6.1.2.1.1.3.0'
IF_oper = '1.3.6.1.2.1.2.2.1.8.'
IF_type = '1.3.6.1.2.1.2.2.1.3.'
IF_last = '1.3.6.1.2.1.2.2.1.9.'
IF_desc = '1.3.6.1.2.1.2.2.1.2.'
for y in hosts:
try:
test = 0
x = 1
lastlist = []
intlist = []
sw = (y, community, port)
if DEBUG: print "DEBUG: Connecting to switch with following parameters: {}\n".format(sw)
snmp_data = snmp_get_oid(sw, oid=OID_name)
print "*" * 80
print "Switch name: %s" % snmp_extract(snmp_data)
snmp_data = snmp_get_oid(sw, oid=OID_descr)
print "Switch description: %s" % "\n".join(textwrap.wrap(snmp_extract(snmp_data),65))
snmp_data = snmp_get_oid(sw, oid=SYS_uptime)
ticks = snmp_extract(snmp_data)
seconds = float(ticks)/100
sysup = timedelta(seconds=seconds)
print "Systemuptime: %s" % sysup
print "*" * 80 + "\n"
while test == 0:
snmp_data = snmp_get_oid(sw, oid=IF_type+str(x))
type = snmp_extract(snmp_data)
if 'No Such Instance' in type:
test = 1
else:
oper = snmp_get_oid(sw,oid=IF_oper+str(x))
oper = snmp_extract(oper)
if int(oper) == 2:
last = snmp_get_oid(sw, oid=IF_last+str(x))
lastlist.append(snmp_extract(last))
descr = snmp_get_oid(sw, oid=IF_desc+str(x))
intlist.append(snmp_extract(descr))
x = x+1
y = 0
for x in lastlist:
down = float(ticks) - float(x)
seconds = float(down)/100
sysup1 = timedelta(seconds=seconds)
print "Interface %s id down for: %s" % (intlist[y], sysup1)
y = y + 1
except:
if verbose == "y":
print "Error occurred when connecting to switch: %s" % str(sw)
pass
def set_range(ip_range):
hosts = []
block = ip_range.split('.')
for x, y in enumerate(block):
if '-' in y:
blockrange = y.split('-')
for z in range(int(blockrange[0]), int(blockrange[1]) + 1):
ipaddr = '.'.join(block[:x] + [str(z)] + block[x + 1:])
hosts += set_range(ipaddr)
break
else:
hosts.append(ip_range)
return hosts
def main():
DEBUG = False
# Parse all arguments
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--community", default="public", help="Provide SNMP community string, default is public (v3 not supported yet)")
parser.add_argument("-p", "--port", default=161, help="Provide SNMP port number. Default value used will be 161")
parser.add_argument("-i", "--ip", help="Provide ip address / range")
parser.add_argument("-v", "--verbose", default='y', help="Enable (y) or disable (n) verbose mode")
args = parser.parse_args()
community = str(args.community)
port = str(args.port)
ip_range = str(args.ip)
verbose = str(args.verbose)
# Display parameters
if verbose == 'y':
print "\n" + "#" * 80
print "# Parameters used to collect port downtime:".ljust(78), "#"
print "# SNMP community name: {}".format(community).ljust(78), "#"
print "# SNMP port number:{}".format(port).ljust(78), "#"
print "# IP-Range:{}".format(ip_range).ljust(78), "#"
# Call set_range function to spit ip ranges in ip addresses lists
if DEBUG: print"# DEBUG: Split following ip range: {}".format(ip_range).ljust(78), "#"
hosts = set_range(ip_range)
if verbose == 'y':
print "# Total ip addresses that will be scanned for ports downtime are: {}".format(len(hosts)).ljust(78), "#"
print "#" * 80 + "\n"
# Call SNMP function to collect all parameters
collect_snmp(community, port, hosts, verbose)
if __name__ == "__main__":
main()