entity.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // entity.go — ENTITY-MIB (RFC 6933, before RFC 4133) best-effort reader.
  2. //
  3. // Some SNMP switches leave sysDescr as the only identity and publish no
  4. // firmware anywhere else, yet they carry the firmware and the short model in
  5. // entPhysicalTable. The chassis(3) row is the unit itself: a stackable switch
  6. // can put a stack(11) row first, so choosing by row index is wrong.
  7. //
  8. // Firmware uses entPhysicalSoftwareRev first and entPhysicalFirmwareRev as
  9. // the fallback: both columns are standard but vendors populate different
  10. // ones. A missing column stays empty, the same null contract as #441/#1036.
  11. package snmp
  12. import (
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "github.com/gosnmp/gosnmp"
  17. )
  18. const (
  19. OidEntPhysicalTable = ".1.3.6.1.2.1.47.1.1.1.1"
  20. oidEntPhysicalClass = OidEntPhysicalTable + ".5"
  21. oidEntPhysicalHardwareRev = OidEntPhysicalTable + ".8"
  22. oidEntPhysicalFirmwareRev = OidEntPhysicalTable + ".9"
  23. oidEntPhysicalSoftwareRev = OidEntPhysicalTable + ".10"
  24. oidEntPhysicalSerialNum = OidEntPhysicalTable + ".11"
  25. oidEntPhysicalModelName = OidEntPhysicalTable + ".13"
  26. )
  27. const entPhysicalClassChassis = 3
  28. // EntityInfo is the identity the ENTITY-MIB publishes for the unit.
  29. type EntityInfo struct {
  30. Firmware string
  31. ModelShort string
  32. }
  33. type entityRow struct {
  34. index int
  35. class int
  36. firmwareRev string
  37. softwareRev string
  38. modelName string
  39. serialNum string
  40. }
  41. // PollEntityInfo walks entPhysicalTable once and returns the chassis row.
  42. // A device without the table yields an empty EntityInfo and no error, like
  43. // the other optional probes.
  44. func PollEntityInfo(s *gosnmp.GoSNMP) (EntityInfo, error) {
  45. pdus, err := walkSafe(s, OidEntPhysicalTable)
  46. if err != nil {
  47. return EntityInfo{}, nil
  48. }
  49. return ParseEntityTable(pdus), nil
  50. }
  51. // ParseEntityTable selects the chassis row and falls back to the first row
  52. // that publishes anything, in index order.
  53. func ParseEntityTable(pdus []gosnmp.SnmpPDU) EntityInfo {
  54. rows := map[int]*entityRow{}
  55. for _, v := range pdus {
  56. col, idx := entityColumn(v.Name)
  57. if col == "" || idx == 0 {
  58. continue
  59. }
  60. row := rows[idx]
  61. if row == nil {
  62. row = &entityRow{index: idx}
  63. rows[idx] = row
  64. }
  65. switch col {
  66. case oidEntPhysicalClass:
  67. row.class = int(uint64Val(v))
  68. case oidEntPhysicalFirmwareRev:
  69. row.firmwareRev = strings.TrimSpace(stringVal(v))
  70. case oidEntPhysicalSoftwareRev:
  71. row.softwareRev = strings.TrimSpace(stringVal(v))
  72. case oidEntPhysicalModelName:
  73. row.modelName = strings.TrimSpace(stringVal(v))
  74. case oidEntPhysicalSerialNum:
  75. row.serialNum = strings.TrimSpace(stringVal(v))
  76. }
  77. }
  78. if len(rows) == 0 {
  79. return EntityInfo{}
  80. }
  81. ordered := make([]*entityRow, 0, len(rows))
  82. for _, row := range rows {
  83. ordered = append(ordered, row)
  84. }
  85. sort.Slice(ordered, func(i, j int) bool { return ordered[i].index < ordered[j].index })
  86. chosen := ordered[0]
  87. for _, row := range ordered {
  88. if row.class == entPhysicalClassChassis && row.hasIdentity() {
  89. chosen = row
  90. break
  91. }
  92. if !chosen.hasIdentity() && row.hasIdentity() {
  93. chosen = row
  94. }
  95. }
  96. return EntityInfo{Firmware: chosen.firmware(), ModelShort: chosen.modelName}
  97. }
  98. func (r *entityRow) hasIdentity() bool {
  99. return r.firmware() != "" || r.modelName != "" || r.serialNum != ""
  100. }
  101. func (r *entityRow) firmware() string {
  102. if r.softwareRev != "" {
  103. return r.softwareRev
  104. }
  105. return r.firmwareRev
  106. }
  107. // entityColumn splits an entPhysicalTable OID into its column and row index.
  108. func entityColumn(name string) (string, int) {
  109. if !strings.HasPrefix(name, OidEntPhysicalTable+".") {
  110. return "", 0
  111. }
  112. rest := strings.TrimPrefix(name, OidEntPhysicalTable+".")
  113. col, idx, ok := strings.Cut(rest, ".")
  114. if !ok || strings.Contains(idx, ".") {
  115. return "", 0
  116. }
  117. n, err := strconv.Atoi(idx)
  118. if err != nil || n <= 0 {
  119. return "", 0
  120. }
  121. return OidEntPhysicalTable + "." + col, n
  122. }