Call it mode.

This commit is contained in:
David Newhall II 2019-06-23 19:55:26 -07:00
parent e650f1f412
commit df46550a4f
8 changed files with 48 additions and 22 deletions

View File

@ -83,9 +83,14 @@ is provided so the application can be easily adapted to any environment.
errors will be logged. Using this with debug=true adds line numbers to
any error logs.
lambda default: false
* Until reports of astonishing success with this mode, it is EXPERIMENTAL.
Setting this to true will invoke a run-once mode where the application
mode default: "influx"
* Value: influx
This default mode runs this application as a daemon. It will poll
the controller at the configured interval. Providing an invalid value
will run in this default mode.
* Value: influxlambda - (the only other available option right now)
Setting this value will invoke a run-once mode where the application
immediately polls the controller and reports the metrics to InfluxDB.
Then it exits. This mode is useful in an AWS Lambda or a crontab where
the execution timings are controlled. This mode may also be adapted

View File

@ -20,10 +20,16 @@
# Recommend enabling debug with this setting for better error logging.
#quiet = false
# Lambda mode makes the application exit after collecting and reporting metrics one time.
# This mode requires an external process like an AWS Lambda or a simple crontab to keep
# the timings accurate on UniFi Poller run intervals.
#lambda = false
# Which mode to run this application in. The default mode is "influx". Providing
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
#mode = "influx"
# If the poller experiences an error from the UniFi controller or from InfluxDB
# it will exit. If you do not want it to exit, change max_errors to -1. You can

View File

@ -3,7 +3,7 @@
"interval": "30s",
"debug": false,
"quiet": false,
"lambda": false,
"mode": "influx",
"max_errors": 0,
"influx_url": "http://127.0.0.1:8086",
"influx_user": "unifi",

View File

@ -36,11 +36,17 @@
<quiet>false</quiet>
<!--
# Lambda mode makes the application exit after collecting and reporting metrics one time.
# This mode requires an external process like an AWS Lambda or a simple crontab to keep
# the timings accurate on UniFi Poller run intervals.
# Which mode to run this application in. The default mode is "influx". Providing
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
-->
<lambda>false</lambda>
<mode>influx</mode>
<!--
# If the poller experiences an error from the UniFi controller or from InfluxDB

View File

@ -21,10 +21,16 @@ debug: false
# Recommend enabling debug with this setting for better error logging.
quiet: false
# Lambda mode makes the application exit after collecting and reporting metrics one time.
# This mode requires an external process like an AWS Lambda or a simple crontab to keep
# the timings accurate on UniFi Poller run intervals.
lambda: false
# Which mode to run this application in. The default mode is "influx". Providing
# an invalid mode will also result in "influx". In this default mode the application
# runs as a daemon and polls the controller at the configured interval.
#
# There is only one other option at this time: "influxlambda"
#
# Lambda mode makes the application exit after collecting and reporting metrics
# to InfluxDB one time. This mode requires an external process like an AWS Lambda
# or a simple crontab to keep the timings accurate on UniFi Poller run intervals.
mode: "influx"
# If the poller experiences an error from the UniFi controller or from InfluxDB
# it will exit. If you do not want it to exit, change max_errors to -1. You can

View File

@ -56,7 +56,7 @@ type Config struct {
Debug bool `json:"debug" toml:"debug" xml:"debug" yaml:"debug"`
Quiet bool `json:"quiet,_omitempty" toml:"quiet,_omitempty" xml:"quiet" yaml:"quiet"`
VerifySSL bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"`
Lambda bool `json:"lambda" toml:"lambda" xml:"lambda" yaml:"lambda"`
Mode string `json:"mode" toml:"mode" xml:"mode" yaml:"mode"`
InfluxURL string `json:"influx_url,_omitempty" toml:"influx_url,_omitempty" xml:"influx_url" yaml:"influx_url"`
InfluxUser string `json:"influx_user,_omitempty" toml:"influx_user,_omitempty" xml:"influx_user" yaml:"influx_user"`
InfluxPass string `json:"influx_pass,_omitempty" toml:"influx_pass,_omitempty" xml:"influx_pass" yaml:"influx_pass"`

View File

@ -96,8 +96,8 @@ func (u *UnifiPoller) Run() (err error) {
if err = u.GetInfluxDB(); err != nil {
return err
}
switch {
case u.Lambda:
switch strings.ToLower(u.Mode) {
case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda":
u.LogDebugf("Lambda Mode Enabled")
return u.CollectAndReport()
default:
@ -128,13 +128,12 @@ func (u *UnifiPoller) GetUnifi() (err error) {
}
u.Unifi.ErrorLog = u.LogErrorf // Log all errors.
u.Unifi.DebugLog = u.LogDebugf // Log debug messages.
// this may fail? but we'll try one more time with u.CheckSites below.
v, err := u.GetServer()
if err != nil {
v.ServerVersion = "unknown"
return err
}
u.Logf("Authenticated to UniFi Controller at %s version %s as user %s", u.UnifiBase, v.ServerVersion, u.UnifiUser)
if err = u.CheckSites(); err != nil {
if err := u.CheckSites(); err != nil {
return err
}
u.Logf("Polling UniFi Controller Sites: %v", u.Sites)

View File

@ -11,7 +11,11 @@ import (
)
// CheckSites makes sure the list of provided sites exists on the controller.
// This does not run in Lambda (run-once) mode.
func (u *UnifiPoller) CheckSites() error {
if strings.Contains(strings.ToLower(u.Mode), "lambda") {
return nil // Skip this in lambda mode.
}
sites, err := u.GetSites()
if err != nil {
return err