Align points with ticker.

This commit is contained in:
David Newhall II 2019-07-14 02:48:41 -07:00
parent c0a20c1101
commit 033b0fce1d
3 changed files with 6 additions and 3 deletions

View File

@ -35,6 +35,7 @@ type UnifiPoller struct {
ShowVer bool ShowVer bool
Flag *pflag.FlagSet Flag *pflag.FlagSet
errorCount int errorCount int
LastCheck time.Time
influx.Client influx.Client
*unifi.Unifi *unifi.Unifi
*Config *Config

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"time"
"code.golift.io/unifi" "code.golift.io/unifi"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -96,6 +97,7 @@ func (u *UnifiPoller) Run() (err error) {
switch strings.ToLower(u.Mode) { switch strings.ToLower(u.Mode) {
case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda": case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda":
u.LogDebugf("Lambda Mode Enabled") u.LogDebugf("Lambda Mode Enabled")
u.LastCheck = time.Now()
return u.CollectAndReport() return u.CollectAndReport()
default: default:
return u.PollController() return u.PollController()

View File

@ -48,7 +48,7 @@ FIRST:
func (u *UnifiPoller) PollController() error { func (u *UnifiPoller) PollController() error {
log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.Round(time.Second)) log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.Round(time.Second))
ticker := time.NewTicker(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() _ = u.CollectAndReport()
if u.MaxErrors >= 0 && u.errorCount > u.MaxErrors { if u.MaxErrors >= 0 && u.errorCount > u.MaxErrors {
return errors.Errorf("reached maximum error count, stopping poller (%d > %d)", 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. // CollectAndReport collects measurements and reports them to influxdb.
// Can be called once or in a ticker loop. This function and all the ones below // 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 // 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. // called in two places in this library. One returns an error, one does not.
func (u *UnifiPoller) CollectAndReport() error { func (u *UnifiPoller) CollectAndReport() error {
metrics, err := u.CollectMetrics() 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. // 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. // This also creates an InfluxDB writer, and returns an error if that fails.
func (u *UnifiPoller) CollectMetrics() (*Metrics, error) { 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 var err error
// Get the sites we care about. // Get the sites we care about.
m.Sites, err = u.GetFilteredSites() m.Sites, err = u.GetFilteredSites()