|
| 1 | +import csv |
| 2 | +from datetime import datetime |
| 3 | +import os |
| 4 | +import json |
| 5 | +import argparse |
| 6 | +import sys |
| 7 | + |
| 8 | +import meraki |
| 9 | + |
| 10 | +import urllib.parse |
| 11 | +import platform |
| 12 | + |
| 13 | +# This example pulls API calls from the passed in org_id from the last timespan |
| 14 | +# seconds, where the default timespan is 900 (hint 24 hours = 3600 seconds) and |
| 15 | +# generates a CSV file with the data. |
| 16 | +# |
| 17 | +# Either input your API key below by uncommenting line 10 and changing line 16 to api_key=API_KEY, |
| 18 | +# or set an environment variable (preferred) to define your API key. The former is insecure and not recommended. |
| 19 | +# For example, in Linux/macOS: export MERAKI_DASHBOARD_API_KEY=093b24e85df15a3e66f1fc359f4c48493eaa1b73 |
| 20 | +# API_KEY = '093b24e85df15a3e66f1fc359f4c48493eaa1b73' |
| 21 | +# |
| 22 | +# Optionally, Cisco partners can set their BE GEO ID by using export BE_GEO_ID=XXXXXX |
| 23 | +# where XXXXX is a valid BE GEO ID. This is used for metrics collection. |
| 24 | +# |
| 25 | +# Optionally, a calling application can be set by using export MERAKI_PYTHON_SDK_CALLER=YYYYY |
| 26 | +# where YYYYY is a string identifying the application, script, or whatever piece of code |
| 27 | +# is callig the Meraki Python SDK |
| 28 | + |
| 29 | + |
| 30 | +def main(org_id, timespan): |
| 31 | + # Instantiate a Meraki dashboard API session |
| 32 | + dashboard = meraki.DashboardAPI( |
| 33 | + base_url='https://api-mp.meraki.com/api/v1/', |
| 34 | + print_console=False, |
| 35 | + output_log=False, |
| 36 | + ) |
| 37 | + |
| 38 | + # Get list of API usage data and start the output csv string |
| 39 | + apiUsage = dashboard.organizations.getOrganizationApiRequests(org_id, timespan=timespan, total_pages=-1) |
| 40 | + csvString = 'method,host,path,queryString,tsDate,tsTime,responseCode,sourceIp,userAgent,' |
| 41 | + csvString += 'implementation,implementationVersion,distro,distroVersion,system,systemRelease,' |
| 42 | + csvString += 'cpu,be_geo_id,caller\r\n' |
| 43 | + cumulativeAPIcalls = 0; |
| 44 | + for use in apiUsage: |
| 45 | + csvString += use['method'] + ',' |
| 46 | + csvString += use['host'] + ',' |
| 47 | + csvString += use['path'] + ',' |
| 48 | + csvString += use['queryString'] + ',' |
| 49 | + csvString += use['ts'].split('T')[0] + ',' |
| 50 | + csvString += use['ts'].split('T')[1].replace('Z','') + ',' |
| 51 | + csvString += str(use['responseCode']) + ',' |
| 52 | + csvString += use['sourceIp'] + ',' |
| 53 | + |
| 54 | + # Special User-Agent processing |
| 55 | + if 'python-meraki' in use['userAgent']: |
| 56 | + print(use['userAgent']) |
| 57 | + userAgent = use['userAgent'].split(' ') |
| 58 | + csvString += userAgent[0] + ',' |
| 59 | + if len(userAgent) > 1: |
| 60 | + if "implementation" in userAgent[1]: |
| 61 | + userAgentDict = json.loads(urllib.parse.unquote(userAgent[1])) |
| 62 | + csvString += userAgentDict['implementation']['name'] + ',' |
| 63 | + csvString += userAgentDict['implementation']['version'] + ',' |
| 64 | + csvString += userAgentDict['distro']['name'] + ',' |
| 65 | + csvString += userAgentDict['distro']['version'] + ',' |
| 66 | + csvString += userAgentDict['system']['name'] + ',' |
| 67 | + csvString += userAgentDict['system']['release'] + ',' |
| 68 | + csvString += userAgentDict['cpu'] + ',' |
| 69 | + if "be_geo_id" in userAgentDict: |
| 70 | + csvString += userAgentDict['be_geo_id'] + ',' |
| 71 | + else: |
| 72 | + csvString += ',' |
| 73 | + if "application" in userAgentDict: |
| 74 | + csvString += userAgentDict['application'] + ',' |
| 75 | + elif "caller" in userAgentDict: |
| 76 | + csvString += userAgentDict['caller'] + ',' |
| 77 | + else: |
| 78 | + csvString += ',' |
| 79 | + else: |
| 80 | + csvString += ',,,,,,,,,' |
| 81 | + else: |
| 82 | + csvString += ',,,,,,,,,' |
| 83 | + else: |
| 84 | + csvString += use['userAgent']+ ',' |
| 85 | + csvString += ',,,,,,,,,' |
| 86 | + |
| 87 | + csvString += '\r\n' |
| 88 | + |
| 89 | + # Output the file |
| 90 | + now = datetime.now() |
| 91 | + dt_string = now.strftime("%Y-%m-%d_%H-%M-%S") |
| 92 | + filename = org_id + '_' + str(timespan) + '_' + dt_string + '.csv' |
| 93 | + file = open(filename, 'w') |
| 94 | + file.write(csvString) |
| 95 | + file.close() |
| 96 | + print('Results written to ' + filename) |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + # First check for API key |
| 100 | + if "MERAKI_DASHBOARD_API_KEY" not in os.environ: |
| 101 | + print('You must set the MERAKI_DASHBOARD_API_KEY variable') |
| 102 | + sys.exit() |
| 103 | + |
| 104 | + # Now check arguments |
| 105 | + parser = argparse.ArgumentParser(description='Generate a CSV file of Meraki API activity for an organization.') |
| 106 | + parser.add_argument('org_id', help='Organization id to pull API activity from') |
| 107 | + parser.add_argument('--timespan', type=int, default=900, |
| 108 | + help='The timespan (in seconds) for which the information will be fetched. Default = 900 (15 mins)') |
| 109 | + args = parser.parse_args() |
| 110 | + print('About to run with org_id: ' + args.org_id + ' and timespan: ' + str(args.timespan)) |
| 111 | + |
| 112 | + # Finally, let's roll |
| 113 | + start_time = datetime.now() |
| 114 | + main(args.org_id, args.timespan) |
| 115 | + end_time = datetime.now() |
| 116 | + print(f'\nScript complete, total runtime {end_time - start_time}') |
0 commit comments