Comments (4)
Yes but you will still run into an issue with rate limiting. Do you have an example of what you are doing?
from automation-scripts.
Yes but you will still run into an issue with rate limiting. Do you have an example of what you are doing?
This is my actual script :
`#!/usr/bin/python3
import csv
import datetime
import json
import requests
import sys
def get_network_name(network_id, networks):
return [element for element in networks if network_id == element['id']][0]['name']
if name == 'main':
# Import API key and org ID from login.py
try:
import login
(API_KEY, ORG_ID) = (login.api_key, login.org_id)
except ImportError:
API_KEY = ""
ORG_ID = ""
# Find all appliance networks (MX, Z1, Z3, vMX100)
session = requests.session()
headers = {'X-Cisco-Meraki-API-Key': API_KEY, 'Content-Type': 'application/json'}
try:
name = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + ORG_ID, headers=headers).text)['name']
except:
sys.exit('Incorrect API key or org ID, as no valid data returned')
networks = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + ORG_ID + '/networks', headers=headers).text)
inventory = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + ORG_ID + '/inventory', headers=headers).text)
appliances = [device for device in inventory if device['model'][:2] in ('MX', 'Z1', 'Z3', 'vM') and device['networkId'] is not None]
devices = [device for device in inventory if device not in appliances and device['networkId'] is not None]
# Output CSV of appliances' info
today = datetime.date.today()
csv_file1 = open(name + '_appliances'+'.csv', 'w', encoding='utf-8')
fieldnames = ['Reseau', 'Equipement', 'Modele', 'Statut Internet Pincipal (WAN 1)', 'Statut Internet Secondaire 4G/LTE (WAN 2)']
writer = csv.DictWriter(csv_file1, fieldnames=fieldnames, restval='')
writer.writeheader()
# Iterate through appliances
for appliance in appliances:
network_name = get_network_name(appliance['networkId'], networks)
print('Looking into network ' + network_name)
device_name = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'], headers=headers).text)['name']
try:
perfscore = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'] + '/performance', headers=headers).text)['perfScore']
except:
perfscore = None
try:
print('Found appliance ' + device_name)
except:
print('Found appliance ' + appliance['serial'])
uplinks_info = dict.fromkeys(['WAN1', 'WAN2', 'Cellular'])
uplinks_info['WAN1'] = dict.fromkeys(['interface', 'status', 'ip', 'gateway', 'publicIp', 'dns', 'usingStaticIp'])
uplinks_info['WAN2'] = dict.fromkeys(['interface', 'status', 'ip', 'gateway', 'publicIp', 'dns', 'usingStaticIp'])
uplinks_info['Cellular'] = dict.fromkeys(['interface', 'status', 'ip', 'provider', 'publicIp', 'model', 'connectionType'])
uplinks = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'] + '/uplink', headers=headers).text)
for uplink in uplinks:
if uplink['interface'] == 'WAN 1':
for key in uplink.keys():
uplinks_info['WAN1'][key] = uplink[key]
elif uplink['interface'] == 'WAN 2':
for key in uplink.keys():
uplinks_info['WAN2'][key] = uplink[key]
elif uplink['interface'] == 'Cellular':
for key in uplink.keys():
uplinks_info['Cellular'][key] = uplink[key]
if network_name != 'F.D.LABO.N002': #ajouter or pour ajouter des exception note il n'affichera jamais rien pour ce device
if appliance['model'] == 'MX68' and uplinks_info['WAN2']['status'] == 'Not connected' or uplinks_info['WAN2']['status'] == 'Failed' or uplinks_info['WAN1']['status'] == 'Not connected' or uplinks_info['WAN1']['status'] == 'Failed':
#Ecriture de ligne dans le csv uniquement si une des conditions si dessus est applicable
writer.writerow({'Reseau': network_name, 'Equipement': device_name, 'Modele': appliance['model'], 'Status Internet Pincipal (WAN 1)': uplinks_info['WAN1']['status'], 'Status Internet Secondaire 4G/LTE (WAN 2)': uplinks_info['WAN2']['status']})
csv_file1.close()
`
from automation-scripts.
As noted by @sorscode, the API has a rate limit of 5 calls per second, which is not adjustable. It does allow bursting during the first couple of seconds, but that will not have a significant effect on your script.
Opportunities to speed up the code:
device_name = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'], headers=headers).text)['name']
-> You already got this info when you ran the GET inventory call
uplinks = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'] + '/uplink', headers=headers).text)
-> Is it possible use this org-wide call instead? Not sure if it will provide all the info you want though: https://api.meraki.com/api_docs#return-the-uplink-loss-and-latency-for-every-mx-in-the-organization-from-at-latest-2-minutes-ago
Other than those, as far as I can see, you are fetching information from dashboard as efficiently as API v0.6 allows. If you would like to influence what will be available in future versions of the API, to better fit scenarios you are seeing, I would recommend to hop onto the Develper Early Access community: https://meraki.cisco.com/blog/2019/11/introducing-the-early-access-developer-program/
from automation-scripts.
In addition, If your script is currently running slower than 5 requests per second, please consider using the new API mega proxy api-mp.meraki.com as the target for your calls, which can increase performance.
If you do use this, I would recommend to build your code in a way that you can swap back to api.meraki.com when api-mp is eventually phased out and merged into api.meraki.com. This is not expected to happen anytime soon though. One way to do this, is to use the Meraki module for Python 3, which lets you configure the base URL: https://github.com/meraki/dashboard-api-python
from automation-scripts.
Related Issues (20)
- Migrate code to new API HOT 4
- Merakilicensealert.py keeps returning TypeError: list indices must be integers or slices, not str HOT 5
- uplink.py multiple Orgs HOT 5
- "@ ERROR 09: Unable to contact Meraki cloud" for deviceupdownstatus.py HOT 2
- "mxfirewallcontrol.py" does not like "tag" filter HOT 4
- manageadmins.py not adding adminstrators HOT 6
- Rerwite of mxfirewallcontrol.py for Dashboard API v1 HOT 1
- <Tag> all ports with <type> HOT 10
- Rewrite scripts to use API key saved as environment variable HOT 8
- Terraform provider HOT 1
- Two seperate issues for mxfirewallcontrol.py HOT 7
- mxfirewallcontrol.py specify cellularFirewallRules HOT 1
- Single LAN is not available for networks in passthrough mode HOT 5
- Issue with csv writing HOT 4
- backup_configs - Operations CSV Files HOT 1
- migrate_cat3k.py question HOT 7
- AttributeError: 'DashboardAPI' object has no attribute 'mx_l3_firewall' HOT 3
- manageadmins.py stopped working HOT 2
- mx_firewall_control - Unable to update ruleset from input-file or directly HOT 1
- Issue with manage_devices.py and manage_network.py
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from automation-scripts.