diff --git a/CHANGELOG.md b/CHANGELOG.md index 837c848..99b7a0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog hmcli -## [v1.2.0 - 2024-06-22] -### Changed +## [v1.2.1 - 2024-06-23] - renamed from check_hm to hmcli +- use common.IsNumeric +- rename Get/SetHMPlugin to Get/SetPlugin - update dependencies ## [v1.1.1 - 2024-05-25] diff --git a/cmd/datapoint.go b/cmd/datapoint.go index 01fe1ea..7d0b6cb 100644 --- a/cmd/datapoint.go +++ b/cmd/datapoint.go @@ -4,6 +4,8 @@ import ( "fmt" "regexp" + "github.com/tommi2day/gomodules/common" + "github.com/atc0005/go-nagios" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -190,10 +192,10 @@ func datapointCheck(cmd *cobra.Command, _ []string) error { // boolean case "4": // float - preparePD = isNumeric(dp.Value) + preparePD = common.IsNumeric(dp.Value) case "16": // integer - preparePD = isNumeric(dp.Value) + preparePD = common.IsNumeric(dp.Value) case "20": // text } diff --git a/cmd/datapoint_test.go b/cmd/datapoint_test.go index eed2d39..fe40d5b 100644 --- a/cmd/datapoint_test.go +++ b/cmd/datapoint_test.go @@ -73,7 +73,7 @@ func TestDatapoint(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "datapoint check command should not return an error:%s", err) assert.NotEmpty(t, out, "datapoint check command should not return an empty string") @@ -95,7 +95,7 @@ func TestDatapoint(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "datapoint command should not return an error:%s", err) assert.NotEmpty(t, out, "datapoint command should not return an empty string") @@ -116,7 +116,7 @@ func TestDatapoint(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.Errorf(t, err, "datapoint command should not return an error:%s", err) assert.Containsf(t, out, "UNKNOWN: datapoint id 4743 not found", diff --git a/cmd/mastervalues.go b/cmd/mastervalues.go index 368c14b..3473598 100644 --- a/cmd/mastervalues.go +++ b/cmd/mastervalues.go @@ -7,6 +7,7 @@ import ( "github.com/atc0005/go-nagios" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/tommi2day/gomodules/common" "github.com/tommi2day/gomodules/hmlib" ) @@ -238,7 +239,7 @@ func mastervalueCheck(cmd *cobra.Command, _ []string) error { output := fmt.Sprintf("%s=%s", n, v) // prepare performance data only for numeric values - preparePD := isNumeric(v) + preparePD := common.IsNumeric(v) log.Debugf("value is numeric: %v", preparePD) var performanceData []nagios.PerformanceData if preparePD { diff --git a/cmd/mastervalues_test.go b/cmd/mastervalues_test.go index a626532..e2b1575 100644 --- a/cmd/mastervalues_test.go +++ b/cmd/mastervalues_test.go @@ -74,7 +74,7 @@ func TestMasterValues(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "mastervalues command should not return an error:%s", err) assert.NotEmpty(t, out, "mastervalues command should not return an empty string") @@ -97,7 +97,7 @@ func TestMasterValues(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "mastervalues command should not return an error:%s", err) assert.NotEmpty(t, out, "mastervalues command should not return an empty string") @@ -119,7 +119,7 @@ func TestMasterValues(t *testing.T) { } p := nagios.NewPlugin() p.SkipOSExit() - SetHmPlugin(p) + SetPlugin(p) out, err := common.CmdRun(RootCmd, args) assert.Errorf(t, err, "mastervalues command should not return an error:%s", err) assert.Containsf(t, out, "device with id 4743 not in response", diff --git a/cmd/nagios.go b/cmd/nagios.go index 5747d10..07aa26c 100644 --- a/cmd/nagios.go +++ b/cmd/nagios.go @@ -1,13 +1,11 @@ package cmd import ( - "regexp" - "github.com/atc0005/go-nagios" log "github.com/sirupsen/logrus" ) -var hmPlugin = nagios.NewPlugin() +var plugin = nagios.NewPlugin() const ( stateOK = "OK" @@ -18,7 +16,7 @@ const ( // NagiosResult is a helper function to return a nagios plugin result func NagiosResult(status string, output string, longOutput string, perfdata []nagios.PerformanceData) *nagios.Plugin { - p := GetHmPlugin() + p := GetPlugin() // Second, immediately defer ReturnCheckResults() so that it runs as the // last step in your client code. If you do not defer ReturnCheckResults() @@ -71,18 +69,12 @@ func NagiosResult(status string, output string, longOutput string, perfdata []na return p } -// SetHmPlugin sets the nagios plugin for the library -func SetHmPlugin(plugin *nagios.Plugin) { - hmPlugin = plugin -} - -// GetHmPlugin returns the nagios plugin for the library -func GetHmPlugin() *nagios.Plugin { - return hmPlugin +// SetPlugin sets the nagios plugin for the library +func SetPlugin(p *nagios.Plugin) { + plugin = p } -func isNumeric(s string) bool { - re := regexp.MustCompile(`^[+-\\d.]+$`) - r := re.MatchString(s) - return r +// GetPlugin returns the nagios plugin for the library +func GetPlugin() *nagios.Plugin { + return plugin } diff --git a/cmd/nagios_test.go b/cmd/nagios_test.go index c42a9ae..d5cd46a 100644 --- a/cmd/nagios_test.go +++ b/cmd/nagios_test.go @@ -31,7 +31,7 @@ func TestNagios(t *testing.T) { for _, tt := range tests { checkHM := nagios.NewPlugin() - SetHmPlugin(checkHM) + SetPlugin(checkHM) checkHM.SkipOSExit() t.Run(tt.name, func(t *testing.T) { p := NagiosResult(tt.status, "no notifications", "", tt.perfdata) @@ -44,26 +44,3 @@ func TestNagios(t *testing.T) { }) } } - -func TestIsNumeric(t *testing.T) { - tests := []struct { - name string - input string - expected bool - }{ - {"isNumeric", "123", true}, - {"isNumericFloat", "123.456", true}, - {"isNumericNegative", "-123", true}, - {"isNumericNegativeFloat", "+123.456", true}, - {"isNumericEmpty", "", false}, - {"isNumericString", "abc", false}, - {"isNumericStringWithNumber", "abc123", false}, - {"isNumericStringWithNumberNegative", "abc-123", false}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.expected, isNumeric(tt.input), "isNumeric should return %v", tt.expected) - }) - } -} diff --git a/cmd/notification.go b/cmd/notification.go index a669872..51aa70d 100644 --- a/cmd/notification.go +++ b/cmd/notification.go @@ -42,7 +42,7 @@ func getNotifications(cmd *cobra.Command, _ []string) error { return fmt.Errorf("cannot compile ignore regexp: %v", err) } } - p := GetHmPlugin() + p := GetPlugin() _, err = hmlib.GetDeviceList("", false) if err != nil { return err diff --git a/cmd/notification_test.go b/cmd/notification_test.go index 51242b9..2e90427 100644 --- a/cmd/notification_test.go +++ b/cmd/notification_test.go @@ -43,7 +43,7 @@ func TestNotifications(t *testing.T) { "--unit-test", } p := nagios.NewPlugin() - SetHmPlugin(p) + SetPlugin(p) p.SkipOSExit() out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "notifications command should not return an error:%s", err) @@ -61,7 +61,7 @@ func TestNotifications(t *testing.T) { "--unit-test", } p := nagios.NewPlugin() - SetHmPlugin(p) + SetPlugin(p) p.SkipOSExit() out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "notifications command should not return an error:%s", err) diff --git a/cmd/rootcmd.go b/cmd/rootcmd.go index 5ca89fa..6f46227 100644 --- a/cmd/rootcmd.go +++ b/cmd/rootcmd.go @@ -65,7 +65,7 @@ func init() { func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err) - p := GetHmPlugin() + p := GetPlugin() p.Errors = append(p.Errors, err) log.Debugf("return UNKNOWN, errors: %v", p.Errors) NagiosResult("UNKNOWN", "", "", nil) diff --git a/cmd/sysvar.go b/cmd/sysvar.go index 74ebb84..7645ab7 100644 --- a/cmd/sysvar.go +++ b/cmd/sysvar.go @@ -7,6 +7,7 @@ import ( "github.com/atc0005/go-nagios" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/tommi2day/gomodules/common" "github.com/tommi2day/gomodules/hmlib" ) @@ -107,7 +108,7 @@ func sysvarCheck(cmd *cobra.Command, _ []string) error { output := fmt.Sprintf("%s=%s (%s)", entry.Name, v, entry.Unit) // prepare performance data if value is numeric - preparePD := isNumeric(v) + preparePD := common.IsNumeric(v) log.Debugf("value is numeric: %v", preparePD) var performanceData []nagios.PerformanceData if preparePD { diff --git a/cmd/sysvar_test.go b/cmd/sysvar_test.go index bc83ca4..d0ff310 100644 --- a/cmd/sysvar_test.go +++ b/cmd/sysvar_test.go @@ -67,7 +67,7 @@ func TestSysvar(t *testing.T) { } p := nagios.NewPlugin() - SetHmPlugin(p) + SetPlugin(p) p.SkipOSExit() out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "sysvars command should not return an error:%s", err) @@ -97,7 +97,7 @@ func TestSysvar(t *testing.T) { } p := nagios.NewPlugin() - SetHmPlugin(p) + SetPlugin(p) p.SkipOSExit() out, err := common.CmdRun(RootCmd, args) assert.NoErrorf(t, err, "sysvars command should not return an error:%s", err)