From 033b0fce1d2b21f01c855e0b7adb4a063eb2eab6 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 14 Jul 2019 02:48:41 -0700 Subject: [PATCH] Align points with ticker. --- core/poller/unifipoller/config.go | 1 + core/poller/unifipoller/start.go | 2 ++ core/poller/unifipoller/unifi.go | 6 +++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/poller/unifipoller/config.go b/core/poller/unifipoller/config.go index f2e817bc..65ded912 100644 --- a/core/poller/unifipoller/config.go +++ b/core/poller/unifipoller/config.go @@ -35,6 +35,7 @@ type UnifiPoller struct { ShowVer bool Flag *pflag.FlagSet errorCount int + LastCheck time.Time influx.Client *unifi.Unifi *Config diff --git a/core/poller/unifipoller/start.go b/core/poller/unifipoller/start.go index 5d762767..2520459a 100644 --- a/core/poller/unifipoller/start.go +++ b/core/poller/unifipoller/start.go @@ -8,6 +8,7 @@ import ( "log" "os" "strings" + "time" "code.golift.io/unifi" "github.com/BurntSushi/toml" @@ -96,6 +97,7 @@ func (u *UnifiPoller) Run() (err error) { switch strings.ToLower(u.Mode) { case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda": u.LogDebugf("Lambda Mode Enabled") + u.LastCheck = time.Now() return u.CollectAndReport() default: return u.PollController() diff --git a/core/poller/unifipoller/unifi.go b/core/poller/unifipoller/unifi.go index 9123738e..a62b777c 100644 --- a/core/poller/unifipoller/unifi.go +++ b/core/poller/unifipoller/unifi.go @@ -48,7 +48,7 @@ FIRST: func (u *UnifiPoller) PollController() error { log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.Round(time.Second)) ticker := time.NewTicker(u.Interval.Round(time.Second)) - for range ticker.C { + for u.LastCheck = range ticker.C { _ = u.CollectAndReport() if u.MaxErrors >= 0 && u.errorCount > u.MaxErrors { return errors.Errorf("reached maximum error count, stopping poller (%d > %d)", u.errorCount, u.MaxErrors) @@ -60,7 +60,7 @@ func (u *UnifiPoller) PollController() error { // CollectAndReport collects measurements and reports them to influxdb. // Can be called once or in a ticker loop. This function and all the ones below // handle their own logging. An error is returned so the calling function may -// determine if there was a read or write erorr and act on it. This is currently +// determine if there was a read or write error and act on it. This is currently // called in two places in this library. One returns an error, one does not. func (u *UnifiPoller) CollectAndReport() error { metrics, err := u.CollectMetrics() @@ -78,7 +78,7 @@ func (u *UnifiPoller) CollectAndReport() error { // CollectMetrics grabs all the measurements from a UniFi controller and returns them. // This also creates an InfluxDB writer, and returns an error if that fails. func (u *UnifiPoller) CollectMetrics() (*Metrics, error) { - m := &Metrics{TS: time.Now()} + m := &Metrics{TS: u.LastCheck} // At this point, it's the Current Check. var err error // Get the sites we care about. m.Sites, err = u.GetFilteredSites()