Return switchports matching a specific tag in v1?

ChrisLaird
Here to help

Return switchports matching a specific tag in v1?

I'm trying to monitor a specific set of switchports across our network. All of them have the same tag (cameras), but are spread across ~300 switches and ~10,000 switchports. Of those 10,000 ports, I'm only interested in the 85 that match the tag.

 

In the old v0 API, I'm finding plenty of examples that would have worked, and ChatGPT is happy to write me a script that should have solved my problem in about 5 seconds. But it seems that, in the new v1 API, I can only query ports by switch, not by tag, so I'll need to return every switch, then every port, then filter out just the ports I need. Then, querying those ports to get their usage within a time period seems like an additional layer of issues in v1.

 

Is there some mechanism to do this that I should be seeing? If it used to be as simple as "https://api.meraki.com/api/v0/networks/{network_id}/switchPorts/usage?timespan={six_hours_ago_unix}-...}", I'm hoping that they gave us a new method when they took the old method away.

2 Replies 2
David_Jirku
Meraki Employee
Meraki Employee

How about the org-wide switch port API call

 

https://developer.cisco.com/meraki/api-v1/#!get-organization-switch-ports-by-switch

 

and filter the results by tag, which is returned?

rhbirkelund
Kind of a big deal

I haven't tested this in action, but something I suppose it's something along the lines of this. Feel free to use it as inspiration.

#! /usr/bin/env python3

import meraki
from datetime import datetime

def main():
    dashboard = meraki.DashboardAPI()

    organization_id = ''
    TargetTag = ''

    # https://developer.cisco.com/meraki/api-v1/#!get-organization-switch-ports-by-switch
    AllSwitchportsBySwitch = dashboard.switch.getOrganizationSwitchPortsBySwitch(
        organization_id, total_pages='all'
    )

    Target = []
    for switch in AllSwitchportsBySwitch:
        TargetPortIds = []
        for port in switch['ports']:
            if TargetTag in port['tags']:
                TargetPortIds.append(port['portId'])
        Target.append(
            {
                "serial": switch['serial'],
                "name": switch['name'],
                "portId": TargetPortIds,
            }
        )

    for switch in Target:
        # https://developer.cisco.com/meraki/api-v1/#!get-device-switch-ports-statuses
        SwitchPorts = dashboard.switch.getDeviceSwitchPortsStatuses(
            switch['serial']
        )
        for port in SwitchPorts:
            if port['portId'] in switch['portId']:
                print(port['usageInKb'])

if __name__ == "__main__":
    StartTime = datetime.now()
    main()
    print("\nTime to execute:",datetime.now()-StartTime)
LinkedIn ::: https://blog.rhbirkelund.dk/

Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂

All code examples are provided as is. Responsibility for Code execution lies solely your own.
Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.