Skip to content

Commit

Permalink
Merge pull request #22 from mcktr/feature/command-line-parsing
Browse files Browse the repository at this point in the history
Use go-cmdline library
  • Loading branch information
mcktr authored Feb 20, 2019
2 parents 5d1c331 + 7e10cdf commit db6ecbd
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 111 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
>
Expand Down
4 changes: 2 additions & 2 deletions cmd/check_fritz/check_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cmd/check_fritz/check_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
44 changes: 22 additions & 22 deletions cmd/check_fritz/check_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_fritz/check_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
72 changes: 36 additions & 36 deletions cmd/check_fritz/check_smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -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()
Expand Down
36 changes: 18 additions & 18 deletions cmd/check_fritz/check_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit db6ecbd

Please sign in to comment.