From 6b2e7b475a322adb9e0c6cafc89661f36a87ae63 Mon Sep 17 00:00:00 2001 From: Denis Shaposhnikov <993498+dsh2dsh@users.noreply.github.com> Date: Wed, 8 May 2024 20:12:35 +0200 Subject: [PATCH] Output num of connected devices OK: syncthing server alive: XXXXXXX (localhost) connected: 4 | 'connected'=4 --- README.md | 1 + cmd/health.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 931b322..e1ff200 100644 --- a/README.md +++ b/README.md @@ -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 ``` ``` diff --git a/cmd/health.go b/cmd/health.go index 6e8e1d0..2288755 100644 --- a/cmd/health.go +++ b/cmd/health.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "strconv" "github.com/dsh2dsh/go-monitoringplugin/v2" "github.com/spf13/cobra" @@ -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 { @@ -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) @@ -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 @@ -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,