Skip to content

Commit

Permalink
refactor: use common.IsNumeric
Browse files Browse the repository at this point in the history
rename Get/SetHMPlugin to Get/SetPlugin
  • Loading branch information
Tommi2Day committed Jun 22, 2024
1 parent 929b6e0 commit a613e88
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 58 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
6 changes: 4 additions & 2 deletions cmd/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/datapoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion cmd/mastervalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions cmd/mastervalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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",
Expand Down
24 changes: 8 additions & 16 deletions cmd/nagios.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
25 changes: 1 addition & 24 deletions cmd/nagios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
}
}
2 changes: 1 addition & 1 deletion cmd/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rootcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a613e88

Please sign in to comment.