Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import annotations
- from pathlib import Path
- from typing import NamedTuple
- DEV = Path("/proc/net/dev")
- class ReceiveStats(NamedTuple):
- bytes: int
- packets: int
- errs: int
- drop: int
- fifo: int
- frame: int
- compressed: int
- multicast: int
- class TransmitStats(NamedTuple):
- bytes: int
- packets: int
- errs: int
- drop: int
- fifo: int
- colls: int
- carrier: int
- compressed: int
- class StatsResult(NamedTuple):
- interface: str
- receive: ReceiveStats
- transmit: TransmitStats
- def interface_stats(interface: str) -> StatsResult | None:
- with DEV.open() as fd:
- # skip header
- for _ in range(2):
- next(fd)
- # read statistics
- for iface, *data in map(str.split, fd):
- iface = iface.removesuffix(":")
- if iface == interface:
- # convert all data to int
- data = tuple(map(int, data))
- # instanciate from data a NamedTuple
- receive = ReceiveStats(*data[0:8])
- transmit = TransmitStats(*data[8:])
- # return combined NamedTuple
- return StatsResult(iface, receive, transmit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement