-
Notifications
You must be signed in to change notification settings - Fork 285
/
cmxnotificationreceiver.py
252 lines (201 loc) · 6.54 KB
/
cmxnotificationreceiver.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!flask/bin/python
"""
Cisco CMX Notification Receiver
A simple example demonstrating how to interact with the CMX API.
Default port: 5004
Cisco Meraki CMX Documentation
https://documentation.meraki.com/MR/Monitoring_and_Reporting/CMX_Analytics#CMX_Location_API
Written by: Matthew DeNapoli
"""
# Libraries
from pprint import pprint
from flask import Flask
from flask import json
from flask import request
from flask import render_template
from flask import Response
from queue import Queue
import sys, getopt
import json
import requests
import shutil
import atexit
import os
import time
mapImages = []
requests.packages.urllib3.disable_warnings()
app = Flask(__name__)
queue = Queue()
@app.route('/go', methods=['GET'])
def get_go():
global mapImages
global locationdata
return render_template('index.html', mapImages = mapImages, \
clientsraw = locationdata.text)
@app.route("/notification_stream")
def notification_handler_stream():
def notification_stream():
while True:
the_notification = queue.get(True)
print("Sending {}".format(the_notification))
yield "data: " + the_notification + "\n\n"
return Response(notification_stream(), mimetype="text/event-stream")
@app.route("/notification_handler", methods=['POST'])
def notification_handler():
queue.put(json.dumps(request.get_json(force=True)))
return "OK"
def create_notification(msg):
global url
global initials
global username
global password
endpoint = "https://" + url + "/api/config/v1/notification"
payload = \
{
"name": initials + "LocationUpdate",
"userId": "learning",
"rules": [
{
"conditions": []
}
],
"subscribers": [
{
"receivers": [
{
"uri": msg + ":80" + \
"/notification_handler",
"messageFormat": "JSON",
"headers": None,
"qos": "AT_MOST_ONCE"
}
]
}
],
"enabled": True,
"internal": False,
"cloud": True,
"enableMacScrambling": False,
"macScramblingSalt": "learning",
"notificationType": "LocationUpdate"
}
headers = {"Content-Type": "application/json"}
print(json.dumps(payload))
print(headers)
response = requests.request("PUT", endpoint, \
data=json.dumps(payload), headers=headers, auth=(username, password), verify=False)
print(response.text)
def get_maps():
global url
global username
global password
global mapImages
endpoint = "https://" + url + "/api/config/v1/maps"
print("trying " + endpoint)
try:
mapdata = requests.request("GET", endpoint, auth=(username, password), verify=False)
print("got maps")
mapdatajson = json.loads(mapdata.text)
except Exception as e:
print(e)
#Extract the hierarchy, image information, and actual image
for campus in mapdatajson["campuses"]:
for building in campus["buildingList"]:
for floor in building["floorList"]:
mapImages.append(
{
"hierarchy" : campus["name"] + ">" + \
building["name"] + ">" + \
floor["name"],
"imageName" : floor["image"]["imageName"],
"gpsMarkers" : floor["gpsMarkers"]
}
)
#Get actual image file and save locally
endpoint = "https://" + url + \
"/api/config/v1/maps/imagesource/" + \
floor["image"]["imageName"]
print("trying " + endpoint)
try:
response = requests.request("GET", endpoint, \
auth=(username, password), stream=True, verify=False)
print("Got Map")
with open("static/" + \
floor["image"]["imageName"], 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
except Exception as e:
print(e)
def initialize_client_locations():
global url
global username
global password
global locationdata
#Get the client location data
endpoint = "https://" + url + "/api/location/v3/clients"
print("trying " + endpoint)
try:
locationdata = requests.request("GET", endpoint, \
auth=(username, password), verify=False)
print("got data")
except Exception as e:
print(e)
#defining function to run on shutdown
def delete_notification():
global url
global username
global password
global initials
endpoint = "https://" + url + "/api/config/v1/notifications/" + \
initials + "LocationUpdate"
try:
requests.request("DELETE", endpoint, auth=(username, password), \
verify=False)
except Exception as e:
print(e)
atexit.register(delete_notification)
# Launch application with supplied arguments
def main(argv):
global url
global initials
global username
global password
try:
opts, args = getopt.getopt(argv,"hu:p:l:i:",["username=","password=",\
"url=","initials="])
except getopt.GetoptError:
print ('cmxnotificationreceiver.py -u <username> -p \ <password> -l <url> -i <initials>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('cmxnotificationreceiver.py -u <username> -p <password> \
-l <url> -i <initials>')
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-l", "--url"):
url = arg
elif opt in ("-i", "--initials"):
initials = arg
print ('username: '+ username)
print ('password: '+ password)
print ('url: '+ url)
print ('initials: ' + initials)
if __name__ == '__main__':
main(sys.argv[1:])
time.sleep(10)
tunnels = requests.request("GET", \
"http://ngrok:4040/api/tunnels", \
verify=False)
tunnels = json.loads(tunnels.text)
tunnels = tunnels["tunnels"]
for tunnel in tunnels:
if tunnel['proto'] == 'http':
msg = tunnel['public_url']
print(msg)
get_maps()
initialize_client_locations()
create_notification(msg)
app.run(host="0.0.0.0", port=5004,threaded=True,debug=False)