Skip to content

Commit

Permalink
Query all sync groups on downstream_max/upstream_max
Browse files Browse the repository at this point in the history
Changes the downstream_max and upstream_max method to query all found
sync groups to ensure a correct calculated max down-/up-stream value.

refs #72
  • Loading branch information
mcktr committed Jun 12, 2020
1 parent 894645f commit 9f7b9e2
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 36 deletions.
109 changes: 91 additions & 18 deletions cmd/check_fritz/check_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,124 @@ import (

// CheckDownstreamMax checks the maximum downstream that is available on this internet connection
func CheckDownstreamMax(aI ArgumentInformation) {
resps := make(chan []byte)
errs := make(chan error)
// First query to collect total number of sync groups
initialResponses := make(chan []byte)
initialErrors := 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)

res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)
initialSoapRequest := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
initialSoapRequest.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&initialSoapRequest, initialResponses, initialErrors, aI.Debug)

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

soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResp, res)
initialSoapResponse := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&initialSoapResponse, initialResponse)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

downstream, err := strconv.ParseFloat(soapResp.NewMaxDS, 64)
soapResponses := []fritz.WANCommonInterfaceOnlineMonitorResponse{}
soapResponses = append(soapResponses, initialSoapResponse)

totalNumberSyncGroups, err := strconv.Atoi(initialSoapResponse.NewTotalNumberSyncGroups)
if err != nil {
panic(err)
fmt.Printf("UNKNOWN - %s\n", err)
return
}

downstream = downstream * 8 / 1000000
perfData := perfdata.CreatePerformanceData("downstream_max", downstream, "")
// If there are more sync groups query all other sync groups, starting with the next one
if totalNumberSyncGroups > 1 {
for i := 1; i < totalNumberSyncGroups; i++ {
responses := make(chan []byte)
errors := make(chan error)

soapRequest := initialSoapRequest
soapRequest.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", strconv.Itoa(i)))

go fritz.DoSoapRequest(&soapRequest, responses, errors, aI.Debug)

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

soapResponse := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResponse, response)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

soapResponses = append(soapResponses, soapResponse)
}
}

downstreamCombined := 0.0
output := ""
performanceData := []perfdata.PerformanceData{}
GlobalReturnCode = exitOk

for _, r := range soapResponses {
downstream, err := strconv.ParseFloat(r.NewMaxDS, 64)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

downstreamCombined += downstream
downstream = downstream * 8 / 1000000

output += fmt.Sprintf(", Downstream SyncGroup '%s': %.2f Mbit/s", r.NewSyncGroupName, downstream)

pd := perfdata.CreatePerformanceData("downstream_max_"+fmt.Sprintf("%s", r.NewSyncGroupName), downstream, "")

if thresholds.IsSet(aI.Warning) {
pd.SetWarning(*aI.Warning)

if thresholds.CheckLower(*aI.Warning, downstream) {
GlobalReturnCode = exitWarning
}
}

if thresholds.IsSet(aI.Critical) {
pd.SetCritical(*aI.Critical)

if thresholds.CheckLower(*aI.Critical, downstream) {
GlobalReturnCode = exitCritical
}
}

performanceData = append(performanceData, *pd)
}

downstreamCombined = downstreamCombined * 8 / 1000000
pdDownstreamCombined := perfdata.CreatePerformanceData("downstream_max", downstreamCombined, "")

if thresholds.IsSet(aI.Warning) {
perfData.SetWarning(*aI.Warning)
pdDownstreamCombined.SetWarning(*aI.Warning)

if thresholds.CheckLower(*aI.Warning, downstream) {
if thresholds.CheckLower(*aI.Warning, downstreamCombined) {
GlobalReturnCode = exitWarning
}
}

if thresholds.IsSet(aI.Critical) {
perfData.SetCritical(*aI.Critical)
pdDownstreamCombined.SetCritical(*aI.Critical)

if thresholds.CheckLower(*aI.Critical, downstream) {
if thresholds.CheckLower(*aI.Critical, downstreamCombined) {
GlobalReturnCode = exitCritical
}
}

output := " - Max Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s " + perfData.GetPerformanceDataAsString()
performanceData = append(performanceData, *pdDownstreamCombined)

output = " - Max Downstream: " + fmt.Sprintf("%.2f", downstreamCombined) + " Mbit/s" + output + perfdata.FormatAsString(performanceData)

switch GlobalReturnCode {
case exitOk:
Expand Down
109 changes: 91 additions & 18 deletions cmd/check_fritz/check_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,124 @@ import (

// CheckUpstreamMax checks the maximum upstream that is available on this internet connection
func CheckUpstreamMax(aI ArgumentInformation) {
resps := make(chan []byte)
errs := make(chan error)
// First query to collect total number of sync groups
initialResponses := make(chan []byte)
initialErrors := 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)

res, err := fritz.ProcessSoapResponse(resps, errs, 1, *aI.Timeout)
initialSoapRequest := fritz.CreateNewSoapData(*aI.Username, *aI.Password, *aI.Hostname, *aI.Port, "/upnp/control/wancommonifconfig1", "WANCommonInterfaceConfig", "X_AVM-DE_GetOnlineMonitor")
initialSoapRequest.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", "0"))
go fritz.DoSoapRequest(&initialSoapRequest, initialResponses, initialErrors, aI.Debug)

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

soapResp := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResp, res)
initialSoapResponse := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&initialSoapResponse, initialResponse)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

upstream, err := strconv.ParseFloat(soapResp.NewMaxUS, 64)
soapResponses := []fritz.WANCommonInterfaceOnlineMonitorResponse{}
soapResponses = append(soapResponses, initialSoapResponse)

totalNumberSyncGroups, err := strconv.Atoi(initialSoapResponse.NewTotalNumberSyncGroups)
if err != nil {
panic(err)
fmt.Printf("UNKNOWN - %s\n", err)
return
}

upstream = upstream * 8 / 1000000
perfData := perfdata.CreatePerformanceData("upstream_max", upstream, "")
// If there are more sync groups query all other sync groups, starting with the next one
if totalNumberSyncGroups > 1 {
for i := 1; i < totalNumberSyncGroups; i++ {
responses := make(chan []byte)
errors := make(chan error)

soapRequest := initialSoapRequest
soapRequest.AddSoapDataVariable(fritz.CreateNewSoapVariable("NewSyncGroupIndex", strconv.Itoa(i)))

go fritz.DoSoapRequest(&soapRequest, responses, errors, aI.Debug)

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

soapResponse := fritz.WANCommonInterfaceOnlineMonitorResponse{}
err = fritz.UnmarshalSoapResponse(&soapResponse, response)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

soapResponses = append(soapResponses, soapResponse)
}
}

upstreamCombined := 0.0
output := ""
performanceData := []perfdata.PerformanceData{}
GlobalReturnCode = exitOk

for _, r := range soapResponses {
upstream, err := strconv.ParseFloat(r.NewMaxUS, 64)
if err != nil {
fmt.Printf("UNKNOWN - %s\n", err)
return
}

upstreamCombined += upstream
upstream = upstream * 8 / 1000000

output += fmt.Sprintf(", Upstream SyncGroup '%s': %.2f Mbit/s", r.NewSyncGroupName, upstream)

pd := perfdata.CreatePerformanceData("upstream_max_"+fmt.Sprintf("%s", r.NewSyncGroupName), upstream, "")

if thresholds.IsSet(aI.Warning) {
pd.SetWarning(*aI.Warning)

if thresholds.CheckLower(*aI.Warning, upstream) {
GlobalReturnCode = exitWarning
}
}

if thresholds.IsSet(aI.Critical) {
pd.SetCritical(*aI.Critical)

if thresholds.CheckLower(*aI.Critical, upstream) {
GlobalReturnCode = exitCritical
}
}

performanceData = append(performanceData, *pd)
}

upstreamCombined = upstreamCombined * 8 / 1000000
pdUpstreamCombined := perfdata.CreatePerformanceData("upstream_max", upstreamCombined, "")

if thresholds.IsSet(aI.Warning) {
perfData.SetWarning(*aI.Warning)
pdUpstreamCombined.SetWarning(*aI.Warning)

if thresholds.CheckLower(*aI.Warning, upstream) {
if thresholds.CheckLower(*aI.Warning, upstreamCombined) {
GlobalReturnCode = exitWarning
}
}

if thresholds.IsSet(aI.Critical) {
perfData.SetCritical(*aI.Critical)
pdUpstreamCombined.SetCritical(*aI.Critical)

if thresholds.CheckLower(*aI.Critical, upstream) {
if thresholds.CheckLower(*aI.Critical, upstreamCombined) {
GlobalReturnCode = exitCritical
}
}

output := " - Max Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s " + perfData.GetPerformanceDataAsString()
performanceData = append(performanceData, *pdUpstreamCombined)

output = " - Max Upstream: " + fmt.Sprintf("%.2f", upstreamCombined) + " Mbit/s" + output + perfdata.FormatAsString(performanceData)

switch GlobalReturnCode {
case exitOk:
Expand Down
10 changes: 10 additions & 0 deletions modules/perfdata/perfdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ func (pd *PerformanceData) SetMaximum(Maximum float64) {
func (pd *PerformanceData) GetPerformanceDataAsString() string {
return fmt.Sprintf("| '%s'=%f%s;%s;%s;%s;%s", pd.Label, pd.Value, pd.UOM, pd.Warning, pd.Critical, pd.Minimum, pd.Maximum)
}

func FormatAsString(pds []PerformanceData) string {
output := " |"

for _, pd := range pds {
output += fmt.Sprintf(" '%s'=%f%s;%s;%s;%s;%s", pd.Label, pd.Value, pd.UOM, pd.Warning, pd.Critical, pd.Minimum, pd.Maximum)
}

return output
}

0 comments on commit 9f7b9e2

Please sign in to comment.