forked from meraki/automation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceupdownstatus.py
400 lines (328 loc) · 14.5 KB
/
deviceupdownstatus.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
readMe = '''This is a script to print a list of all devices in a organization's inventory and their
up/down status. The script will not return up/down status for MV security cameras, as this
was not supported at time of writing. Note that this script is very old and SNMP no longer
needs to be used for getting device up/down statuses. For a more modern approach, use this
endpoint instead: https://developer.cisco.com/meraki/api-v1/#!get-organization-devices-statuses
To run the script, enter:
python deviceupdownstatus.py -k <apikey> -o <org name> [-a <snmp auth key> -p <snmp priv key>]
Mandatory arguments:
-k <apikey> : Your Meraki Dashboard API key
-o <org name> : Your Dashboard Organization name
Optional arguments to use SNMPv3:
-a <snmp auth key> : SNMPv3 authentication key. Required for SNMPv3
-p <snmp priv key> : SNMPv3 privacy key. Required for SNMPv3
Example:
python deviceupdownstatus.py -k 1234 -o "Meraki Inc" -a authpass123 -p privpass123
This script was developed using Python 3.6.4. You will need the Requests and PySNMP modules
to run it. You can install these modules via pip:
pip install requests
pip install pysnmp
More info on these modules:
http://python-requests.org
http://pysnmp.sourceforge.net'''
import sys, getopt, requests, json, time
from pysnmp.hlapi import *
from datetime import datetime
#Used for time.sleep(API_EXEC_DELAY). Delay added to avoid hitting dashboard API max request rate
API_EXEC_DELAY = 0.21
#connect and read timeouts for the Requests module
REQUESTS_CONNECT_TIMEOUT = 30
REQUESTS_READ_TIMEOUT = 30
#used by merakirequestthrottler(). DO NOT MODIFY
LAST_MERAKI_REQUEST = datetime.now()
class c_serialstatus:
def __init__(self):
self.serial = ''
self.status = ''
class c_deviceinfo:
def __init__(self):
self.serial = ''
self.model = ''
self.name = ''
self.networkId = ''
self.status = '' #this will be filled via SNMP
class c_snmpinfo:
def __init__(self):
self.host = ''
self.usercommunity = '' # SNMPv3 user / SNMPv2c community. Same in Meraki Dashboard
self.v3enabled = False
self.v2cenabled = False
self.authkey = '' #not returned by API
self.privkey = '' #not returned by API
def printusertext(p_message):
#prints a line of text that is meant for the user to read
#do not process these lines when chaining scripts
print('@ %s' % p_message)
def printhelp():
print(readMe)
def snmppolldevicestatuses (p_shardhost, p_usercommunity, p_authkey = '', p_privkey = ''):
#returns a list of c_serialstatus objects containing serial numbers of devices returned by SNMP and their up/down status
#note that SNMP only returns status for devices assigned to a network, not the whole inventory
returnlist = []
serialslist = []
statuslist = []
flag_snmpv3 = True
if p_authkey == '' or p_privkey == '':
flag_snmpv3 = False
#snmp poll serials' list
if flag_snmpv3:
try:
g = nextCmd(SnmpEngine(),
UsmUserData(p_usercommunity, p_authkey, p_privkey,
authProtocol=usmHMACSHAAuthProtocol,
privProtocol=usmAesCfb128Protocol),
UdpTransportTarget((p_shardhost, 16100)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.29671.1.1.4.1.8')),
lexicographicMode = False,
lookupMib = False)
except:
printusertext ("ERROR 01: SNMPv3 nextCmd failed")
sys.exit(2)
else: #using SNMPv2c
try:
g = nextCmd(SnmpEngine(),
CommunityData(p_usercommunity),
UdpTransportTarget((p_shardhost, 16100)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.29671.1.1.4.1.8')),
lexicographicMode = False,
lookupMib = False)
except:
printusertext ("ERROR 02: SNMPv2c nextCmd failed")
sys.exit(2)
flag_continue = True
while flag_continue:
try:
errorIndication, errorStatus, errorIndex, varBinds = next(g)
except StopIteration:
flag_continue = False
except:
printusertext ("ERROR 03: SNMP next failed")
sys.exit(2)
if flag_continue:
for vb in varBinds:
try:
#mash everything to a str and grab the right characters. this works more reliably
crashbuffer = str(vb)[-17:-3]
serialslist.append(crashbuffer)
except:
printusertext ('WARNING: SNMP poll for serials returned no data')
#snmp poll statuses' list
if flag_snmpv3:
try:
g = nextCmd(SnmpEngine(),
UsmUserData(p_usercommunity, p_authkey, p_privkey,
authProtocol=usmHMACSHAAuthProtocol,
privProtocol=usmAesCfb128Protocol),
UdpTransportTarget((p_shardhost, 16100)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.29671.1.1.4.1.3')),
lexicographicMode = False,
lookupMib = False)
except:
printusertext ("ERROR 04: SNMPv3 nextCmd failed")
sys.exit(2)
else: #using SNMPv2c
try:
g = nextCmd(SnmpEngine(),
CommunityData(p_usercommunity),
UdpTransportTarget((p_shardhost, 16100)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.29671.1.1.4.1.3')),
lexicographicMode = False,
lookupMib = False)
except:
printusertext ("ERROR 05: SNMPv2c nextCmd failed")
sys.exit(2)
flag_continue = True
while flag_continue:
try:
errorIndication, errorStatus, errorIndex, varBinds = next(g)
except StopIteration:
flag_continue = False
except:
printusertext ("ERROR 06: SNMP next failed")
sys.exit(2)
if flag_continue:
for vb in varBinds:
try:
crashbuffer = vb[len(vb)-1]
statuslist.append(crashbuffer)
except:
printusertext ('WARNING: SNMP poll for statuses returned no data')
lastitem = len(statuslist)
for i in range (0, lastitem):
returnlist.append(c_serialstatus())
returnlist[i].serial = serialslist[i]
if statuslist[i] == 1:
returnlist[i].status = 'up'
else:
returnlist[i].status = 'down'
return (returnlist)
def merakirequestthrottler(p_requestcount=1):
#makes sure there is enough time between API requests to Dashboard not to hit shaper
global LAST_MERAKI_REQUEST
if (datetime.now()-LAST_MERAKI_REQUEST).total_seconds() < (API_EXEC_DELAY*p_requestcount):
time.sleep(API_EXEC_DELAY*p_requestcount)
LAST_MERAKI_REQUEST = datetime.now()
return
def getorgid(p_apikey, p_orgname):
#looks up org id for a specific org name
#on failure returns 'null'
merakirequestthrottler()
try:
r = requests.get('https://api.meraki.com/api/v0/organizations', headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
except:
printusertext('ERROR 07: Unable to contact Meraki cloud')
sys.exit(2)
if r.status_code != requests.codes.ok:
return 'null'
rjson = r.json()
for record in rjson:
if record['name'] == p_orgname:
return record['id']
return('null')
def getsnmpinfo(p_apikey, p_orgid):
#Looks up shard URL for a specific org. Use this URL instead of 'dashboard.meraki.com'
# when making API calls with API accounts that can access multiple orgs.
#On failure returns 'null'
merakirequestthrottler()
try:
r = requests.get('https://api.meraki.com/api/v0/organizations/%s/snmp' % p_orgid, headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT))
except:
printusertext('ERROR 08: Unable to contact Meraki cloud')
sys.exit(2)
returnobject = c_snmpinfo()
if r.status_code != requests.codes.ok:
returnobject.host = 'null'
return (returnobject)
rjson = r.json()
returnobject.host = rjson['hostname']
returnobject.v3enabled = rjson['v3Enabled']
returnobject.v2cenabled = rjson['v2cEnabled']
if rjson['v2cEnabled']:
returnobject.usercommunity = rjson['v2CommunityString']
elif rjson['v3Enabled']:
returnobject.usercommunity = rjson['v3User']
return(returnobject)
def getinventory(p_apikey, p_shardhost, p_orgid):
#returns a list of all networks in an organization
#on failure returns a single record with 'null' name and id
merakirequestthrottler()
try:
r = requests.get('https://api.meraki.com/api/v0/organizations/%s/inventory' % p_orgid, headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 09: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append(c_deviceinfo())
returnvalue[0].serial = 'null'
return(returnvalue)
rjson = r.json()
rjlen = len(rjson)
for i in range (0, rjlen):
returnvalue.append(c_deviceinfo())
returnvalue[i].serial = rjson[i]['serial']
returnvalue[i].model = rjson[i]['model']
if rjson[i]['networkId'] is None:
returnvalue[i].networkId = ''
else:
returnvalue[i].networkId = rjson[i]['networkId']
return(returnvalue)
def getdevicename(p_apikey, p_shardhost, p_nwid, p_serial):
#returns a list of all networks in an organization
#on failure returns a single record with 'null' name and id
merakirequestthrottler()
try:
r = requests.get('https://api.meraki.com/api/v0/networks/%s/devices/%s' % (p_nwid, p_serial), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'}, timeout=(REQUESTS_CONNECT_TIMEOUT, REQUESTS_READ_TIMEOUT) )
except:
printusertext('ERROR 10: Unable to contact Meraki cloud')
sys.exit(2)
if r.status_code != requests.codes.ok:
return('')
rjson = r.json()
if rjson['name'] is None:
return(rjson['mac'])
return(rjson['name'])
def main(argv):
#initialize variables for command line arguments
arg_apikey = ''
arg_orgname = ''
arg_authkey = ''
arg_privkey = ''
#get command line arguments
try:
opts, args = getopt.getopt(argv, 'hk:o:a:p:')
except getopt.GetoptError:
printhelp()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printhelp()
sys.exit()
elif opt == '-k':
arg_apikey = arg
elif opt == '-o':
arg_orgname = arg
elif opt == '-a':
arg_authkey = arg
elif opt == '-p':
arg_privkey = arg
#check if all parameters are required parameters have been given
if arg_apikey == '':
printhelp()
sys.exit(2)
#resolve orgid
orgid = getorgid(arg_apikey, arg_orgname)
#get SNMP info
#this call sometimes fails. implementing a try-verify-wait-repeat loop
MAX_SHARD_RESOLVE_TRIES = 10
flag_unabletoresolveshard = True
for i in range (0, MAX_SHARD_RESOLVE_TRIES):
snmpinfo = getsnmpinfo(arg_apikey, orgid)
if snmpinfo.host == 'null':
time.sleep(API_EXEC_DELAY*(i+1))
else:
flag_unabletoresolveshard = False
break
if flag_unabletoresolveshard:
printusertext('ERROR 11: Unable to read data for org "%s"' % record.name)
sys.exit(2)
#get device inventory
inventory = getinventory(arg_apikey, snmpinfo.host, orgid)
if inventory[0].serial == 'null':
printusertext('ERROR 12: Unable to read inventory via API')
sys.exit(2)
#poll device up/down status via SNMP
if arg_authkey != '' and arg_privkey != '':
if snmpinfo.v3enabled:
updownstatus = snmppolldevicestatuses(snmpinfo.host, snmpinfo.usercommunity, arg_authkey, arg_privkey)
else:
printusertext('ERROR 13: Unable to poll via SNMPv3 (not enabled in org)')
sys.exit(2)
else:
if snmpinfo.v2cenabled:
updownstatus = snmppolldevicestatuses(snmpinfo.host, snmpinfo.usercommunity)
else:
printusertext('ERROR 14: Unable to poll via SNMPv2c (not enabled in org)')
sys.exit(2)
for device in inventory:
if device.networkId != '':
if device.model[:2] == 'MV':
device.status = 'n/a' #currently SNMP does not return up/down for MV cameras
flag_devicelocated = False
for state in updownstatus:
if device.serial == state.serial:
device.status = state.status
flag_devicelocated = True
break
device.name = getdevicename(arg_apikey, snmpinfo.host, device.networkId, device.serial)
else:
device.status = "not in use"
print ('DEVICE NAME MODEL SERIAL STATUS')
for device in inventory:
print('%-30s%-15s%-20s%-10s' % (device.name, device.model, device.serial, device.status))
if __name__ == '__main__':
main(sys.argv[1:])