Add work/tbx/meraki_api.md
This commit is contained in:
833
work/tbx/meraki_api.md
Normal file
833
work/tbx/meraki_api.md
Normal file
@@ -0,0 +1,833 @@
|
|||||||
|
Great! Let's explore some freely available JSON data sources on the web that you can use to practice `jq`, and then we can touch on how to use the Meraki Dashboard API.
|
||||||
|
|
||||||
|
## Free JSON Data Sources
|
||||||
|
|
||||||
|
### 1. JSONPlaceholder
|
||||||
|
JSONPlaceholder is a free online REST API that you can use for testing and prototyping.
|
||||||
|
|
||||||
|
**API Endpoint:** `https://jsonplaceholder.typicode.com/`
|
||||||
|
|
||||||
|
**Example:** Fetch a list of users
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s https://jsonplaceholder.typicode.com/users | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. OpenWeatherMap
|
||||||
|
OpenWeatherMap provides weather data, and you can get a free API key for accessing their services.
|
||||||
|
|
||||||
|
**API Endpoint:** `http://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY`
|
||||||
|
|
||||||
|
**Example:** Fetch current weather for London
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s "http://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. The Dog API
|
||||||
|
The Dog API provides random images and information about dogs.
|
||||||
|
|
||||||
|
**API Endpoint:** `https://dog.ceo/api/breeds/list/all`
|
||||||
|
|
||||||
|
**Example:** Fetch a list of all dog breeds
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s https://dog.ceo/api/breeds/list/all | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. SpaceX API
|
||||||
|
The SpaceX API provides information about launches, rockets, and other SpaceX-related data.
|
||||||
|
|
||||||
|
**API Endpoint:** `https://api.spacexdata.com/v4/launches/latest`
|
||||||
|
|
||||||
|
**Example:** Fetch the latest SpaceX launch information
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s https://api.spacexdata.com/v4/launches/latest | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Working with the Meraki Dashboard API
|
||||||
|
|
||||||
|
The Meraki Dashboard API provides programmatic access to Cisco Meraki's cloud-managed networking products. To use the Meraki API, you'll need an API key from your Meraki dashboard account.
|
||||||
|
|
||||||
|
### Getting Started with the Meraki Dashboard API
|
||||||
|
|
||||||
|
1. **Get an API Key:**
|
||||||
|
Log in to the Meraki dashboard, navigate to your profile, and generate an API key.
|
||||||
|
|
||||||
|
2. **API Endpoint:**
|
||||||
|
The base URL for the Meraki API is `https://api.meraki.com/api/v1`.
|
||||||
|
|
||||||
|
3. **Example:** Fetch a list of organizations
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example Usage of `jq` with the Meraki Dashboard API
|
||||||
|
|
||||||
|
#### Fetch a List of Networks in an Organization
|
||||||
|
|
||||||
|
1. **Get Organization ID:**
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Fetch Networks in an Organization:**
|
||||||
|
Replace `ORGANIZATION_ID` with the actual organization ID obtained from the previous command.
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations/ORGANIZATION_ID/networks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Practical Exercises with the Meraki Dashboard API
|
||||||
|
|
||||||
|
#### 1. List All Devices in a Network
|
||||||
|
|
||||||
|
Replace `NETWORK_ID` with the actual network ID.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/networks/NETWORK_ID/devices" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Get Device Uplink Status
|
||||||
|
|
||||||
|
Replace `SERIAL` with the device serial number.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/devices/SERIAL/uplinks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
|
||||||
|
Practicing with freely available JSON data from the web and exploring APIs like the Meraki Dashboard API provides a hands-on approach to learning how to work with `jq`. Use the examples provided to get started, and experiment with different queries and filters to deepen your understanding.
|
||||||
|
|
||||||
|
If you have any specific questions or need further assistance, feel free to ask!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Sure! Let's review our conversation and integrate all the topics we've covered into a cohesive approach for working with the Meraki Dashboard API. Here's a step-by-step guide that incorporates the key points discussed, focusing on using `curl` and Python for interacting with the API, processing data, and automating tasks.
|
||||||
|
|
||||||
|
### Comprehensive Guide to Working with the Meraki Dashboard API
|
||||||
|
|
||||||
|
#### 1. Authorization and Setup
|
||||||
|
|
||||||
|
Before making any API calls, set up your environment by obtaining your Meraki API key and exporting it for use in `curl` commands.
|
||||||
|
|
||||||
|
**Setting Up Authorization:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export MERAKI_API_KEY="your_api_key"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Making API Calls with `curl`
|
||||||
|
|
||||||
|
**Find Your Organization ID:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" > organizations.json
|
||||||
|
```
|
||||||
|
|
||||||
|
**List Networks in an Organization:**
|
||||||
|
|
||||||
|
Replace `{organizationId}` with your actual organization ID.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/networks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/networks" > networks.json
|
||||||
|
```
|
||||||
|
|
||||||
|
**List Devices in an Organization:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/devices" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/devices" > devices.json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Creating and Managing Action Batches
|
||||||
|
|
||||||
|
**Create an Action Batch:**
|
||||||
|
|
||||||
|
This example creates an action batch to update a switch port configuration. Replace `{organizationId}` with your organization ID.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -X POST https://api.meraki.com/api/v1/organizations/{organizationId}/actionBatches \
|
||||||
|
-L \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
-H 'Authorization: Bearer $MERAKI_API_KEY' \
|
||||||
|
-d '{
|
||||||
|
"confirmed": true,
|
||||||
|
"synchronous": true,
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"resource": "/devices/QXXX-XXXX-XXXX/switchPorts/3",
|
||||||
|
"operation": "update",
|
||||||
|
"body": {
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**List All Action Batches:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/actionBatches" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Get a Specific Action Batch:**
|
||||||
|
|
||||||
|
Replace `{id}` with the actual action batch ID.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/actionBatches/{id}" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Delete an Action Batch:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -X DELETE -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/actionBatches/{id}"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. Automation and Scripting with `curl`
|
||||||
|
|
||||||
|
Automate repetitive tasks using shell scripts.
|
||||||
|
|
||||||
|
**Automate Data Fetch:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Fetch Meraki data and save to files
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
ORGANIZATION_ID="your_organization_id"
|
||||||
|
BASE_URL="https://api.meraki.com/api/v1"
|
||||||
|
|
||||||
|
# Fetch organizations
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations" > organizations.json
|
||||||
|
echo "Organizations data saved to organizations.json"
|
||||||
|
|
||||||
|
# Fetch networks
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/networks" > networks.json
|
||||||
|
echo "Networks data saved to networks.json"
|
||||||
|
|
||||||
|
# Fetch devices
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/devices" > devices.json
|
||||||
|
echo "Devices data saved to devices.json"
|
||||||
|
|
||||||
|
# Fetch uplinks for a specific device
|
||||||
|
DEVICE_SERIAL="your_device_serial"
|
||||||
|
curl -s "$BASE_URL/organizations/$ORGANIZATION_ID/devices/uplinks/addresses/byDevice?serials[]=$DEVICE_SERIAL" -H "Authorization: Bearer $API_KEY" > uplinks.json
|
||||||
|
echo "Uplinks data saved to uplinks.json"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5. Data Processing with Python
|
||||||
|
|
||||||
|
Use Python for more complex data processing and analysis.
|
||||||
|
|
||||||
|
**Loading and Analyzing Data with Python:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
# Load organizations data
|
||||||
|
with open('organizations.json') as f:
|
||||||
|
organizations = json.load(f)
|
||||||
|
|
||||||
|
# Load networks data
|
||||||
|
with open('networks.json') as f:
|
||||||
|
networks = json.load(f)
|
||||||
|
|
||||||
|
# Load devices data
|
||||||
|
with open('devices.json') as f:
|
||||||
|
devices = json.load(f)
|
||||||
|
|
||||||
|
# Load uplinks data
|
||||||
|
with open('uplinks.json') as f:
|
||||||
|
uplinks = json.load(f)
|
||||||
|
|
||||||
|
# Example: Print organization names and IDs
|
||||||
|
for org in organizations:
|
||||||
|
print(f"Organization ID: {org['id']}, Name: {org['name']}")
|
||||||
|
|
||||||
|
# Example: Find and print offline devices
|
||||||
|
offline_devices = [device for device in devices if device.get('status') != 'online']
|
||||||
|
print(f"Number of offline devices: {len(offline_devices)}")
|
||||||
|
for device in offline_devices:
|
||||||
|
print(f"Offline Device: {device['name']} (Serial: {device['serial']})")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 6. Monitoring and Alerts with `curl`
|
||||||
|
|
||||||
|
Set up monitoring and alerts using `curl` in conjunction with shell scripts.
|
||||||
|
|
||||||
|
**Monitoring Device Status:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Monitor device status and alert if any device is offline
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
ORGANIZATION_ID="your_organization_id"
|
||||||
|
BASE_URL="https://api.meraki.com/api/v1"
|
||||||
|
OUTPUT_FILE="device_status.json"
|
||||||
|
|
||||||
|
# Fetch devices
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/devices" > $OUTPUT_FILE
|
||||||
|
|
||||||
|
# Check device statuses
|
||||||
|
offline_devices=$(jq '.[] | select(.status != "online")' $OUTPUT_FILE)
|
||||||
|
|
||||||
|
if [ -n "$offline_devices" ]; then
|
||||||
|
echo "ALERT: Some devices are offline!"
|
||||||
|
echo "$offline_devices" | jq '.'
|
||||||
|
else
|
||||||
|
echo "All devices are online."
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 7. Data Analysis and Visualization with Python
|
||||||
|
|
||||||
|
**Analyzing Device Data with pandas:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Load data from file
|
||||||
|
df = pd.read_json('devices.json')
|
||||||
|
|
||||||
|
# Perform analysis
|
||||||
|
offline_devices = df[df['status'] != 'online']
|
||||||
|
print(f"Number of offline devices: {len(offline_devices)}")
|
||||||
|
|
||||||
|
# Visualize data
|
||||||
|
df['status'].value_counts().plot(kind='bar')
|
||||||
|
plt.title('Device Status Distribution')
|
||||||
|
plt.xlabel('Status')
|
||||||
|
plt.ylabel('Count')
|
||||||
|
plt.show()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Global Meraki Path Parameter IDs
|
||||||
|
|
||||||
|
Understanding the path parameters and their corresponding API operations helps in navigating the Meraki Dashboard API efficiently. Use the provided table to identify the necessary path parameters and the corresponding endpoints.
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
|
||||||
|
By following this comprehensive guide, you can efficiently interact with the Meraki Dashboard API, automate tasks, monitor network status, and gain insights from your Meraki network data. Using `curl` for API requests and Python for data processing allows you to create robust and scalable solutions for managing your Meraki infrastructure.
|
||||||
|
|
||||||
|
If you have any further questions or need additional examples, feel free to ask!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Efficient Workflow with the Meraki API Using `curl` and Python
|
||||||
|
|
||||||
|
Let's create an efficient workflow for interacting with the Meraki API, primarily using `curl` and then supplementing with Python where necessary. This approach will focus on fetching, storing, processing, and automating tasks with `curl`, and using Python for more complex data manipulation and analysis.
|
||||||
|
|
||||||
|
### 1. Authorization and Setup
|
||||||
|
|
||||||
|
Set up your environment with your Meraki API key.
|
||||||
|
|
||||||
|
**Example: Setting Up Authorization**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export MERAKI_API_KEY="your_api_key"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Making API Calls with `curl`
|
||||||
|
|
||||||
|
#### Find Your Organization ID
|
||||||
|
|
||||||
|
**Request:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" > organizations.json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### List Networks in an Organization
|
||||||
|
|
||||||
|
Replace `{organizationId}` with your actual organization ID.
|
||||||
|
|
||||||
|
**Request:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/networks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/networks" > networks.json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### List Devices in an Organization
|
||||||
|
|
||||||
|
**Request:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/devices" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations/{organizationId}/devices" > devices.json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Get Device Uplink Addresses
|
||||||
|
|
||||||
|
**Request for One Device:**
|
||||||
|
|
||||||
|
Replace `{serial}` with the actual device serial number.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s "https://api.meraki.com/api/v1/organizations/{organizationId}/devices/uplinks/addresses/byDevice?serials[]={serial}" -H 'Authorization: Bearer $MERAKI_API_KEY' | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
**Store the Response:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s "https://api.meraki.com/api/v1/organizations/{organizationId}/devices/uplinks/addresses/byDevice?serials[]={serial}" -H 'Authorization: Bearer $MERAKI_API_KEY' > uplinks.json
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Automation and Scripting with `curl`
|
||||||
|
|
||||||
|
Automate repetitive tasks using shell scripts.
|
||||||
|
|
||||||
|
**Example: Automating Data Fetch**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Fetch Meraki data and save to files
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
ORGANIZATION_ID="your_organization_id"
|
||||||
|
BASE_URL="https://api.meraki.com/api/v1"
|
||||||
|
|
||||||
|
# Fetch organizations
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations" > organizations.json
|
||||||
|
echo "Organizations data saved to organizations.json"
|
||||||
|
|
||||||
|
# Fetch networks
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/networks" > networks.json
|
||||||
|
echo "Networks data saved to networks.json"
|
||||||
|
|
||||||
|
# Fetch devices
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/devices" > devices.json
|
||||||
|
echo "Devices data saved to devices.json"
|
||||||
|
|
||||||
|
# Fetch uplinks for a specific device
|
||||||
|
DEVICE_SERIAL="your_device_serial"
|
||||||
|
curl -s "$BASE_URL/organizations/$ORGANIZATION_ID/devices/uplinks/addresses/byDevice?serials[]=$DEVICE_SERIAL" -H "Authorization: Bearer $API_KEY" > uplinks.json
|
||||||
|
echo "Uplinks data saved to uplinks.json"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Data Processing with Python
|
||||||
|
|
||||||
|
Use Python for more complex data processing and analysis.
|
||||||
|
|
||||||
|
**Example: Loading and Analyzing Data with Python**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
# Load organizations data
|
||||||
|
with open('organizations.json') as f:
|
||||||
|
organizations = json.load(f)
|
||||||
|
|
||||||
|
# Load networks data
|
||||||
|
with open('networks.json') as f:
|
||||||
|
networks = json.load(f)
|
||||||
|
|
||||||
|
# Load devices data
|
||||||
|
with open('devices.json') as f:
|
||||||
|
devices = json.load(f)
|
||||||
|
|
||||||
|
# Load uplinks data
|
||||||
|
with open('uplinks.json') as f:
|
||||||
|
uplinks = json.load(f)
|
||||||
|
|
||||||
|
# Example: Print organization names and IDs
|
||||||
|
for org in organizations:
|
||||||
|
print(f"Organization ID: {org['id']}, Name: {org['name']}")
|
||||||
|
|
||||||
|
# Example: Find and print offline devices
|
||||||
|
offline_devices = [device for device in devices if device.get('status') != 'online']
|
||||||
|
print(f"Number of offline devices: {len(offline_devices)}")
|
||||||
|
for device in offline_devices:
|
||||||
|
print(f"Offline Device: {device['name']} (Serial: {device['serial']})")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Monitoring and Alerts with `curl`
|
||||||
|
|
||||||
|
Use `curl` in conjunction with shell scripts to set up monitoring and alerts.
|
||||||
|
|
||||||
|
**Example: Monitoring Device Status**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Monitor device status and alert if any device is offline
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
ORGANIZATION_ID="your_organization_id"
|
||||||
|
BASE_URL="https://api.meraki.com/api/v1"
|
||||||
|
OUTPUT_FILE="device_status.json"
|
||||||
|
|
||||||
|
# Fetch devices
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/organizations/$ORGANIZATION_ID/devices" > $OUTPUT_FILE
|
||||||
|
|
||||||
|
# Check device statuses
|
||||||
|
offline_devices=$(jq '.[] | select(.status != "online")' $OUTPUT_FILE)
|
||||||
|
|
||||||
|
if [ -n "$offline_devices" ]; then
|
||||||
|
echo "ALERT: Some devices are offline!"
|
||||||
|
echo "$offline_devices" | jq '.'
|
||||||
|
else
|
||||||
|
echo "All devices are online."
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. Data Analysis and Visualization with Python
|
||||||
|
|
||||||
|
**Example: Analyzing Device Data with pandas**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Load data from file
|
||||||
|
df = pd.read_json('devices.json')
|
||||||
|
|
||||||
|
# Perform analysis
|
||||||
|
offline_devices = df[df['status'] != 'online']
|
||||||
|
print(f"Number of offline devices: {len(offline_devices)}")
|
||||||
|
|
||||||
|
# Visualize data
|
||||||
|
df['status'].value_counts().plot(kind='bar')
|
||||||
|
plt.title('Device Status Distribution')
|
||||||
|
plt.xlabel('Status')
|
||||||
|
plt.ylabel('Count')
|
||||||
|
plt.show()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
|
||||||
|
Using `curl` for making API calls and shell scripting for automation allows for efficient interaction with the Meraki API. Python serves as a powerful tool for more complex data processing, analysis, and visualization tasks. By integrating these tools into your workflow, you can automate tasks, monitor network status, and gain insights from your Meraki network data.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Interacting with APIs, including the Meraki Dashboard API, typically involves a series of steps to automate tasks, gather data, and make data-driven decisions. Since API calls can be ephemeral, it's important to think about how to efficiently use the returned JSON payloads. Here’s a structured approach to interacting with the Meraki API and working with the JSON data it returns.
|
||||||
|
|
||||||
|
### Typical Workflow for Interacting with the Meraki API
|
||||||
|
|
||||||
|
1. **Authorization and Setup**
|
||||||
|
2. **Making API Calls**
|
||||||
|
3. **Storing and Processing Data**
|
||||||
|
4. **Automation and Scripting**
|
||||||
|
5. **Monitoring and Alerts**
|
||||||
|
6. **Data Analysis and Visualization**
|
||||||
|
|
||||||
|
### 1. Authorization and Setup
|
||||||
|
|
||||||
|
Before making any API calls, you need to set up your environment, including obtaining an API key and setting up any required tools (like Postman, Python libraries, or shell scripts).
|
||||||
|
|
||||||
|
**Example: Setting Up Authorization**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export MERAKI_API_KEY="your_api_key"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Making API Calls
|
||||||
|
|
||||||
|
Use tools like `curl`, Postman, or a programming language (e.g., Python) to make API calls. These calls fetch data from Meraki's servers.
|
||||||
|
|
||||||
|
**Example: Fetching Organizations**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Storing and Processing Data
|
||||||
|
|
||||||
|
API responses can be stored in databases or files for further processing. Storing data locally allows for repeated analysis without re-fetching data.
|
||||||
|
|
||||||
|
**Example: Saving API Response to a File**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "Authorization: Bearer $MERAKI_API_KEY" "https://api.meraki.com/api/v1/organizations" > organizations.json
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example: Loading and Processing Data with Python**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
with open('organizations.json') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
# Process the data
|
||||||
|
for org in data:
|
||||||
|
print(f"Organization ID: {org['id']}, Name: {org['name']}")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Automation and Scripting
|
||||||
|
|
||||||
|
Automate repetitive tasks using scripts. This can include fetching data periodically, updating configurations, or processing logs.
|
||||||
|
|
||||||
|
**Example: Automating Data Fetch with a Shell Script**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Script to fetch Meraki organizations and save to a file
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
API_URL="https://api.meraki.com/api/v1/organizations"
|
||||||
|
OUTPUT_FILE="organizations.json"
|
||||||
|
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" $API_URL > $OUTPUT_FILE
|
||||||
|
echo "Data saved to $OUTPUT_FILE"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example: Scheduling Script with Cron**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Edit crontab to run the script daily at midnight
|
||||||
|
crontab -e
|
||||||
|
|
||||||
|
# Add the following line
|
||||||
|
0 0 * * * /path/to/your/script.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Monitoring and Alerts
|
||||||
|
|
||||||
|
Set up monitoring to watch for specific conditions or thresholds. This can include network performance, device status, or security events. Use tools like Nagios, Zabbix, or even custom scripts.
|
||||||
|
|
||||||
|
**Example: Monitoring Device Status with Python**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import requests
|
||||||
|
|
||||||
|
API_KEY = 'your_api_key'
|
||||||
|
ORGANIZATION_ID = 'your_organization_id'
|
||||||
|
HEADERS = {
|
||||||
|
'Authorization': f'Bearer {API_KEY}'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fetch device status
|
||||||
|
response = requests.get(f'https://api.meraki.com/api/v1/organizations/{ORGANIZATION_ID}/devices', headers=HEADERS)
|
||||||
|
devices = response.json()
|
||||||
|
|
||||||
|
# Check device statuses
|
||||||
|
for device in devices:
|
||||||
|
if device['status'] != 'online':
|
||||||
|
print(f"Device {device['name']} is offline!")
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. Data Analysis and Visualization
|
||||||
|
|
||||||
|
Analyze the data to gain insights and make informed decisions. Use tools like Jupyter Notebooks, pandas, or visualization libraries like Matplotlib or Tableau.
|
||||||
|
|
||||||
|
**Example: Analyzing Device Data with pandas**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
# Load data from file
|
||||||
|
df = pd.read_json('devices.json')
|
||||||
|
|
||||||
|
# Perform analysis
|
||||||
|
offline_devices = df[df['status'] != 'online']
|
||||||
|
print(f"Number of offline devices: {len(offline_devices)}")
|
||||||
|
|
||||||
|
# Visualize data
|
||||||
|
df['status'].value_counts().plot(kind='bar')
|
||||||
|
plt.show()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Efficient Use of API Responses
|
||||||
|
|
||||||
|
- **Caching:** Store API responses locally to avoid repeated API calls. Use databases like SQLite, MongoDB, or even simple JSON files for small-scale caching.
|
||||||
|
- **Batch Processing:** Process data in batches to minimize the number of API calls. Fetch larger datasets and process them locally.
|
||||||
|
- **Rate Limiting:** Respect API rate limits to avoid being throttled. Implement retry logic and backoff strategies.
|
||||||
|
- **Incremental Updates:** Fetch only updated data instead of the entire dataset to reduce the load and increase efficiency.
|
||||||
|
|
||||||
|
### Practical Examples
|
||||||
|
|
||||||
|
#### Example: Fetching and Storing Network Data
|
||||||
|
|
||||||
|
**Shell Script:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
#!/bin/bash
|
||||||
|
# Fetch networks for an organization and store in a file
|
||||||
|
|
||||||
|
API_KEY="your_api_key"
|
||||||
|
ORGANIZATION_ID="your_organization_id"
|
||||||
|
API_URL="https://api.meraki.com/api/v1/organizations/$ORGANIZATION_ID/networks"
|
||||||
|
OUTPUT_FILE="networks.json"
|
||||||
|
|
||||||
|
curl -s -H "Authorization: Bearer $API_KEY" $API_URL > $OUTPUT_FILE
|
||||||
|
echo "Network data saved to $OUTPUT_FILE"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Python Script:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
API_KEY = 'your_api_key'
|
||||||
|
ORGANIZATION_ID = 'your_organization_id'
|
||||||
|
HEADERS = {
|
||||||
|
'Authorization': f'Bearer {API_KEY}'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fetch networks
|
||||||
|
response = requests.get(f'https://api.meraki.com/api/v1/organizations/{ORGANIZATION_ID}/networks', headers=HEADERS)
|
||||||
|
networks = response.json()
|
||||||
|
|
||||||
|
# Save to file
|
||||||
|
with open('networks.json', 'w') as f:
|
||||||
|
json.dump(networks, f, indent=4)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
|
||||||
|
By following these steps, you can efficiently interact with the Meraki Dashboard API, store and process data, automate tasks, monitor network status, and analyze the data to make informed decisions. The key is to leverage the API for automation, efficient data handling, and actionable insights. If you have further questions or need more specific examples, feel free to ask!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Cisco Meraki organizes its networks and devices in a hierarchical structure that provides a clear and scalable way to manage and monitor your network infrastructure. Understanding this hierarchy is essential for effectively using the Meraki Dashboard and API. Here’s a breakdown of the hierarchy:
|
||||||
|
|
||||||
|
## Meraki Hierarchy
|
||||||
|
|
||||||
|
1. **Organizations**
|
||||||
|
2. **Networks**
|
||||||
|
3. **Devices**
|
||||||
|
4. **Clients**
|
||||||
|
|
||||||
|
### 1. Organizations
|
||||||
|
|
||||||
|
**Organizations** are the top-level entities in the Meraki ecosystem. An organization can represent a company, a business unit, or a large geographical area. Each organization can contain multiple networks.
|
||||||
|
|
||||||
|
- **Key Attributes:**
|
||||||
|
- `id`: Unique identifier for the organization.
|
||||||
|
- `name`: Name of the organization.
|
||||||
|
|
||||||
|
**Example API Request:**
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Networks
|
||||||
|
|
||||||
|
**Networks** are collections of devices and configurations that work together to provide network services. Networks can be configured for different purposes such as office locations, retail stores, or different sites within a campus.
|
||||||
|
|
||||||
|
- **Key Attributes:**
|
||||||
|
- `id`: Unique identifier for the network.
|
||||||
|
- `organizationId`: ID of the organization to which the network belongs.
|
||||||
|
- `name`: Name of the network.
|
||||||
|
- `type`: Type of network (e.g., `wireless`, `switch`, `appliance`, `combined`).
|
||||||
|
|
||||||
|
**Example API Request:**
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations/ORGANIZATION_ID/networks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Devices
|
||||||
|
|
||||||
|
**Devices** are the individual hardware units that make up a network. This includes wireless access points, switches, security appliances, cameras, and other Meraki devices.
|
||||||
|
|
||||||
|
- **Key Attributes:**
|
||||||
|
- `serial`: Unique serial number of the device.
|
||||||
|
- `networkId`: ID of the network to which the device belongs.
|
||||||
|
- `model`: Model of the device (e.g., `MX64`, `MR33`).
|
||||||
|
- `name`: Name of the device.
|
||||||
|
- `mac`: MAC address of the device.
|
||||||
|
|
||||||
|
**Example API Request:**
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/networks/NETWORK_ID/devices" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Clients
|
||||||
|
|
||||||
|
**Clients** are the end devices that connect to the Meraki network. This can include computers, smartphones, tablets, and IoT devices.
|
||||||
|
|
||||||
|
- **Key Attributes:**
|
||||||
|
- `id`: Unique identifier for the client.
|
||||||
|
- `description`: Description of the client.
|
||||||
|
- `mac`: MAC address of the client.
|
||||||
|
- `ip`: IP address assigned to the client.
|
||||||
|
- `usage`: Data usage statistics for the client.
|
||||||
|
|
||||||
|
**Example API Request:**
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/networks/NETWORK_ID/clients" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Practical Usage Example
|
||||||
|
|
||||||
|
Here’s how you can traverse this hierarchy using the Meraki Dashboard API:
|
||||||
|
|
||||||
|
### Step 1: List Organizations
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: List Networks in an Organization
|
||||||
|
|
||||||
|
Replace `ORGANIZATION_ID` with the actual ID of your organization.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/organizations/ORGANIZATION_ID/networks" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: List Devices in a Network
|
||||||
|
|
||||||
|
Replace `NETWORK_ID` with the actual ID of your network.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/networks/NETWORK_ID/devices" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: List Clients in a Network
|
||||||
|
|
||||||
|
Replace `NETWORK_ID` with the actual ID of your network.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -s -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" "https://api.meraki.com/api/v1/networks/NETWORK_ID/clients" | jq .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
Understanding the hierarchy used by Meraki to organize networks and devices is crucial for effective network management and monitoring. The Meraki Dashboard API provides a powerful way to programmatically interact with your network infrastructure, allowing you to automate tasks, gather data, and perform complex queries.
|
||||||
|
|
||||||
|
By practicing with the Meraki Dashboard API and using `jq` to parse and manipulate the JSON data, you can gain deeper insights into your network and improve your overall management capabilities. If you have any further questions or need additional examples, feel free to ask!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
Reference in New Issue
Block a user