Skip to content

Commit

Permalink
Output num of connected devices
Browse files Browse the repository at this point in the history
OK: syncthing server alive: XXXXXXX (localhost)
connected: 4 | 'connected'=4
  • Loading branch information
dsh2dsh committed May 8, 2024
1 parent 2d6c1fa commit 6b2e7b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Global Flags:
$ check_syncthing health -u http://127.0.0.1:8384 -k XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
OK: syncthing server alive: XXXXXXX (localhost)
connected: 4 | 'connected'=4
```

```
Expand Down
30 changes: 30 additions & 0 deletions cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"strconv"

"github.com/dsh2dsh/go-monitoringplugin/v2"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,6 +40,7 @@ type HealthCheck struct {
sysErrors []api.LogLine
system *api.SystemStatus
devices map[string]api.DeviceConfiguration
conns *api.Connections
}

func (self *HealthCheck) applyOptions() *HealthCheck {
Expand All @@ -57,7 +59,9 @@ func (self *HealthCheck) Run() *HealthCheck {
if !self.checkHealth(ctx) || !self.fetch(ctx) {
return self
}

self.resp.WithDefaultOkMessage(healthOkMsg + self.systemName())
self.outputConnected()

if len(self.sysErrors) > 0 {
self.checkSysErrors(self.sysErrors)
Expand All @@ -77,6 +81,7 @@ func (self *HealthCheck) fetch(parentCtx context.Context) bool {
g.Go(func() error { return self.fetchSysErrors(ctx) })
g.Go(func() error { return self.fetchSystemStatus(ctx) })
g.Go(func() error { return self.fetchDevices(ctx) })
g.Go(func() error { return self.fetchConns(ctx) })

self.resp.UpdateStatusOnError(g.Wait(), monitoringplugin.CRITICAL, "", true)
return self.resp.GetStatusCode() == monitoringplugin.OK
Expand Down Expand Up @@ -115,10 +120,35 @@ func (self *HealthCheck) fetchDevices(ctx context.Context) error {
return nil
}

func (self *HealthCheck) fetchConns(ctx context.Context) error {
conns, err := self.client.Connections(ctx)
if err != nil {
return err
}
self.conns = conns
return nil
}

func (self *HealthCheck) systemName() string {
return deviceName(self.system.MyID, self.devices[self.system.MyID].Name)
}

func (self *HealthCheck) outputConnected() {
var connected int
for _, c := range self.conns.Connections {
if c.Connected {
connected++
}
}
self.resp.UpdateStatus(monitoringplugin.OK,
"connected: "+strconv.Itoa(connected))

point := monitoringplugin.NewPerformanceDataPoint("connected", connected)
if err := self.resp.AddPerformanceDataPoint(point); err != nil {
self.resp.UpdateStatusOnError(err, monitoringplugin.UNKNOWN, "", true)
}
}

func (self *HealthCheck) checkSysErrors(sysErrors []api.LogLine) {
lastError := sysErrors[len(sysErrors)-1]
self.resp.UpdateStatus(monitoringplugin.WARNING,
Expand Down

0 comments on commit 6b2e7b4

Please sign in to comment.