Skip to content

Instantly share code, notes, and snippets.

@yujiterada
Created December 4, 2023 22:23
Show Gist options
  • Save yujiterada/b636a5ffa65be067b9a1b313a079b2e4 to your computer and use it in GitHub Desktop.
Save yujiterada/b636a5ffa65be067b9a1b313a079b2e4 to your computer and use it in GitHub Desktop.
Monitor PoE consumption, traffic received, traffic sent, and number of associated clients for MRs
import json
import os
import sys
import re
import logging
import time
import meraki
DATA = {
"L_610800699462125333": {
"Q4CD-U6SQ-XXXX": {
"switch_name": "SB-SW-5F-01",
"ports": {
7: {
"ap_name": "SB-AP-5F-01",
"serial": "Q5AC-D43S-XXXX"
}
}
}
}
}
# Get the API key and organization ID from the environment variables
API_KEY = os.environ.get('MERAKI_API_KEY')
if not API_KEY:
sys.exit()
# Configure logging
logging.basicConfig(filename='application.csv', format='%(message)s', level=logging.INFO)
logger = logging.getLogger()
# Load client data
client_data = {}
if os.path.exists('client_data.txt'): # check if the file exists
with open('client_data.txt', 'r') as infile:
client_data = json.load(infile)
# Obtain current time
timestamp = time.time()
# Initialize the Dashboard API with your API key
m_client = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)
for network_id, network_DATA in DATA.items():
for switch_serial, switch_DATA in network_DATA.items():
switch_name = switch_DATA['switch_name']
port_statuses = m_client.switch.getDeviceSwitchPortsStatuses(
serial=switch_serial, timespan=3600)
for port_status in port_statuses:
ap_DATA = switch_DATA['ports'].get(int(port_status['portId']))
if not ap_DATA:
continue
sent_in_kb = port_status.get('usageInKb', {}).get('sent', 0)
recv_in_kb = port_status.get('usageInKb', {}).get('recv', 0)
power_usage_in_wh = port_status.get('powerUsageInWh', 0)
ap_name = ap_DATA['ap_name']
ap_serial = ap_DATA['serial']
device_clients = m_client.devices.getDeviceClients(serial=ap_serial, timespan=3600)
for client in device_clients:
if not client_data.get(client['id']):
client_data[client['id']] = client
client_ids = '|'.join([client['id'] for client in device_clients])
logger.info('{}, {}, {}, {}, {}, {}, {}, {}, {}'.format(
timestamp, switch_name, switch_serial, sent_in_kb, recv_in_kb,
power_usage_in_wh, ap_name, ap_serial, client_ids))
# Write client details
with open('client_data.txt', 'w') as outfile:
json.dump(client_data, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment