Skip to content

Commit

Permalink
refactor value package
Browse files Browse the repository at this point in the history
  • Loading branch information
babos77 committed Mar 2, 2021
1 parent 67a9d1a commit 841c42c
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 138 deletions.
33 changes: 33 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,36 @@ func getCheckRequest() request.CheckRequest {
PrintPerformanceData: true,
}
}

func generateCheckThresholds(cmd *cobra.Command, warningMin, warningMax, criticalMin, criticalMax string) request.CheckThresholds {
var thresholds request.CheckThresholds
if flagName := warningMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMin = &v
}
if flagName := warningMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMax = &v
}
if flagName := criticalMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMin = &v
}
if flagName := criticalMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMax = &v
}
return thresholds
}
10 changes: 3 additions & 7 deletions cmd/check_cpu_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkCpuLoad)
checkCMD.AddCommand(checkCpuLoad)

checkCpuLoad.Flags().String("warning", "", "warning threshold for cpu load")
checkCpuLoad.Flags().String("critical", "", "critical threshold for cpu load")
checkCpuLoad.Flags().Float64("warning", 0, "warning threshold for cpu load")
checkCpuLoad.Flags().Float64("critical", 0, "critical threshold for cpu load")
}

var checkCpuLoad = &cobra.Command{
Expand All @@ -22,10 +21,7 @@ var checkCpuLoad = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckCPULoadRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
CPULoadThresholds: request.CheckThresholds{
WarningMax: value.New(cmd.Flags().Lookup("warning").Value),
CriticalMax: value.New(cmd.Flags().Lookup("critical").Value),
},
CPULoadThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
}
handleRequest(&r)
},
Expand Down
12 changes: 4 additions & 8 deletions cmd/check_memory_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkMemoryUsage)
checkCMD.AddCommand(checkMemoryUsage)

checkMemoryUsage.Flags().String("warning", "", "warning threshold for memory usage")
checkMemoryUsage.Flags().String("critical", "", "critical threshold for system voltage")
checkMemoryUsage.Flags().Float64("warning", 0, "warning threshold for memory usage")
checkMemoryUsage.Flags().Float64("critical", 0, "critical threshold for system voltage")
}

var checkMemoryUsage = &cobra.Command{
Expand All @@ -21,11 +20,8 @@ var checkMemoryUsage = &cobra.Command{
"The usage will be printed as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckMemoryUsageRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
MemoryUsageThresholds: request.CheckThresholds{
WarningMax: value.New(cmd.Flags().Lookup("warning").Value),
CriticalMax: value.New(cmd.Flags().Lookup("critical").Value),
},
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
MemoryUsageThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
}
handleRequest(&r)
},
Expand Down
12 changes: 4 additions & 8 deletions cmd/check_sbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkSBCCMD)
checkCMD.AddCommand(checkSBCCMD)

checkSBCCMD.Flags().String("system-health-score-warning", "", "warning threshold for system health score")
checkSBCCMD.Flags().String("system-health-score-critical", "", "critical threshold for system health score")
checkSBCCMD.Flags().Float64("system-health-score-warning", 0, "warning threshold for system health score")
checkSBCCMD.Flags().Float64("system-health-score-critical", 0, "critical threshold for system health score")
}

var checkSBCCMD = &cobra.Command{
Expand All @@ -20,11 +19,8 @@ var checkSBCCMD = &cobra.Command{
Long: "Read out sbc specific metrics as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckSBCRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
SystemHealthScoreThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("system-health-score-warning").Value),
CriticalMin: value.New(cmd.Flags().Lookup("system-health-score-critical").Value),
},
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
SystemHealthScoreThresholds: generateCheckThresholds(cmd, "system-health-score-warning", "", "system-health-score-critical", ""),
}
handleRequest(&r)
},
Expand Down
86 changes: 30 additions & 56 deletions cmd/check_ups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkUPSCMD)
checkCMD.AddCommand(checkUPSCMD)

checkUPSCMD.Flags().String("batt-current-warning-min", "", "Warning min threshold for battery current")
checkUPSCMD.Flags().String("batt-current-warning-max", "", "Warning max threshold for battery current")
checkUPSCMD.Flags().String("batt-current-critical-min", "", "Critical min threshold for battery current")
checkUPSCMD.Flags().String("batt-current-critical-max", "", "Critical max threshold for battery current")

checkUPSCMD.Flags().String("batt-temperature-warning-min", "", "Warning min threshold for battery temperature")
checkUPSCMD.Flags().String("batt-temperature-warning-max", "", "Warning max threshold for battery temperature")
checkUPSCMD.Flags().String("batt-temperature-critical-min", "", "Critical min threshold for battery temperature")
checkUPSCMD.Flags().String("batt-temperature-critical-max", "", "Critical max threshold for battery temperature")

checkUPSCMD.Flags().String("current-load-warning-min", "", "Warning min threshold for current load")
checkUPSCMD.Flags().String("current-load-warning-max", "", "Warning max threshold for current load")
checkUPSCMD.Flags().String("current-load-critical-min", "", "Critical min threshold for current load")
checkUPSCMD.Flags().String("current-load-critical-max", "", "Critical max threshold for current load")

checkUPSCMD.Flags().String("rectifier-current-warning-min", "", "Warning min threshold for rectifier current")
checkUPSCMD.Flags().String("rectifier-current-warning-max", "", "Warning max threshold for rectifier current")
checkUPSCMD.Flags().String("rectifier-current-critical-min", "", "Critical min threshold for rectifier current")
checkUPSCMD.Flags().String("rectifier-current-critical-max", "", "Critical max threshold for rectifier current")

checkUPSCMD.Flags().String("system-voltage-warning-min", "", "Warning min threshold for system voltage")
checkUPSCMD.Flags().String("system-voltage-warning-max", "", "Warning max threshold for system voltage")
checkUPSCMD.Flags().String("system-voltage-critical-min", "", "Critical min threshold for system voltage")
checkUPSCMD.Flags().String("system-voltage-critical-max", "", "Critical max threshold for system voltage")
checkUPSCMD.Flags().Float64("batt-current-warning-min", 0, "Warning min threshold for battery current")
checkUPSCMD.Flags().Float64("batt-current-warning-max", 0, "Warning max threshold for battery current")
checkUPSCMD.Flags().Float64("batt-current-critical-min", 0, "Critical min threshold for battery current")
checkUPSCMD.Flags().Float64("batt-current-critical-max", 0, "Critical max threshold for battery current")

checkUPSCMD.Flags().Float64("batt-temperature-warning-min", 0, "Warning min threshold for battery temperature")
checkUPSCMD.Flags().Float64("batt-temperature-warning-max", 0, "Warning max threshold for battery temperature")
checkUPSCMD.Flags().Float64("batt-temperature-critical-min", 0, "Critical min threshold for battery temperature")
checkUPSCMD.Flags().Float64("batt-temperature-critical-max", 0, "Critical max threshold for battery temperature")

checkUPSCMD.Flags().Float64("current-load-warning-min", 0, "Warning min threshold for current load")
checkUPSCMD.Flags().Float64("current-load-warning-max", 0, "Warning max threshold for current load")
checkUPSCMD.Flags().Float64("current-load-critical-min", 0, "Critical min threshold for current load")
checkUPSCMD.Flags().Float64("current-load-critical-max", 0, "Critical max threshold for current load")

checkUPSCMD.Flags().Float64("rectifier-current-warning-min", 0, "Warning min threshold for rectifier current")
checkUPSCMD.Flags().Float64("rectifier-current-warning-max", 0, "Warning max threshold for rectifier current")
checkUPSCMD.Flags().Float64("rectifier-current-critical-min", 0, "Critical min threshold for rectifier current")
checkUPSCMD.Flags().Float64("rectifier-current-critical-max", 0, "Critical max threshold for rectifier current")

checkUPSCMD.Flags().Float64("system-voltage-warning-min", 0, "Warning min threshold for system voltage")
checkUPSCMD.Flags().Float64("system-voltage-warning-max", 0, "Warning max threshold for system voltage")
checkUPSCMD.Flags().Float64("system-voltage-critical-min", 0, "Critical min threshold for system voltage")
checkUPSCMD.Flags().Float64("system-voltage-critical-max", 0, "Critical max threshold for system voltage")
}

var checkUPSCMD = &cobra.Command{
Expand All @@ -43,37 +42,12 @@ var checkUPSCMD = &cobra.Command{
"All UPS statistics will be printed as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckUPSRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
BatteryCurrentThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("batt-current-warning-min").Value),
WarningMax: value.New(cmd.Flags().Lookup("batt-current-warning-max").Value),
CriticalMin: value.New(cmd.Flags().Lookup("batt-current-critical-min").Value),
CriticalMax: value.New(cmd.Flags().Lookup("batt-current-critical-max").Value),
},
BatteryTemperatureThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("batt-temperature-warning-min").Value),
WarningMax: value.New(cmd.Flags().Lookup("batt-temperature-warning-max").Value),
CriticalMin: value.New(cmd.Flags().Lookup("batt-temperature-critical-min").Value),
CriticalMax: value.New(cmd.Flags().Lookup("batt-temperature-critical-max").Value),
},
CurrentLoadThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("current-load-warning-min").Value),
WarningMax: value.New(cmd.Flags().Lookup("current-load-warning-max").Value),
CriticalMin: value.New(cmd.Flags().Lookup("current-load-critical-min").Value),
CriticalMax: value.New(cmd.Flags().Lookup("current-load-critical-max").Value),
},
RectifierCurrentThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("rectifier-current-warning-min").Value),
WarningMax: value.New(cmd.Flags().Lookup("rectifier-current-warning-max").Value),
CriticalMin: value.New(cmd.Flags().Lookup("rectifier-current-critical-min").Value),
CriticalMax: value.New(cmd.Flags().Lookup("rectifier-current-critical-max").Value),
},
SystemVoltageThresholds: request.CheckThresholds{
WarningMin: value.New(cmd.Flags().Lookup("system-voltage-warning-min").Value),
WarningMax: value.New(cmd.Flags().Lookup("system-voltage-warning-max").Value),
CriticalMin: value.New(cmd.Flags().Lookup("system-voltage-critical-min").Value),
CriticalMax: value.New(cmd.Flags().Lookup("system-voltage-critical-max").Value),
},
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
BatteryCurrentThresholds: generateCheckThresholds(cmd, "batt-current-warning-min", "batt-current-warning-max", "batt-current-critical-min", "batt-current-critical-max"),
BatteryTemperatureThresholds: generateCheckThresholds(cmd, "batt-temperature-warning-min", "batt-temperature-warning-max", "batt-temperature-critical-min", "batt-temperature-critical-max"),
CurrentLoadThresholds: generateCheckThresholds(cmd, "current-load-warning-min", "current-load-warning-max", "current-load-warning-max", "current-load-warning-max"),
RectifierCurrentThresholds: generateCheckThresholds(cmd, "rectifier-current-warning-min", "rectifier-current-warning-max", "rectifier-current-critical-min", "rectifier-current-critical-max"),
SystemVoltageThresholds: generateCheckThresholds(cmd, "system-voltage-warning-min", "system-voltage-warning-max", "system-voltage-critical-min", "system-voltage-critical-max"),
}
handleRequest(&r)
},
Expand Down
50 changes: 21 additions & 29 deletions core/request/check_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,62 +49,54 @@ func (c *CheckResponse) GetExitCode() int {
}

type CheckThresholds struct {
WarningMin value.Value `json:"warningMin" xml:"warningMin"`
WarningMax value.Value `json:"warningMax" xml:"warningMax"`
CriticalMin value.Value `json:"criticalMin" xml:"criticalMin"`
CriticalMax value.Value `json:"criticalMax" xml:"criticalMax"`
WarningMin *float64 `json:"warningMin" xml:"warningMin"`
WarningMax *float64 `json:"warningMax" xml:"warningMax"`
CriticalMin *float64 `json:"criticalMin" xml:"criticalMin"`
CriticalMax *float64 `json:"criticalMax" xml:"criticalMax"`
}

func (c *CheckThresholds) validate() error {
if !c.WarningMin.IsEmpty() && !c.WarningMax.IsEmpty() {
if cmp, err := c.WarningMin.Cmp(c.WarningMax); err != nil || cmp != -1 {
return errors.New("warning min and max are invalid")
}
if c.WarningMin != nil && c.WarningMax != nil && *c.WarningMin >= *c.WarningMax {
return errors.New("warning min and max are invalid")
}

if !c.CriticalMin.IsEmpty() && !c.CriticalMax.IsEmpty() {
if cmp, err := c.CriticalMin.Cmp(c.CriticalMax); err != nil || cmp != -1 {
return errors.New("critical min and max are invalid")
}
if c.CriticalMin != nil && c.CriticalMax != nil && *c.CriticalMin >= *c.CriticalMax {
return errors.New("critical min and max are invalid")
}

if !c.CriticalMin.IsEmpty() && !c.WarningMin.IsEmpty() {
if cmp, err := c.CriticalMin.Cmp(c.WarningMin); err != nil || cmp != -1 {
return errors.New("critical and warning min are invalid")
}
if c.CriticalMin != nil && c.WarningMin != nil && *c.CriticalMin >= *c.WarningMin {
return errors.New("critical and warning min are invalid")
}

if !c.WarningMax.IsEmpty() && !c.CriticalMax.IsEmpty() {
if cmp, err := c.WarningMax.Cmp(c.CriticalMax); err != nil || cmp != -1 {
return errors.New("warning and critical max are invalid")
}
if c.WarningMax != nil && c.CriticalMax != nil && *c.WarningMax >= *c.CriticalMax {
return errors.New("critical and warning max are invalid")
}

return nil
}

func (c *CheckThresholds) isEmpty() bool {
return c.WarningMin.IsEmpty() && c.WarningMax.IsEmpty() && c.CriticalMin.IsEmpty() && c.CriticalMax.IsEmpty()
return c.WarningMin == nil && c.WarningMax == nil && c.CriticalMin == nil && c.CriticalMax == nil
}

func (c *CheckThresholds) checkValue(v value.Value) int {
if !c.CriticalMin.IsEmpty() {
if res, err := c.CriticalMin.Cmp(v); err != nil || res != -1 {
if c.CriticalMin != nil {
if res, err := value.New(*c.CriticalMin).Cmp(v); err != nil || res != -1 {
return monitoringplugin.CRITICAL
}
}
if !c.CriticalMax.IsEmpty() {
if res, err := c.CriticalMax.Cmp(v); err != nil || res != 1 {
if c.CriticalMax != nil {
if res, err := value.New(*c.CriticalMax).Cmp(v); err != nil || res != 1 {
return monitoringplugin.CRITICAL
}
}
if !c.WarningMin.IsEmpty() {
if res, err := c.WarningMin.Cmp(v); err != nil || res != -1 {
if c.WarningMin != nil {
if res, err := value.New(*c.WarningMin).Cmp(v); err != nil || res != -1 {
return monitoringplugin.WARNING
}
}
if !c.WarningMax.IsEmpty() {
if res, err := c.WarningMax.Cmp(v); err != nil || res != 1 {
if c.WarningMax != nil {
if res, err := value.New(*c.WarningMax).Cmp(v); err != nil || res != 1 {
return monitoringplugin.WARNING
}
}
Expand Down
31 changes: 17 additions & 14 deletions core/request/request_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
package request

import (
"github.com/inexio/thola/core/value"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCheckThresholds(t *testing.T) {
th1 := CheckThresholds{
WarningMin: value.New(5),
WarningMax: value.New(10),
CriticalMin: value.New(3),
CriticalMax: value.New(12),
WarningMin: getPointer(5),
WarningMax: getPointer(10),
CriticalMin: getPointer(3),
CriticalMax: getPointer(12),
}
assert.NoError(t, th1.validate())

th2 := CheckThresholds{}
assert.NoError(t, th2.validate())

th3 := CheckThresholds{
WarningMax: value.New(3),
WarningMax: getPointer(3),
}
assert.NoError(t, th3.validate())

th4 := CheckThresholds{
WarningMin: value.New(2),
WarningMax: value.New(1),
WarningMin: getPointer(2),
WarningMax: getPointer(1),
}
assert.Error(t, th4.validate())

th5 := CheckThresholds{
CriticalMin: value.New(2),
CriticalMax: value.New(1),
CriticalMin: getPointer(2),
CriticalMax: getPointer(1),
}
assert.Error(t, th5.validate())

th6 := CheckThresholds{
WarningMin: value.New(1),
CriticalMin: value.New(2),
WarningMin: getPointer(1),
CriticalMin: getPointer(2),
}
assert.Error(t, th6.validate())

th7 := CheckThresholds{
WarningMax: value.New(2),
CriticalMax: value.New(1),
WarningMax: getPointer(2),
CriticalMax: getPointer(1),
}
assert.Error(t, th7.validate())
}

func getPointer(f float64) *float64 {
return &f
}
Loading

0 comments on commit 841c42c

Please sign in to comment.