Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nodescraper/enums/eventcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class EventCategory(AutoNameStrEnum):
SBIOS/VBIOS/IFWI Errors
- INFRASTRUCTURE
Network, IT issues, Downtime
- NETWORK
Network configuration, interfaces, routing, neighbors, ethtool data
- RUNTIME
Framework issues, does not include content failures
- UNKNOWN
Expand All @@ -82,5 +84,6 @@ class EventCategory(AutoNameStrEnum):
SW_DRIVER = auto()
BIOS = auto()
INFRASTRUCTURE = auto()
NETWORK = auto()
RUNTIME = auto()
UNKNOWN = auto()
131 changes: 120 additions & 11 deletions nodescraper/plugins/inband/network/network_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#
###############################################################################
import re
from typing import List, Optional, Tuple
from typing import Dict, List, Optional, Tuple

from nodescraper.base import InBandDataCollector
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus
from nodescraper.models import TaskResult

from .networkdata import (
EthtoolInfo,
IpAddress,
Neighbor,
NetworkDataModel,
Expand All @@ -48,6 +49,7 @@ class NetworkCollector(InBandDataCollector[NetworkDataModel, None]):
CMD_ROUTE = "ip route show"
CMD_RULE = "ip rule show"
CMD_NEIGHBOR = "ip neighbor show"
CMD_ETHTOOL_TEMPLATE = "sudo ethtool {interface}"

def _parse_ip_addr(self, output: str) -> List[NetworkInterface]:
"""Parse 'ip addr show' output into NetworkInterface objects.
Expand Down Expand Up @@ -370,6 +372,98 @@ def _parse_ip_neighbor(self, output: str) -> List[Neighbor]:

return neighbors

def _parse_ethtool(self, interface: str, output: str) -> EthtoolInfo:
"""Parse 'ethtool <interface>' output into EthtoolInfo object.

Args:
interface: Name of the network interface
output: Raw output from 'ethtool <interface>' command

Returns:
EthtoolInfo object with parsed data
"""
ethtool_info = EthtoolInfo(interface=interface, raw_output=output)

# Parse line by line
current_section = None
for line in output.splitlines():
line_stripped = line.strip()
if not line_stripped:
continue

# Detect sections (lines ending with colon and no tab prefix)
if line_stripped.endswith(":") and not line.startswith("\t"):
current_section = line_stripped.rstrip(":")
continue

# Parse key-value pairs (lines with colon in the middle)
if ":" in line_stripped:
# Split on first colon
parts = line_stripped.split(":", 1)
if len(parts) == 2:
key = parts[0].strip()
value = parts[1].strip()

# Store in settings dict
ethtool_info.settings[key] = value

# Extract specific important fields
if key == "Speed":
ethtool_info.speed = value
elif key == "Duplex":
ethtool_info.duplex = value
elif key == "Port":
ethtool_info.port = value
elif key == "Auto-negotiation":
ethtool_info.auto_negotiation = value
elif key == "Link detected":
ethtool_info.link_detected = value

# Parse supported/advertised link modes (typically indented list items)
elif current_section in ["Supported link modes", "Advertised link modes"]:
# These are typically list items, possibly with speeds like "10baseT/Half"
if line.startswith("\t") or line.startswith(" "):
mode = line_stripped
if current_section == "Supported link modes":
ethtool_info.supported_link_modes.append(mode)
elif current_section == "Advertised link modes":
ethtool_info.advertised_link_modes.append(mode)

return ethtool_info

def _collect_ethtool_info(self, interfaces: List[NetworkInterface]) -> Dict[str, EthtoolInfo]:
"""Collect ethtool information for all network interfaces.

Args:
interfaces: List of NetworkInterface objects to collect ethtool info for

Returns:
Dictionary mapping interface name to EthtoolInfo
"""
ethtool_data = {}

for iface in interfaces:
cmd = self.CMD_ETHTOOL_TEMPLATE.format(interface=iface.name)
res_ethtool = self._run_sut_cmd(cmd)

if res_ethtool.exit_code == 0:
ethtool_info = self._parse_ethtool(iface.name, res_ethtool.stdout)
ethtool_data[iface.name] = ethtool_info
self._log_event(
category=EventCategory.NETWORK,
description=f"Collected ethtool info for interface: {iface.name}",
priority=EventPriority.INFO,
)
else:
self._log_event(
category=EventCategory.NETWORK,
description=f"Error collecting ethtool info for interface: {iface.name}",
data={"command": res_ethtool.command, "exit_code": res_ethtool.exit_code},
priority=EventPriority.WARNING,
)

return ethtool_data

def collect_data(
self,
args=None,
Expand All @@ -384,37 +478,47 @@ def collect_data(
routes = []
rules = []
neighbors = []
ethtool_data = {}

# Collect interface/address information
res_addr = self._run_sut_cmd(self.CMD_ADDR)
if res_addr.exit_code == 0:
interfaces = self._parse_ip_addr(res_addr.stdout)
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description=f"Collected {len(interfaces)} network interfaces",
priority=EventPriority.INFO,
)
else:
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description="Error collecting network interfaces",
data={"command": res_addr.command, "exit_code": res_addr.exit_code},
priority=EventPriority.ERROR,
console_log=True,
)

# Collect ethtool information for interfaces
if interfaces:
ethtool_data = self._collect_ethtool_info(interfaces)
self._log_event(
category=EventCategory.NETWORK,
description=f"Collected ethtool info for {len(ethtool_data)} interfaces",
priority=EventPriority.INFO,
)

# Collect routing table
res_route = self._run_sut_cmd(self.CMD_ROUTE)
if res_route.exit_code == 0:
routes = self._parse_ip_route(res_route.stdout)
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description=f"Collected {len(routes)} routes",
priority=EventPriority.INFO,
)
else:
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description="Error collecting routes",
data={"command": res_route.command, "exit_code": res_route.exit_code},
priority=EventPriority.WARNING,
Expand All @@ -425,13 +529,13 @@ def collect_data(
if res_rule.exit_code == 0:
rules = self._parse_ip_rule(res_rule.stdout)
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description=f"Collected {len(rules)} routing rules",
priority=EventPriority.INFO,
)
else:
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description="Error collecting routing rules",
data={"command": res_rule.command, "exit_code": res_rule.exit_code},
priority=EventPriority.WARNING,
Expand All @@ -442,25 +546,30 @@ def collect_data(
if res_neighbor.exit_code == 0:
neighbors = self._parse_ip_neighbor(res_neighbor.stdout)
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description=f"Collected {len(neighbors)} neighbor entries",
priority=EventPriority.INFO,
)
else:
self._log_event(
category=EventCategory.OS,
category=EventCategory.NETWORK,
description="Error collecting neighbor table",
data={"command": res_neighbor.command, "exit_code": res_neighbor.exit_code},
priority=EventPriority.WARNING,
)

if interfaces or routes or rules or neighbors:
network_data = NetworkDataModel(
interfaces=interfaces, routes=routes, rules=rules, neighbors=neighbors
interfaces=interfaces,
routes=routes,
rules=rules,
neighbors=neighbors,
ethtool_info=ethtool_data,
)
self.result.message = (
f"Collected network data: {len(interfaces)} interfaces, "
f"{len(routes)} routes, {len(rules)} rules, {len(neighbors)} neighbors"
f"{len(routes)} routes, {len(rules)} rules, {len(neighbors)} neighbors, "
f"{len(ethtool_data)} ethtool entries"
)
self.result.status = ExecutionStatus.OK
return self.result, network_data
Expand Down
20 changes: 19 additions & 1 deletion nodescraper/plugins/inband/network/networkdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# SOFTWARE.
#
###############################################################################
from typing import List, Optional
from typing import Dict, List, Optional

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -90,10 +90,28 @@ class Neighbor(BaseModel):
flags: List[str] = Field(default_factory=list) # Additional flags like "router", "proxy"


class EthtoolInfo(BaseModel):
"""Ethtool information for a network interface"""

interface: str # Interface name this info belongs to
raw_output: str # Raw ethtool command output
settings: Dict[str, str] = Field(default_factory=dict) # Parsed key-value settings
supported_link_modes: List[str] = Field(default_factory=list) # Supported link modes
advertised_link_modes: List[str] = Field(default_factory=list) # Advertised link modes
speed: Optional[str] = None # Link speed (e.g., "10000Mb/s")
duplex: Optional[str] = None # Duplex mode (e.g., "Full")
port: Optional[str] = None # Port type (e.g., "Twisted Pair")
auto_negotiation: Optional[str] = None # Auto-negotiation status (e.g., "on", "off")
link_detected: Optional[str] = None # Link detection status (e.g., "yes", "no")


class NetworkDataModel(DataModel):
"""Complete network configuration data"""

interfaces: List[NetworkInterface] = Field(default_factory=list)
routes: List[Route] = Field(default_factory=list)
rules: List[RoutingRule] = Field(default_factory=list)
neighbors: List[Neighbor] = Field(default_factory=list)
ethtool_info: Dict[str, EthtoolInfo] = Field(
default_factory=dict
) # Interface name -> EthtoolInfo mapping
Loading