Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix downstream_max/upstream_max for cable devices #82

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 70 additions & 9 deletions cmd/check_fritz/check_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,95 @@ import (
"strconv"
"strings"

"github.com/mcktr/check_fritz/modules/fritzutil"

"github.com/mcktr/check_fritz/modules/fritz"
"github.com/mcktr/check_fritz/modules/perfdata"
"github.com/mcktr/check_fritz/modules/thresholds"
)

// CheckDownstreamMax checks the maximum downstream that is available on this internet connection
func CheckDownstreamMax(aI ArgumentInformation) {
// Start of the initial request to get the total number of sync groups; we always start with group id 0
resps := make(chan []byte)
errs := make(chan error)

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, aI.Debug)
initialSoapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
initialSoapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&initialSoapReq, resps, errs, aI.Debug)

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

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResp, res)

downstream, err := strconv.ParseFloat(soapResp.NewMaxDS, 64)
initialSoapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&initialSoapResp, initialResponse)

// Query the total number of sync groups
totalSyncGroups, err := strconv.Atoi(initialSoapResp.NewTotalNumberSyncGroups)
if err != nil {
panic(err)
fmt.Printf("UNKOWN - %s\n", err)
return
}

foundSupportedSyncMode := false
foundSyncMode := make([]string, 0)
var finalSoapResponse *fritz.WANCommonInterfaceOnlineMonitorResponse

for currentSyncGroup := 0; currentSyncGroup < totalSyncGroups; currentSyncGroup++ {
soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}

// We only need to perform additional queries when there are more than 1 sync groups
if totalSyncGroups > 1 {
// Start of additional query attempts (depending on how many sync groups are found)
responseChan := make(chan []byte)
errorChan := make(chan error)

soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", strconv.Itoa(currentSyncGroup)))
go fritz.DoSoapRequest(&soapReq, responseChan, errorChan, aI.Debug)

resp, err := fritz.ProcessSoapResponse(responseChan, errorChan, 1, *aI.Timeout)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

err = fritz.UnmarshalSoapResponse(&soapResp, resp)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}
} else {
soapResp = initialSoapResp
}

syncGroupMode := soapResp.NewSyncGroupMode
foundSyncMode = append(foundSyncMode, syncGroupMode)

// Search for supported sync groups
if fritzutil.Contains(supportedSyncGroupModes, syncGroupMode) {
foundSupportedSyncMode = true
finalSoapResponse = &soapResp

break
}
}

var downstream float64

if foundSupportedSyncMode {
downstream, err = strconv.ParseFloat(finalSoapResponse.NewMaxDS, 64)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}
} else {
fmt.Printf("UNKNOWN - Could not find a supported SyncGroup (%s); found the following: %s\n", strings.Join(supportedSyncGroupModes, ", "), strings.Join(foundSyncMode, ", "))
return
}

downstream = downstream * 8 / 1000000
Expand Down
79 changes: 70 additions & 9 deletions cmd/check_fritz/check_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,95 @@ import (
"strconv"
"strings"

"github.com/mcktr/check_fritz/modules/fritzutil"

"github.com/mcktr/check_fritz/modules/fritz"
"github.com/mcktr/check_fritz/modules/perfdata"
"github.com/mcktr/check_fritz/modules/thresholds"
)

// CheckUpstreamMax checks the maximum upstream that is available on this internet connection
func CheckUpstreamMax(aI ArgumentInformation) {
// Start of the initial request to get the total number of sync groups; we always start with group id 0
resps := make(chan []byte)
errs := make(chan error)

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, aI.Debug)
initialSoapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
initialSoapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&initialSoapReq, resps, errs, aI.Debug)

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

if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResp, res)

upstream, err := strconv.ParseFloat(soapResp.NewMaxUS, 64)
initialSoapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&initialSoapResp, initialResponse)

// Query the total number of sync groups
totalSyncGroups, err := strconv.Atoi(initialSoapResp.NewTotalNumberSyncGroups)
if err != nil {
panic(err)
fmt.Printf("UNKOWN - %s\n", err)
return
}

foundSupportedSyncMode := false
foundSyncMode := make([]string, 0)
var finalSoapResponse *fritz.WANCommonInterfaceOnlineMonitorResponse

for currentSyncGroup := 0; currentSyncGroup < totalSyncGroups; currentSyncGroup++ {
soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}

// We only need to perform additional queries when there are more than 1 sync groups
if totalSyncGroups > 1 {
// Start of additional query attempts (depending on how many sync groups are found)
responseChan := make(chan []byte)
errorChan := make(chan error)

soapReq := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
soapReq.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", strconv.Itoa(currentSyncGroup)))
go fritz.DoSoapRequest(&soapReq, responseChan, errorChan, aI.Debug)

resp, err := fritz.ProcessSoapResponse(responseChan, errorChan, 1, *aI.Timeout)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

err = fritz.UnmarshalSoapResponse(&soapResp, resp)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}
} else {
soapResp = initialSoapResp
}

syncGroupMode := soapResp.NewSyncGroupMode
foundSyncMode = append(foundSyncMode, syncGroupMode)

// Search for supported sync groups
if fritzutil.Contains(supportedSyncGroupModes, syncGroupMode) {
foundSupportedSyncMode = true
finalSoapResponse = &soapResp

break
}
}

var upstream float64

if foundSupportedSyncMode {
upstream, err = strconv.ParseFloat(finalSoapResponse.NewMaxUS, 64)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}
} else {
fmt.Printf("UNKNOWN - Could not find a supported SyncGroup (%s); found the following: %s\n", strings.Join(supportedSyncGroupModes, ", "), strings.Join(foundSyncMode, ", "))
return
}

upstream = upstream * 8 / 1000000
Expand Down
8 changes: 8 additions & 0 deletions cmd/check_fritz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const (
// GlobalReturnCode holds always the last set return code
var GlobalReturnCode = exitUnknown

// Collection of the supported sync group modes for the downstream_max and upstream_max methods
var supportedSyncGroupModes = []string{
"DSL",
"VDSL",
"CABLE",
"IP-Client",
}

// ArgumentInformation is the data structure for the passed arguments
type ArgumentInformation struct {
Hostname *string
Expand Down
12 changes: 12 additions & 0 deletions modules/fritzutil/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fritzutil

// Contains searches for string in a string slice and returns true if the string is found.
func Contains(s []string, v string) bool {
for _, item := range s {
if item == v {
return true
}
}

return false
}
31 changes: 31 additions & 0 deletions modules/fritzutil/find_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fritzutil

import "testing"

func TestContains(t *testing.T) {
s := []string{
"Hello",
"we",
"just",
"test",
"here",
}

f := Contains(s, "Hello")

if f != true {
t.Errorf("Contains was incorrect, want: true, got %t", f)
}

f = Contains(s, "CatDog")

if f != false {
t.Errorf("Contains was incorrect, want: false, got %t", f)
}

f = Contains(s, "hello")

if f != false {
t.Errorf("Contains was incorrect, want: false, got %t", f)
}
}