This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
check_lldp.py
223 lines (194 loc) · 7.18 KB
/
check_lldp.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from pprint import pprint
import time
import json
import yaml
import requests
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
def add_tables_and_views(file_name):
files = {'up_file': open('tables_and_views/' + file_name,'r')}
r=requests.post(url + '/files/helper-files/' + file_name, auth=HTTPBasicAuth(authuser, authpwd), headers={ 'Accept' : 'application/json' }, verify=False,
files=files)
print "loaded table and view " + file_name
def add_rule(file_name):
files = {'topics': open('rules/' + file_name,'r')}
r=requests.post(url + '/topics/', auth=HTTPBasicAuth(authuser, authpwd), headers={ 'Accept' : 'application/json'}, verify=False, files=files)
print 'loaded healthbot rule ' + file_name
def add_playbook(file_name):
files = {'playbooks': open('playbooks/' + file_name,'r')}
r=requests.post(url + '/playbooks/', auth=HTTPBasicAuth(authuser, authpwd), headers={ 'Accept' : 'application/json' }, verify=False, files=files)
print 'loaded healthbot playbook ' + file_name
def add_device(dev):
payload=json.dumps(dev)
r = requests.post(url + '/device/' + dev['device-id'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False, data=payload)
print 'loaded healthbot configuration for the device: ' + dev['device-id']
def add_device_group(group):
payload=json.dumps(group)
r = requests.post(url + '/device-group/' + group['device-group-name'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False, data=payload)
print 'loaded healthbot configuration for the device group: ' + group['device-group-name']
def add_network_group(group):
payload=json.dumps(group)
r = requests.post(url + '/network-group/' + group['network-group-name'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False, data=payload)
print 'loaded healthbot configuration for the network group: ' + group['network-group-name']
def commit():
r = requests.post(url + '/configuration', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print 'healthbot configuration commited!'
# update this section with your healthbot ip address
server = "172.30.52.250"
authuser = "lab"
authpwd = "m0naco"
url = 'https://'+ server + ':8080/api/v1'
headers = { 'Accept' : 'application/json', 'Content-Type' : 'application/json' }
# update this section with your devices details
devices = """[
{
"device-id": "ex4300-9",
"host": "172.30.179.95",
"authentication": {
"password": {
"username": "pytraining",
"password": "Poclab123"
}
}
},
{
"device-id": "ex4300-17",
"host": "172.30.179.73",
"authentication": {
"password": {
"username": "pytraining",
"password": "Poclab123"
}
}
},
{
"device-id": "ex4300-18",
"host": "172.30.179.74",
"authentication": {
"password": {
"username": "pytraining",
"password": "Poclab123"
}
}
}
]"""
# update this section with your device group details
device_group = """{
"device-group-name" : "EX",
"description" : "EX devices",
"devices" : ["ex4300-17", "ex4300-18", "ex4300-9"],
"playbooks" : ["collect-lldp"],
"variable" : [
{
"instance-id" : "collect-lldp-instance-1",
"playbook" : "collect-lldp",
"rule" : "lldp/collect-lldp"
}
]
}"""
# update this section with your device group details
network_group = """{
"network-group-name" : "LLDP",
"description" : "validate LLDP topology",
"playbooks" : ["check-lldp"],
"variable" : [
{
"instance-id" : "ex4300-9---ex4300-17",
"playbook" : "check-lldp",
"rule" : "lldp/check-lldp",
"variable-value" : [
{
"name" : "devicegroup",
"value" : "EX"
},
{
"name" : "localname",
"value" : "ex4300-9"
},
{
"name" : "localport",
"value" : "ge-0/0/0"
},
{
"name" : "remotename",
"value" : "ex4300-17"
},
{
"name" : "remoteport",
"value" : "ge-0/0/0"
}
]
},
{
"instance-id" : "ex4300-9---ex4300-18",
"playbook" : "check-lldp",
"rule" : "lldp/check-lldp",
"variable-value" : [
{
"name" : "devicegroup",
"value" : "EX"
},
{
"name" : "localname",
"value" : "ex4300-9"
},
{
"name" : "localport",
"value" : "ge-0/0/1"
},
{
"name" : "remotename",
"value" : "ex4300-18"
},
{
"name" : "remoteport",
"value" : "ge-0/0/0"
}
]
},
{
"instance-id" : "ex4300-17---ex4300-18",
"playbook" : "check-lldp",
"rule" : "lldp/check-lldp",
"variable-value" : [
{
"name" : "devicegroup",
"value" : "EX"
},
{
"name" : "localname",
"value" : "ex4300-17"
},
{
"name" : "localport",
"value" : "ge-0/0/1"
},
{
"name" : "remotename",
"value" : "ex4300-18"
},
{
"name" : "remoteport",
"value" : "ge-0/0/1"
}
]
}
]
}"""
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
add_tables_and_views('lldp.yml')
add_rule('collect-lldp.rule')
add_playbook('collect-lldp.playbook')
my_devices=yaml.load(devices)
for item in my_devices:
add_device(item)
my_group=yaml.load(device_group)
add_device_group(my_group)
commit()
print 'wait 60 sec'
time.sleep(60)
add_rule('check-lldp.rule')
add_playbook('check-lldp.playbook')
my_network_group=yaml.load(network_group)
add_network_group(my_network_group)
commit()