From 945c906066917555b6b33804d5be2fac239a874c Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Mon, 11 May 2020 21:57:05 +0200 Subject: [PATCH 1/2] Add debug mode parameter Adds debug mode for troubleshooting. --- cmd/check_fritz/check_connection.go | 4 ++-- cmd/check_fritz/check_device.go | 4 ++-- cmd/check_fritz/check_downstream.go | 6 +++--- cmd/check_fritz/check_smart.go | 8 ++++---- cmd/check_fritz/check_upstream.go | 6 +++--- cmd/check_fritz/main.go | 15 +++++++++++++++ modules/fritz/fritz_http.go | 11 ++++++++--- 7 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cmd/check_fritz/check_connection.go b/cmd/check_fritz/check_connection.go index 0607e43..40bfbe0 100644 --- a/cmd/check_fritz/check_connection.go +++ b/cmd/check_fritz/check_connection.go @@ -28,7 +28,7 @@ func CheckConnectionStatus(aI ArgumentInformation) { GlobalReturnCode = exitUnknown return } - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -84,7 +84,7 @@ func CheckConnectionUptime(aI ArgumentInformation) { GlobalReturnCode = exitUnknown return } - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) diff --git a/cmd/check_fritz/check_device.go b/cmd/check_fritz/check_device.go index b8f6484..696659e 100644 --- a/cmd/check_fritz/check_device.go +++ b/cmd/check_fritz/check_device.go @@ -14,7 +14,7 @@ func CheckDeviceUptime(aI ArgumentInformation) { errs := make(chan error) soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/deviceinfo", "DeviceInfo", "GetInfo") - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -55,7 +55,7 @@ func CheckDeviceUpdate(aI ArgumentInformation) { errs := make(chan error) soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/userif", "UserInterface", "GetInfo") - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) diff --git a/cmd/check_fritz/check_downstream.go b/cmd/check_fritz/check_downstream.go index e95325a..1f7a04a 100644 --- a/cmd/check_fritz/check_downstream.go +++ b/cmd/check_fritz/check_downstream.go @@ -17,7 +17,7 @@ func CheckDownstreamMax(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -78,7 +78,7 @@ func CheckDownstreamCurrent(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -141,7 +141,7 @@ func CheckDownstreamUsage(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) diff --git a/cmd/check_fritz/check_smart.go b/cmd/check_fritz/check_smart.go index ef0280b..a55b9a5 100644 --- a/cmd/check_fritz/check_smart.go +++ b/cmd/check_fritz/check_smart.go @@ -16,7 +16,7 @@ func CheckSpecificSmartStatus(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetSpecificDeviceInfos") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable)) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -59,7 +59,7 @@ func CheckSpecificSmartHeaterTemperatur(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetSpecificDeviceInfos") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable)) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -130,7 +130,7 @@ func CheckSpecificSmartSocketPower(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetSpecificDeviceInfos") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable)) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -195,7 +195,7 @@ func CheckSpecificSmartSocketEnergy(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/x_homeauto", "X_AVM-DE_Homeauto", "GetSpecificDeviceInfos") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewAIN", *aI.InputVariable)) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) diff --git a/cmd/check_fritz/check_upstream.go b/cmd/check_fritz/check_upstream.go index 3dfc8ed..7a0d4a7 100644 --- a/cmd/check_fritz/check_upstream.go +++ b/cmd/check_fritz/check_upstream.go @@ -17,7 +17,7 @@ func CheckUpstreamMax(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -78,7 +78,7 @@ func CheckUpstreamCurrent(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) @@ -141,7 +141,7 @@ func CheckUpstreamUsage(aI ArgumentInformation) { soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor") soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0")) - go fritz.DoSoapRequest(&soapReq, resps, errs) + go fritz.DoSoapRequest(&soapReq, resps, errs, aI.Debug) res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout) diff --git a/cmd/check_fritz/main.go b/cmd/check_fritz/main.go index 0889ca6..9806d1c 100644 --- a/cmd/check_fritz/main.go +++ b/cmd/check_fritz/main.go @@ -33,6 +33,7 @@ type ArgumentInformation struct { InputVariable *string Timeout *int Modelgroup *string + Debug bool } func createRequiredArgumentInformation(hostname string, port string, username string, password string, method string, timeout int, modelgroup string) ArgumentInformation { @@ -45,6 +46,7 @@ func createRequiredArgumentInformation(hostname string, port string, username st ai.Method = &method ai.Modelgroup = &modelgroup ai.Timeout = &timeout + ai.Debug = false return ai } @@ -61,6 +63,10 @@ func (ai *ArgumentInformation) createInputVariable(v string) { ai.InputVariable = &v } +func (ai *ArgumentInformation) setDebugMode() { + ai.Debug = true +} + func printVersion() { fmt.Println("check_fritz v" + version) GlobalReturnCode = exitOk @@ -136,6 +142,10 @@ func checkMain(c *cli.Context) error { argInfo.createInputVariable(c.String("ain")) } + if c.IsSet("debug") { + argInfo.setDebugMode() + } + if !checkRequiredFlags(&argInfo) { os.Exit(exitUnknown) } @@ -247,6 +257,11 @@ func main() { Aliases: []string{"c"}, Usage: "Specifies the critical threshold.", }, + &cli.BoolFlag{ + Name: "debug", + Aliases: []string{"d"}, + Usage: "Outputs debug information", + }, }, } diff --git a/modules/fritz/fritz_http.go b/modules/fritz/fritz_http.go index 5b08e69..bb667b7 100644 --- a/modules/fritz/fritz_http.go +++ b/modules/fritz/fritz_http.go @@ -10,7 +10,7 @@ import ( ) // DoSoapRequest does two request to authenticate and handle the SOAP request -func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error) { +func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error, debug bool) { soapClient := createNewSoapClient() // prepare first request @@ -38,7 +38,9 @@ func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error resp.Body.Close() // enable this for debug sessions - // fmt.Println(string(body)) + if debug { + fmt.Println(string(body)) + } // create immediately a new request with authentication req, err = newSoapRequest(soapRequest) @@ -92,7 +94,10 @@ func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error resp.Body.Close() // enable this for debug sessions - // fmt.Println(string(body)) + + if debug { + fmt.Println(string(body)) + } resps <- body } From 1aecb85cabd174bb30f6104bbdd31776aab96f06 Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Mon, 25 May 2020 21:05:30 +0200 Subject: [PATCH 2/2] Adjust debug outout format --- modules/fritz/fritz_http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/fritz/fritz_http.go b/modules/fritz/fritz_http.go index bb667b7..b7b7a4f 100644 --- a/modules/fritz/fritz_http.go +++ b/modules/fritz/fritz_http.go @@ -39,7 +39,7 @@ func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error // enable this for debug sessions if debug { - fmt.Println(string(body)) + fmt.Printf("---\nFrist SOAP Response:\n---\n%v\n---\n", string(body)) } // create immediately a new request with authentication @@ -96,7 +96,7 @@ func DoSoapRequest(soapRequest *SoapData, resps chan<- []byte, errs chan<- error // enable this for debug sessions if debug { - fmt.Println(string(body)) + fmt.Printf("---\nSecond SOAP Response:\n---\n%v\n---\n", string(body)) } resps <- body