diff --git a/.gitignore b/.gitignore index ae5e0972..933a6837 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ *~ /package_build_* /release +MANUAL +MANUAL.html README README.html /unifi-poller_manual.html diff --git a/Makefile b/Makefile index 2c4cbfc5..e2053896 100644 --- a/Makefile +++ b/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) @@ -63,23 +62,23 @@ README.html: md2roff build: $(BINARY) $(BINARY): - go build -o $(BINARY) -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) + go build -o $(BINARY) -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/unifipoller.Version=$(VERSION)" $(PACKAGE) linux: $(BINARY).linux $(BINARY).linux: # Building linux binary. - GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) + GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/unifipoller.Version=$(VERSION)" $(PACKAGE) macos: $(BINARY).macos $(BINARY).macos: # Building darwin binary. - GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) + GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/unifipoller.Version=$(VERSION)" $(PACKAGE) exe: $(BINARY).exe windows: $(BINARY).exe $(BINARY).exe: # Building windows binary. - GOOS=windows go build -o $(BINARY).exe -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) + GOOS=windows go build -o $(BINARY).exe -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/unifipoller.Version=$(VERSION)" $(PACKAGE) # Packages @@ -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/README.md b/README.md index 12bce32c..32058e2b 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ means, if you wanted to do something like make telegraf collect your data instea of UniFi Poller you can achieve that with a little bit of Go code. You could write a small app that acts as a telegraf input plugin using the [unifi](https://github.com/golift/unifi) library to grab the data from your controller. As a bonus, all of the code in UniFi Poller is -[also a library](https://godoc.org/github.com/davidnewhall/unifi-poller/pkg/unifi-poller) +[also a library](https://godoc.org/github.com/davidnewhall/unifi-poller/unifipoller) and can be used in other projects. # What now... diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index 597cea74..768d4af6 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -1,29 +1,14 @@ package main import ( - "fmt" "log" - "os" - unifipoller "github.com/davidnewhall/unifi-poller/pkg/unifi-poller" + "github.com/davidnewhall/unifi-poller/unifipoller" ) +// Keep it simple. func main() { - log.SetFlags(log.LstdFlags) - if err := run(); err != nil { + if err := unifipoller.Start(); err != nil { log.Fatalln("[ERROR]", err) } } - -func run() error { - unifi := &unifipoller.UnifiPoller{} - if unifi.ParseFlags(os.Args[1:]); unifi.ShowVer { - fmt.Printf("unifi-poller v%s\n", unifipoller.Version) - return nil // don't run anything else w/ version request. - } - if err := unifi.GetConfig(); err != nil { - unifi.Flag.Usage() - return err - } - return unifi.Run() -} diff --git a/cmd/unifi-poller/README.md b/examples/MANUAL.md similarity index 100% rename from cmd/unifi-poller/README.md rename to examples/MANUAL.md diff --git a/pkg/unifi-poller/config.go b/unifipoller/config.go similarity index 100% rename from pkg/unifi-poller/config.go rename to unifipoller/config.go diff --git a/pkg/unifi-poller/jsondebug.go b/unifipoller/dumper.go similarity index 100% rename from pkg/unifi-poller/jsondebug.go rename to unifipoller/dumper.go diff --git a/pkg/unifi-poller/helpers.go b/unifipoller/helpers.go similarity index 100% rename from pkg/unifi-poller/helpers.go rename to unifipoller/helpers.go diff --git a/pkg/unifi-poller/poller.go b/unifipoller/unifipoller.go similarity index 87% rename from pkg/unifi-poller/poller.go rename to unifipoller/unifipoller.go index 2636ced0..ad8716b9 100644 --- a/pkg/unifi-poller/poller.go +++ b/unifipoller/unifipoller.go @@ -17,6 +17,22 @@ import ( yaml "gopkg.in/yaml.v2" ) +// Start begins the application from a CLI. +// Parses flags, parses config and executes Run(). +func Start() error { + log.SetFlags(log.LstdFlags) + up := &UnifiPoller{} + if up.ParseFlags(os.Args[1:]); up.ShowVer { + fmt.Printf("unifi-poller v%s\n", Version) + return nil // don't run anything else w/ version request. + } + if err := up.GetConfig(); err != nil { + up.Flag.Usage() + return err + } + return up.Run() +} + // ParseFlags runs the parser. func (u *UnifiPoller) ParseFlags(args []string) { u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) @@ -71,21 +87,18 @@ func (u *UnifiPoller) Run() (err error) { u.LogDebugf("Debug Logging Enabled") } log.Printf("[INFO] UniFi Poller v%v Starting Up! PID: %d", Version, os.Getpid()) - if err = u.GetUnifi(); err != nil { return err } if err = u.GetInfluxDB(); err != nil { return err } - if u.Lambda { - metrics, err := u.CollectMetrics() - if err != nil { - return err - } - return u.ReportMetrics(metrics) + switch { + case u.Lambda: + return u.CollectAndReport() + default: + return u.PollController() } - return u.PollController() } // GetInfluxDB returns an InfluxDB interface. diff --git a/pkg/unifi-poller/unifi.go b/unifipoller/worker.go similarity index 92% rename from pkg/unifi-poller/unifi.go rename to unifipoller/worker.go index 88836428..7ab7709d 100644 --- a/pkg/unifi-poller/unifi.go +++ b/unifipoller/worker.go @@ -43,10 +43,7 @@ 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 { - metrics, err := u.CollectMetrics() - if err == nil { - u.LogError(u.ReportMetrics(metrics), "reporting metrics") - } + _ = 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) } @@ -54,6 +51,18 @@ func (u *UnifiPoller) PollController() error { return nil } +// CollectAndReport collects measurements and reports them to influxdb. +// Can be called once or in a ticker loop. +func (u *UnifiPoller) CollectAndReport() error { + metrics, err := u.CollectMetrics() + if err != nil { + return err + } + err = u.ReportMetrics(metrics) + u.LogError(err, "reporting metrics") + return err +} + // CollectMetrics grabs all the measurements from a UniFi controller and returns them. // This also creates an InfluxDB writer, and returns error if that fails. func (u *UnifiPoller) CollectMetrics() (*Metrics, error) {