Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ It provides output using go-check, allowing easy integration into Icinga 2 check
- Icinga-compatible check results with perfdata
- Configurable output via command-line flags

## Known Bugs

- Only the first 25 ports are supported for port statistic monitoring
(This is because of a limitation in the AVLine API)

## Documentation

### Installation
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/icinga/check-netgear

go 1.25

require github.com/NETWAYS/go-check v0.6.3

require github.com/spf13/pflag v1.0.10 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/NETWAYS/go-check v0.6.3 h1:Rcunu4JvlulbKjIef1H4pUiFO88atANpJ1YAqIwcJxI=
github.com/NETWAYS/go-check v0.6.3/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
197 changes: 197 additions & 0 deletions internal/checks/checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package checks

import (
"fmt"
"slices"

"github.com/NETWAYS/go-check"
"github.com/NETWAYS/go-check/perfdata"
"github.com/NETWAYS/go-check/result"
"github.com/icinga/check-netgear/internal/utils"
"github.com/icinga/check-netgear/netgear"
)

// CheckCPU creates a partialResult with the CPU information
func CheckCPU(cpuUsage float64, noPerfdata bool, warn float64, crit float64) (*result.PartialResult, error) {
status := utils.StatusByThreshold(cpuUsage, warn, crit)
partial := result.PartialResult{
Output: fmt.Sprintf("CPU Usage: %.2f%%", cpuUsage),
}
if err := partial.SetState(status); err != nil {
return nil, err
}
if !noPerfdata {
partial.Perfdata.Add(&perfdata.Perfdata{Label: "CPU", Value: cpuUsage, Min: 0, Max: 100})
}
return &partial, nil
}

// CheckMemory creates a partialResult with the memory information
func CheckMemory(memUsage float64, noPerfdata bool, warn float64, crit float64) (*result.PartialResult, error) {
status := utils.StatusByThreshold(memUsage, warn, crit)
partial := result.PartialResult{
Output: fmt.Sprintf("RAM Usage: %.2f%%", memUsage),
}
if err := partial.SetState(status); err != nil {
return nil, err
}
if !noPerfdata {
partial.Perfdata.Add(&perfdata.Perfdata{Label: "RAM", Value: memUsage, Min: 0, Max: 100})
}
return &partial, nil
}

// CheckTemperature creates a partialResult with the temperature information
func CheckTemperature(sensors []netgear.SensorDetail, noPerfdata bool, warn float64, crit float64) (*result.PartialResult, error) {
partial := result.PartialResult{Output: "Temperature"}
worst := check.OK

for _, s := range sensors {
status := utils.StatusByThreshold(s.Temperature, warn, crit)
if status > worst {
worst = status
}

sub := result.PartialResult{
Output: fmt.Sprintf("%s: %.1f°C", s.Description, s.Temperature),
}
if err := sub.SetState(status); err != nil {
return nil, err
}
if !noPerfdata {
sub.Perfdata.Add(&perfdata.Perfdata{Label: s.Description, Value: s.Temperature, Min: 0})
}
partial.AddSubcheck(sub)
}

if err := partial.SetState(worst); err != nil {
return nil, err
}
return &partial, nil
}

// CheckFans creates a partialResult with the fans information
func CheckFans(noPerfdata bool, fanName string, fanSpeed float64, warn float64, crit float64) (*result.PartialResult, error) {
status := utils.StatusByThreshold(fanSpeed, warn, crit)

partial := result.PartialResult{Output: "Fans"}
sub := result.PartialResult{
Output: fmt.Sprintf("%s: %.0f RPM", fanName, fanSpeed),
}
if err := sub.SetState(status); err != nil {
return nil, err
}
if !noPerfdata {
sub.Perfdata.Add(&perfdata.Perfdata{Label: "Fan Speed", Value: fanSpeed, Min: 0})
}
partial.AddSubcheck(sub)
if err := partial.SetState(status); err != nil {
return nil, err
}

return &partial, nil
}

// CheckPorts creates a partialResult with the port information
func CheckPorts(inRows, outRows []netgear.PortStatisticRow, portsToCheck []int, noPerfdata bool, warn float64, crit float64) (*result.PartialResult, error) {
overall := result.PartialResult{Output: "Ports Statistics"}
worst := check.OK

for i := range inRows {
in := inRows[i]
out := outRows[i]

if !slices.Contains(portsToCheck, in.Port) {
continue
}

portCheck := result.PartialResult{Output: fmt.Sprintf("Port %v", in.Port)}

inLoss := utils.LossPercent(in.InDropPkts, in.InTotalPkts)
outLoss := utils.LossPercent(out.OutDropPkts, out.OutTotalPkts)

inStatus := utils.StatusByThreshold(inLoss, warn, crit)
outStatus := utils.StatusByThreshold(outLoss, warn, crit)

portStatus := max(inStatus, outStatus)
worst = max(worst, portStatus)

addPerfSubcheck := func(label string, loss float64, status int) error {
sub := result.PartialResult{
Output: fmt.Sprintf("%s: %.2f%% loss", label, loss),
}
if err := sub.SetState(status); err != nil {
return err
}
if !noPerfdata {
sub.Perfdata.Add(&perfdata.Perfdata{
Label: fmt.Sprintf("port %v %s loss", in.Port, label),
Value: loss, Min: 0, Max: 100,
})
}
portCheck.AddSubcheck(sub)
return nil
}

if err := addPerfSubcheck("IN", inLoss, inStatus); err != nil {
return nil, err
}
if err := addPerfSubcheck("OUT", outLoss, outStatus); err != nil {
return nil, err
}

if err := portCheck.SetState(portStatus); err != nil {
return nil, err
}
overall.AddSubcheck(portCheck)
}

if err := overall.SetState(worst); err != nil {
return nil, err
}
return &overall, nil
}

// CheckPoe creates a partialResult with information about every port's POE status
func CheckPoe(ports []netgear.PoePort, noPerfdata bool) (*result.PartialResult, error) {
partial := result.PartialResult{Output: "Power over Ethernet Statistics"}
worst := check.OK

for _, port := range ports {
state := "disabled"
if port.Enable {
state = "enabled"
}

status := check.OK
if port.CurrentPower > port.PowerLimit {
status = check.Critical
} else if port.CurrentPower == port.PowerLimit {
status = check.Warning
}

worst = max(worst, status)

poeCheck := result.PartialResult{
Output: fmt.Sprintf(
"Port %v is %v. Current power: %.2f/%.2fV",
port.Port, state, port.CurrentPower/1000, port.PowerLimit/1000,
),
}
if err := poeCheck.SetState(status); err != nil {
return nil, err
}
if !noPerfdata {
poeCheck.Perfdata.Add(&perfdata.Perfdata{
Label: fmt.Sprintf("port %v power", port.Port),
Value: port.CurrentPower, Min: 0, Max: port.PowerLimit,
})
}
partial.AddSubcheck(poeCheck)
}

if err := partial.SetState(worst); err != nil {
return nil, err
}
return &partial, nil
}
21 changes: 21 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import "github.com/NETWAYS/go-check"

func StatusByThreshold(value, warn, crit float64) int {
switch {
case value >= crit:
return check.Critical
case value >= warn:
return check.Warning
default:
return check.OK
}
}

func LossPercent(drop, total float64) float64 {
if total <= 0 {
return 0
}
return drop / total * 100
}
Loading