I have 300 networks. I'm trying to write a python script to loop through every network and put an email address in for WAN appliance -> A WAN appliance goes offline for 30 minutes. I can't seem to get it to work. I also had an AI write one and it got closers it seems but still doesn't work. I used this documentation. https://developer.cisco.com/meraki/api-v1/update-network-alerts-settings/ I think I could have manually done this by now. Any help much appreciated. Latest is this...
import requests
import json
# Define your API key and base URL
api_key = 'YOUR_API_KEY'
base_url = 'https://api.meraki.com/api/v1'
org_id = 'YOUR_VERIFIED_ORG_ID'
email_to_add = 'user@company.com'
# Define the headers
headers = {
'X-Cisco-Meraki-API-Key': api_key,
'Content-Type': 'application/json'
}
# Define the alert condition for gateway down
alert_settings = {
"defaultDestinations": {
"emails": [email_to_add],
"allAdmins": False,
"snmp": False
},
"alerts": [
{
"type": "gatewayDown",
"enabled": True,
"alertDestinations": {
"emails": [email_to_add],
"allAdmins": False,
"snmp": False
},
"alertCondition": {
"timeout": 30
}
}
]
}
# Fetch all networks in the organization
networks_response = requests.get(f'{base_url}/organizations/{org_id}/networks', headers=headers)
networks = networks_response.json()
# Loop through each network and update alert settings
for network in networks:
network_id = network['id']
alert_settings_endpoint = f'{base_url}/networks/{network_id}/alerts/settings'
# Fetch current alert settings for the network
current_settings_response = requests.get(alert_settings_endpoint, headers=headers)
current_settings = current_settings_response.json()
# Update with new alert settings
current_settings["defaultDestinations"] = alert_settings["defaultDestinations"]
# Ensure gateway down alert is set correctly
gateway_alert_exists = False
for alert in current_settings["alerts"]:
if alert["type"] == "gatewayDown":
alert["enabled"] = True
alert["alertDestinations"] = alert_settings["alerts"][0]["alertDestinations"]
if "alertCondition" in alert:
alert["alertCondition"]["timeout"] = alert_settings["alerts"][0]["alertCondition"]["timeout"]
else:
alert["alertCondition"] = alert_settings["alerts"][0]["alertCondition"]
gateway_alert_exists = True
break
if not gateway_alert_exists:
current_settings["alerts"].append(alert_settings["alerts"][0])
# Update the alert settings
update_response = requests.put(alert_settings_endpoint, headers=headers, data=json.dumps(current_settings))
if update_response.status_code == 200:
print(f'Successfully updated alerts for network {network["name"]}')
else:
print(f'Failed to update alerts for network {network["name"]}')
print(f'Status Code: {update_response.status_code}')
print(f'Response Text: {update_response.text}')
print(f'Request Data: {json.dumps(current_settings, indent=2)}')
print("Completed updating alerts for all networks.")