-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdnac.py
More file actions
228 lines (210 loc) · 7.54 KB
/
Copy pathdnac.py
File metadata and controls
228 lines (210 loc) · 7.54 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
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
223
224
225
226
227
228
'''
Copyright (c) 2018 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.0 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
'''
__copyright__ = "Copyright (c) 2018 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.0"
import requests
import json
import sys
import uuid
import time
from requests.auth import HTTPBasicAuth
#Global variables
BASIC_AUTH_API = "https://{}/api/system/v1/identitymgmt/login"
DEVICES_API = "https://{}/api/v1/network-device/{}/{}"
DEVICE_CT_API = "https://{}/api/v1/network-device/count"
DNACAAP_ITSM_POST_API = "https://{}/api/dnacaap/v1/dnacaap/core/dna/events/{}/event"
ITSM_BAPI_NAME="Publish DNA Event"
ITSM_SYSTEM_NAME = "ServiceNow"
TEST_EVENT_POST_BODY= '''
{
"id": "{}",
"name": "Test",
"category": "Info",
"domain": "EOL",
"description": "This is a test message",
"type": "Network",
"status": "New",
"severity": "P5",
"timestamp": "",
"tenantId": "",
"namespace": "",
"version": "",
"thresholdDefinitions": "",
"assignedTo": "",
"isCorrelatedEvent": "",
"actualServiceId": "",
"workflowIndicator": "RFC",
"enrichmentInfo": {
"details":"Testing ITSM"
}
}
'''
HWEOL_EVENT_POST_BODY= '''
{
"id": "{}",
"name": "Hardware End of Life",
"category": "Warn",
"domain": "EOL",
"description": "This device may be end-of-life",
"type": "Network",
"status": "New",
"severity": "P2",
"timestamp": "",
"tenantId": "",
"namespace": "",
"version": "",
"thresholdDefinitions": "",
"assignedTo": "",
"isCorrelatedEvent": "",
"actualServiceId": "",
"workflowIndicator": "RFC",
"enrichmentInfo": {
"details":
{
"device": {
"macAddress": "{}",
"hostname": "{}",
"node": "{}",
"softwareVersion": "{}",
"productId": "{}"
},
"eol":{
"announceDate" : "{}",
"eoSaleDate": "{}",
"eoSupportDate": "{}",
"MigrationPid": "{}"
}
}
}
}
'''
PSIRT_EVENT_POST_BODY = '''
{
"id": "{}",
"name": "Security Advisories",
"category": "Warn",
"domain": "EOL",
"description": "This device has security advisories",
"type": "Network",
"status": "New",
"severity": "P2",
"timestamp": "",
"tenantId": "",
"namespace": "",
"version": "",
"thresholdDefinitions": "",
"assignedTo": "",
"isCorrelatedEvent": "",
"actualServiceId": "",
"workflowIndicator": "RFC",
"enrichmentInfo": {
"details":
{
"device": {
"macAddress": "{}",
"hostname": "{}",
"node": "{}",
"softwareVersion": "{}",
"productId": "{}"
},
"psirts":{
"psirts": "{}"
}
}
}
}
'''
class Dnac_session:
#Login to DNAC
def __init__(self,ip, user, pwd):
self.sess = requests.Session()
self.sess.cookie = {}
self.ip = ip
self.authtoken = ""
try:
r = self.sess.get(BASIC_AUTH_API.format(ip), auth=HTTPBasicAuth(user, pwd), timeout=10, verify=False)
if r.status_code == 200:
# Add the JWT token to header
ckies = r.headers['Set-Cookie'].split(";")
for ckie in ckies:
try:
name, value = ckie.split('=')
except ValueError:
name, value = "", ""
if name == "X-JWT-ACCESS-TOKEN":
self.authtoken = value
self.sess.cookie[name] = value
print("\n########### DNAC Login Successful ##########")
else:
print(r.content.decode("utf-8"))
sys.exit(1)
except requests.exceptions.ConnectionError:
sys.exit('Unable to connect to Cluster')
#Get Event id (UUID)
def get_eventid(self):
e_id = uuid.uuid4()
return e_id
#Get proper date format
def date_format(self,epoch_num):
# convert into secs and make it human readable
format_date = time.strftime('%Y-%m-%d', time.localtime(epoch_num / 1000))
return format_date
#Get Device Count
def get_dev_count(self):
r = self.sess.get(DEVICE_CT_API.format(self.ip), cookies=self.sess.cookie, verify=False)
ct_resp = r.json()
return ct_resp["response"]
#Get Devices
def get_devices(self, start, limit):
r = self.sess.get(DEVICES_API.format(self.ip, start, limit), cookies=self.sess.cookie, verify=False)
dev_resp = r.json()
return dev_resp
#Post to ServiceNow
def post_itsm(self,dev,type):
eventid = self.get_eventid()
body=""
if(type == "HWEOL"):
body = json.loads(HWEOL_EVENT_POST_BODY)
body["id"] = str(eventid)
body["enrichmentInfo"]["details"]["device"]["macAddress"] = dev["mac"]
body["enrichmentInfo"]["details"]["device"]["hostname"] = dev["hostname"]
body["enrichmentInfo"]["details"]["device"]["node"] = dev["serial"]
body["enrichmentInfo"]["details"]["device"]["softwareVersion"] = dev["swVersion"]
body["enrichmentInfo"]["details"]["device"]["productId"] = dev["pid"]
body["enrichmentInfo"]["details"]["eol"]["announceDate"] = self.date_format(int(dev["hweol"]["externalAnnounceDate"]))
body["enrichmentInfo"]["details"]["eol"]["eoSaleDate"] = self.date_format(int(dev["hweol"]["hweoxEndOfSaleDate"]))
body["enrichmentInfo"]["details"]["eol"]["eoSupportDate"] = self.date_format(int(dev["hweol"]["hweoxLastSupportDate"]))
body["enrichmentInfo"]["details"]["eol"]["MigrationPid"] = dev["hweol"]["migrationPidInfo"][0]["migrationPid"]
elif(type == "PSIRT"):
body = json.loads(PSIRT_EVENT_POST_BODY)
body["id"] = str(eventid)
body["enrichmentInfo"]["details"]["device"]["macAddress"] = dev["mac"]
body["enrichmentInfo"]["details"]["device"]["hostname"] = dev["hostname"]
body["enrichmentInfo"]["details"]["device"]["node"] = dev["serial"]
body["enrichmentInfo"]["details"]["device"]["softwareVersion"] = dev["swVersion"]
body["enrichmentInfo"]["details"]["device"]["productId"] = dev["pid"]
body["enrichmentInfo"]["details"]["psirts"] = dev["psirts"]
elif(type == "TEST"):
body = json.loads(TEST_EVENT_POST_BODY)
body["id"] = str(eventid)
# Add Extra BAPI name header
headers = {"__bapiName" :ITSM_BAPI_NAME,"end_system": ITSM_SYSTEM_NAME,"X-AUTH-TOKEN" : self.authtoken }
self.sess.headers = headers
r = self.sess.post(DNACAAP_ITSM_POST_API.format(self.ip,eventid), headers=self.sess.headers, json=body, verify=False)
if r.status_code == 202:
itsm_resp = r.json()
return itsm_resp["message"]
else:
return "No ITSM"
sys.exit(1)