Skip to content
Open
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
15 changes: 9 additions & 6 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ func pduValueAsString(pdu *gosnmp.SnmpPDU, typ, displayHint string, metrics Metr
// Prepend the length, as it is explicit in an index.
parts = append([]int{len(v)}, parts...)
}
str, _, _ := indexOidsAsString(parts, typ, 0, false, nil)
str, _, _ := indexOidsAsString(parts, typ, "", 0, false, nil)
return strings.ToValidUTF8(str, "�")
case nil:
return ""
Expand All @@ -858,7 +858,7 @@ func pduValueAsString(pdu *gosnmp.SnmpPDU, typ, displayHint string, metrics Metr
// Convert oids to a string index value.
//
// Returns the string, the oids that were used and the oids left over.
func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool, enumValues map[int]string) (string, []int, []int) {
func indexOidsAsString(indexOids []int, typ, displayHint string, fixedSize int, implied bool, enumValues map[int]string) (string, []int, []int) {
if typeMapping, ok := combinedTypeMapping[typ]; ok {
subOid, valueOids := splitOid(indexOids, 2)
if typ == "InetAddressMissingSize" {
Expand All @@ -868,15 +868,15 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
var str string
var used, remaining []int
if t, ok := typeMapping[subOid[0]]; ok {
str, used, remaining = indexOidsAsString(valueOids, t, 0, false, enumValues)
str, used, remaining = indexOidsAsString(valueOids, t, displayHint, 0, false, enumValues)
return str, append(subOid, used...), remaining
}
if typ == "InetAddressMissingSize" {
// We don't know the size, so pass everything remaining.
return indexOidsAsString(indexOids, "OctetString", 0, true, enumValues)
return indexOidsAsString(indexOids, "OctetString", displayHint, 0, true, enumValues)
}
// The 2nd oid is the length.
return indexOidsAsString(indexOids, "OctetString", subOid[1]+2, false, enumValues)
return indexOidsAsString(indexOids, "OctetString", displayHint, subOid[1]+2, false, enumValues)
}

switch typ {
Expand Down Expand Up @@ -912,6 +912,9 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
if len(parts) == 0 {
return "", subOid, indexOids
}
if v, ok := applyDisplayHint(displayHint, parts); ok {
return v, subOid, indexOids
}
return fmt.Sprintf("0x%X", string(parts)), subOid, indexOids
case "DisplayString":
var subOid []int
Expand Down Expand Up @@ -970,7 +973,7 @@ func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string

// Covert indexes to useful strings.
for _, index := range metric.Indexes {
str, subOid, remainingOids := indexOidsAsString(indexOids, index.Type, index.FixedSize, index.Implied, index.EnumValues)
str, subOid, remainingOids := indexOidsAsString(indexOids, index.Type, index.DisplayHint, index.FixedSize, index.Implied, index.EnumValues)
// The labelvalue is the text form of the index oids.
labels[index.Labelname] = str
// Save its oid in case we need it for lookups.
Expand Down
6 changes: 6 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,12 @@ func TestIndexesToLabels(t *testing.T) {
oidToPdu: map[string]gosnmp.SnmpPDU{},
result: map[string]string{"l": "0x4120FF"},
},
{
oid: []int{4, 127, 0, 0, 1},
metric: config.Metric{Indexes: []*config.Index{{Labelname: "l", Type: "OctetString", DisplayHint: "1d."}}},
oidToPdu: map[string]gosnmp.SnmpPDU{},
result: map[string]string{"l": "127.0.0.1"},
},
{
oid: []int{65, 32, 255},
metric: config.Metric{Indexes: []*config.Index{{Labelname: "l", Type: "OctetString", FixedSize: 3}}},
Expand Down
11 changes: 6 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,12 @@ type Metric struct {
}

type Index struct {
Labelname string `yaml:"labelname"`
Type string `yaml:"type"`
FixedSize int `yaml:"fixed_size,omitempty"`
Implied bool `yaml:"implied,omitempty"`
EnumValues map[int]string `yaml:"enum_values,omitempty"`
Labelname string `yaml:"labelname"`
Type string `yaml:"type"`
FixedSize int `yaml:"fixed_size,omitempty"`
Implied bool `yaml:"implied,omitempty"`
EnumValues map[int]string `yaml:"enum_values,omitempty"`
DisplayHint string `yaml:"display_hint,omitempty"`
}

type Lookup struct {
Expand Down
3 changes: 3 additions & 0 deletions generator/FORMAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ modules:
fixed_size: 8 # Only possible for OctetString/DisplayString types.
# If only one length is possible this is it. Otherwise
# this will be 0 or missing.
display_hint: 1d. # Only possible for OctetString types.
# RFC 2579 DISPLAY-HINT to format OctetString values as readable strings.
# Useful when vendor-specific types display as hex instead of text.
- labelname: someOtherString
type: OctetString
implied: true # Only possible for OctetString/DisplayString types.
Expand Down