From 7e10cdfbdcf1107cc5d4c835af72441c4150890e Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Thu, 14 Feb 2019 18:27:47 +0100 Subject: [PATCH] Implement go-cmdline library This implements the go-cmdline library for commmandline parsing. With using the go-cmdline library we gain support for short options as well as long options (e.g. -H fritz.box / --hostname fritz.box). refs #1 --- README.md | 20 ++--- cmd/check_fritz/check_connection.go | 4 +- cmd/check_fritz/check_device.go | 2 +- cmd/check_fritz/check_downstream.go | 44 +++++------ cmd/check_fritz/check_interface.go | 2 +- cmd/check_fritz/check_smart.go | 72 +++++++++--------- cmd/check_fritz/check_upstream.go | 36 ++++----- cmd/check_fritz/main.go | 112 ++++++++++++++++++++++------ pkg/thresholds/thresholds.go | 9 +++ pkg/thresholds/thresholds_test.go | 16 ++++ 10 files changed, 206 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index 6415be4..91e9809 100644 --- a/README.md +++ b/README.md @@ -38,16 +38,16 @@ Heimnetz -> Netzwerk -> Netzwerkeinstellungen -> Heimnetzfreigaben -> Zugriff f ## Usage -| Parameter | Description | -|-------------|-----------------------------------------------------------------------------------------------| -| `-hostname` | **Optional.** IP-Address or Hostname of the Fritz!Box. Defaults to `fritz.box`. | -| `-port` | **Optional.** Port for TR-064 over SSL. Defaults to `49443`. | -| `-username` | **Optional.** Fritz!Box web interface Username for authentication. Defaults to `dslf-config`. | -| `-password` | **Required.** Fritz!Box web interface Password for authentication. | -| `-method` | **Optional.** Defines the used check method. Defaults to `connection_status`. | -| `-warning` | **Optional.** Defines a warning threshold. Defaults to none. | -| `-critical` | **Optinal.** Defines a critical threshold. Defaults to none. | -| `-index` | **Optinal.** Defines a index value required by some check methods. Defaults to none. | +| Parameter (short) | Parameter (long) | Description | +| --- | ------------ | ----------------------------------------------------------------------------------------------- | +| `-H` | `--hostname` | **Optional.** IP-Address or Hostname of the Fritz!Box. Defaults to `fritz.box`. | +| `-P` | `--port` | **Optional.** Port for TR-064 over SSL. Defaults to `49443`. | +| `-u` | `--username` | **Optional.** Fritz!Box web interface Username for authentication. Defaults to `dslf-config`. | +| `-p` | `--password` | **Required.** Fritz!Box web interface Password for authentication. | +| `-m` | `--method` | **Optional.** Defines the used check method. Defaults to `connection_status`. | +| `-w` | `--warning` | **Optional.** Defines a warning threshold. Defaults to none. | +| `-c` | `--critical` | **Optinal.** Defines a critical threshold. Defaults to none. | +| `-i` | `--index` | **Optinal.** Defines a index value required by some check methods. Defaults to none. | > **Note:** > diff --git a/cmd/check_fritz/check_connection.go b/cmd/check_fritz/check_connection.go index 7d5822b..c7d9886 100644 --- a/cmd/check_fritz/check_connection.go +++ b/cmd/check_fritz/check_connection.go @@ -11,7 +11,7 @@ import ( // CheckConnectionStatus checks the internet connection status func CheckConnectionStatus(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo") err := fritz.DoSoapRequest(&soapReq) @@ -40,7 +40,7 @@ func CheckConnectionStatus(aI ArgumentInformation) { // CheckConnectionUptime checks the uptime of the internet connection func CheckConnectionUptime(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo") err := fritz.DoSoapRequest(&soapReq) diff --git a/cmd/check_fritz/check_device.go b/cmd/check_fritz/check_device.go index d5ad8dd..d4976ef 100644 --- a/cmd/check_fritz/check_device.go +++ b/cmd/check_fritz/check_device.go @@ -10,7 +10,7 @@ import ( // CheckDeviceUptime checks the uptime of the device func CheckDeviceUptime(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/deviceinfo", "DeviceInfo", "GetInfo") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/deviceinfo", "DeviceInfo", "GetInfo") err := fritz.DoSoapRequest(&soapReq) diff --git a/cmd/check_fritz/check_downstream.go b/cmd/check_fritz/check_downstream.go index 4408e5b..4909e25 100644 --- a/cmd/check_fritz/check_downstream.go +++ b/cmd/check_fritz/check_downstream.go @@ -13,7 +13,7 @@ import ( // CheckDownstreamMax checks the maximum downstream that is available on this internet connection func CheckDownstreamMax(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewSyncGroupIndex", "0")) err := fritz.DoSoapRequest(&soapReq) @@ -41,20 +41,20 @@ func CheckDownstreamMax(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckLower(*aI.Warning, downstream) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckLower(aI.Warning, downstream) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckLower(aI.Critical, downstream) { - GlobalReturnCode = exitCritical + if thresholds.CheckLower(*aI.Critical, downstream) { + GlobalReturnCode = exitCritical + } } output := " - Max Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s " + perfData.GetPerformanceDataAsString() @@ -74,7 +74,7 @@ func CheckDownstreamMax(aI ArgumentInformation) { // CheckDownstreamCurrent checks the current used downstream func CheckDownstreamCurrent(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewSyncGroupIndex", "0")) err := fritz.DoSoapRequest(&soapReq) @@ -104,20 +104,20 @@ func CheckDownstreamCurrent(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckUpper(*aI.Warning, downstream) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckUpper(aI.Warning, downstream) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckUpper(aI.Critical, downstream) { - GlobalReturnCode = exitCritical + if thresholds.CheckUpper(*aI.Critical, downstream) { + GlobalReturnCode = exitCritical + } } output := " - Current Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n " + perfData.GetPerformanceDataAsString() diff --git a/cmd/check_fritz/check_interface.go b/cmd/check_fritz/check_interface.go index 7df55e3..eca7baa 100644 --- a/cmd/check_fritz/check_interface.go +++ b/cmd/check_fritz/check_interface.go @@ -9,7 +9,7 @@ import ( // CheckInterfaceUpdate checks if a new firmware is available func CheckInterfaceUpdate(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/userif", "UserInterface", "GetInfo") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/userif", "UserInterface", "GetInfo") err := fritz.DoSoapRequest(&soapReq) diff --git a/cmd/check_fritz/check_smart.go b/cmd/check_fritz/check_smart.go index 7469965..e8cb3b4 100644 --- a/cmd/check_fritz/check_smart.go +++ b/cmd/check_fritz/check_smart.go @@ -12,8 +12,8 @@ import ( // CheckSmartThermometer checks the temperature of a smart home thermometer device func CheckSmartThermometer(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") - fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(aI.Index))) + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") + fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(*aI.Index))) err := fritz.DoSoapRequest(&soapReq) @@ -46,20 +46,20 @@ func CheckSmartThermometer(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckLower(*aI.Warning, currentTemp) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckLower(aI.Warning, currentTemp) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckLower(aI.Critical, currentTemp) { - GlobalReturnCode = exitCritical + if thresholds.CheckLower(*aI.Critical, currentTemp) { + GlobalReturnCode = exitCritical + } } output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentTemp) + " °C " + perfData.GetPerformanceDataAsString() @@ -79,8 +79,8 @@ func CheckSmartThermometer(aI ArgumentInformation) { // CheckSmartSocketPower checks the current watt usage on the smart socket func CheckSmartSocketPower(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") - fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(aI.Index))) + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") + fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(*aI.Index))) err := fritz.DoSoapRequest(&soapReq) @@ -107,20 +107,20 @@ func CheckSmartSocketPower(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckUpper(*aI.Warning, currentPower) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckUpper(aI.Warning, currentPower) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckUpper(aI.Critical, currentPower) { - GlobalReturnCode = exitCritical + if thresholds.CheckUpper(*aI.Critical, currentPower) { + GlobalReturnCode = exitCritical + } } output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentPower) + " W " + perfData.GetPerformanceDataAsString() @@ -140,8 +140,8 @@ func CheckSmartSocketPower(aI ArgumentInformation) { // CheckSmartSocketEnergy checks total power consumption of the last year on the smart socket func CheckSmartSocketEnergy(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") - fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(aI.Index))) + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetGenericDeviceInfos") + fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewIndex", strconv.Itoa(*aI.Index))) err := fritz.DoSoapRequest(&soapReq) @@ -168,20 +168,20 @@ func CheckSmartSocketEnergy(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckUpper(*aI.Warning, currentEnergy) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckUpper(aI.Warning, currentEnergy) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckUpper(aI.Critical, currentEnergy) { - GlobalReturnCode = exitCritical + if thresholds.CheckUpper(*aI.Critical, currentEnergy) { + GlobalReturnCode = exitCritical + } } output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentEnergy) + " kWh " + perfData.GetPerformanceDataAsString() diff --git a/cmd/check_fritz/check_upstream.go b/cmd/check_fritz/check_upstream.go index 61b58d3..a86fdba 100644 --- a/cmd/check_fritz/check_upstream.go +++ b/cmd/check_fritz/check_upstream.go @@ -12,7 +12,7 @@ import ( // CheckUpstreamMax checks the maximum upstream that is available on this internet connection func CheckUpstreamMax(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewSyncGroupIndex", "0")) err := fritz.DoSoapRequest(&soapReq) @@ -40,18 +40,18 @@ func CheckUpstreamMax(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) } - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) } - if thresholds.CheckLower(aI.Warning, upstream) { + if thresholds.CheckLower(*aI.Warning, upstream) { GlobalReturnCode = exitWarning } - if thresholds.CheckLower(aI.Critical, upstream) { + if thresholds.CheckLower(*aI.Critical, upstream) { GlobalReturnCode = exitCritical } @@ -72,7 +72,7 @@ func CheckUpstreamMax(aI ArgumentInformation) { // CheckUpstreamCurrent checks the current used upstream func CheckUpstreamCurrent(aI ArgumentInformation) { - soapReq := fritz.NewSoapRequest(aI.Username, aI.Password, aI.Hostname, aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") + soapReq := fritz.NewSoapRequest(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") fritz.AddSoapRequestVariable(&soapReq, fritz.NewSoapRequestVariable("NewSyncGroupIndex", "0")) err := fritz.DoSoapRequest(&soapReq) @@ -102,20 +102,20 @@ func CheckUpstreamCurrent(aI ArgumentInformation) { GlobalReturnCode = exitOk - if thresholds.GetThresholdsStatus(aI.Warning) { - perfData.SetWarning(aI.Warning) - } + if thresholds.IsSet(aI.Warning) { + perfData.SetWarning(*aI.Warning) - if thresholds.GetThresholdsStatus(aI.Critical) { - perfData.SetCritical(aI.Critical) + if thresholds.CheckUpper(*aI.Warning, upstream) { + GlobalReturnCode = exitWarning + } } - if thresholds.CheckUpper(aI.Warning, upstream) { - GlobalReturnCode = exitWarning - } + if thresholds.IsSet(aI.Critical) { + perfData.SetCritical(*aI.Critical) - if thresholds.CheckUpper(aI.Critical, upstream) { - GlobalReturnCode = exitCritical + if thresholds.CheckUpper(*aI.Critical, upstream) { + GlobalReturnCode = exitCritical + } } output := " - Current Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n " + perfData.GetPerformanceDataAsString() diff --git a/cmd/check_fritz/main.go b/cmd/check_fritz/main.go index e9e96ed..11a719d 100644 --- a/cmd/check_fritz/main.go +++ b/cmd/check_fritz/main.go @@ -1,9 +1,11 @@ package main import ( - "flag" "fmt" "os" + "strconv" + + cmdline "github.com/galdor/go-cmdline" ) // program version @@ -20,14 +22,57 @@ var GlobalReturnCode = exitUnknown // ArgumentInformation is the data structure for the passed arguments type ArgumentInformation struct { - Hostname string - Port string - Username string - Password string - Method string - Warning float64 - Critical float64 - Index int + Hostname *string + Port *string + Username *string + Password *string + Method *string + Warning *float64 + Critical *float64 + Index *int +} + +func createRequiredArgumentInformation(hostname string, port string, username string, password string, method string) ArgumentInformation { + var ai ArgumentInformation + + ai.Hostname = &hostname + ai.Port = &port + ai.Username = &username + ai.Password = &password + ai.Method = &method + + return ai +} + +func (ai *ArgumentInformation) createWarningThreshold(warning string) { + warn, err := strconv.ParseFloat(warning, 64) + + if HandleError(err) { + return + } + + ai.Warning = &warn + +} + +func (ai *ArgumentInformation) createCriticalThreshold(critical string) { + crit, err := strconv.ParseFloat(critical, 64) + + if HandleError(err) { + return + } + + ai.Critical = &crit +} + +func (ai *ArgumentInformation) createIndex(index string) { + ind, err := strconv.Atoi(index) + + if HandleError(err) { + return + } + + ai.Index = &ind } func getVersion() string { @@ -70,24 +115,49 @@ func HandleError(err error) bool { } func main() { - var hostname = flag.String("hostname", "fritz.box", "Specify the hostname") - var port = flag.String("port", "49443", "SSL port") - var username = flag.String("username", "dslf-config", "Specify the username") - var password = flag.String("password", "", "Specify the password") - var method = flag.String("method", "connection_status", "Specify the used method. (Default: status)") - var warning = flag.Float64("warning", -1, "Specify the warning threshold") - var critical = flag.Float64("critical", -1, "Specify the critical threshold") - var index = flag.Int("index", -1, "Specify an index") + cmdline := cmdline.New() + + cmdline.AddOption("H", "hostname", "value", "Specifies the hostname. (Default: fritz.box)") + cmdline.AddOption("P", "port", "value", "Specifies the SSL port. (Default: 49443)") + cmdline.AddOption("u", "username", "value", "Specifies the username. (Default: dslf-config)") + cmdline.AddOption("p", "password", "value", "Specifies the password.") + cmdline.AddOption("m", "method", "value", "Specifies the check method. (Default: connection_status)") + cmdline.AddOption("w", "warning", "value", "Specifies the warning threshold.") + cmdline.AddOption("c", "critical", "value", "Specifies the critical threshold.") + cmdline.AddOption("i", "index", "value", "Specifies the index.") + + cmdline.SetOptionDefault("hostname", "fritz.box") + cmdline.SetOptionDefault("port", "49443") + cmdline.SetOptionDefault("username", "dslf-config") + cmdline.SetOptionDefault("method", "connection_status") + + cmdline.Parse(os.Args) + + hostname := cmdline.OptionValue("hostname") + port := cmdline.OptionValue("port") + username := cmdline.OptionValue("username") + password := cmdline.OptionValue("password") + method := cmdline.OptionValue("method") + + aI := createRequiredArgumentInformation(hostname, port, username, password, method) + + if cmdline.IsOptionSet("warning") { + aI.createWarningThreshold(cmdline.OptionValue("warning")) + } - flag.Parse() + if cmdline.IsOptionSet("critical") { + aI.createCriticalThreshold(cmdline.OptionValue("critical")) + } - aI := ArgumentInformation{*hostname, *port, *username, *password, *method, *warning, *critical, *index} + if cmdline.IsOptionSet("index") { + aI.createIndex(cmdline.OptionValue("index")) + } - if !checkRequiredFlags(aI.Hostname, aI.Port, aI.Username, aI.Password) { + if !checkRequiredFlags(*aI.Hostname, *aI.Port, *aI.Username, *aI.Password) { os.Exit(exitUnknown) } - switch aI.Method { + switch *aI.Method { case "connection_status": CheckConnectionStatus(aI) case "connection_uptime": diff --git a/pkg/thresholds/thresholds.go b/pkg/thresholds/thresholds.go index 51aa8d1..9c7b07b 100644 --- a/pkg/thresholds/thresholds.go +++ b/pkg/thresholds/thresholds.go @@ -1,5 +1,14 @@ package thresholds +// IsSet returns true if the threshold is specified otherwise false +func IsSet(threshold *float64) bool { + if threshold == nil { + return false + } + + return true +} + // GetThresholdsStatus returns true if thresholds are set otherwise false func GetThresholdsStatus(threshold float64) bool { if threshold == -1.0 { diff --git a/pkg/thresholds/thresholds_test.go b/pkg/thresholds/thresholds_test.go index 8c5ff81..3864bb6 100644 --- a/pkg/thresholds/thresholds_test.go +++ b/pkg/thresholds/thresholds_test.go @@ -2,6 +2,22 @@ package thresholds import "testing" +func TestIsSet(t *testing.T) { + result := IsSet(nil) + + if result != false { + t.Errorf("IsSet was incorrect, got: %t, want: %t", result, false) + } + + f := 7.6 + + result = IsSet(&f) + + if result != true { + t.Errorf("IsSet was incorrect, got: %t, want: %t", result, true) + } +} + func TestCheckLower(t *testing.T) { result := CheckLower(3, 2.9)