Giter VIP home page Giter VIP logo

Comments (4)

sorscode avatar sorscode commented on August 21, 2024

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.

Thedarklife74 avatar Thedarklife74 commented on August 21, 2024

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.

mpapazog avatar mpapazog commented on August 21, 2024

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.

mpapazog avatar mpapazog commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.