-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1_run_config_chg.py
170 lines (139 loc) · 5.49 KB
/
ex1_run_config_chg.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# imports
import cPickle as pickle
import os.path
from getpass import getpass
from datetime import datetime
from snmp_helper import snmp_get_oid_v3, snmp_extract
from email_helper import send_mail
# contstants
# SNMP constant assignments
RUN_LAST_CHANGED = '1.3.6.1.4.1.9.9.43.1.1.1.0'
SYS_NAME = '1.3.6.1.2.1.1.5.0'
SYS_UPTIME = '1.3.6.1.2.1.1.3.0'
# 300 seconds (converted to hundredths of seconds)
RELOAD_WINDOW = 300 * 100
DEBUG = True
# Get saved objects using pickle
def obtain_saved_objects(file_name):
'''
Read in saved objects from pickle file.
Return them as a dictionary.
'''
# check to see if the file exists
if not os.path.isfile(file_name):
return {}
# read in saved network devices
net_devices = {} #initialize the dictionary
with open(file_name, 'r') as f:
while True:
try:
tmp_device = pickle.load(f)
net_devices[tmp_device.device_name] = tmp_device
except EOFError:
break
return net_devices
# send notification
def send_notification(net_device):
'''
Send email notification about modified device.
'''
current_time = datetime.now()
sender = '[email protected]'
recipient = '[email protected]'
subject = 'Device {0} was modified'.format(net_device.device_name)
message = '''
The running configuration of {0} was modified.
This change was detected at {1}
'''.format(net_device.device_name, current_time)
if send_mail(recipient, subject, message, sender):
print "Email notification sent to {}".format(recipient)
return True
# network device objects
class NetworkDevice(object):
'''
Object to store network device information
'''
def __init__(self, device_name, uptime, last_changed, config_changed=False):
self.device_name = device_name
self.uptime = uptime
# The updtime value in hundredths of seconds when it was last changed
self.last_changed = last_changed
self.run_config_changed = config_changed
# main function
def main():
'''
Check if the running-configuration has changed, send an email notification when
this occurs.
'''
# pickle file that stores previous running config last changed time stamp
net_dev_file = 'netdev.pkl'
# SNMP connection parameters
rtr1_ip_addr = '184.105.247.70'
rtr2_ip_addr = '184.105.247.71'
my_key = 'galileo1' #not using getpass
a_user = 'pysnmp'
auth_key = my_key
encrypt_key = my_key
snmp_user = (a_user, auth_key, encrypt_key)
pynet_rtr1 = (rtr1_ip_addr, 161)
pynet_rtr2 = (rtr2_ip_addr, 161)
print '\n*** Checking for device changes ***'
saved_devices = obtain_saved_objects(net_dev_file)
print '{0} devices were previously saved\n'.format(len(saved_devices))
# temporarily store the current devices in a dictionary
current_devices = {}
# Connect to each device and get last changed time
for a_device in (pynet_rtr1, pynet_rtr2):
snmp_results = []
for oid in (SYS_NAME, SYS_UPTIME, RUN_LAST_CHANGED):
try:
value = snmp_extract(snmp_get_oid_v3(a_device, snmp_user, oid=oid))
snmp_results.append(int(value))
except ValueError:
snmp_results.append(value)
device_name, uptime, last_changed = snmp_results
if DEBUG:
print '\nConnected to device = {0}'.format(device_name)
print 'Last changed timestamp = {0}'.format(last_changed)
print 'Uptime = {0}'.format(uptime)
# See if the device has been previously saved
if device_name in saved_devices:
saved_device = saved_devices[device_name]
print "{0} {1}".format(device_name, (35 - len(device_name))*'.'),
# Check for a reboot, did uptiome decrease or last_changed decrease?
if uptime < saved_device.uptime or last_changed < saved_device.last_changed:
if last_changed <= RELOAD_WINDOW:
print "DEVICE RELOADED...not changed"
current_devices[device_name] = NetworkDevice(device_name, uptime,
last_changed, False)
else:
print 'DEVICE RELOADED...and changed'
current_devices[device_name] = NetworkDevice(device_name, uptime,
last_changed, True)
send_notification(current_devices[device_name])
elif last_changed == saved_device.last_changed:
# Running config last changed is the sysName
print 'Not changed'
current_devices[device_name] = NetworkDevice(device_name, uptime,
last_changed, False)
elif last_changed > saved_device.last_changed:
# running config was modified
print "CHANGED"
current_devices[device_name] = NetworkDevice(device_name, uptime,
last_changed, True)
else:
raise ValueError()
else:
# New device, just save it
print "{0} {1}".format(device_name, (35 - len(device_name))*'.'),
print 'Saving new device'
current_devices[device_name] = NetworkDevice(device_name, uptime,
last_changed, False)
# Write devices to a pickle file
with open(net_dev_file, 'w') as f:
for dev_obj in current_devices.values():
pickle.dump(dev_obj, f)
print
if __name__ == '__main__':
main()