Add shared_names config and port uptime tracking

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-31 12:06:59 -08:00
parent df0c999284
commit d1b4de01e8
5 changed files with 56 additions and 4 deletions

38
snmp.go
View File

@@ -73,10 +73,14 @@ func snmpToInt(val interface{}) (int, bool) {
switch v := val.(type) {
case int:
return v, true
case uint:
case int32:
return int(v), true
case int64:
return int(v), true
case uint:
return int(v), true
case uint32:
return int(v), true
case uint64:
return int(v), true
default:
@@ -126,12 +130,33 @@ func (t *Tendrils) querySNMPDevice(node *Node, ip net.IP) {
t.querySysName(snmp, node)
t.queryInterfaceMACs(snmp, node, ifNames)
t.queryInterfaceStats(snmp, node, ifNames)
sysUpTime := t.getSysUpTime(snmp)
t.queryInterfaceStats(snmp, node, ifNames, sysUpTime)
t.queryPoEBudget(snmp, node)
t.queryBridgeMIB(snmp, node, ifNames)
t.queryDHCPBindings(snmp)
}
func (t *Tendrils) getSysUpTime(snmp *gosnmp.GoSNMP) uint64 {
oid := "1.3.6.1.2.1.1.3.0"
result, err := snmp.Get([]string{oid})
if err != nil {
return 0
}
if len(result.Variables) == 0 {
return 0
}
v, ok := snmpToInt(result.Variables[0].Value)
if !ok {
log.Printf("[ERROR] failed to parse sysUpTime: type=%T value=%v", result.Variables[0].Value, result.Variables[0].Value)
return 0
}
return uint64(v)
}
func (t *Tendrils) querySysName(snmp *gosnmp.GoSNMP, node *Node) {
oid := "1.3.6.1.2.1.1.5.0"
@@ -195,8 +220,9 @@ func (t *Tendrils) queryInterfaceMACs(snmp *gosnmp.GoSNMP, node *Node, ifNames m
}
}
func (t *Tendrils) queryInterfaceStats(snmp *gosnmp.GoSNMP, node *Node, ifNames map[int]string) {
func (t *Tendrils) queryInterfaceStats(snmp *gosnmp.GoSNMP, node *Node, ifNames map[int]string, sysUpTime uint64) {
ifOperStatus := t.getInterfaceTable(snmp, "1.3.6.1.2.1.2.2.1.8")
ifLastChange := t.getInterfaceTable(snmp, "1.3.6.1.2.1.2.2.1.9")
ifHighSpeed := t.getInterfaceTable(snmp, "1.3.6.1.2.1.31.1.1.1.15")
ifInErrors := t.getInterfaceTable(snmp, "1.3.6.1.2.1.2.2.1.14")
ifOutErrors := t.getInterfaceTable(snmp, "1.3.6.1.2.1.2.2.1.20")
@@ -236,6 +262,12 @@ func (t *Tendrils) queryInterfaceStats(snmp *gosnmp.GoSNMP, node *Node, ifNames
stats.Speed = uint64(speed) * 1000000
}
if lastChange, ok := ifLastChange[ifIndex]; ok && sysUpTime > 0 {
if uint64(lastChange) <= sysUpTime {
stats.Uptime = (sysUpTime - uint64(lastChange)) / 100
}
}
if inErr, ok := ifInErrors[ifIndex]; ok {
stats.InErrors = uint64(inErr)
}