Skip to content

Version 0.1.0 is a first version, for trial. See what is not proven →

bogolang
FR

← Examples

meraki_inventory

Reads the networks and devices of a Meraki organisation and lists firmware that is not the one wanted.

This program reads the networks and the devices of one Meraki organisation through the Dashboard API. It prints the devices as a table, counts them by model, and lists the devices that do not run the firmware you want for their product family. Every request is a read.

Run it from this folder, on the saved answers in saved:

bogo run --replay saved meraki_inventory.bogo
bogo test meraki_inventory.bogo

With --replay saved nothing is contacted and no API key is needed. The saved answers were written by hand. The organisation, the names, the serial numbers and the addresses are invented.

To use it on your own organisation:

  1. Store your API key once: bogo login --api meraki.
  2. Set org to the name or the number of your organisation, and wanted to your own firmware baseline.
  3. Run it without --replay. It then really connects to the Meraki cloud. Add --save-replay saved to keep the answers for a later replay.
meraki_inventory.bogo
edition 2027
"""The devices of one Meraki organisation, and those not on the firmware we want."""


class MerakiInventory:
    """Reads the networks and devices of an organisation through the Dashboard API.

    Every request is a read. The API key is the token stored under the name "meraki"
    with `bogo login --api meraki`; it is never printed.
    """

    org = "Example Org"
    # the firmware each product family should run; change to your own baseline
    wanted: dict[str, str] = {"MX": "MX 18.211.2", "MS": "MS 17.1.4", "MR": "MR 31.1.6"}

    @staticmethod
    def family(model: str) -> str:
        return model[:2].upper()

    @staticmethod
    def behind(devices: list[Row], wanted: dict[str, str]) -> list[Row]:
        """The devices whose family has a wanted firmware and that run another one."""
        out: list[Row] = []
        for d in devices:
            want = wanted.get(MerakiInventory.family(d.model))
            if want is not None and d.firmware != want:
                out.append(d)
        return out

    @staticmethod
    def per_model(devices: list[Row]) -> dict[str, int]:
        count: dict[str, int] = {}
        for d in devices:
            count[d.model] = count.get(d.model, 0) + 1
        return dict(sorted(count.items()))

    @staticmethod
    def main(args: list[str]) -> None:
        m = meraki(org=MerakiInventory.org)
        networks = {n.id: n.name for n in m.networks()}
        devices = m.devices()
        print(f"{m.org_name} ({m.org_id}): {len(networks)} networks, {len(devices)} devices")
        print()
        table(devices, ["name", "model", "serial", "lanIp", "firmware"])
        print()
        for model, n in MerakiInventory.per_model(devices).items():
            print(f"{n:3}  {model}")
        print()
        late = MerakiInventory.behind(devices, MerakiInventory.wanted)
        for d in late:
            want = MerakiInventory.wanted[MerakiInventory.family(d.model)]
            print(
                f"Behind: {d.name} in {networks.get(d.networkId, '?')} runs {d.firmware}, wanted {want}"
            )
        print(f"{len(late)} of {len(devices)} devices are not on the wanted firmware.")


class TestMerakiInventory:
    def test_family_of_a_model(self) -> None:
        assert MerakiInventory.family("MS225-48LP") == "MS"
        assert MerakiInventory.family("mr46") == "MR"

    def test_behind_skips_matching_and_unknown_families(self) -> None:
        devices = [
            Row({"name": "a", "model": "MX68", "firmware": "MX 18.211.2"}),
            Row({"name": "b", "model": "MR36", "firmware": "MR 30.7"}),
            Row({"name": "c", "model": "MG21", "firmware": "MG 1.0"}),
        ]
        late = MerakiInventory.behind(devices, MerakiInventory.wanted)
        assert [d.name for d in late] == ["b"]

    def test_per_model_counts(self) -> None:
        devices = [Row({"model": "MR46"}), Row({"model": "MX85"}), Row({"model": "MR46"})]
        assert MerakiInventory.per_model(devices) == {"MR46": 2, "MX85": 1}