Skip to content

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

bogolang
FR

← Examples

snapshot_compare

Takes snapshots before a maintenance and shows what moved after it.

Takes a snapshot of each device (routes, CDP neighbours, interfaces, configuration) before a maintenance and saves it in the folder before. After the work it takes new snapshots and prints what moved on each device, then one summary table. Uptimes and hold times are not counted as changes.

The two moments need two sets of answers, so there are two replay folders: saved_before holds the answers before the work, saved the answers after it. The folder before already holds the snapshots taken from saved_before, so the second command works alone.

bogo run --replay saved_before snapshot_compare.bogo before
bogo run --replay saved snapshot_compare.bogo
bogo test snapshot_compare.bogo

To use it on your own network:

  • List your devices in devices.csv (name, ip, platform).
  • Before the work, run bogo run snapshot_compare.bogo before. After it, run bogo run snapshot_compare.bogo.
  • Without --replay it really connects to each device over SSH, with the login stored by bogo login. The sessions are read-only.
  • A snapshot file holds the configuration of the device. Keep it as carefully as a backup.
snapshot_compare.bogo
edition 2027
"""Snapshots of the devices before a maintenance, and what moved after it."""

from pathlib import Path


class SnapshotCompare:
    """Run with the word "before" ahead of the work: it saves one snapshot file per device
    in the folder before. Run with no word after the work: it takes new snapshots,
    compares them with the saved ones and prints what moved."""

    folder: str = "before"

    @staticmethod
    def file_of(d: Device) -> str:
        return f"{SnapshotCompare.folder}/{d.name}.json"

    @staticmethod
    def counts(c: SnapshotDiff) -> dict[str, str]:
        """One line of the summary table: what was added (+), removed (-) or changed (~)."""
        return {
            "routes": f"+{len(c.routes_added)} -{len(c.routes_removed)}",
            "neighbours": f"+{len(c.neighbors_added)} -{len(c.neighbors_removed)}",
            "interfaces": f"+{len(c.interfaces_added)} -{len(c.interfaces_removed)} ~{len(c.interfaces_changed)}",
            "config lines": f"+{len(c.config.added)} -{len(c.config.removed)}",
            "result": "CHANGED" if c else "same",
        }

    @staticmethod
    def main(args: list[str]) -> None:
        devices: list[Device] = inventory("devices.csv")
        if args == ["before"]:
            Path(SnapshotCompare.folder).mkdir(exist_ok=True)
            for d in devices:
                s: Snapshot = snapshot(d)
                s.save(SnapshotCompare.file_of(d))
                print(f"saved {SnapshotCompare.file_of(d)}: {s}")
            return

        rows: list[dict[str, str]] = []
        for d in devices:
            before: Snapshot = load_snapshot(SnapshotCompare.file_of(d))
            change: SnapshotDiff = compare(before, snapshot(d))
            print(f"== {d.name}, against the snapshot of {before.taken}")
            print(change)
            print()
            rows.append({"device": d.name, **SnapshotCompare.counts(change)})
        table(rows)


class TestSnapshotCompare:
    def snap(self, routes: list[Row], config: str) -> Snapshot:
        d: Device = Device(name="r1", ip=192.0.2.1, platform="iosxe")
        return Snapshot("r1", "iosxe", now(), routes, [], [], Config(d, config, ""))

    def route(self, network: str) -> Row:
        return Row(
            {
                "protocol": "C",
                "network": network,
                "prefix_length": "26",
                "nexthop_ip": "",
                "nexthop_if": "GigabitEthernet0/0/2",
            }
        )

    def test_a_new_route_and_line_are_counted(self) -> None:
        a: Snapshot = self.snap([], "hostname r1\n")
        b: Snapshot = self.snap(
            [self.route("192.0.2.64")], "hostname r1\nntp server 198.51.100.41\n"
        )
        got: dict[str, str] = SnapshotCompare.counts(compare(a, b))
        assert got["routes"] == "+1 -0"
        assert got["config lines"] == "+1 -0"
        assert got["result"] == "CHANGED"

    def test_nothing_moved_is_same(self) -> None:
        a: Snapshot = self.snap([self.route("192.0.2.0")], "hostname r1\n")
        got: dict[str, str] = SnapshotCompare.counts(compare(a, a))
        assert got["result"] == "same"
        assert str(compare(a, a)) == "No difference."