Skip to content

Commit

Permalink
Merge pull request #58 from mcktr/feature/timeout
Browse files Browse the repository at this point in the history
Implement timeout parameter
  • Loading branch information
mcktr authored Sep 4, 2019
2 parents eeb47ba + 888a54b commit d79b8ef
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Ensure that you also set the executable bit with `chmod +x check_fritz`.
| `-c` | `--critical` | **Optional.** Defines a critical threshold. Defaults to none. |
| `-i` | `--index` | **DEPRECATED.** **Optional.** Defines a index value required by some check methods. Defaults to none. _Marked for removal in v1.2.0._ |
| `-a` | `--ain` | **Optional.** Defines the AIN required by smart device check methods. Defaults to none. |
| `-t` | `--timeout` | **Optional.** Defines the timeout in seconds to wait for an answer from the Fritz!Box. Defaults to `90`. |

> **Note:**
>
Expand Down Expand Up @@ -112,6 +113,7 @@ object CheckCommand "fritz" {
"--critical" = "$fritz_critical$"
"--index" = "$fritz_index$"
"--ain" = "$fritz_ain$"
"--timeout" = "$fritz_timeout"
}
vars.fritz_hostname = "$address$"
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 @@ -16,7 +16,7 @@ func CheckConnectionStatus(aI ArgumentInformation) {
soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo")
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -59,7 +59,7 @@ func CheckConnectionUptime(aI ArgumentInformation) {
soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wanpppconn1", "WANPPPConnection", "GetInfo")
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/check_fritz/check_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func CheckDeviceUptime(aI ArgumentInformation) {
soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/deviceinfo", "DeviceInfo", "GetInfo")
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -57,7 +57,7 @@ func CheckDeviceUpdate(aI ArgumentInformation) {
soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/userif", "UserInterface", "GetInfo")
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/check_fritz/check_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CheckDownstreamMax(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func CheckDownstreamCurrent(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -143,7 +143,7 @@ func CheckDownstreamUsage(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down
16 changes: 8 additions & 8 deletions cmd/check_fritz/check_smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CheckSpecificSmartStatus(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -61,7 +61,7 @@ func CheckSpecificSmartHeaterTemperatur(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -132,7 +132,7 @@ func CheckSpecificSmartSocketPower(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -197,7 +197,7 @@ func CheckSpecificSmartSocketEnergy(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -266,7 +266,7 @@ func CheckSmartStatus(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewIndex", *aI.Index))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -309,7 +309,7 @@ func CheckSmartHeaterTemperatur(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewIndex", *aI.Index))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -380,7 +380,7 @@ func CheckSmartSocketPower(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewIndex", *aI.Index))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -445,7 +445,7 @@ func CheckSmartSocketEnergy(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewIndex", *aI.Index))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/check_fritz/check_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CheckUpstreamMax(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func CheckUpstreamCurrent(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down Expand Up @@ -143,7 +143,7 @@ func CheckUpstreamUsage(aI ArgumentInformation) {
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&soapReq, resps, errs)

res, err := fritz.ProcessSoapResponse(resps, errs, 1)
res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
Expand Down
20 changes: 18 additions & 2 deletions cmd/check_fritz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type ArgumentInformation struct {
Critical *float64
Index *string
InputVariable *string
Timeout *int
}

func createRequiredArgumentInformation(hostname string, port string, username string, password string, method string) ArgumentInformation {
func createRequiredArgumentInformation(hostname string, port string, username string, password string, method string, timeout string) ArgumentInformation {
var ai ArgumentInformation

ai.Hostname = &hostname
Expand All @@ -42,6 +43,8 @@ func createRequiredArgumentInformation(hostname string, port string, username st
ai.Password = &password
ai.Method = &method

ai.createTimeout(timeout)

return ai
}

Expand Down Expand Up @@ -74,6 +77,16 @@ func (ai *ArgumentInformation) createInputVariable(v string) {
ai.InputVariable = &v
}

func (ai *ArgumentInformation) createTimeout(t string) {
timeout, err := strconv.Atoi(t)

if HandleError(err) {
return
}

ai.Timeout = &timeout
}

func printVersion() {
fmt.Println("check_fritz v" + version)
GlobalReturnCode = exitOk
Expand Down Expand Up @@ -121,13 +134,15 @@ func main() {
cmdline.AddOption("c", "critical", "value", "Specifies the critical threshold.")
cmdline.AddOption("i", "index", "value", "DEPRECATED: Specifies the index.")
cmdline.AddOption("a", "ain", "value", "Specifies the AIN for smart devices.")
cmdline.AddOption("t", "timeout", "value", "Specifies the timeout for the request.")

cmdline.AddFlag("V", "version", "Returns the version")

cmdline.SetOptionDefault("hostname", "fritz.box")
cmdline.SetOptionDefault("port", "49443")
cmdline.SetOptionDefault("username", "dslf-config")
cmdline.SetOptionDefault("method", "connection_status")
cmdline.SetOptionDefault("timeout", "90")

cmdline.Parse(os.Args)

Expand All @@ -140,8 +155,9 @@ func main() {
username := cmdline.OptionValue("username")
password := cmdline.OptionValue("password")
method := cmdline.OptionValue("method")
timeout := cmdline.OptionValue("timeout")

aI := createRequiredArgumentInformation(hostname, port, username, password, method)
aI := createRequiredArgumentInformation(hostname, port, username, password, method, timeout)

if cmdline.IsOptionSet("warning") {
aI.createWarningThreshold(cmdline.OptionValue("warning"))
Expand Down
8 changes: 4 additions & 4 deletions modules/fritz/fritz_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fritz

import (
"encoding/xml"
"fmt"
"time"
)

Expand Down Expand Up @@ -176,7 +177,7 @@ func UnmarshalSoapResponse(resp TR064Response, inputXML [][]byte) error {
}

// ProcessSoapResponse handles the SOAP response from channels
func ProcessSoapResponse(resps chan []byte, errs chan error, count int) ([][]byte, error) {
func ProcessSoapResponse(resps chan []byte, errs chan error, count int, timeout int) ([][]byte, error) {
results := make([][]byte, 0)

for {
Expand All @@ -194,9 +195,8 @@ func ProcessSoapResponse(resps chan []byte, errs chan error, count int) ([][]byt
if count <= 0 {
break
}
case <-time.After(60 * time.Second):
// TODO: Timeout
panic("Timeout")
case <-time.After(time.Duration(timeout) * time.Second):
return nil, fmt.Errorf("Ran into a timeout after %d seconds", timeout)
}

if count <= 0 || timedout {
Expand Down

0 comments on commit d79b8ef

Please sign in to comment.