From 1c5f18e04fb5e4a1f94671385d6f8735ef73384c Mon Sep 17 00:00:00 2001 From: Chris Grindstaff Date: Wed, 24 Jul 2024 03:40:06 -0400 Subject: [PATCH] fix: Harvest should collect power metrics from a1000 and a900 clusters (#3063) Those platforms have sensors with slightly different names that Harvest needs to accommodate. Fixes: #3062 --- cmd/collectors/power.go | 8 +- cmd/collectors/sensor_test.go | 50 +++ .../testdata/rest-sensors-a1000.json | 296 ++++++++++++++++++ .../testdata/rest-sensors-a900.json | 200 ++++++++++++ 4 files changed, 550 insertions(+), 4 deletions(-) create mode 100644 cmd/collectors/testdata/rest-sensors-a1000.json create mode 100644 cmd/collectors/testdata/rest-sensors-a900.json diff --git a/cmd/collectors/power.go b/cmd/collectors/power.go index c2ffd61f1..aafe2e7c9 100644 --- a/cmd/collectors/power.go +++ b/cmd/collectors/power.go @@ -79,11 +79,11 @@ type environmentMetric struct { var ambientRegex = regexp.MustCompile(`^(Ambient Temp|Ambient Temp \d|PSU\d AmbTemp|PSU\d Inlet|PSU\d Inlet Temp|In Flow Temp|Front Temp|Bat_Ambient \d|Riser Inlet Temp)$`) -var powerInRegex = regexp.MustCompile(`^PSU\d (InPwr Monitor|InPower|PIN|Power In)$`) +var powerInRegex = regexp.MustCompile(`^PSU\d (InPwr Monitor|InPower|PIN|Power In|In Pwr)$`) -var voltageRegex = regexp.MustCompile(`^PSU\d (\d+V|InVoltage|VIN|AC In Volt)$`) +var voltageRegex = regexp.MustCompile(`^PSU\d (\d+V|InVoltage|VIN|AC In Volt|In Volt)$`) -var CurrentRegex = regexp.MustCompile(`^PSU\d (\d+V Curr|Curr|InCurrent|Curr IIN|AC In Curr)$`) +var currentRegex = regexp.MustCompile(`^PSU\d (\d+V Curr|Curr|InCurrent|Curr IIN|AC In Curr|In Curr)$`) var eMetrics = []string{ "average_ambient_temperature", @@ -128,7 +128,7 @@ func calculateEnvironmentMetrics(data *matrix.Matrix, logger *logging.Logger, va isAmbientMatch := ambientRegex.MatchString(sensorName) isPowerMatch := powerInRegex.MatchString(sensorName) isVoltageMatch := voltageRegex.MatchString(sensorName) - isCurrentMatch := CurrentRegex.MatchString(sensorName) + isCurrentMatch := currentRegex.MatchString(sensorName) if sensorType == "thermal" && isAmbientMatch { if value, ok := metric.GetValueFloat64(instance); ok { diff --git a/cmd/collectors/sensor_test.go b/cmd/collectors/sensor_test.go index 56c1cef5f..5e1d2578d 100644 --- a/cmd/collectors/sensor_test.go +++ b/cmd/collectors/sensor_test.go @@ -7,8 +7,10 @@ import ( "github.com/netapp/harvest/v2/pkg/matrix" "github.com/netapp/harvest/v2/pkg/tree" "github.com/netapp/harvest/v2/pkg/tree/node" + "github.com/tidwall/gjson" "os" "path/filepath" + "regexp" "strings" "testing" ) @@ -162,3 +164,51 @@ func TestSensor_Run(t *testing.T) { } } } + +func TestPowerRegex(t *testing.T) { + tests := []struct { + name string + path string + wantPower int + wantCurrent int + wantVoltage int + }{ + {name: "a1000", path: "testdata/rest-sensors-a1000.json", wantPower: 4, wantCurrent: 4, wantVoltage: 4}, + {name: "a900", path: "testdata/rest-sensors-a900.json", wantPower: 0, wantCurrent: 4, wantVoltage: 4}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + data, err := os.ReadFile(tt.path) + if err != nil { + panic(err) + } + powerMatches := countMatches(powerInRegex, data) + if powerMatches != tt.wantPower { + t.Errorf("power regex\ngot=%v\nwant=%v", powerMatches, tt.wantPower) + } + + currentMatches := countMatches(currentRegex, data) + if currentMatches != tt.wantCurrent { + t.Errorf("current regex\ngot=%v\nwant=%v", currentMatches, tt.wantCurrent) + } + + voltageMatches := countMatches(voltageRegex, data) + if voltageMatches != tt.wantVoltage { + t.Errorf("voltage regex\ngot=%v\nwant=%v", voltageMatches, tt.wantVoltage) + } + }) + } +} + +func countMatches(sensorRegex *regexp.Regexp, data []byte) int { + names := gjson.GetBytes(data, "records.#.name").Array() + matches := 0 + for _, name := range names { + if sensorRegex.MatchString(name.String()) { + matches++ + } + } + + return matches +} diff --git a/cmd/collectors/testdata/rest-sensors-a1000.json b/cmd/collectors/testdata/rest-sensors-a1000.json new file mode 100644 index 000000000..a60b51983 --- /dev/null +++ b/cmd/collectors/testdata/rest-sensors-a1000.json @@ -0,0 +1,296 @@ +{ + "records": [ + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 81, + "name": "PSU1 In Volt", + "type": "voltage", + "value": 210000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 84000, + "warning_low_threshold": 90000, + "warning_high_threshold": 264000, + "critical_high_threshold": 276000, + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/81" + } + } + } + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 82, + "name": "PSU1 In Curr", + "type": "current", + "value": 2100, + "value_units": "mA", + "threshold_state": "normal", + "critical_low_threshold": 0, + "warning_high_threshold": 14100, + "critical_high_threshold": 15000, + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/82" + } + } + } + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 85, + "name": "PSU1 In Pwr", + "type": "unknown", + "value": 448, + "value_units": "W", + "threshold_state": "normal", + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/85" + } + } + } + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 91, + "name": "PSU2 In Volt", + "type": "voltage", + "value": 210000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 84000, + "warning_low_threshold": 90000, + "warning_high_threshold": 264000, + "critical_high_threshold": 276000, + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/91" + } + } + } + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 92, + "name": "PSU2 In Curr", + "type": "current", + "value": 2100, + "value_units": "mA", + "threshold_state": "normal", + "critical_low_threshold": 0, + "warning_high_threshold": 14100, + "critical_high_threshold": 15000, + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/92" + } + } + } + { + "node": { + "uuid": "52ad059c-349b-11ef-b340-d039eaa9ee10", + "name": "a1k-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/52ad059c-349b-11ef-b340-d039eaa9ee10" + } + } + }, + "index": 95, + "name": "PSU2 In Pwr", + "type": "unknown", + "value": 456, + "value_units": "W", + "threshold_state": "normal", + "_links": { + "self": { + "href": "/api/cluster/sensors/52ad059c-349b-11ef-b340-d039eaa9ee10/95" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 81, + "name": "PSU1 In Volt", + "type": "voltage", + "value": 212000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 84000, + "warning_low_threshold": 90000, + "warning_high_threshold": 264000, + "critical_high_threshold": 276000, + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/81" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 82, + "name": "PSU1 In Curr", + "type": "current", + "value": 2100, + "value_units": "mA", + "threshold_state": "normal", + "critical_low_threshold": 0, + "warning_high_threshold": 14100, + "critical_high_threshold": 15000, + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/82" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 85, + "name": "PSU1 In Pwr", + "type": "unknown", + "value": 448, + "value_units": "W", + "threshold_state": "normal", + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/85" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 91, + "name": "PSU2 In Volt", + "type": "voltage", + "value": 210000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 84000, + "warning_low_threshold": 90000, + "warning_high_threshold": 264000, + "critical_high_threshold": 276000, + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/91" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 92, + "name": "PSU2 In Curr", + "type": "current", + "value": 2100, + "value_units": "mA", + "threshold_state": "normal", + "critical_low_threshold": 0, + "warning_high_threshold": 14100, + "critical_high_threshold": 15000, + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/92" + } + } + } + { + "node": { + "uuid": "800c0fcb-349b-11ef-8a0d-d039eaa9edb6", + "name": "a1k-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/800c0fcb-349b-11ef-8a0d-d039eaa9edb6" + } + } + }, + "index": 95, + "name": "PSU2 In Pwr", + "type": "unknown", + "value": 464, + "value_units": "W", + "threshold_state": "normal", + "_links": { + "self": { + "href": "/api/cluster/sensors/800c0fcb-349b-11ef-8a0d-d039eaa9edb6/95" + } + } + } + ] +} \ No newline at end of file diff --git a/cmd/collectors/testdata/rest-sensors-a900.json b/cmd/collectors/testdata/rest-sensors-a900.json new file mode 100644 index 000000000..7c4f93bce --- /dev/null +++ b/cmd/collectors/testdata/rest-sensors-a900.json @@ -0,0 +1,200 @@ +{ + "records": [ + { + "node": { + "uuid": "2a094823-3352-11ef-bdec-d039ea48b049", + "name": "a900-1-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/2a094823-3352-11ef-bdec-d039ea48b049" + } + } + }, + "index": 73, + "name": "PSU2 AC In Volt", + "type": "voltage", + "value": 213000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 88000, + "warning_low_threshold": 90000, + "critical_high_threshold": 255000, + "_links": { + "self": { + "href": "/api/cluster/sensors/2a094823-3352-11ef-bdec-d039ea48b049/73" + } + } + } + { + "node": { + "uuid": "2a094823-3352-11ef-bdec-d039ea48b049", + "name": "a900-1-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/2a094823-3352-11ef-bdec-d039ea48b049" + } + } + }, + "index": 74, + "name": "PSU2 AC In Curr", + "type": "current", + "value": 2450, + "value_units": "mA", + "threshold_state": "normal", + "warning_high_threshold": 14000, + "critical_high_threshold": 14980, + "_links": { + "self": { + "href": "/api/cluster/sensors/2a094823-3352-11ef-bdec-d039ea48b049/74" + } + } + } + { + "node": { + "uuid": "2a094823-3352-11ef-bdec-d039ea48b049", + "name": "a900-1-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/2a094823-3352-11ef-bdec-d039ea48b049" + } + } + }, + "index": 78, + "name": "PSU4 AC In Volt", + "type": "voltage", + "value": 213000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 88000, + "warning_low_threshold": 90000, + "critical_high_threshold": 255000, + "_links": { + "self": { + "href": "/api/cluster/sensors/2a094823-3352-11ef-bdec-d039ea48b049/78" + } + } + } + { + "node": { + "uuid": "2a094823-3352-11ef-bdec-d039ea48b049", + "name": "a900-1-01", + "_links": { + "self": { + "href": "/api/cluster/nodes/2a094823-3352-11ef-bdec-d039ea48b049" + } + } + }, + "index": 79, + "name": "PSU4 AC In Curr", + "type": "current", + "value": 2240, + "value_units": "mA", + "threshold_state": "normal", + "warning_high_threshold": 14000, + "critical_high_threshold": 14980, + "_links": { + "self": { + "href": "/api/cluster/sensors/2a094823-3352-11ef-bdec-d039ea48b049/79" + } + } + } + { + "node": { + "uuid": "853459cb-3352-11ef-bd03-d039ea482ffa", + "name": "a900-1-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/853459cb-3352-11ef-bd03-d039ea482ffa" + } + } + }, + "index": 73, + "name": "PSU1 AC In Volt", + "type": "voltage", + "value": 213000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 88000, + "warning_low_threshold": 90000, + "critical_high_threshold": 255000, + "_links": { + "self": { + "href": "/api/cluster/sensors/853459cb-3352-11ef-bd03-d039ea482ffa/73" + } + } + } + { + "node": { + "uuid": "853459cb-3352-11ef-bd03-d039ea482ffa", + "name": "a900-1-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/853459cb-3352-11ef-bd03-d039ea482ffa" + } + } + }, + "index": 74, + "name": "PSU1 AC In Curr", + "type": "current", + "value": 2450, + "value_units": "mA", + "threshold_state": "normal", + "warning_high_threshold": 14000, + "critical_high_threshold": 14980, + "_links": { + "self": { + "href": "/api/cluster/sensors/853459cb-3352-11ef-bd03-d039ea482ffa/74" + } + } + } + { + "node": { + "uuid": "853459cb-3352-11ef-bd03-d039ea482ffa", + "name": "a900-1-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/853459cb-3352-11ef-bd03-d039ea482ffa" + } + } + }, + "index": 78, + "name": "PSU3 AC In Volt", + "type": "voltage", + "value": 213000, + "value_units": "mV", + "threshold_state": "normal", + "critical_low_threshold": 88000, + "warning_low_threshold": 90000, + "critical_high_threshold": 255000, + "_links": { + "self": { + "href": "/api/cluster/sensors/853459cb-3352-11ef-bd03-d039ea482ffa/78" + } + } + } + { + "node": { + "uuid": "853459cb-3352-11ef-bd03-d039ea482ffa", + "name": "a900-1-02", + "_links": { + "self": { + "href": "/api/cluster/nodes/853459cb-3352-11ef-bd03-d039ea482ffa" + } + } + }, + "index": 79, + "name": "PSU3 AC In Curr", + "type": "current", + "value": 2240, + "value_units": "mA", + "threshold_state": "normal", + "warning_high_threshold": 14000, + "critical_high_threshold": 14980, + "_links": { + "self": { + "href": "/api/cluster/sensors/853459cb-3352-11ef-bd03-d039ea482ffa/79" + } + } + } + ] +} \ No newline at end of file