From 1eba3fa72fa4cf14c22fac78d681ff588e9ce213 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 03:58:05 -0700 Subject: [PATCH 1/7] Change to newer toml library. Add untested support for xml, json and yaml config file formats. --- integrations/influxunifi/.gitignore | 2 + integrations/influxunifi/Gopkg.lock | 38 +++++++++---------- integrations/influxunifi/Gopkg.toml | 4 -- .../influxunifi/pkg/unifi-poller/config.go | 15 +++----- .../influxunifi/pkg/unifi-poller/poller.go | 27 ++++++++++--- .../influxunifi/pkg/unifi-poller/unifi.go | 4 +- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/integrations/influxunifi/.gitignore b/integrations/influxunifi/.gitignore index 31237553..ae5e0972 100644 --- a/integrations/influxunifi/.gitignore +++ b/integrations/influxunifi/.gitignore @@ -1,9 +1,11 @@ /up.conf /unifi-poller /unifi-poller*.gz +/unifi-poller*.zip /unifi-poller*.1 /unifi-poller*.deb /unifi-poller*.rpm +/unifi-poller.exe /unifi-poller.macos /unifi-poller.linux /unifi-poller.rb diff --git a/integrations/influxunifi/Gopkg.lock b/integrations/influxunifi/Gopkg.lock index 81253253..5147185d 100644 --- a/integrations/influxunifi/Gopkg.lock +++ b/integrations/influxunifi/Gopkg.lock @@ -1,6 +1,14 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. +[[projects]] + digest = "1:9f3b30d9f8e0d7040f729b82dcbc8f0dead820a133b3147ce355fc451f32d761" + name = "github.com/BurntSushi/toml" + packages = ["."] + pruneopts = "UT" + revision = "3012a1dbe2e4bd1391d42b32f0577cb7bbc7f005" + version = "v0.3.1" + [[projects]] digest = "1:28ef1378055e34f154c8efcd8863a3e53a276c58cc7fc0d0a32d6b9eed6f6cfc" name = "github.com/golift/unifi" @@ -21,25 +29,6 @@ pruneopts = "UT" revision = "8ff2fc3824fcb533795f9a2f233275f0bb18d6c5" -[[projects]] - digest = "1:b56c589214f01a5601e0821387db484617392d0042f26234bf2da853a2f498a1" - name = "github.com/naoina/go-stringutil" - packages = ["."] - pruneopts = "UT" - revision = "6b638e95a32d0c1131db0e7fe83775cbea4a0d0b" - version = "v0.1.0" - -[[projects]] - digest = "1:f58c3d0e46b64878d00652fedba24ee879725191ab919dca7b62586859281c04" - name = "github.com/naoina/toml" - packages = [ - ".", - "ast", - ] - pruneopts = "UT" - revision = "e6f5723bf2a66af014955e0888881314cf294129" - version = "v0.1.1" - [[projects]] digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b" name = "github.com/pkg/errors" @@ -56,15 +45,24 @@ revision = "298182f68c66c05229eb03ac171abe6e309ee79a" version = "v1.0.3" +[[projects]] + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/BurntSushi/toml", "github.com/golift/unifi", "github.com/influxdata/influxdb1-client/v2", - "github.com/naoina/toml", "github.com/pkg/errors", "github.com/spf13/pflag", + "gopkg.in/yaml.v2", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/integrations/influxunifi/Gopkg.toml b/integrations/influxunifi/Gopkg.toml index 46258e0f..2765dc95 100644 --- a/integrations/influxunifi/Gopkg.toml +++ b/integrations/influxunifi/Gopkg.toml @@ -24,10 +24,6 @@ # go-tests = true # unused-packages = true -[[constraint]] - name = "github.com/naoina/toml" - version = "0.1.1" - [prune] go-tests = true unused-packages = true diff --git a/integrations/influxunifi/pkg/unifi-poller/config.go b/integrations/influxunifi/pkg/unifi-poller/config.go index 196f7d81..b1804785 100644 --- a/integrations/influxunifi/pkg/unifi-poller/config.go +++ b/integrations/influxunifi/pkg/unifi-poller/config.go @@ -1,7 +1,6 @@ package unifipoller import ( - "strings" "time" "github.com/golift/unifi" @@ -67,14 +66,10 @@ type Config struct { } // Dur is used to UnmarshalTOML into a time.Duration value. -type Dur struct{ value time.Duration } +type Dur struct{ time.Duration } -// UnmarshalTOML parses a duration type from a config file. -func (v *Dur) UnmarshalTOML(data []byte) error { - unquoted := strings.Trim(string(data), `"`) - dur, err := time.ParseDuration(unquoted) - if err == nil { - v.value = dur - } - return err +// UnmarshalText parses a duration type from a config file. +func (d *Dur) UnmarshalText(data []byte) (err error) { + d.Duration, err = time.ParseDuration(string(data)) + return } diff --git a/integrations/influxunifi/pkg/unifi-poller/poller.go b/integrations/influxunifi/pkg/unifi-poller/poller.go index 80065149..ba75d2ec 100644 --- a/integrations/influxunifi/pkg/unifi-poller/poller.go +++ b/integrations/influxunifi/pkg/unifi-poller/poller.go @@ -1,16 +1,20 @@ package unifipoller import ( + "encoding/json" + "encoding/xml" "fmt" "io/ioutil" "log" "os" + "strings" + "github.com/BurntSushi/toml" "github.com/golift/unifi" influx "github.com/influxdata/influxdb1-client/v2" - "github.com/naoina/toml" "github.com/pkg/errors" flag "github.com/spf13/pflag" + yaml "gopkg.in/yaml.v2" ) // ParseFlags runs the parser. @@ -28,7 +32,7 @@ func (u *UnifiPoller) ParseFlags(args []string) { } // GetConfig parses and returns our configuration data. -func (u *UnifiPoller) GetConfig() error { +func (u *UnifiPoller) GetConfig() (err error) { // Preload our defaults. u.Config = &Config{ InfluxURL: defaultInfxURL, @@ -38,15 +42,26 @@ func (u *UnifiPoller) GetConfig() error { UnifiUser: defaultUnifUser, UnifiPass: os.Getenv("UNIFI_PASSWORD"), UnifiBase: defaultUnifURL, - Interval: Dur{value: defaultInterval}, + Interval: Dur{defaultInterval}, Sites: []string{"default"}, } - if buf, err := ioutil.ReadFile(u.ConfigFile); err != nil { + var buf []byte + switch buf, err = ioutil.ReadFile(u.ConfigFile); { + case err != nil: return err - // This is where the defaults in the config variable are overwritten. - } else if err := toml.Unmarshal(buf, u.Config); err != nil { + default: + err = toml.Unmarshal(buf, u.Config) + case strings.HasSuffix(u.ConfigFile, ".json"): + err = json.Unmarshal(buf, u.Config) + case strings.HasSuffix(u.ConfigFile, ".xml"): + err = xml.Unmarshal(buf, u.Config) + case strings.HasSuffix(u.ConfigFile, ".yaml"): + err = yaml.Unmarshal(buf, u.Config) + } + if err != nil { return err } + if u.DumpJSON != "" { u.Quiet = true } diff --git a/integrations/influxunifi/pkg/unifi-poller/unifi.go b/integrations/influxunifi/pkg/unifi-poller/unifi.go index 5bedf73d..19c39c96 100644 --- a/integrations/influxunifi/pkg/unifi-poller/unifi.go +++ b/integrations/influxunifi/pkg/unifi-poller/unifi.go @@ -38,8 +38,8 @@ FIRST: // PollController runs forever, polling unifi, and pushing to influx. func (u *UnifiPoller) PollController() error { - log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.value) - ticker := time.NewTicker(u.Interval.value) + log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.Round(time.Second)) + ticker := time.NewTicker(u.Interval.Round(time.Second)) var err error for range ticker.C { m := &Metrics{} From 77ae604a5b91b4bf23fcf156bfacd8c0df461529 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 04:09:12 -0700 Subject: [PATCH 2/7] More untested thingies. --- .../influxunifi/examples/up.conf.example | 6 +-- .../influxunifi/examples/up.json.example | 15 +++++++ .../influxunifi/examples/up.xml.example | 16 +++++++ .../influxunifi/examples/up.yaml.example | 42 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 integrations/influxunifi/examples/up.json.example create mode 100644 integrations/influxunifi/examples/up.xml.example create mode 100644 integrations/influxunifi/examples/up.yaml.example diff --git a/integrations/influxunifi/examples/up.conf.example b/integrations/influxunifi/examples/up.conf.example index 26c200ed..ecbe22c0 100644 --- a/integrations/influxunifi/examples/up.conf.example +++ b/integrations/influxunifi/examples/up.conf.example @@ -1,6 +1,6 @@ -# unifi-poller primary configuration file. # -# commented lines are defaults, uncomment to change. # -###################################################### +# unifi-poller primary configuration file. TOML FORMAT # +# commented lines are defaults, uncomment to change. # +######################################################## # If the controller has more than one site, specify which sites to poll here. # If only one site, "default" is likely the correct name. diff --git a/integrations/influxunifi/examples/up.json.example b/integrations/influxunifi/examples/up.json.example new file mode 100644 index 00000000..bc3f408b --- /dev/null +++ b/integrations/influxunifi/examples/up.json.example @@ -0,0 +1,15 @@ +{ + "sites": ["default"], + "interval": "30s", + "debug": false, + "quiet": false, + "max_errors": 0, + "influx_url": "http://127.0.0.1:8086", + "influx_user": "unifi", + "influx_pass": "unifi", + "influx_db": "unifi", + "unifi_user": "influxdb", + "unifi_pass": "4BB9345C-2341-48D7-99F5-E01B583FF77F", + "unifi_url": "https://127.0.0.1:8443", + "verify_ssl": false +} diff --git a/integrations/influxunifi/examples/up.xml.example b/integrations/influxunifi/examples/up.xml.example new file mode 100644 index 00000000..9054d75d --- /dev/null +++ b/integrations/influxunifi/examples/up.xml.example @@ -0,0 +1,16 @@ + +false +unifi +unifi +http://127.0.0.1:8086 +unifi +30s +0 +false + + default + +4BB9345C-2341-48D7-99F5-E01B583FF77F +https://127.0.0.1:8443 +influxdb +false diff --git a/integrations/influxunifi/examples/up.yaml.example b/integrations/influxunifi/examples/up.yaml.example new file mode 100644 index 00000000..502745b0 --- /dev/null +++ b/integrations/influxunifi/examples/up.yaml.example @@ -0,0 +1,42 @@ +# unifi-poller primary configuration file. YAML FORMAT # +# provided values are defaults # +######################################################## +--- +# If the controller has more than one site, specify which sites to poll here. +# If only one site, "default" is likely the correct name. +# Change default to all to poll all sites, no matter their names. +sites: + - default + +# The Unifi Controller only updates traffic stats about every 30 seconds. +# Setting this to something lower may lead to "zeros" in your data. You've been warned. +interval: "30s" + +# Turns on line numbers, microsecond logging, and a per-device log. +debug: false + +# Turns off per-device log and per-interval log. Logs only errors. +# Recommend using debug with this setting for better error logging. +quiet: false + +# 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. +max_errors: 0 + +# InfluxDB does not require auth by default, so the user/password are probably unimportant. +influx_url: "http://127.0.0.1:8086" +influx_user: "unifi" +influx_pass: "unifi" +# Be sure to create this database. +influx_db: "unifi" + +# Make a read-only user in the Unifi Admin Settings. +unifi_user: "influxdb" +unifi_pass: "" +unifi_url: "https://127.0.0.1:8443" + +# If your Unifi controller has a valid SSL certificate, you can enable +# this option to validate it. Otherwise, any SSL certificate is valid. +verify_ssl: false From 24a1014fecc3dbcbb3d5cb529449eb13885b436b Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 04:22:10 -0700 Subject: [PATCH 3/7] Tested and working. --- integrations/influxunifi/examples/README.md | 4 +++- integrations/influxunifi/examples/up.xml.example | 5 ++--- integrations/influxunifi/pkg/unifi-poller/unifi.go | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/integrations/influxunifi/examples/README.md b/integrations/influxunifi/examples/README.md index d9799785..3522385f 100644 --- a/integrations/influxunifi/examples/README.md +++ b/integrations/influxunifi/examples/README.md @@ -14,4 +14,6 @@ new benefits to the existing dashboards. When that happens I update them. Keeping an Example set allows you to update too, inspect the changes, and apply them to your own custom dashboards. -This folder also contains an example configuration file and some dashboard screenshots. +This folder also contains dashboard screenshots and example configuration files in four supported formats. +You can use any format you want for the config file, just give it the appropriate prefix for the format. +ie. an xml file should end with `.xml`. diff --git a/integrations/influxunifi/examples/up.xml.example b/integrations/influxunifi/examples/up.xml.example index 9054d75d..dfb6a07f 100644 --- a/integrations/influxunifi/examples/up.xml.example +++ b/integrations/influxunifi/examples/up.xml.example @@ -7,9 +7,8 @@ 30s 0 false - - default - +default +site2 4BB9345C-2341-48D7-99F5-E01B583FF77F https://127.0.0.1:8443 influxdb diff --git a/integrations/influxunifi/pkg/unifi-poller/unifi.go b/integrations/influxunifi/pkg/unifi-poller/unifi.go index 19c39c96..816d8db1 100644 --- a/integrations/influxunifi/pkg/unifi-poller/unifi.go +++ b/integrations/influxunifi/pkg/unifi-poller/unifi.go @@ -22,6 +22,7 @@ func (u *UnifiPoller) CheckSites() error { } u.Logf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) if StringInSlice("all", u.Sites) { + u.Sites = []string{"all"} return nil } FIRST: From 5f5950dba9bd224f711138b08ae202b6e43a2711 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 04:29:07 -0700 Subject: [PATCH 4/7] fixes --- .../influxunifi/examples/up.json.example | 2 +- .../influxunifi/examples/up.xml.example | 56 ++++++++++++++++--- .../influxunifi/pkg/unifi-poller/poller.go | 4 +- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/integrations/influxunifi/examples/up.json.example b/integrations/influxunifi/examples/up.json.example index bc3f408b..2d2c415f 100644 --- a/integrations/influxunifi/examples/up.json.example +++ b/integrations/influxunifi/examples/up.json.example @@ -9,7 +9,7 @@ "influx_pass": "unifi", "influx_db": "unifi", "unifi_user": "influxdb", - "unifi_pass": "4BB9345C-2341-48D7-99F5-E01B583FF77F", + "unifi_pass": "", "unifi_url": "https://127.0.0.1:8443", "verify_ssl": false } diff --git a/integrations/influxunifi/examples/up.xml.example b/integrations/influxunifi/examples/up.xml.example index dfb6a07f..c7d28a72 100644 --- a/integrations/influxunifi/examples/up.xml.example +++ b/integrations/influxunifi/examples/up.xml.example @@ -1,15 +1,57 @@ + + + +default + + +30s + + false + + +false + + +0 + + unifi unifi http://127.0.0.1:8086 unifi -30s -0 -false -default -site2 -4BB9345C-2341-48D7-99F5-E01B583FF77F -https://127.0.0.1:8443 + + + influxdb + +https://127.0.0.1:8443 + false diff --git a/integrations/influxunifi/pkg/unifi-poller/poller.go b/integrations/influxunifi/pkg/unifi-poller/poller.go index ba75d2ec..203bd684 100644 --- a/integrations/influxunifi/pkg/unifi-poller/poller.go +++ b/integrations/influxunifi/pkg/unifi-poller/poller.go @@ -49,14 +49,14 @@ func (u *UnifiPoller) GetConfig() (err error) { switch buf, err = ioutil.ReadFile(u.ConfigFile); { case err != nil: return err - default: - err = toml.Unmarshal(buf, u.Config) case strings.HasSuffix(u.ConfigFile, ".json"): err = json.Unmarshal(buf, u.Config) case strings.HasSuffix(u.ConfigFile, ".xml"): err = xml.Unmarshal(buf, u.Config) case strings.HasSuffix(u.ConfigFile, ".yaml"): err = yaml.Unmarshal(buf, u.Config) + default: + err = toml.Unmarshal(buf, u.Config) } if err != nil { return err From 70d1b1995e1cff6a7703bdc6450ab9864380dbbb Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 04:34:57 -0700 Subject: [PATCH 5/7] more fixes. --- integrations/influxunifi/.travis.yml | 5 +- .../influxunifi/examples/up.xml.example | 88 ++++++++++--------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/integrations/influxunifi/.travis.yml b/integrations/influxunifi/.travis.yml index cf4adb23..e8ab9d95 100644 --- a/integrations/influxunifi/.travis.yml +++ b/integrations/influxunifi/.travis.yml @@ -34,10 +34,11 @@ script: # test and build everything - rvm 2.0.0 do make release after_success: + # Display Release Folder + - ls -l release/ - | declare -r SSH_FILE="$(mktemp -u $HOME/.ssh/XXXXX)" - # Display Release Folder - ls -l release/ + echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config # Get deploy key for golift/homebrew-mugs. openssl aes-256-cbc -K $encrypted_9f3147001275_key -iv $encrypted_9f3147001275_iv -in ".travis/github_deploy_key.enc" -out "$SSH_FILE" -d diff --git a/integrations/influxunifi/examples/up.xml.example b/integrations/influxunifi/examples/up.xml.example index c7d28a72..e49f3277 100644 --- a/integrations/influxunifi/examples/up.xml.example +++ b/integrations/influxunifi/examples/up.xml.example @@ -5,53 +5,55 @@ # provided values are defaults # ######################################################## --> + + + default - -default + + 30s - -30s + + false - -false + + false - -false + + 0 - -0 - - -unifi -unifi -http://127.0.0.1:8086 -unifi + + unifi + unifi + http://127.0.0.1:8086 + unifi - -influxdb - -https://127.0.0.1:8443 - -false + + influxdb + + https://127.0.0.1:8443 + + false + + From 733fbf3deeaf1de930e58a5e85e2903a7143ef68 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 04:56:17 -0700 Subject: [PATCH 6/7] Fix logging --- integrations/influxunifi/.travis.yml | 1 - .../influxunifi/cmd/unifi-poller/start.go | 1 + .../influxunifi/pkg/unifi-poller/poller.go | 21 +++++++------------ 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/integrations/influxunifi/.travis.yml b/integrations/influxunifi/.travis.yml index e8ab9d95..c3608b3e 100644 --- a/integrations/influxunifi/.travis.yml +++ b/integrations/influxunifi/.travis.yml @@ -38,7 +38,6 @@ after_success: - ls -l release/ - | declare -r SSH_FILE="$(mktemp -u $HOME/.ssh/XXXXX)" - echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config # Get deploy key for golift/homebrew-mugs. openssl aes-256-cbc -K $encrypted_9f3147001275_key -iv $encrypted_9f3147001275_iv -in ".travis/github_deploy_key.enc" -out "$SSH_FILE" -d diff --git a/integrations/influxunifi/cmd/unifi-poller/start.go b/integrations/influxunifi/cmd/unifi-poller/start.go index 383ac91f..5ed7608f 100644 --- a/integrations/influxunifi/cmd/unifi-poller/start.go +++ b/integrations/influxunifi/cmd/unifi-poller/start.go @@ -9,6 +9,7 @@ import ( ) 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) diff --git a/integrations/influxunifi/pkg/unifi-poller/poller.go b/integrations/influxunifi/pkg/unifi-poller/poller.go index 203bd684..a2ab555e 100644 --- a/integrations/influxunifi/pkg/unifi-poller/poller.go +++ b/integrations/influxunifi/pkg/unifi-poller/poller.go @@ -44,29 +44,22 @@ func (u *UnifiPoller) GetConfig() (err error) { UnifiBase: defaultUnifURL, Interval: Dur{defaultInterval}, Sites: []string{"default"}, + Quiet: u.DumpJSON != "", } + u.Logf("Loading Configuration File: %s", u.ConfigFile) var buf []byte switch buf, err = ioutil.ReadFile(u.ConfigFile); { case err != nil: return err case strings.HasSuffix(u.ConfigFile, ".json"): - err = json.Unmarshal(buf, u.Config) + return json.Unmarshal(buf, u.Config) case strings.HasSuffix(u.ConfigFile, ".xml"): - err = xml.Unmarshal(buf, u.Config) + return xml.Unmarshal(buf, u.Config) case strings.HasSuffix(u.ConfigFile, ".yaml"): - err = yaml.Unmarshal(buf, u.Config) + return yaml.Unmarshal(buf, u.Config) default: - err = toml.Unmarshal(buf, u.Config) + return toml.Unmarshal(buf, u.Config) } - if err != nil { - return err - } - - if u.DumpJSON != "" { - u.Quiet = true - } - u.Logf("Loaded Configuration: %s", u.ConfigFile) - return nil } // Run invokes all the application logic and routines. @@ -74,7 +67,7 @@ func (u *UnifiPoller) Run() (err error) { if u.DumpJSON != "" { return u.DumpJSONPayload() } - if log.SetFlags(0); u.Debug { + if u.Debug { log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) u.LogDebugf("Debug Logging Enabled") } From 8647bce35fe04db9769a690038dae6fbc672dc82 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 21 Jun 2019 05:04:15 -0700 Subject: [PATCH 7/7] fix build --- integrations/influxunifi/.travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/influxunifi/.travis.yml b/integrations/influxunifi/.travis.yml index c3608b3e..cd34e7b0 100644 --- a/integrations/influxunifi/.travis.yml +++ b/integrations/influxunifi/.travis.yml @@ -30,7 +30,7 @@ script: - docker run -d --name unifi-poller golift/unifi-poller | tee docker_id # Once we figure out how to keep it running we can remove the -a in ps args. - docker ps -a | grep -q unifi-poller -- docker logs $(&1 | grep -Eq "Loaded Configuration[:] /etc/unifi-poller/up.conf" +- docker logs $(&1 | grep -Eq "Loading Configuration File[:] /etc/unifi-poller/up.conf" # test and build everything - rvm 2.0.0 do make release after_success: