Skip to content

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

bogolang
FR

← Examples

device_table

Reads show version from each device of an inventory, four at a time, and prints model, version and uptime.

Reads the devices from devices.csv, opens a read-only session to each one (four at a time), and runs show version. The answer is read as a table with .table(), and the program prints one line per device: model, software version, uptime in days, and a note when a device restarted in the last day.

Run it on the saved outputs in saved, with no device:

bogo run --replay saved device_table.bogo
bogo test device_table.bogo

To use it on your own network:

  • Put your devices in devices.csv: the columns name and ip are needed, platform, role and site are optional. Or give another file: bogo run device_table.bogo my_devices.csv.
  • Store a login once with bogo login default.
  • Run it without --replay: then it really connects to each device over SSH.
  • bogo run --save-replay saved device_table.bogo keeps the real outputs for a later replay.
device_table.bogo
edition 2027
"""What runs where? Software version, model and uptime of every device in an inventory."""


class DeviceTable:
    """Reads devices.csv, asks each device for show version, and prints one table."""

    recent: Duration = 1d

    @staticmethod
    def number(text: str) -> int:
        return int(text) if text else 0

    @staticmethod
    def uptime(v: Row) -> Duration:
        """The uptime of one parsed show version line, as one Duration."""
        days: int = (
            365 * DeviceTable.number(v.uptime_years)
            + 7 * DeviceTable.number(v.uptime_weeks)
            + DeviceTable.number(v.uptime_days)
        )
        return (
            Duration(days, "d")
            + Duration(DeviceTable.number(v.uptime_hours), "h")
            + Duration(DeviceTable.number(v.uptime_minutes), "min")
        )

    @staticmethod
    def note(up: Duration, reason: str) -> str:
        """A word for the engineer when a device restarted lately."""
        return f"restarted lately ({reason})" if up < DeviceTable.recent else ""

    @staticmethod
    def main(args: list[str]) -> None:
        devices: list[Device] = inventory(args[0] if args else "devices.csv")
        rows: list[dict[str, object]] = []
        for d in devices parallel 4:
            with ssh(d) as s:
                v: Row = s.show("version").table()[0]
            up: Duration = DeviceTable.uptime(v)
            rows.append(
                {
                    "device": d.name,
                    "site": d.site,
                    "model": v.hardware,
                    "version": v.version,
                    "uptime": round(up.to("d"), 1),
                    "note": DeviceTable.note(up, v.reload_reason),
                }
            )
        rows.sort(key=lambda r: str(r["device"]))
        table(rows)
        print(
            f"\n{len(rows)} devices, {len({r['version'] for r in rows})} software versions. Uptime in days."
        )


class TestDeviceTable:
    def test_uptime_adds_every_part(self) -> None:
        v: Row = Row(
            {
                "uptime_years": "1",
                "uptime_weeks": "2",
                "uptime_days": "3",
                "uptime_hours": "12",
                "uptime_minutes": "0",
            }
        )
        assert DeviceTable.uptime(v).to("d") == 365 + 14 + 3 + 0.5

    def test_missing_parts_count_as_zero(self) -> None:
        v: Row = Row(
            {
                "uptime_years": "",
                "uptime_weeks": "",
                "uptime_days": "",
                "uptime_hours": "6",
                "uptime_minutes": "30",
            }
        )
        assert DeviceTable.uptime(v).to("min") == 390

    def test_note_only_after_a_recent_restart(self) -> None:
        assert DeviceTable.note(6h, "PowerOn") == "restarted lately (PowerOn)"
        assert DeviceTable.note(3d, "PowerOn") == ""