diff --git a/integrations/influxunifi/.gitignore b/integrations/influxunifi/.gitignore index ae5e0972..933a6837 100644 --- a/integrations/influxunifi/.gitignore +++ b/integrations/influxunifi/.gitignore @@ -15,6 +15,8 @@ *~ /package_build_* /release +MANUAL +MANUAL.html README README.html /unifi-poller_manual.html diff --git a/integrations/influxunifi/Makefile b/integrations/influxunifi/Makefile index 2c4cbfc5..8fc00a07 100644 --- a/integrations/influxunifi/Makefile +++ b/integrations/influxunifi/Makefile @@ -7,7 +7,6 @@ MAINT=David Newhall II DESC=This daemon polls a UniFi controller at a short interval and stores the collected measurements in an Influx Database. GOLANGCI_LINT_ARGS=--enable-all -D gochecknoglobals PACKAGE:=./cmd/$(BINARY) -LIBRARY:=./pkg/$(BINARY) DOCKER_REPO=golift MD2ROFF_BIN=github.com/github/hub/md2roff-bin @@ -46,9 +45,9 @@ clean: man: $(BINARY).1.gz $(BINARY).1.gz: md2roff # Building man page. Build dependency first: md2roff - go run $(MD2ROFF_BIN) --manual $(BINARY) --version $(VERSION) --date "$$(date)" cmd/$(BINARY)/README.md - gzip -9nc cmd/$(BINARY)/README > $(BINARY).1.gz - mv cmd/$(BINARY)/README.html $(BINARY)_manual.html + go run $(MD2ROFF_BIN) --manual $(BINARY) --version $(VERSION) --date "$$(date)" examples/MANUAL.md + gzip -9nc examples/MANUAL > $(BINARY).1.gz + mv examples/MANUAL.html $(BINARY)_manual.html md2roff: go get $(MD2ROFF_BIN) @@ -148,7 +147,7 @@ $(BINARY).rb: v$(VERSION).tar.gz.sha256 # Run code tests and lint. test: lint # Testing. - go test -race -covermode=atomic $(PACKAGE) $(LIBRARY) + go test -race -covermode=atomic ./... lint: # Checking lint. golangci-lint run $(GOLANGCI_LINT_ARGS) diff --git a/integrations/influxunifi/cmd/unifi-poller/main.go b/integrations/influxunifi/cmd/unifi-poller/main.go new file mode 100644 index 00000000..8f17b276 --- /dev/null +++ b/integrations/influxunifi/cmd/unifi-poller/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "log" + + unifipoller "github.com/davidnewhall/unifi-poller/pkg/unifi-poller" +) + +// Keep it simple. +func main() { + if err := unifipoller.Start(); err != nil { + log.Fatalln("[ERROR]", err) + } +} diff --git a/integrations/influxunifi/cmd/unifi-poller/start.go b/integrations/influxunifi/cmd/unifi-poller/start.go deleted file mode 100644 index 5ed7608f..00000000 --- a/integrations/influxunifi/cmd/unifi-poller/start.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - - unifipoller "github.com/davidnewhall/unifi-poller/pkg/unifi-poller" -) - -func main() { - log.SetFlags(log.LstdFlags) - unifi := &unifipoller.UnifiPoller{} - if unifi.ParseFlags(os.Args[1:]); unifi.ShowVer { - fmt.Printf("unifi-poller v%s\n", unifipoller.Version) - return // don't run anything else w/ version request. - } - if err := unifi.GetConfig(); err != nil { - unifi.Flag.Usage() - log.Fatalf("[ERROR] config file '%v': %v", unifi.ConfigFile, err) - } - if err := unifi.Run(); err != nil { - log.Fatalln("[ERROR]", err) - } -} diff --git a/integrations/influxunifi/cmd/unifi-poller/README.md b/integrations/influxunifi/examples/MANUAL.md similarity index 88% rename from integrations/influxunifi/cmd/unifi-poller/README.md rename to integrations/influxunifi/examples/MANUAL.md index f56daef0..a89fb096 100644 --- a/integrations/influxunifi/cmd/unifi-poller/README.md +++ b/integrations/influxunifi/examples/MANUAL.md @@ -83,6 +83,21 @@ 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. + 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 + to run in other collector scripts and apps like telegraf or diamond. + This mode can also be combined with a "test database" in InfluxDB to + give yourself a "test config file" you may run ad-hoc to test changes. + max_errors default: 0 If you restart the UniFI controller, the poller will lose access until it is restarted. Specifying a number greater than -1 for max_errors will diff --git a/integrations/influxunifi/examples/up.conf.example b/integrations/influxunifi/examples/up.conf.example index 3d0bcd1b..7f3f33d4 100644 --- a/integrations/influxunifi/examples/up.conf.example +++ b/integrations/influxunifi/examples/up.conf.example @@ -1,4 +1,4 @@ -# unifi-poller primary configuration file. TOML FORMAT # +# UniFi Poller primary configuration file. TOML FORMAT # # commented lines are defaults, uncomment to change. # ######################################################## @@ -20,7 +20,18 @@ # Recommend enabling debug with this setting for better error logging. #quiet = false -# If the poller experiences an error from the UniFi Controller or from InfluxDB +# 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 # adjust the config to tolerate more errors by setting this to a higher value. # Recommend setting this between 0 and 5. See man page for more explanation. diff --git a/integrations/influxunifi/examples/up.json.example b/integrations/influxunifi/examples/up.json.example index 3ce54de4..4ebbf7e8 100644 --- a/integrations/influxunifi/examples/up.json.example +++ b/integrations/influxunifi/examples/up.json.example @@ -3,6 +3,7 @@ "interval": "30s", "debug": false, "quiet": false, + "mode": "influx", "max_errors": 0, "influx_url": "http://127.0.0.1:8086", "influx_user": "unifi", diff --git a/integrations/influxunifi/examples/up.xml.example b/integrations/influxunifi/examples/up.xml.example index ccd6cf98..ba09ee5a 100644 --- a/integrations/influxunifi/examples/up.xml.example +++ b/integrations/influxunifi/examples/up.xml.example @@ -1,7 +1,7 @@ @@ -36,7 +36,20 @@ false + influx + +