add --log-nodes flag for comprehensive node logging

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-18 08:37:13 -08:00
parent 09a99064c3
commit 37b30fe788
3 changed files with 49 additions and 13 deletions

View File

@@ -12,6 +12,7 @@ func main() {
noLLDP := flag.Bool("no-lldp", false, "disable LLDP discovery")
noSNMP := flag.Bool("no-snmp", false, "disable SNMP discovery")
logEvents := flag.Bool("log-events", false, "log node events")
logNodes := flag.Bool("log-nodes", false, "log full node details on changes")
debugARP := flag.Bool("debug-arp", false, "debug ARP discovery")
debugLLDP := flag.Bool("debug-lldp", false, "debug LLDP discovery")
debugSNMP := flag.Bool("debug-snmp", false, "debug SNMP discovery")
@@ -23,6 +24,7 @@ func main() {
t.DisableLLDP = *noLLDP
t.DisableSNMP = *noSNMP
t.LogEvents = *logEvents
t.LogNodes = *logNodes
t.DebugARP = *debugARP
t.DebugLLDP = *debugLLDP
t.DebugSNMP = *debugSNMP

View File

@@ -15,21 +15,26 @@ type Interface struct {
}
func (i *Interface) String() string {
name := i.Name
if name == "" {
name = "??"
}
var ips []string
for _, ip := range i.IPs {
ips = append(ips, ip.String())
}
sort.Strings(ips)
if len(ips) == 0 {
return fmt.Sprintf("%s/%s", name, i.MAC)
var parts []string
parts = append(parts, i.MAC.String())
if i.Name != "" {
parts = append(parts, fmt.Sprintf("(%s)", i.Name))
}
return fmt.Sprintf("%s/%s %v", name, i.MAC, ips)
if len(ips) > 0 {
parts = append(parts, fmt.Sprintf("%v", ips))
}
result := parts[0]
for _, p := range parts[1:] {
result += " " + p
}
return result
}
type Node struct {
@@ -131,11 +136,16 @@ func (n *Nodes) Update(mac net.HardwareAddr, ips []net.IP, ifaceName, nodeName,
node.Name = nodeName
}
if len(added) > 0 && n.t.LogEvents {
if isNew {
log.Printf("[add] %s %v (via %s)", node, added, source)
} else {
log.Printf("[update] %s +%v (via %s)", node, added, source)
if len(added) > 0 {
if n.t.LogEvents {
if isNew {
log.Printf("[add] %s %v (via %s)", node, added, source)
} else {
log.Printf("[update] %s +%v (via %s)", node, added, source)
}
}
if n.t.LogNodes {
n.logNode(node)
}
}
}
@@ -172,6 +182,10 @@ func (n *Nodes) Merge(macs []net.HardwareAddr, source string) {
}
n.mergeNodes(targetID, ids[i])
}
if n.t.LogNodes {
n.logNode(n.nodes[targetID])
}
}
func (n *Nodes) mergeNodes(keepID, mergeID int) {
@@ -223,6 +237,25 @@ func (n *Nodes) GetByMAC(mac net.HardwareAddr) *Node {
return nil
}
func (n *Nodes) logNode(node *Node) {
name := node.Name
if name == "" {
name = "??"
}
log.Printf("[node] %s", name)
var macKeys []string
for macKey := range node.Interfaces {
macKeys = append(macKeys, macKey)
}
sort.Strings(macKeys)
for _, macKey := range macKeys {
iface := node.Interfaces[macKey]
log.Printf("[node] %s", iface)
}
}
func (n *Nodes) All() []*Node {
n.mu.RLock()
defer n.mu.RUnlock()

View File

@@ -17,6 +17,7 @@ type Tendrils struct {
DisableLLDP bool
DisableSNMP bool
LogEvents bool
LogNodes bool
DebugARP bool
DebugLLDP bool
DebugSNMP bool