Skip to content

Commit

Permalink
Fixes an interface type conversion bug (#24)
Browse files Browse the repository at this point in the history
* Fixes string conversion bug

This upstream commit broke interface type conversions:
gosnmp/gosnmp#264
The underlying value of the interface was changed from string to a byte
slice.

* Small changes to make the Go linter happy
  • Loading branch information
nkinkade authored Feb 22, 2022
1 parent f25b67c commit 3ce8960
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func mustGetIfaces(client snmp.Client, machine string) map[string]map[string]str
rtx.Must(err, "Failed to walk the ifAlias OID")

ifaces := map[string]map[string]string{
"machine": map[string]string{
"machine": {
"iface": "",
"ifAlias": "",
"ifDescr": "",
},
"uplink": map[string]string{
"uplink": {
"iface": "",
"ifAlias": "",
"ifDescr": "",
Expand All @@ -69,7 +69,7 @@ func mustGetIfaces(client snmp.Client, machine string) map[string]map[string]str
oidParts := strings.Split(pdu.Name, ".")
iface := oidParts[len(oidParts)-1]

val := strings.TrimSpace(pdu.Value.(string))
val := strings.TrimSpace(string(pdu.Value.([]byte)))
if val == machine {
ifDescrOid := createOID(ifDescrOidStub, iface)
oidMap, err := getOidsString(client, []string{ifDescrOid})
Expand Down Expand Up @@ -113,7 +113,7 @@ func getOidsString(client snmp.Client, oids []string) (map[string]string, error)
oidMap := make(map[string]string)
result, err := client.Get(oids)
for _, pdu := range result.Variables {
oidMap[pdu.Name] = pdu.Value.(string)
oidMap[pdu.Name] = string(pdu.Value.([]byte))
}
return oidMap, err
}
Expand All @@ -137,7 +137,7 @@ func getOidsInt64(client snmp.Client, oids []string) (map[string]uint64, error)
case uint64:
oidMap[pdu.Name] = value
default:
err = fmt.Errorf("Unknown type %T of SNMP type %v for OID %v", value, pdu.Type, pdu.Name)
err = fmt.Errorf("unknown type %T of SNMP type %v for OID %v", value, pdu.Type, pdu.Name)
return nil, err
}
}
Expand Down
28 changes: 14 additions & 14 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ var machine = "mlab2"

var c = config.Config{
Metrics: []config.Metric{
config.Metric{
{
Name: "ifHCInOctets",
Description: "Ingress octets.",
OidStub: ifHCInOctetsOidStub,
MlabUplinkName: "switch.octets.uplink.rx",
MlabMachineName: "switch.octets.local.rx",
},
config.Metric{
{
Name: "ifOutDiscards",
Description: "Egress discards.",
OidStub: ifOutDiscardsOidStub,
Expand All @@ -56,7 +56,7 @@ var snmpPacketMachine = gosnmp.SnmpPacket{
{
Name: ifDescrMachineOID,
Type: gosnmp.OctetString,
Value: "xe-0/0/12",
Value: []byte("xe-0/0/12"),
},
},
}
Expand All @@ -66,7 +66,7 @@ var snmpPacketUplink = gosnmp.SnmpPacket{
{
Name: ifDescrUplinkOID,
Type: gosnmp.OctetString,
Value: "xe-0/0/45",
Value: []byte("xe-0/0/45"),
},
},
}
Expand Down Expand Up @@ -141,12 +141,12 @@ func (m *mockSwitchClient) BulkWalkAll(rootOid string) (results []gosnmp.SnmpPDU
{
Name: ifDescrMachineOID,
Type: gosnmp.OctetString,
Value: "mlab2",
Value: []byte("mlab2"),
},
{
Name: ifDescrUplinkOID,
Type: gosnmp.OctetString,
Value: "uplink-10g",
Value: []byte("uplink-10g"),
},
}, nil
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func Test_New(t *testing.T) {
m := New(s, c, target, hostname)

var expectedMetricsOIDs = map[string]*oid{
ifOutDiscardsMachineOID: &oid{
ifOutDiscardsMachineOID: {
name: "ifOutDiscards",
previousValue: 0,
scope: "machine",
Expand All @@ -209,7 +209,7 @@ func Test_New(t *testing.T) {
Samples: []archive.Sample{},
},
},
ifOutDiscardsUplinkOID: &oid{
ifOutDiscardsUplinkOID: {
name: "ifOutDiscards",
previousValue: 0,
scope: "uplink",
Expand All @@ -222,7 +222,7 @@ func Test_New(t *testing.T) {
Samples: []archive.Sample{},
},
},
ifHCInOctetsMachineOID: &oid{
ifHCInOctetsMachineOID: {
name: "ifHCInOctets",
previousValue: 0,
scope: "machine",
Expand All @@ -235,7 +235,7 @@ func Test_New(t *testing.T) {
Samples: []archive.Sample{},
},
},
ifHCInOctetsUplinkOID: &oid{
ifHCInOctetsUplinkOID: {
name: "ifHCInOctets",
previousValue: 0,
scope: "uplink",
Expand Down Expand Up @@ -272,22 +272,22 @@ func Test_Collect(t *testing.T) {
prometheus.DefaultRegisterer = prometheus.NewRegistry()

var expectedValues = map[string]map[string]uint64{
ifOutDiscardsMachineOID: map[string]uint64{
ifOutDiscardsMachineOID: {
"run1Prev": 0,
"run2Prev": 0,
"run2Sample": 0,
},
ifOutDiscardsUplinkOID: map[string]uint64{
ifOutDiscardsUplinkOID: {
"run1Prev": 3,
"run2Prev": 8,
"run2Sample": 5,
},
ifHCInOctetsMachineOID: map[string]uint64{
ifHCInOctetsMachineOID: {
"run1Prev": 275,
"run2Prev": 511,
"run2Sample": 236,
},
ifHCInOctetsUplinkOID: map[string]uint64{
ifHCInOctetsUplinkOID: {
"run1Prev": 437,
"run2Prev": 624,
"run2Sample": 187,
Expand Down

0 comments on commit 3ce8960

Please sign in to comment.