Skip to content

Commit

Permalink
feat: added network info collecting
Browse files Browse the repository at this point in the history
No fancy body, chatgpt didn't understand this commit
  • Loading branch information
tymees committed Jan 30, 2025
1 parent 6f139de commit 4c877ec
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
25 changes: 25 additions & 0 deletions humitifier-common/src/humitifier_common/artefacts/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,28 @@ class Package(BaseModel):
@fact(group="generic")
class PackageList(list[Package]):
pass


##
## Network info
##


class AddressInfo(BaseModel):
family: str
address: str
scope: str


class NetworkInterface(BaseModel):
name: str
altnames: list[str]
link_type: str
mac_address: str
flags: list[str]
addresses: list[AddressInfo]


@fact(group="generic")
class NetworkInterfaces(list[NetworkInterface]):
pass
43 changes: 43 additions & 0 deletions humitifier-scanner/src/humitifier_scanner/collectors/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .backend import CollectInfo, ShellCollector
from humitifier_scanner.executor.linux_shell import LinuxShellExecutor, ShellOutput
from humitifier_common.artefacts import (
AddressInfo,
Block,
BlockDevice,
Blocks,
Expand All @@ -13,6 +14,8 @@
HostnameCtl,
Memory,
MemoryRange,
NetworkInterface,
NetworkInterfaces,
Package,
PackageList,
User,
Expand Down Expand Up @@ -297,3 +300,43 @@ def _parse_result(result: ShellOutput):
packages.append(Package(name=name, version=version))

return PackageList(packages)


class NetworkInterfacesFactCollector(ShellCollector):
fact = NetworkInterfaces

def collect_from_shell(
self, shell_executor: LinuxShellExecutor, info: CollectInfo
) -> NetworkInterfaces:

ip_cmd = shell_executor.execute("ip -j addr show")

json_str = "".join(ip_cmd.stdout)

data = json.loads(json_str)

interfaces = []

for interface in data:
addresses = []
for address in interface["addr_info"]:
addresses.append(
AddressInfo(
family=address["family"],
address=f"{address['local']}/{address['prefixlen']}",
scope=address["scope"],
)
)

interfaces.append(
NetworkInterface(
name=interface["ifname"],
altnames=interface.get("altnames", []),
link_type=interface["link_type"],
mac_address=interface["address"],
flags=interface["flags"],
addresses=addresses,
)
)

return NetworkInterfaces(interfaces)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
HostnameCtl,
IsWordpress,
Memory,
NetworkInterfaces,
PackageList,
PuppetAgent,
Uptime,
Expand Down Expand Up @@ -306,6 +307,36 @@ def get_context(self, **kwargs) -> dict:
return context


class NetworkInterfacesVisualizer(SearchableCardsVisualizer):
title = "Network Interfaces"
artefact = NetworkInterfaces

def get_items(self) -> list[Card]:
output = []

for interface in self.artefact_data:
content_items = {
"Altnames": ", ".join(interface.altnames),
"Link type": interface.link_type,
"Flags": ", ".join(interface.flags),
}
search_value = f"{interface.name} {interface.altnames}"
for address in interface.addresses:
search_value += f" {address.address}"
content_items[address.family] = address.address

output.append(
Card(
title=interface.name,
aside=interface.mac_address,
content_items=content_items,
search_value=search_value,
)
)

return output


class ZFSVisualizer(ArtefactVisualizer):
title = "ZFS"
artefact = ZFS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class V2ScanVisualizer(ComponentScanVisualizer):
visualizers.MemoryVisualizer,
visualizers.ZFSVisualizer,
visualizers.HardwareVisualizer,
visualizers.NetworkInterfacesVisualizer,
visualizers.HostMetaVisualizer,
visualizers.HostMetaVHostsVisualizer,
visualizers.HostnameCtlVisualizer,
Expand Down

0 comments on commit 4c877ec

Please sign in to comment.