2
0

fix help comment IPv4->IPv6

This commit is contained in:
Kilian Krause 2023-07-09 22:23:55 +02:00
parent 7f00c93001
commit 7b85a0d388

View File

@ -12,22 +12,28 @@ import capport.utils.nft_set
from . import cptypes from . import cptypes
def print_metric(name: str, mtype: str, value, *, now: typing.Optional[int] = None, help: typing.Optional[str] = None): def print_metric(
name: str,
mtype: str,
value,
*,
now: typing.Optional[int] = None,
help: typing.Optional[str] = None,
):
# no labels in our names for now, always print help and type # no labels in our names for now, always print help and type
if help: if help:
print(f'# HELP {name} {help}') print(f"# HELP {name} {help}")
print(f'# TYPE {name} {mtype}') print(f"# TYPE {name} {mtype}")
if now: if now:
print(f'{name} {value} {now}') print(f"{name} {value} {now}")
else: else:
print(f'{name} {value}') print(f"{name} {value}")
async def amain(client_ifname: str): async def amain(client_ifname: str):
ns = capport.utils.nft_set.NftSet() ns = capport.utils.nft_set.NftSet()
captive_allowed_entries: typing.Set[cptypes.MacAddress] = { captive_allowed_entries: typing.Set[cptypes.MacAddress] = {
entry['mac'] entry["mac"] for entry in ns.list()
for entry in ns.list()
} }
seen_allowed_entries: typing.Set[cptypes.MacAddress] = set() seen_allowed_entries: typing.Set[cptypes.MacAddress] = set()
total_ipv4 = 0 total_ipv4 = 0
@ -48,49 +54,51 @@ async def amain(client_ifname: str):
total_ipv6 += 1 total_ipv6 += 1
unique_ipv6.add(mac) unique_ipv6.add(mac)
print_metric( print_metric(
'capport_allowed_macs', "capport_allowed_macs",
'gauge', "gauge",
len(captive_allowed_entries), len(captive_allowed_entries),
help='Number of allowed client mac addresses', help="Number of allowed client mac addresses",
) )
print_metric( print_metric(
'capport_allowed_neigh_macs', "capport_allowed_neigh_macs",
'gauge', "gauge",
len(seen_allowed_entries), len(seen_allowed_entries),
help='Number of allowed client mac addresses seen in neighbor cache', help="Number of allowed client mac addresses seen in neighbor cache",
) )
print_metric( print_metric(
'capport_unique', "capport_unique",
'gauge', "gauge",
len(unique_clients), len(unique_clients),
help='Number of clients (mac addresses) in client network seen in neighbor cache', help="Number of clients (mac addresses) in client network seen in neighbor cache",
) )
print_metric( print_metric(
'capport_unique_ipv4', "capport_unique_ipv4",
'gauge', "gauge",
len(unique_ipv4), len(unique_ipv4),
help='Number of IPv4 clients (unique per mac) in client network seen in neighbor cache', help="Number of IPv4 clients (unique per mac) in client network seen in neighbor cache",
) )
print_metric( print_metric(
'capport_unique_ipv6', "capport_unique_ipv6",
'gauge', "gauge",
len(unique_ipv6), len(unique_ipv6),
help='Number of IPv4 clients (unique per mac) in client network seen in neighbor cache', help="Number of IPv6 clients (unique per mac) in client network seen in neighbor cache",
) )
print_metric( print_metric(
'capport_total_ipv4', "capport_total_ipv4",
'gauge', "gauge",
total_ipv4, total_ipv4,
help='Number of IPv4 addresses seen in neighbor cache', help="Number of IPv4 addresses seen in neighbor cache",
) )
print_metric( print_metric(
'capport_total_ipv6', "capport_total_ipv6",
'gauge', "gauge",
total_ipv6, total_ipv6,
help='Number of IPv6 addresses seen in neighbor cache', help="Number of IPv6 addresses seen in neighbor cache",
) )
def main(): def main():
assert len(sys.argv) == 2, "Need name of client interface as argument" if len(sys.argv) != 2:
trio.run(amain, sys.argv[1]) print("Need name of client interface as argument")
sys.exit(1)
trio.run(amain, sys.argv[1:])