Skip to content

Commit

Permalink
Merge pull request #20 from mcktr/feature/perfdata-output
Browse files Browse the repository at this point in the history
Implement performance data output
  • Loading branch information
mcktr authored Feb 19, 2019
2 parents f50472c + fd3de63 commit 5d1c331
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 17 deletions.
13 changes: 12 additions & 1 deletion cmd/check_fritz/check_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"fmt"
"strconv"

"github.com/mcktr/check_fritz/pkg/perfdata"

"github.com/mcktr/check_fritz/pkg/fritz"
)
Expand Down Expand Up @@ -53,7 +56,15 @@ func CheckConnectionUptime(aI ArgumentInformation) {
return
}

fmt.Print("OK - Connection Uptime: " + resp.NewUptime + "\n")
uptime, err := strconv.ParseFloat(resp.NewUptime, 64)

if HandleError(err) {
return
}

perfData := perfdata.CreatePerformanceData("uptime", uptime, "s")

fmt.Print("OK - Connection Uptime: " + fmt.Sprintf("%.f", uptime) + " " + perfData.GetPerformanceDataAsString() + "\n")

GlobalReturnCode = exitOk
}
12 changes: 11 additions & 1 deletion cmd/check_fritz/check_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"strconv"

"github.com/mcktr/check_fritz/pkg/fritz"
"github.com/mcktr/check_fritz/pkg/perfdata"
)

// CheckDeviceUptime checks the uptime of the device
Expand All @@ -24,7 +26,15 @@ func CheckDeviceUptime(aI ArgumentInformation) {
return
}

fmt.Print("OK - Device Uptime: " + resp.NewUpTime + "\n")
uptime, err := strconv.ParseFloat(resp.NewUpTime, 64)

if HandleError(err) {
return
}

perfData := perfdata.CreatePerformanceData("uptime", uptime, "s")

fmt.Print("OK - Device Uptime: " + fmt.Sprintf("%.0f", uptime) + " " + perfData.GetPerformanceDataAsString() + "\n")

GlobalReturnCode = exitOk
}
36 changes: 30 additions & 6 deletions cmd/check_fritz/check_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"

"github.com/mcktr/check_fritz/pkg/perfdata"

"github.com/mcktr/check_fritz/pkg/fritz"
"github.com/mcktr/check_fritz/pkg/thresholds"
)
Expand All @@ -29,6 +31,7 @@ func CheckDownstreamMax(aI ArgumentInformation) {
}

downstream, err := strconv.ParseFloat(resp.NewMaxDS, 64)
perfData := perfdata.CreatePerformanceData("downstream_max", downstream, "")

if HandleError(err) {
return
Expand All @@ -38,6 +41,14 @@ func CheckDownstreamMax(aI ArgumentInformation) {

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}

if thresholds.CheckLower(aI.Warning, downstream) {
GlobalReturnCode = exitWarning
}
Expand All @@ -46,13 +57,15 @@ func CheckDownstreamMax(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

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

switch GlobalReturnCode {
case exitOk:
fmt.Print("OK - Max Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n")
fmt.Print("OK" + output + "\n")
case exitWarning:
fmt.Print("WARNING - Max Downstream " + fmt.Sprintf("%.2f", downstream) + " Mbit/s\n")
fmt.Print("WARNING" + output + "\n")
case exitCritical:
fmt.Print("CRITICAL - Max Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n")
fmt.Print("CRITICAL" + output + "\n")
default:
GlobalReturnCode = exitUnknown
fmt.Print("UNKNWON - Not able to calculate maximum downstream\n")
Expand Down Expand Up @@ -87,9 +100,18 @@ func CheckDownstreamCurrent(aI ArgumentInformation) {
}

downstream = downstream * 8 / 1000000
perfData := perfdata.CreatePerformanceData("downstream_current", downstream, "")

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}

if thresholds.CheckUpper(aI.Warning, downstream) {
GlobalReturnCode = exitWarning
}
Expand All @@ -98,13 +120,15 @@ func CheckDownstreamCurrent(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

output := " - Current Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n " + perfData.GetPerformanceDataAsString()

switch GlobalReturnCode {
case exitOk:
fmt.Print("OK - Current Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n")
fmt.Print("OK" + output + "\n")
case exitWarning:
fmt.Print("WARNING - Current Downstream " + fmt.Sprintf("%.2f", downstream) + " Mbit/s\n")
fmt.Print("WARNING" + output + "\n")
case exitCritical:
fmt.Print("CRITICAL - Current Downstream: " + fmt.Sprintf("%.2f", downstream) + " Mbit/s \n")
fmt.Print("CRITICAL" + output + "\n")
default:
GlobalReturnCode = exitUnknown
fmt.Print("UNKNWON - Not able to calculate current downstream\n")
Expand Down
36 changes: 33 additions & 3 deletions cmd/check_fritz/check_smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strconv"

"github.com/mcktr/check_fritz/pkg/perfdata"

"github.com/mcktr/check_fritz/pkg/fritz"
"github.com/mcktr/check_fritz/pkg/thresholds"
)
Expand Down Expand Up @@ -40,17 +42,27 @@ func CheckSmartThermometer(aI ArgumentInformation) {
}

currentTemp = currentTemp / 10.0
perfData := perfdata.CreatePerformanceData("temperature", currentTemp, "")

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(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.Critical, currentTemp) {
GlobalReturnCode = exitCritical
}
output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentTemp) + " °C"

output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentTemp) + " °C " + perfData.GetPerformanceDataAsString()

switch GlobalReturnCode {
case exitOk:
Expand Down Expand Up @@ -85,6 +97,7 @@ func CheckSmartSocketPower(aI ArgumentInformation) {
}

currentPower, err := strconv.ParseFloat(resp.NewMultimeterPower, 64)
perfData := perfdata.CreatePerformanceData("power", currentPower, "")

if HandleError(err) {
return
Expand All @@ -94,6 +107,14 @@ func CheckSmartSocketPower(aI ArgumentInformation) {

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}

if thresholds.CheckUpper(aI.Warning, currentPower) {
GlobalReturnCode = exitWarning
}
Expand All @@ -102,7 +123,7 @@ func CheckSmartSocketPower(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentPower) + " W"
output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentPower) + " W " + perfData.GetPerformanceDataAsString()

switch GlobalReturnCode {
case exitOk:
Expand Down Expand Up @@ -143,9 +164,18 @@ func CheckSmartSocketEnergy(aI ArgumentInformation) {
}

currentEnergy = currentEnergy / 1000.0
perfData := perfdata.CreatePerformanceData("energy", currentEnergy, "")

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}

if thresholds.CheckUpper(aI.Warning, currentEnergy) {
GlobalReturnCode = exitWarning
}
Expand All @@ -154,7 +184,7 @@ func CheckSmartSocketEnergy(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentEnergy) + " kWh"
output := "- " + resp.NewProductName + " " + resp.NewFirmwareVersion + " - " + resp.NewDeviceName + " " + resp.NewPresent + " " + fmt.Sprintf("%.2f", currentEnergy) + " kWh " + perfData.GetPerformanceDataAsString()

switch GlobalReturnCode {
case exitOk:
Expand Down
34 changes: 28 additions & 6 deletions cmd/check_fritz/check_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

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

Expand Down Expand Up @@ -35,9 +36,17 @@ func CheckUpstreamMax(aI ArgumentInformation) {
}

upstream = upstream * 8 / 1000000
perfData := perfdata.CreatePerformanceData("upstream_max", upstream, "")

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}
if thresholds.CheckLower(aI.Warning, upstream) {
GlobalReturnCode = exitWarning
}
Expand All @@ -46,13 +55,15 @@ func CheckUpstreamMax(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

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

switch GlobalReturnCode {
case exitOk:
fmt.Print("OK - Max Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n")
fmt.Print("OK" + output + "\n")
case exitWarning:
fmt.Print("WARNING - Max Upstream " + fmt.Sprintf("%.2f", upstream) + " Mbit/s\n")
fmt.Print("WARNING" + output + "\n")
case exitCritical:
fmt.Print("CRITICAL - Max Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n")
fmt.Print("CRITICAL" + output + "\n")
default:
GlobalReturnCode = exitUnknown
fmt.Print("UNKNWON - Not able to calculate maximum upstream\n")
Expand Down Expand Up @@ -87,9 +98,18 @@ func CheckUpstreamCurrent(aI ArgumentInformation) {
}

upstream = upstream * 8 / 1000000
perfData := perfdata.CreatePerformanceData("upstream_current", upstream, "")

GlobalReturnCode = exitOk

if thresholds.GetThresholdsStatus(aI.Warning) {
perfData.SetWarning(aI.Warning)
}

if thresholds.GetThresholdsStatus(aI.Critical) {
perfData.SetCritical(aI.Critical)
}

if thresholds.CheckUpper(aI.Warning, upstream) {
GlobalReturnCode = exitWarning
}
Expand All @@ -98,13 +118,15 @@ func CheckUpstreamCurrent(aI ArgumentInformation) {
GlobalReturnCode = exitCritical
}

output := " - Current Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n " + perfData.GetPerformanceDataAsString()

switch GlobalReturnCode {
case exitOk:
fmt.Print("OK - Current Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n")
fmt.Print("OK" + output + "\n")
case exitWarning:
fmt.Print("WARNING - Current Upstream " + fmt.Sprintf("%.2f", upstream) + " Mbit/s\n")
fmt.Print("WARNING" + output + "\n")
case exitCritical:
fmt.Print("CRITICAL - Current Upstream: " + fmt.Sprintf("%.2f", upstream) + " Mbit/s \n")
fmt.Print("CRITICAL" + output + "\n")
default:
GlobalReturnCode = exitUnknown
fmt.Print("UNKNWON - Not able to calculate current upstream\n")
Expand Down
52 changes: 52 additions & 0 deletions pkg/perfdata/perfdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package perfdata

import "fmt"

// PerformanceData is the data structure for check performance data
type PerformanceData struct {
Label string
Value float64
UOM string
Warning string
Critical string
Minimum string
Maximum string
}

// CreatePerformanceData creates and returns a new PerformanceData object
func CreatePerformanceData(Label string, Value float64, UOM string) *PerformanceData {
var pd PerformanceData

pd.Label = Label
pd.Value = Value
pd.UOM = UOM

return &pd
}

// SetWarning sets the warning value of a PerformanceData object
func (pd *PerformanceData) SetWarning(Warning float64) {
pd.Warning = fmt.Sprintf("%f", Warning)

}

// SetCritical sets the critical value of a PerformanceData object
func (pd *PerformanceData) SetCritical(Critical float64) {
pd.Critical = fmt.Sprintf("%f", Critical)

}

// SetMinimum sets the minimum value of a PerformanceData object
func (pd *PerformanceData) SetMinimum(Minimum float64) {
pd.Minimum = fmt.Sprintf("%f", Minimum)
}

// SetMaximum sets the maximum value of a PerformanceData object
func (pd *PerformanceData) SetMaximum(Maximum float64) {
pd.Maximum = fmt.Sprintf("%f", Maximum)
}

// GetPerformanceDataAsString returns a PerformanceData object as formatted string
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)
}
Loading

0 comments on commit 5d1c331

Please sign in to comment.