-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathncc-event-listener-demo.py
executable file
·189 lines (171 loc) · 5.75 KB
/
ncc-event-listener-demo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
#
# Copyright (c) 2018 Cisco and/or its affiliates
#
# This sample works with NETCONF ebents of the format:
#
# <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
# <eventTime>2018-10-27T18:33:54+00:00</eventTime>
# <linkDown xmlns='urn:ietf:params:xml:ns:yang:smiv2:IF-MIB'>
# <object-2>
# <ifIndex>4</ifIndex>
# <ifAdminStatus>down</ifAdminStatus>
# </object-2>
# <object-3>
# <ifIndex>4</ifIndex>
# <ifOperStatus>down</ifOperStatus>
# </object-3>
# </linkDown>
# </notification>
#
import sys
from argparse import ArgumentParser
from functools import partial
from ncclient import manager
from lxml import etree
import logging
import time
import datetime
import threading
import re
#
# Get the if-index for an interface from the IOS XE
# Cisco-IOS-XE-interfaces-oper model. The {} are for substitution
# using .format(...).
#
intf_query = '''
<interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
<interface>
<name>{}</name>
<if-index/>
</interface>
</interfaces>
'''
#
# Use the IOS XE native model Cisco-IOS-XE-native to bring up an
# iterface (CLI equivalent of "no shutdown"). The {} are for
# substitution using .format(...).
#
intf_no_shut = '''
<nc:config xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<{}>
<name>{}</name>
<shutdown nc:operation="remove"/>
</{}>
</interface>
</native>
</nc:config>
'''
#
# Simple example listener to run on a standalone thread.
#
def listener(mgr=None, stream_name=None, if_name=None, if_index=None):
assert mgr is not None
assert stream_name is not None
assert if_name is not None
assert if_index is not None
#
# assume if_name is a simple IOS XE-style interface name like
# 'GigabitEthernet4'; this woukd clearly be inappropriate for
# production code!!
#
p = re.compile(r'([a-zA-Z]+)([0-9]+)')
m = p.match(if_name)
if not m:
print('cannot find if type and index')
return
if_type = m.group(1)
if_ind = m.group(2)
intf_no_shut_resolved = intf_no_shut.format(if_type, if_ind, if_type)
print('Monitoring interface {} with SNMP if-index {}'.format(
if_name, if_index))
#
# Create a subscription and loop waiting for notifications form
# the device mgr is connected to. Note that other threads can
# still use the connection.
#
mgr.create_subscription(stream_name=stream_name)
while True:
# blocking call, but will see ALL notifications
notif = mgr.take_notification()
if notif:
root = notif.notification_ele
linkDown = root.xpath(
'//ns1:linkDown/*[ns1:ifIndex={}]/ns1:ifAdminStatus'.format(if_index),
namespaces={
'ns1': 'urn:ietf:params:xml:ns:yang:smiv2:IF-MIB',
})
if len(linkDown) > 0:
print('linkDown event -> ifAdminStatus = {}'.format(linkDown[0].text))
print('Bringing interface back up...')
mgr.edit_config(
intf_no_shut_resolved,
target='running',
format='xml')
print('Done')
else:
pass
if __name__ == '__main__':
parser = ArgumentParser(description='Select your simple poller parameters:')
# Input parameters
parser.add_argument('--host', type=str, required=True,
help="The device IP or DN")
parser.add_argument('-u', '--username', type=str, default='cisco',
help="Go on, guess!")
parser.add_argument('-p', '--password', type=str, default='cisco',
help="Yep, this one too! ;-)")
parser.add_argument('--port', type=int, default=830,
help="Specify this if you want a non-default port")
parser.add_argument('-v', '--verbose', action='store_true',
help="Do I really need to explain?")
# other options
parser.add_argument('--stream', type=str, required=True,
help="Event stream to register on")
parser.add_argument('--if-name', type=str, required=True,
help="Interface name to watch for link state events on")
args = parser.parse_args()
if args.verbose:
handler = logging.StreamHandler()
for l in ['ncclient.transport.ssh', 'ncclient.transport.session', 'ncclient.operations.rpc']:
logger = logging.getLogger(l)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
#
# Connect
#
def unknown_host_cb(host, fingerprint):
return True
m = manager.connect(host=args.host,
port=args.port,
username=args.username,
password=args.password,
allow_agent=False,
look_for_keys=False,
hostkey_verify=False,
unknown_host_cb=unknown_host_cb)
#
# find the interface if-index for the provided name
#
r = m.get(filter=('subtree', intf_query.format(args.if_name)))
if_index = r.data_ele.xpath(
'//ns1:if-index',
namespaces={
'ns1': 'http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper',
})
if len(if_index) == 0:
print('No ifIndex found got {}'.format(args.if_name))
sys.exit(1)
#
# run the listener
#
thr = threading.Thread(
target=partial(
listener,
mgr=m,
stream_name=args.stream,
if_name=args.if_name,
if_index=int(if_index[0].text)))
thr.start()
thr.join()