Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Version 2.2.1. Support device tags to tell two UPSes apart. (#73)
Browse files Browse the repository at this point in the history
config:
      deviceTags:
        - "serverName:cusomterUps"
        - "someTag:someThing"
        <etc>
  • Loading branch information
MatthewHink authored Oct 12, 2021
1 parent 25ccd73 commit f0671c2
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

PLUGIN_NAME := snmp
PLUGIN_VERSION := 2.2.0
PLUGIN_VERSION := 2.2.1
IMAGE_NAME := vaporio/snmp-plugin
BIN_NAME := synse-snmp-plugin

Expand Down
14 changes: 11 additions & 3 deletions pkg/devices/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ func logDeviceProtos(t *testing.T, protos []*config.DeviceProto, header string)
for _, proto := range protos {
t.Logf(".. [proto: %s] Count device instances: %d", proto.Type, len(proto.Instances))
for _, instance := range proto.Instances {
t.Logf(" device: %v %v %v %v %v row:%v column:%v\n",
t.Logf(" device: %v %v %v %v %v row:%v column:%v, tags: %v\n",
instance.Data["table_name"],
proto.Type,
instance.Info,
instance.Data["oid"],
instance.Data["base_oid"],
instance.Data["row"],
instance.Data["column"],
proto.Tags,
)
}

Expand All @@ -77,7 +78,8 @@ func TestDevices(t *testing.T) { // nolint: gocyclo
"127.0.0.1", // Endpoint
1024, // Port
securityParameters,
"public", // Context name
"public", // Context name
[]string{"serverName:customerUps"}, // tags
)
assert.NoError(t, err)

Expand Down Expand Up @@ -163,7 +165,7 @@ func TestDevices(t *testing.T) { // nolint: gocyclo
t.Logf("device[%d]: %+v", i, devices[i])
}

// Read each device
// Read each device. Check device tags.
t.Logf("Reading each device.")
for i := 0; i < len(devices); i++ {
context, err := devices[i].Read() // Call Read through the device's function pointer.
Expand All @@ -175,6 +177,12 @@ func TestDevices(t *testing.T) { // nolint: gocyclo
for j := 0; j < len(readings); j++ {
t.Logf("Reading[%d][%d]: %T, %+v", i, j, readings[j], readings[j])
}

// Check device tags.
assert.Len(t, devices[i].Tags, 1)
assert.Equal(t, "default", devices[i].Tags[0].Namespace)
assert.Equal(t, "serverName", devices[i].Tags[0].Annotation)
assert.Equal(t, "customerUps", devices[i].Tags[0].Label)
}
t.Log("Finished reading each device.")
}
17 changes: 14 additions & 3 deletions pkg/snmp/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func NewSecurityParameters(
}

// DeviceConfig is a thin wrapper around the configuration for gosnmp using SNMP V3.
// Tags are included here to expose on a Synse scan.
type DeviceConfig struct {
Version string // SNMP protocol version. Currently only SNMP V3 is supported.
Endpoint string // Endpoint of the SNMP server to connect to.
Expand All @@ -80,6 +81,7 @@ type DeviceConfig struct {
Retries int // The number of retries on the connection.
SecurityParameters *SecurityParameters // SNMP V3 security parameters.
Port uint16 // UDP port to connect to.
Tags []string // List of synse device tags.
}

// checkForEmptyString checks for an empty string variable and fails with an
Expand All @@ -91,13 +93,14 @@ func checkForEmptyString(variable string, variableName string) (err error) {
return nil
}

// NewDeviceConfig creates an DeviceConfig.
// NewDeviceConfig creates a DeviceConfig.
func NewDeviceConfig(
version string,
endpoint string,
port uint16,
securityParameters *SecurityParameters,
contextName string) (*DeviceConfig, error) {
contextName string,
tags []string) (*DeviceConfig, error) {

// Check parameters.
versionUpper := strings.ToUpper(version)
Expand All @@ -123,6 +126,7 @@ func NewDeviceConfig(
ContextName: contextName,
Timeout: time.Duration(30) * time.Second,
Retries: 3,
Tags: tags,
}, nil
}

Expand Down Expand Up @@ -228,13 +232,19 @@ func GetDeviceConfig(instanceData map[string]interface{}) (*DeviceConfig, error)
return nil, err
}

tags, ok := instanceData["deviceTags"].([]string)
if !ok {
tags = []string{}
}

// Create the config.
return NewDeviceConfig(
version,
endpoint,
port,
securityParameters,
contextName)
contextName,
tags)
}

// ToMap serializes DeviceConfig to map[string]interface{}.
Expand All @@ -249,6 +259,7 @@ func (deviceConfig *DeviceConfig) ToMap() (m map[string]interface{}, err error)
m["endpoint"] = deviceConfig.Endpoint
m["port"] = deviceConfig.Port
m["contextName"] = deviceConfig.ContextName
m["deviceTags"] = deviceConfig.Tags

securityParameters := deviceConfig.SecurityParameters
m["userName"] = securityParameters.UserName
Expand Down
6 changes: 4 additions & 2 deletions pkg/snmp/core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestClient(t *testing.T) {
"127.0.0.1", // Endpoint
1024, // Port
securityParameters,
"public", // Context name
"public", // Context name
[]string{}, // tags (none)
)
assert.NoError(t, err)

Expand Down Expand Up @@ -283,7 +284,8 @@ func TestDeviceConfigSerialization(t *testing.T) {
"127.0.0.1", // Endpoint
1024, // Port
securityParameters,
"public", // Context name
"public", // Context name
[]string{}, // tags (none)
)
assert.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion pkg/snmp/core/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestTable(t *testing.T) {
"127.0.0.1", // Endpoint
1024, // Port
securityParameters,
"public", // Context name
"public", // Context name
[]string{}, // tags (none)
)
assert.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions pkg/snmp/mibs/ups_mib/ups_alarms_headers_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (enumerator UpsAlarmsHeadersTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
1 change: 1 addition & 0 deletions pkg/snmp/mibs/ups_mib/ups_alarms_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (enumerator UpsAlarmsTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
7 changes: 7 additions & 0 deletions pkg/snmp/mibs/ups_mib/ups_battery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

voltageProto := &config.DeviceProto{
Expand All @@ -94,6 +95,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

currentProto := &config.DeviceProto{
Expand All @@ -102,6 +104,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

temperatureProto := &config.DeviceProto{
Expand All @@ -110,6 +113,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

percentageProto := &config.DeviceProto{
Expand All @@ -118,6 +122,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

minutesProto := &config.DeviceProto{
Expand All @@ -126,6 +131,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

secondsProto := &config.DeviceProto{
Expand All @@ -134,6 +140,7 @@ func (enumerator UpsBatteryTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
13 changes: 8 additions & 5 deletions pkg/snmp/mibs/ups_mib/ups_bypass_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (enumerator UpsBypassTableDeviceEnumerator) DeviceEnumerator(
mib := table.Mib.(*UpsMib)
model := mib.UpsIdentityTable.UpsIdentity.Model

snmpDeviceConfigMap, err := table.SnmpServerBase.DeviceConfig.ToMap()
if err != nil {
return nil, err
}

// We will have "voltage", "current", and "power" device kinds.
// There is probably a better way of doing this, but this just gets things to
// where they need to be for now.
Expand All @@ -76,6 +81,7 @@ func (enumerator UpsBypassTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

currentProto := &config.DeviceProto{
Expand All @@ -84,6 +90,7 @@ func (enumerator UpsBypassTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

powerProto := &config.DeviceProto{
Expand All @@ -92,6 +99,7 @@ func (enumerator UpsBypassTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand All @@ -100,11 +108,6 @@ func (enumerator UpsBypassTableDeviceEnumerator) DeviceEnumerator(
powerProto,
}

snmpDeviceConfigMap, err := table.SnmpServerBase.DeviceConfig.ToMap()
if err != nil {
return nil, err
}

for i := 0; i < len(table.Rows); i++ {
// upsBypassVoltage ---------------------------------------------------
// deviceData gets shimmed into the DeviceConfig for each synse device.
Expand Down
1 change: 1 addition & 0 deletions pkg/snmp/mibs/ups_mib/ups_identity_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (enumerator UpsIdentityTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
5 changes: 5 additions & 0 deletions pkg/snmp/mibs/ups_mib/ups_input_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ func (enumerator UpsInputTableDeviceEnumerator) DeviceEnumerator(
// We will have "frequency", "voltage", "current", and "power" device kinds.
// There is probably a better way of doing this, but this just gets things to
// where they need to be for now.
// TODO: Shim in the tags here.
frequencyProto := &config.DeviceProto{
Type: "frequency",
Context: map[string]string{
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

voltageProto := &config.DeviceProto{
Expand All @@ -89,6 +91,7 @@ func (enumerator UpsInputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

currentProto := &config.DeviceProto{
Expand All @@ -97,6 +100,7 @@ func (enumerator UpsInputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

powerProto := &config.DeviceProto{
Expand All @@ -105,6 +109,7 @@ func (enumerator UpsInputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
3 changes: 2 additions & 1 deletion pkg/snmp/mibs/ups_mib/ups_mib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestUpsMib(t *testing.T) { // nolint: gocyclo
"127.0.0.1", // Endpoint
1024, // Port
securityParameters,
"public", // Context name
"public", // Context name
[]string{}, // tags (none)
)
assert.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions pkg/snmp/mibs/ups_mib/ups_output_headers_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (enumerator UpsOutputHeadersTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

frequencyProto := &config.DeviceProto{
Expand All @@ -87,6 +88,7 @@ func (enumerator UpsOutputHeadersTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down
5 changes: 5 additions & 0 deletions pkg/snmp/mibs/ups_mib/ups_output_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (enumerator UpsOutputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

voltageProto := &config.DeviceProto{
Expand All @@ -89,6 +90,7 @@ func (enumerator UpsOutputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

currentProto := &config.DeviceProto{
Expand All @@ -97,6 +99,7 @@ func (enumerator UpsOutputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

powerProto := &config.DeviceProto{
Expand All @@ -105,6 +108,7 @@ func (enumerator UpsOutputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

percentageProto := &config.DeviceProto{
Expand All @@ -113,6 +117,7 @@ func (enumerator UpsOutputTableDeviceEnumerator) DeviceEnumerator(
"model": model,
},
Instances: []*config.DeviceInstance{},
Tags: snmpDeviceConfigMap["deviceTags"].([]string),
}

devices = []*config.DeviceProto{
Expand Down

0 comments on commit f0671c2

Please sign in to comment.