From 5d40ad8f35069ef23518659ffbc40972b80ad207 Mon Sep 17 00:00:00 2001 From: unifi-poller-bot Date: Wed, 12 Jun 2019 20:32:33 -0700 Subject: [PATCH 01/12] Add json dump debug feature. --- cmd/unifi-poller/config.go | 17 ++++++- cmd/unifi-poller/jsondebug.go | 87 +++++++++++++++++++++++++++++++++++ cmd/unifi-poller/main.go | 63 +++++++++++++++---------- 3 files changed, 140 insertions(+), 27 deletions(-) create mode 100644 cmd/unifi-poller/jsondebug.go diff --git a/cmd/unifi-poller/config.go b/cmd/unifi-poller/config.go index 1b75860a..203a88fd 100644 --- a/cmd/unifi-poller/config.go +++ b/cmd/unifi-poller/config.go @@ -1,6 +1,10 @@ package main -import "time" +import ( + "time" + + "github.com/spf13/pflag" +) // Version is injected by the Makefile var Version = "development" @@ -17,11 +21,20 @@ const ( defaultUnifURL = "https://127.0.0.1:8443" ) +// UnifiPoller contains the application startup data. +type UnifiPoller struct { + ConfigFile string + DumpJSON string + ShowVer bool + *Config + Flag *pflag.FlagSet +} + // Config represents the data needed to poll a controller and report to influxdb. type Config struct { Interval Dur `json:"interval,_omitempty" toml:"interval,_omitempty" xml:"interval" yaml:"interval"` Debug bool `json:"debug" toml:"debug" xml:"debug" yaml:"debug"` - Quiet bool `json:"quiet" toml:"quiet" xml:"quiet" yaml:"quiet"` + 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"` 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"` diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go new file mode 100644 index 00000000..011738bd --- /dev/null +++ b/cmd/unifi-poller/jsondebug.go @@ -0,0 +1,87 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + + "github.com/golift/unifi" + "github.com/pkg/errors" +) + +// DumpJSON prints raw json from the Unifi Controller. +func (c *Config) DumpJSON(filter string) error { + c.Quiet = true + controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) + if err != nil { + return err + } + fmt.Fprintln(os.Stderr, "Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) + if err := c.CheckSites(controller); err != nil { + return err + } + controller.ErrorLog = func(m string, v ...interface{}) { + fmt.Fprintf(os.Stderr, m, v...) + } // Log all errors to stderr. + + switch sites, err := filterSites(controller, c.Sites); { + case err != nil: + return err + case StringInSlice(filter, []string{"d", "device", "devices"}): + return c.DumpClientsJSON(sites, controller) + case StringInSlice(filter, []string{"client", "clients", "c"}): + return c.DumpDeviceJSON(sites, controller) + default: + return errors.New("must provide filter: devices, clients") + } +} + +func errorLog(m string, v ...interface{}) { + fmt.Fprintf(os.Stderr, m, v...) +} + +// DumpClientsJSON prints the raw json for clients in a Unifi Controller. +func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) error { + for _, s := range sites { + path := fmt.Sprintf(unifi.ClientPath, s.Name) + req, err := controller.UniReq(path, "") + if err != nil { + return err + } + resp, err := controller.Do(req) + if err != nil { + return err + } + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "Dumping Client JSON for site %s (%s)\n", s.Desc, s.Name) + fmt.Println(string(body)) + } + return nil +} + +// DumpDeviceJSON prints the raw json for devices in a Unifi Controller. +func (c *Config) DumpDeviceJSON(sites []unifi.Site, controller *unifi.Unifi) error { + for _, s := range sites { + path := fmt.Sprintf(unifi.DevicePath, s.Name) + req, err := controller.UniReq(path, "") + if err != nil { + return err + } + resp, err := controller.Do(req) + if err != nil { + return err + } + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "Dumping Device JSON for site %s (%s)\n", s.Desc, s.Name) + fmt.Println(string(body)) + } + return nil +} diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index f7a233e2..562dfc22 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -21,20 +21,29 @@ type Asset interface { } func main() { - configFile := parseFlags() - log.Println("Unifi-Poller Starting Up! PID:", os.Getpid()) - config, err := GetConfig(configFile) - if err != nil { - flag.Usage() - log.Fatalf("[ERROR] config file '%v': %v", configFile, err) + u := &UnifiPoller{} + if u.ParseFlags(os.Args[1:]); u.ShowVer { + fmt.Printf("unifi-poller v%s\n", Version) + os.Exit(0) // don't run anything else. } - if err := config.Run(); err != nil { + if err := u.GetConfig(); err != nil { + u.Flag.Usage() + log.Fatalf("[ERROR] config file '%v': %v", u.ConfigFile, err) + } + if u.DumpJSON != "" { + if err := u.Config.DumpJSON(u.DumpJSON); err != nil { + log.Fatalln("[ERROR] dumping JSON:", err) + } + return + } + if err := u.Config.Run(); err != nil { log.Fatalln("[ERROR]", err) } } // Run invokes all the application logic and routines. func (c *Config) Run() error { + log.Println("Unifi-Poller Starting Up! PID:", os.Getpid()) // Create an authenticated session to the Unifi Controller. controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) if err != nil { @@ -71,18 +80,17 @@ func (c *Config) Run() error { return nil } -func parseFlags() string { - flag.Usage = func() { +// ParseFlags runs the parser. +func (u *UnifiPoller) ParseFlags(args []string) { + u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) + u.Flag.Usage = func() { fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") - flag.PrintDefaults() + u.Flag.PrintDefaults() } - configFile := flag.StringP("config", "c", defaultConfFile, "Poller Config File (TOML Format)") - version := flag.BoolP("version", "v", false, "Print the version and exit") - if flag.Parse(); *version { - fmt.Printf("unifi-poller v%s\n", Version) - os.Exit(0) // don't run anything else. - } - return *configFile + u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", "This debug option prints the json payload for a device and exits.") + u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") + u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") + _ = u.Flag.Parse(args) } // CheckSites makes sure the list of provided sites exists on the controller. @@ -114,9 +122,9 @@ FIRST: } // GetConfig parses and returns our configuration data. -func GetConfig(configFile string) (Config, error) { +func (u *UnifiPoller) GetConfig() error { // Preload our defaults. - config := Config{ + u.Config = &Config{ InfluxURL: defaultInfxURL, InfluxUser: defaultInfxUser, InfluxPass: defaultInfxPass, @@ -127,14 +135,19 @@ func GetConfig(configFile string) (Config, error) { Interval: Dur{value: defaultInterval}, Sites: []string{"default"}, } - if buf, err := ioutil.ReadFile(configFile); err != nil { - return config, err + if buf, err := ioutil.ReadFile(u.ConfigFile); err != nil { + return err // This is where the defaults in the config variable are overwritten. - } else if err := toml.Unmarshal(buf, &config); err != nil { - return config, err + } else if err := toml.Unmarshal(buf, u.Config); err != nil { + return err } - log.Println("Loaded Configuration:", configFile) - return config, nil + if u.DumpJSON != "" { + u.Quiet = true + } + if !u.Config.Quiet { + log.Println("Loaded Configuration:", u.ConfigFile) + } + return nil } // PollUnifiController runs forever, polling and pushing. From 2d33076c9bc7044a04744530827791cc51930077 Mon Sep 17 00:00:00 2001 From: unifi-poller-bot Date: Wed, 12 Jun 2019 20:39:09 -0700 Subject: [PATCH 02/12] Update readme. --- cmd/unifi-poller/README.md | 13 ++++++++++++- cmd/unifi-poller/jsondebug.go | 6 +----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/unifi-poller/README.md b/cmd/unifi-poller/README.md index 64bce18e..2b8e8159 100644 --- a/cmd/unifi-poller/README.md +++ b/cmd/unifi-poller/README.md @@ -13,7 +13,7 @@ unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in In ## OPTIONS -`unifi-poller [-c ] [-h] [-v]` +`unifi-poller [-c ] [-j ] [-h] [-v]` -c, --config Provide a configuration file (instead of the default). @@ -21,6 +21,17 @@ unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in In -v, --version Display version and exit. + -j, --dumpjson + This is a debug option; use this when you are missing data in your graphs, + and/or you want to inspect the raw data coming from the controller. The + filter only accept two options: devices or clients. This will print a lot + of information. Recommend piping it into a file and/or into jq for better + visualization. This requires a working config file that; one that contains + working authentication details for a Unifi Controller. This only dumps + data for sites listed in the config file. The application exits after + printing the JSON payload; it does not daemonize or report to InfluxDB + with this option. + -h, --help Display usage and exit. diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go index 011738bd..be3f86cd 100644 --- a/cmd/unifi-poller/jsondebug.go +++ b/cmd/unifi-poller/jsondebug.go @@ -21,7 +21,7 @@ func (c *Config) DumpJSON(filter string) error { return err } controller.ErrorLog = func(m string, v ...interface{}) { - fmt.Fprintf(os.Stderr, m, v...) + fmt.Fprintf(os.Stderr, "[ERROR] "+m, v...) } // Log all errors to stderr. switch sites, err := filterSites(controller, c.Sites); { @@ -36,10 +36,6 @@ func (c *Config) DumpJSON(filter string) error { } } -func errorLog(m string, v ...interface{}) { - fmt.Fprintf(os.Stderr, m, v...) -} - // DumpClientsJSON prints the raw json for clients in a Unifi Controller. func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) error { for _, s := range sites { From bac02d938a6d2b4d7730e718e8e57b694c0b16fb Mon Sep 17 00:00:00 2001 From: unifi-poller-bot Date: Wed, 12 Jun 2019 21:02:40 -0700 Subject: [PATCH 03/12] Split things up a bit --- cmd/unifi-poller/config.go | 8 +- cmd/unifi-poller/helpers.go | 35 +++++ cmd/unifi-poller/main.go | 255 ++++++++---------------------------- cmd/unifi-poller/unifi.go | 132 +++++++++++++++++++ 4 files changed, 225 insertions(+), 205 deletions(-) create mode 100644 cmd/unifi-poller/helpers.go create mode 100644 cmd/unifi-poller/unifi.go diff --git a/cmd/unifi-poller/config.go b/cmd/unifi-poller/config.go index 203a88fd..089a8520 100644 --- a/cmd/unifi-poller/config.go +++ b/cmd/unifi-poller/config.go @@ -3,6 +3,7 @@ package main import ( "time" + influx "github.com/influxdata/influxdb1-client/v2" "github.com/spf13/pflag" ) @@ -21,13 +22,18 @@ const ( defaultUnifURL = "https://127.0.0.1:8443" ) +// Asset is used to give all devices and clients a common interface. +type Asset interface { + Points() ([]*influx.Point, error) +} + // UnifiPoller contains the application startup data. type UnifiPoller struct { ConfigFile string DumpJSON string ShowVer bool + Flag *pflag.FlagSet *Config - Flag *pflag.FlagSet } // Config represents the data needed to poll a controller and report to influxdb. diff --git a/cmd/unifi-poller/helpers.go b/cmd/unifi-poller/helpers.go new file mode 100644 index 00000000..df9fa5ca --- /dev/null +++ b/cmd/unifi-poller/helpers.go @@ -0,0 +1,35 @@ +package main + +import ( + "log" + "strings" +) + +// hasErr checks a list of errors for a non-nil. +func hasErr(errs []error) bool { + for _, err := range errs { + if err != nil { + return true + } + } + return false +} + +// logErrors writes a slice of errors, with a prefix, to log-out. +func logErrors(errs []error, prefix string) { + for _, err := range errs { + if err != nil { + log.Println("[ERROR]", prefix+":", err.Error()) + } + } +} + +// StringInSlice returns true if a string is in a slice. +func StringInSlice(str string, slc []string) bool { + for _, s := range slc { + if strings.EqualFold(s, str) { + return true + } + } + return false +} diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index 562dfc22..54f635f9 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -5,8 +5,6 @@ import ( "io/ioutil" "log" "os" - "strings" - "time" "github.com/golift/unifi" influx "github.com/influxdata/influxdb1-client/v2" @@ -15,11 +13,6 @@ import ( flag "github.com/spf13/pflag" ) -// Asset is used to give all devices and clients a common interface. -type Asset interface { - Points() ([]*influx.Point, error) -} - func main() { u := &UnifiPoller{} if u.ParseFlags(os.Args[1:]); u.ShowVer { @@ -30,19 +23,62 @@ func main() { u.Flag.Usage() log.Fatalf("[ERROR] config file '%v': %v", u.ConfigFile, err) } - if u.DumpJSON != "" { - if err := u.Config.DumpJSON(u.DumpJSON); err != nil { - log.Fatalln("[ERROR] dumping JSON:", err) - } - return - } - if err := u.Config.Run(); err != nil { + if err := u.Run(); err != nil { log.Fatalln("[ERROR]", err) } } +// ParseFlags runs the parser. +func (u *UnifiPoller) ParseFlags(args []string) { + u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) + u.Flag.Usage = func() { + fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") + u.Flag.PrintDefaults() + } + u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", "This debug option prints the json payload for a device and exits.") + u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") + u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") + _ = u.Flag.Parse(args) +} + +// GetConfig parses and returns our configuration data. +func (u *UnifiPoller) GetConfig() error { + // Preload our defaults. + u.Config = &Config{ + InfluxURL: defaultInfxURL, + InfluxUser: defaultInfxUser, + InfluxPass: defaultInfxPass, + InfluxDB: defaultInfxDb, + UnifiUser: defaultUnifUser, + UnifiPass: os.Getenv("UNIFI_PASSWORD"), + UnifiBase: defaultUnifURL, + Interval: Dur{value: defaultInterval}, + Sites: []string{"default"}, + } + if buf, err := ioutil.ReadFile(u.ConfigFile); 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 { + return err + } + if u.DumpJSON != "" { + u.Quiet = true + } + if !u.Config.Quiet { + log.Println("Loaded Configuration:", u.ConfigFile) + } + return nil +} + // Run invokes all the application logic and routines. -func (c *Config) Run() error { +func (u *UnifiPoller) Run() error { + c := u.Config + if u.DumpJSON != "" { + if err := c.DumpJSON(u.DumpJSON); err != nil { + log.Fatalln("[ERROR] dumping JSON:", err) + } + return nil + } log.Println("Unifi-Poller Starting Up! PID:", os.Getpid()) // Create an authenticated session to the Unifi Controller. controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) @@ -79,192 +115,3 @@ func (c *Config) Run() error { c.PollUnifiController(controller, infdb) return nil } - -// ParseFlags runs the parser. -func (u *UnifiPoller) ParseFlags(args []string) { - u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) - u.Flag.Usage = func() { - fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") - u.Flag.PrintDefaults() - } - u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", "This debug option prints the json payload for a device and exits.") - u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") - u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") - _ = u.Flag.Parse(args) -} - -// CheckSites makes sure the list of provided sites exists on the controller. -func (c *Config) CheckSites(controller *unifi.Unifi) error { - sites, err := controller.GetSites() - if err != nil { - return err - } - if !c.Quiet { - msg := []string{} - for _, site := range sites { - msg = append(msg, site.Name+" ("+site.Desc+")") - } - log.Printf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) - } - if StringInSlice("all", c.Sites) { - return nil - } -FIRST: - for _, s := range c.Sites { - for _, site := range sites { - if s == site.Name { - continue FIRST - } - } - return errors.Errorf("configured site not found on controller: %v", s) - } - return nil -} - -// GetConfig parses and returns our configuration data. -func (u *UnifiPoller) GetConfig() error { - // Preload our defaults. - u.Config = &Config{ - InfluxURL: defaultInfxURL, - InfluxUser: defaultInfxUser, - InfluxPass: defaultInfxPass, - InfluxDB: defaultInfxDb, - UnifiUser: defaultUnifUser, - UnifiPass: os.Getenv("UNIFI_PASSWORD"), - UnifiBase: defaultUnifURL, - Interval: Dur{value: defaultInterval}, - Sites: []string{"default"}, - } - if buf, err := ioutil.ReadFile(u.ConfigFile); 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 { - return err - } - if u.DumpJSON != "" { - u.Quiet = true - } - if !u.Config.Quiet { - log.Println("Loaded Configuration:", u.ConfigFile) - } - return nil -} - -// PollUnifiController runs forever, polling and pushing. -func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) { - log.Println("[INFO] Everything checks out! Beginning Poller Routine.") - ticker := time.NewTicker(c.Interval.value) - - for range ticker.C { - sites, err := filterSites(controller, c.Sites) - if err != nil { - logErrors([]error{err}, "uni.GetSites()") - } - // Get all the points. - clients, err := controller.GetClients(sites) - if err != nil { - logErrors([]error{err}, "uni.GetClients()") - } - devices, err := controller.GetDevices(sites) - if err != nil { - logErrors([]error{err}, "uni.GetDevices()") - } - bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB}) - if err != nil { - logErrors([]error{err}, "influx.NewBatchPoints") - continue - } - // Batch all the points. - if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) { - logErrors(errs, "asset.Points()") - } - if err := infdb.Write(bp); err != nil { - logErrors([]error{err}, "infdb.Write(bp)") - } - if !c.Quiet { - log.Printf("[INFO] Logged Unifi States. Sites: %d Clients: %d, Wireless APs: %d, Gateways: %d, Switches: %d", - len(sites), len(clients.UCLs), len(devices.UAPs), len(devices.USGs), len(devices.USWs)) - } - } -} - -// filterSites returns a list of sites to fetch data for. -// Omits requested but unconfigured sites. -func filterSites(controller *unifi.Unifi, filter []string) ([]unifi.Site, error) { - sites, err := controller.GetSites() - if err != nil { - return nil, err - } else if len(filter) < 1 || StringInSlice("all", filter) { - return sites, nil - } - var i int - for _, s := range sites { - // Only include valid sites in the request filter. - if StringInSlice(s.Name, filter) { - sites[i] = s - i++ - } - } - return sites[:i], nil -} - -// batchPoints combines all device and client data into influxdb data points. -func batchPoints(devices *unifi.Devices, clients *unifi.Clients, bp influx.BatchPoints) (errs []error) { - process := func(asset Asset) error { - if asset == nil { - return nil - } - influxPoints, err := asset.Points() - if err != nil { - return err - } - bp.AddPoints(influxPoints) - return nil - } - if devices != nil { - for _, asset := range devices.UAPs { - errs = append(errs, process(asset)) - } - for _, asset := range devices.USGs { - errs = append(errs, process(asset)) - } - for _, asset := range devices.USWs { - errs = append(errs, process(asset)) - } - } - if clients != nil { - for _, asset := range clients.UCLs { - errs = append(errs, process(asset)) - } - } - return -} - -// hasErr checks a list of errors for a non-nil. -func hasErr(errs []error) bool { - for _, err := range errs { - if err != nil { - return true - } - } - return false -} - -// logErrors writes a slice of errors, with a prefix, to log-out. -func logErrors(errs []error, prefix string) { - for _, err := range errs { - if err != nil { - log.Println("[ERROR]", prefix+":", err.Error()) - } - } -} - -// StringInSlice returns true if a string is in a slice. -func StringInSlice(str string, slc []string) bool { - for _, s := range slc { - if strings.EqualFold(s, str) { - return true - } - } - return false -} diff --git a/cmd/unifi-poller/unifi.go b/cmd/unifi-poller/unifi.go new file mode 100644 index 00000000..3db402c6 --- /dev/null +++ b/cmd/unifi-poller/unifi.go @@ -0,0 +1,132 @@ +package main + +import ( + "log" + "strings" + "time" + + "github.com/golift/unifi" + influx "github.com/influxdata/influxdb1-client/v2" + "github.com/pkg/errors" +) + +// CheckSites makes sure the list of provided sites exists on the controller. +func (c *Config) CheckSites(controller *unifi.Unifi) error { + sites, err := controller.GetSites() + if err != nil { + return err + } + if !c.Quiet { + msg := []string{} + for _, site := range sites { + msg = append(msg, site.Name+" ("+site.Desc+")") + } + log.Printf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) + } + if StringInSlice("all", c.Sites) { + return nil + } +FIRST: + for _, s := range c.Sites { + for _, site := range sites { + if s == site.Name { + continue FIRST + } + } + return errors.Errorf("configured site not found on controller: %v", s) + } + return nil +} + +// PollUnifiController runs forever, polling and pushing. +func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) { + log.Println("[INFO] Everything checks out! Beginning Poller Routine.") + ticker := time.NewTicker(c.Interval.value) + + for range ticker.C { + // Get the sites we care about. + sites, err := filterSites(controller, c.Sites) + if err != nil { + logErrors([]error{err}, "uni.GetSites()") + } + // Get all the points. + clients, err := controller.GetClients(sites) + if err != nil { + logErrors([]error{err}, "uni.GetClients()") + } + devices, err := controller.GetDevices(sites) + if err != nil { + logErrors([]error{err}, "uni.GetDevices()") + } + // Make a new Points Batcher. + bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB}) + if err != nil { + logErrors([]error{err}, "influx.NewBatchPoints") + continue + } + // Batch (and send) all the points. + if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) { + logErrors(errs, "asset.Points()") + } + if err := infdb.Write(bp); err != nil { + logErrors([]error{err}, "infdb.Write(bp)") + } + // Talk about the data. + if !c.Quiet { + log.Printf("[INFO] Logged Unifi States. Sites: %d Clients: %d, Wireless APs: %d, Gateways: %d, Switches: %d", + len(sites), len(clients.UCLs), len(devices.UAPs), len(devices.USGs), len(devices.USWs)) + } + } +} + +// batchPoints combines all device and client data into influxdb data points. +func batchPoints(devices *unifi.Devices, clients *unifi.Clients, bp influx.BatchPoints) (errs []error) { + process := func(asset Asset) error { + if asset == nil { + return nil + } + influxPoints, err := asset.Points() + if err != nil { + return err + } + bp.AddPoints(influxPoints) + return nil + } + if devices != nil { + for _, asset := range devices.UAPs { + errs = append(errs, process(asset)) + } + for _, asset := range devices.USGs { + errs = append(errs, process(asset)) + } + for _, asset := range devices.USWs { + errs = append(errs, process(asset)) + } + } + if clients != nil { + for _, asset := range clients.UCLs { + errs = append(errs, process(asset)) + } + } + return +} + +// filterSites returns a list of sites to fetch data for. +// Omits requested but unconfigured sites. +func filterSites(controller *unifi.Unifi, filter []string) ([]unifi.Site, error) { + sites, err := controller.GetSites() + if err != nil { + return nil, err + } else if len(filter) < 1 || StringInSlice("all", filter) { + return sites, nil + } + var i int + for _, s := range sites { + // Only include valid sites in the request filter. + if StringInSlice(s.Name, filter) { + sites[i] = s + i++ + } + } + return sites[:i], nil +} From 77d7f8855d8cada33c34acb65c89eb12831522cd Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:19:44 -0700 Subject: [PATCH 04/12] fix build --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f009af4e..d483864b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,8 @@ before_install: - curl -sLo $GOPATH/bin/dep https://github.com/golang/dep/releases/download/v0.5.3/dep-darwin-amd64 - chmod +x $GOPATH/bin/dep # download super-linter: golangci-lint -- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest +- curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh +#- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest install: - dep ensure - rvm $brew_ruby do gem install --no-document fpm From 4dbfd58197d8127f6b93552a8f7aba7a658d037b Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:25:03 -0700 Subject: [PATCH 05/12] really fix build --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d483864b..635bb4c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ before_install: - curl -sLo $GOPATH/bin/dep https://github.com/golang/dep/releases/download/v0.5.3/dep-darwin-amd64 - chmod +x $GOPATH/bin/dep # download super-linter: golangci-lint -- curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh +- curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest #- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest install: - dep ensure From c9d0e26f3e38236ce6ff762c3f13fddafb80ccd0 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:31:14 -0700 Subject: [PATCH 06/12] fix long line --- cmd/unifi-poller/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index 54f635f9..adba6deb 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -35,7 +35,8 @@ func (u *UnifiPoller) ParseFlags(args []string) { fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") u.Flag.PrintDefaults() } - u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", "This debug option prints the json payload for a device and exits.") + u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", + "This debug option prints the json payload for a device and exits.") u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") _ = u.Flag.Parse(args) From 915610226dbf60928888678cd70d5f70362ec0c8 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:53:52 -0700 Subject: [PATCH 07/12] Fix up the code. --- cmd/unifi-poller/README.md | 4 +-- cmd/unifi-poller/jsondebug.go | 53 +++++++++++++++++------------------ 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/cmd/unifi-poller/README.md b/cmd/unifi-poller/README.md index 2b8e8159..a9dbebf3 100644 --- a/cmd/unifi-poller/README.md +++ b/cmd/unifi-poller/README.md @@ -24,9 +24,9 @@ unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in In -j, --dumpjson This is a debug option; use this when you are missing data in your graphs, and/or you want to inspect the raw data coming from the controller. The - filter only accept two options: devices or clients. This will print a lot + filter only accepts two options: devices or clients. This will print a lot of information. Recommend piping it into a file and/or into jq for better - visualization. This requires a working config file that; one that contains + visualization. This requires a valid config file that; one that contains working authentication details for a Unifi Controller. This only dumps data for sites listed in the config file. The application exits after printing the JSON payload; it does not daemonize or report to InfluxDB diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go index be3f86cd..545d29a1 100644 --- a/cmd/unifi-poller/jsondebug.go +++ b/cmd/unifi-poller/jsondebug.go @@ -28,9 +28,9 @@ func (c *Config) DumpJSON(filter string) error { case err != nil: return err case StringInSlice(filter, []string{"d", "device", "devices"}): - return c.DumpClientsJSON(sites, controller) - case StringInSlice(filter, []string{"client", "clients", "c"}): return c.DumpDeviceJSON(sites, controller) + case StringInSlice(filter, []string{"client", "clients", "c"}): + return c.DumpClientsJSON(sites, controller) default: return errors.New("must provide filter: devices, clients") } @@ -40,21 +40,9 @@ func (c *Config) DumpJSON(filter string) error { func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) error { for _, s := range sites { path := fmt.Sprintf(unifi.ClientPath, s.Name) - req, err := controller.UniReq(path, "") - if err != nil { + if err := dumpJSON(path, "Client", s, controller); err != nil { return err } - resp, err := controller.Do(req) - if err != nil { - return err - } - body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() - if err != nil { - return err - } - fmt.Fprintf(os.Stderr, "Dumping Client JSON for site %s (%s)\n", s.Desc, s.Name) - fmt.Println(string(body)) } return nil } @@ -63,21 +51,30 @@ func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) er func (c *Config) DumpDeviceJSON(sites []unifi.Site, controller *unifi.Unifi) error { for _, s := range sites { path := fmt.Sprintf(unifi.DevicePath, s.Name) - req, err := controller.UniReq(path, "") - if err != nil { + if err := dumpJSON(path, "Device", s, controller); err != nil { return err } - resp, err := controller.Do(req) - if err != nil { - return err - } - body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() - if err != nil { - return err - } - fmt.Fprintf(os.Stderr, "Dumping Device JSON for site %s (%s)\n", s.Desc, s.Name) - fmt.Println(string(body)) } return nil } + +func dumpJSON(path, what string, site unifi.Site, controller *unifi.Unifi) error { + req, err := controller.UniReq(path, "") + if err != nil { + return err + } + resp, err := controller.Do(req) + if err != nil { + return err + } + defer func() { + _ = resp.Body.Close() + }() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + fmt.Fprintf(os.Stderr, "Dumping %s JSON for site %s (%s)\n", what, site.Desc, site.Name) + fmt.Println(string(body)) + return nil +} From 4606f95384396d3423590d1331c5c72219535fcb Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:56:46 -0700 Subject: [PATCH 08/12] minor --- cmd/unifi-poller/jsondebug.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go index 545d29a1..a7fa3d57 100644 --- a/cmd/unifi-poller/jsondebug.go +++ b/cmd/unifi-poller/jsondebug.go @@ -24,7 +24,8 @@ func (c *Config) DumpJSON(filter string) error { fmt.Fprintf(os.Stderr, "[ERROR] "+m, v...) } // Log all errors to stderr. - switch sites, err := filterSites(controller, c.Sites); { + sites, err := filterSites(controller, c.Sites) + switch { case err != nil: return err case StringInSlice(filter, []string{"d", "device", "devices"}): From 091ba2bc68463bd8bbf408e15905ce6c0777c113 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 23:40:11 -0700 Subject: [PATCH 09/12] More cleanup. --- cmd/unifi-poller/main.go | 31 +++++++++++++++---------------- cmd/unifi-poller/unifi.go | 10 ++++++---- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index adba6deb..dbf9c11f 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -17,7 +17,7 @@ func main() { u := &UnifiPoller{} if u.ParseFlags(os.Args[1:]); u.ShowVer { fmt.Printf("unifi-poller v%s\n", Version) - os.Exit(0) // don't run anything else. + return // don't run anything else. } if err := u.GetConfig(); err != nil { u.Flag.Usage() @@ -66,7 +66,7 @@ func (u *UnifiPoller) GetConfig() error { u.Quiet = true } if !u.Config.Quiet { - log.Println("Loaded Configuration:", u.ConfigFile) + log.Println("[INFO] Loaded Configuration:", u.ConfigFile) } return nil } @@ -75,29 +75,28 @@ func (u *UnifiPoller) GetConfig() error { func (u *UnifiPoller) Run() error { c := u.Config if u.DumpJSON != "" { - if err := c.DumpJSON(u.DumpJSON); err != nil { - log.Fatalln("[ERROR] dumping JSON:", err) - } - return nil + return c.DumpJSON(u.DumpJSON) } - log.Println("Unifi-Poller Starting Up! PID:", os.Getpid()) + if log.SetFlags(0); c.Debug { + log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) + log.Println("[DEBUG] Debug Logging Enabled") + } + log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid()) // Create an authenticated session to the Unifi Controller. controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) if err != nil { return errors.Wrap(err, "unifi controller") } + if c.Debug { + controller.DebugLog = log.Printf // Log debug messages. + } + controller.ErrorLog = log.Printf // Log all errors. if !c.Quiet { - log.Println("Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) + log.Println("[INFO] Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) } if err := c.CheckSites(controller); err != nil { return err } - controller.ErrorLog = log.Printf // Log all errors. - if log.SetFlags(0); c.Debug { - log.Println("Debug Logging Enabled") - log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) - controller.DebugLog = log.Printf // Log debug messages. - } infdb, err := influx.NewHTTPClient(influx.HTTPConfig{ Addr: c.InfluxURL, Username: c.InfluxUser, @@ -110,8 +109,8 @@ func (u *UnifiPoller) Run() error { // Doing it this way allows debug error logs (line numbers, etc) controller.DebugLog = nil } else { - log.Println("Logging Unifi Metrics to InfluXDB @", c.InfluxURL, "as user", c.InfluxUser) - log.Printf("Polling Unifi Controller (sites %v), interval: %v", c.Sites, c.Interval.value) + log.Println("[INFO] Polling Unifi Controller Sites:", c.Sites) + log.Println("[INFO] Logging Measurements to InfluxDB at", c.InfluxURL, "as user", c.InfluxUser) } c.PollUnifiController(controller, infdb) return nil diff --git a/cmd/unifi-poller/unifi.go b/cmd/unifi-poller/unifi.go index 3db402c6..83a55136 100644 --- a/cmd/unifi-poller/unifi.go +++ b/cmd/unifi-poller/unifi.go @@ -21,7 +21,7 @@ func (c *Config) CheckSites(controller *unifi.Unifi) error { for _, site := range sites { msg = append(msg, site.Name+" ("+site.Desc+")") } - log.Printf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) + log.Printf("[INFO] Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) } if StringInSlice("all", c.Sites) { return nil @@ -40,7 +40,7 @@ FIRST: // PollUnifiController runs forever, polling and pushing. func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) { - log.Println("[INFO] Everything checks out! Beginning Poller Routine.") + log.Println("[INFO] Everything checks out! Poller started, interval:", c.Interval.value) ticker := time.NewTicker(c.Interval.value) for range ticker.C { @@ -73,8 +73,10 @@ func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Clien } // Talk about the data. if !c.Quiet { - log.Printf("[INFO] Logged Unifi States. Sites: %d Clients: %d, Wireless APs: %d, Gateways: %d, Switches: %d", - len(sites), len(clients.UCLs), len(devices.UAPs), len(devices.USGs), len(devices.USWs)) + log.Printf("[INFO] Unifi Measurements Recorded. Sites: %d Clients: %d, "+ + "Wireless APs: %d, Gateways: %d, Switches: %d, Metrics: %d", + len(sites), len(clients.UCLs), + len(devices.UAPs), len(devices.USGs), len(devices.USWs), len(bp.Points())) } } } From cfe22aed636f4c7f46e6db2b877779dea4602fb3 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 13 Jun 2019 00:08:46 -0700 Subject: [PATCH 10/12] Better output formatting. --- cmd/unifi-poller/helpers.go | 7 +++++ cmd/unifi-poller/jsondebug.go | 4 +-- cmd/unifi-poller/main.go | 58 ++++++++++++++++++++--------------- cmd/unifi-poller/unifi.go | 23 +++++++------- 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/cmd/unifi-poller/helpers.go b/cmd/unifi-poller/helpers.go index df9fa5ca..7bc5cd44 100644 --- a/cmd/unifi-poller/helpers.go +++ b/cmd/unifi-poller/helpers.go @@ -33,3 +33,10 @@ func StringInSlice(str string, slc []string) bool { } return false } + +// Logf prints a log entry if quiet is false. +func (c *Config) Logf(m string, v ...interface{}) { + if !c.Quiet { + log.Printf("[INFO] "+m, v...) + } +} diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go index a7fa3d57..3f200bf6 100644 --- a/cmd/unifi-poller/jsondebug.go +++ b/cmd/unifi-poller/jsondebug.go @@ -16,7 +16,7 @@ func (c *Config) DumpJSON(filter string) error { if err != nil { return err } - fmt.Fprintln(os.Stderr, "Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) + fmt.Fprintln(os.Stderr, "[INFO] Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) if err := c.CheckSites(controller); err != nil { return err } @@ -75,7 +75,7 @@ func dumpJSON(path, what string, site unifi.Site, controller *unifi.Unifi) error if err != nil { return err } - fmt.Fprintf(os.Stderr, "Dumping %s JSON for site %s (%s)\n", what, site.Desc, site.Name) + fmt.Fprintf(os.Stderr, "[INFO] Dumping %s JSON for site %s (%s)\n", what, site.Desc, site.Name) fmt.Println(string(body)) return nil } diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index dbf9c11f..238cc7b1 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -65,9 +65,7 @@ func (u *UnifiPoller) GetConfig() error { if u.DumpJSON != "" { u.Quiet = true } - if !u.Config.Quiet { - log.Println("[INFO] Loaded Configuration:", u.ConfigFile) - } + u.Config.Logf("Loaded Configuration: %s", u.ConfigFile) return nil } @@ -82,36 +80,46 @@ func (u *UnifiPoller) Run() error { log.Println("[DEBUG] Debug Logging Enabled") } log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid()) - // Create an authenticated session to the Unifi Controller. - controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) + controller, err := c.GetController() if err != nil { - return errors.Wrap(err, "unifi controller") - } - if c.Debug { - controller.DebugLog = log.Printf // Log debug messages. - } - controller.ErrorLog = log.Printf // Log all errors. - if !c.Quiet { - log.Println("[INFO] Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) - } - if err := c.CheckSites(controller); err != nil { return err } + infdb, err := c.GetInfluxDB() + if err != nil { + return err + } + c.PollUnifiController(controller, infdb) + return nil +} + +func (c *Config) GetInfluxDB() (influx.Client, error) { infdb, err := influx.NewHTTPClient(influx.HTTPConfig{ Addr: c.InfluxURL, Username: c.InfluxUser, Password: c.InfluxPass, }) if err != nil { - return errors.Wrap(err, "influxdb") + return nil, errors.Wrap(err, "influxdb") } - if c.Quiet { - // Doing it this way allows debug error logs (line numbers, etc) - controller.DebugLog = nil - } else { - log.Println("[INFO] Polling Unifi Controller Sites:", c.Sites) - log.Println("[INFO] Logging Measurements to InfluxDB at", c.InfluxURL, "as user", c.InfluxUser) - } - c.PollUnifiController(controller, infdb) - return nil + c.Logf("Logging Measurements to InfluxDB at %s as user %s", c.InfluxURL, c.InfluxUser) + return infdb, nil +} + +func (c *Config) GetController() (*unifi.Unifi, error) { + // Create an authenticated session to the Unifi Controller. + controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) + if err != nil { + return nil, errors.Wrap(err, "unifi controller") + } + controller.ErrorLog = log.Printf // Log all errors. + // Doing it this way allows debug error logs (line numbers, etc) + if c.Debug && !c.Quiet { + controller.DebugLog = log.Printf // Log debug messages. + } + c.Logf("Authenticated to Unifi Controller at %s as user %s", c.UnifiBase, c.UnifiUser) + if err := c.CheckSites(controller); err != nil { + return nil, err + } + c.Logf("Polling Unifi Controller Sites: %v", c.Sites) + return controller, nil } diff --git a/cmd/unifi-poller/unifi.go b/cmd/unifi-poller/unifi.go index 83a55136..c1f4a734 100644 --- a/cmd/unifi-poller/unifi.go +++ b/cmd/unifi-poller/unifi.go @@ -16,13 +16,11 @@ func (c *Config) CheckSites(controller *unifi.Unifi) error { if err != nil { return err } - if !c.Quiet { - msg := []string{} - for _, site := range sites { - msg = append(msg, site.Name+" ("+site.Desc+")") - } - log.Printf("[INFO] Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) + msg := []string{} + for _, site := range sites { + msg = append(msg, site.Name+" ("+site.Desc+")") } + c.Logf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) if StringInSlice("all", c.Sites) { return nil } @@ -72,12 +70,15 @@ func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Clien logErrors([]error{err}, "infdb.Write(bp)") } // Talk about the data. - if !c.Quiet { - log.Printf("[INFO] Unifi Measurements Recorded. Sites: %d Clients: %d, "+ - "Wireless APs: %d, Gateways: %d, Switches: %d, Metrics: %d", - len(sites), len(clients.UCLs), - len(devices.UAPs), len(devices.USGs), len(devices.USWs), len(bp.Points())) + var fieldcount int + for _, p := range bp.Points() { + i, _ := p.Fields() + fieldcount += len(i) } + c.Logf("Unifi Measurements Recorded. Sites: %d Clients: %d, "+ + "Wireless APs: %d, Gateways: %d, Switches: %d, Points: %d, Fields: %d", + len(sites), len(clients.UCLs), + len(devices.UAPs), len(devices.USGs), len(devices.USWs), len(bp.Points()), fieldcount) } } From 32c3b3700093c7cf3e9c5b262aad56094de1720a Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 13 Jun 2019 00:16:13 -0700 Subject: [PATCH 11/12] change up counter --- cmd/unifi-poller/unifi.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/unifi-poller/unifi.go b/cmd/unifi-poller/unifi.go index c1f4a734..ff893dd9 100644 --- a/cmd/unifi-poller/unifi.go +++ b/cmd/unifi-poller/unifi.go @@ -70,15 +70,16 @@ func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Clien logErrors([]error{err}, "infdb.Write(bp)") } // Talk about the data. - var fieldcount int + var fieldcount, pointcount int for _, p := range bp.Points() { + pointcount++ i, _ := p.Fields() fieldcount += len(i) } c.Logf("Unifi Measurements Recorded. Sites: %d Clients: %d, "+ "Wireless APs: %d, Gateways: %d, Switches: %d, Points: %d, Fields: %d", len(sites), len(clients.UCLs), - len(devices.UAPs), len(devices.USGs), len(devices.USWs), len(bp.Points()), fieldcount) + len(devices.UAPs), len(devices.USGs), len(devices.USWs), pointcount, fieldcount) } } From 1a0f70776376539526669af849b1b0291880ccb2 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 13 Jun 2019 00:48:16 -0700 Subject: [PATCH 12/12] Cleanup --- cmd/unifi-poller/config.go | 5 +++- cmd/unifi-poller/jsondebug.go | 39 ++++++++++++------------ cmd/unifi-poller/main.go | 56 +++++++++++++++++------------------ cmd/unifi-poller/unifi.go | 35 +++++++++++----------- 4 files changed, 69 insertions(+), 66 deletions(-) diff --git a/cmd/unifi-poller/config.go b/cmd/unifi-poller/config.go index 089a8520..26f5e4d7 100644 --- a/cmd/unifi-poller/config.go +++ b/cmd/unifi-poller/config.go @@ -3,6 +3,7 @@ package main import ( "time" + "github.com/golift/unifi" influx "github.com/influxdata/influxdb1-client/v2" "github.com/spf13/pflag" ) @@ -27,12 +28,14 @@ type Asset interface { Points() ([]*influx.Point, error) } -// UnifiPoller contains the application startup data. +// UnifiPoller contains the application startup data, and auth info for unifi & influx. type UnifiPoller struct { ConfigFile string DumpJSON string ShowVer bool Flag *pflag.FlagSet + influx.Client + *unifi.Unifi *Config } diff --git a/cmd/unifi-poller/jsondebug.go b/cmd/unifi-poller/jsondebug.go index 3f200bf6..1f89e496 100644 --- a/cmd/unifi-poller/jsondebug.go +++ b/cmd/unifi-poller/jsondebug.go @@ -9,39 +9,38 @@ import ( "github.com/pkg/errors" ) -// DumpJSON prints raw json from the Unifi Controller. -func (c *Config) DumpJSON(filter string) error { - c.Quiet = true - controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) +// DumpJSONPayload prints raw json from the Unifi Controller. +func (u *UnifiPoller) DumpJSONPayload() (err error) { + u.Quiet = true + u.Unifi, err = unifi.NewUnifi(u.UnifiUser, u.UnifiPass, u.UnifiBase, u.VerifySSL) if err != nil { return err } - fmt.Fprintln(os.Stderr, "[INFO] Authenticated to Unifi Controller @", c.UnifiBase, "as user", c.UnifiUser) - if err := c.CheckSites(controller); err != nil { + fmt.Fprintln(os.Stderr, "[INFO] Authenticated to Unifi Controller @", u.UnifiBase, "as user", u.UnifiUser) + if err := u.CheckSites(); err != nil { return err } - controller.ErrorLog = func(m string, v ...interface{}) { + u.Unifi.ErrorLog = func(m string, v ...interface{}) { fmt.Fprintf(os.Stderr, "[ERROR] "+m, v...) } // Log all errors to stderr. - sites, err := filterSites(controller, c.Sites) - switch { + switch sites, err := u.filterSites(u.Sites); { case err != nil: return err - case StringInSlice(filter, []string{"d", "device", "devices"}): - return c.DumpDeviceJSON(sites, controller) - case StringInSlice(filter, []string{"client", "clients", "c"}): - return c.DumpClientsJSON(sites, controller) + case StringInSlice(u.DumpJSON, []string{"d", "device", "devices"}): + return u.DumpDeviceJSON(sites) + case StringInSlice(u.DumpJSON, []string{"client", "clients", "c"}): + return u.DumpClientsJSON(sites) default: return errors.New("must provide filter: devices, clients") } } // DumpClientsJSON prints the raw json for clients in a Unifi Controller. -func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) error { +func (u *UnifiPoller) DumpClientsJSON(sites []unifi.Site) error { for _, s := range sites { path := fmt.Sprintf(unifi.ClientPath, s.Name) - if err := dumpJSON(path, "Client", s, controller); err != nil { + if err := u.dumpJSON(path, "Client", s); err != nil { return err } } @@ -49,22 +48,22 @@ func (c *Config) DumpClientsJSON(sites []unifi.Site, controller *unifi.Unifi) er } // DumpDeviceJSON prints the raw json for devices in a Unifi Controller. -func (c *Config) DumpDeviceJSON(sites []unifi.Site, controller *unifi.Unifi) error { +func (u *UnifiPoller) DumpDeviceJSON(sites []unifi.Site) error { for _, s := range sites { path := fmt.Sprintf(unifi.DevicePath, s.Name) - if err := dumpJSON(path, "Device", s, controller); err != nil { + if err := u.dumpJSON(path, "Device", s); err != nil { return err } } return nil } -func dumpJSON(path, what string, site unifi.Site, controller *unifi.Unifi) error { - req, err := controller.UniReq(path, "") +func (u *UnifiPoller) dumpJSON(path, what string, site unifi.Site) error { + req, err := u.UniReq(path, "") if err != nil { return err } - resp, err := controller.Do(req) + resp, err := u.Do(req) if err != nil { return err } diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index 238cc7b1..4c439aed 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -70,56 +70,56 @@ func (u *UnifiPoller) GetConfig() error { } // Run invokes all the application logic and routines. -func (u *UnifiPoller) Run() error { - c := u.Config +func (u *UnifiPoller) Run() (err error) { if u.DumpJSON != "" { - return c.DumpJSON(u.DumpJSON) + return u.DumpJSONPayload() } - if log.SetFlags(0); c.Debug { + if log.SetFlags(0); u.Debug { log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) log.Println("[DEBUG] Debug Logging Enabled") } log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid()) - controller, err := c.GetController() - if err != nil { + + if err = u.GetUnifi(); err != nil { return err } - infdb, err := c.GetInfluxDB() - if err != nil { + if err = u.GetInfluxDB(); err != nil { return err } - c.PollUnifiController(controller, infdb) + u.PollController() return nil } -func (c *Config) GetInfluxDB() (influx.Client, error) { - infdb, err := influx.NewHTTPClient(influx.HTTPConfig{ - Addr: c.InfluxURL, - Username: c.InfluxUser, - Password: c.InfluxPass, +// GetInfluxDB returns an influxdb interface. +func (u *UnifiPoller) GetInfluxDB() (err error) { + u.Client, err = influx.NewHTTPClient(influx.HTTPConfig{ + Addr: u.InfluxURL, + Username: u.InfluxUser, + Password: u.InfluxPass, }) if err != nil { - return nil, errors.Wrap(err, "influxdb") + return errors.Wrap(err, "influxdb") } - c.Logf("Logging Measurements to InfluxDB at %s as user %s", c.InfluxURL, c.InfluxUser) - return infdb, nil + u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.InfluxURL, u.InfluxUser) + return nil } -func (c *Config) GetController() (*unifi.Unifi, error) { +// GetUnifi returns a Unifi controller interface. +func (u *UnifiPoller) GetUnifi() (err error) { // Create an authenticated session to the Unifi Controller. - controller, err := unifi.NewUnifi(c.UnifiUser, c.UnifiPass, c.UnifiBase, c.VerifySSL) + u.Unifi, err = unifi.NewUnifi(u.UnifiUser, u.UnifiPass, u.UnifiBase, u.VerifySSL) if err != nil { - return nil, errors.Wrap(err, "unifi controller") + return errors.Wrap(err, "unifi controller") } - controller.ErrorLog = log.Printf // Log all errors. + u.Unifi.ErrorLog = log.Printf // Log all errors. // Doing it this way allows debug error logs (line numbers, etc) - if c.Debug && !c.Quiet { - controller.DebugLog = log.Printf // Log debug messages. + if u.Debug && !u.Quiet { + u.Unifi.DebugLog = log.Printf // Log debug messages. } - c.Logf("Authenticated to Unifi Controller at %s as user %s", c.UnifiBase, c.UnifiUser) - if err := c.CheckSites(controller); err != nil { - return nil, err + u.Logf("Authenticated to Unifi Controller at %s as user %s", u.UnifiBase, u.UnifiUser) + if err = u.CheckSites(); err != nil { + return err } - c.Logf("Polling Unifi Controller Sites: %v", c.Sites) - return controller, nil + u.Logf("Polling Unifi Controller Sites: %v", u.Sites) + return nil } diff --git a/cmd/unifi-poller/unifi.go b/cmd/unifi-poller/unifi.go index ff893dd9..63727adf 100644 --- a/cmd/unifi-poller/unifi.go +++ b/cmd/unifi-poller/unifi.go @@ -11,8 +11,8 @@ import ( ) // CheckSites makes sure the list of provided sites exists on the controller. -func (c *Config) CheckSites(controller *unifi.Unifi) error { - sites, err := controller.GetSites() +func (u *UnifiPoller) CheckSites() error { + sites, err := u.GetSites() if err != nil { return err } @@ -20,12 +20,12 @@ func (c *Config) CheckSites(controller *unifi.Unifi) error { for _, site := range sites { msg = append(msg, site.Name+" ("+site.Desc+")") } - c.Logf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) - if StringInSlice("all", c.Sites) { + u.Logf("Found %d site(s) on controller: %v", len(msg), strings.Join(msg, ", ")) + if StringInSlice("all", u.Sites) { return nil } FIRST: - for _, s := range c.Sites { + for _, s := range u.Sites { for _, site := range sites { if s == site.Name { continue FIRST @@ -36,28 +36,28 @@ FIRST: return nil } -// PollUnifiController runs forever, polling and pushing. -func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) { - log.Println("[INFO] Everything checks out! Poller started, interval:", c.Interval.value) - ticker := time.NewTicker(c.Interval.value) +// PollController runs forever, polling unifi, and pushing to influx. +func (u *UnifiPoller) PollController() { + log.Println("[INFO] Everything checks out! Poller started, interval:", u.Interval.value) + ticker := time.NewTicker(u.Interval.value) for range ticker.C { // Get the sites we care about. - sites, err := filterSites(controller, c.Sites) + sites, err := u.filterSites(u.Sites) if err != nil { logErrors([]error{err}, "uni.GetSites()") } // Get all the points. - clients, err := controller.GetClients(sites) + clients, err := u.GetClients(sites) if err != nil { logErrors([]error{err}, "uni.GetClients()") } - devices, err := controller.GetDevices(sites) + devices, err := u.GetDevices(sites) if err != nil { logErrors([]error{err}, "uni.GetDevices()") } // Make a new Points Batcher. - bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: c.InfluxDB}) + bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{Database: u.InfluxDB}) if err != nil { logErrors([]error{err}, "influx.NewBatchPoints") continue @@ -66,9 +66,10 @@ func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Clien if errs := batchPoints(devices, clients, bp); errs != nil && hasErr(errs) { logErrors(errs, "asset.Points()") } - if err := infdb.Write(bp); err != nil { + if err := u.Write(bp); err != nil { logErrors([]error{err}, "infdb.Write(bp)") } + // Talk about the data. var fieldcount, pointcount int for _, p := range bp.Points() { @@ -76,7 +77,7 @@ func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Clien i, _ := p.Fields() fieldcount += len(i) } - c.Logf("Unifi Measurements Recorded. Sites: %d Clients: %d, "+ + u.Logf("Unifi Measurements Recorded. Sites: %d Clients: %d, "+ "Wireless APs: %d, Gateways: %d, Switches: %d, Points: %d, Fields: %d", len(sites), len(clients.UCLs), len(devices.UAPs), len(devices.USGs), len(devices.USWs), pointcount, fieldcount) @@ -117,8 +118,8 @@ func batchPoints(devices *unifi.Devices, clients *unifi.Clients, bp influx.Batch // filterSites returns a list of sites to fetch data for. // Omits requested but unconfigured sites. -func filterSites(controller *unifi.Unifi, filter []string) ([]unifi.Site, error) { - sites, err := controller.GetSites() +func (u *UnifiPoller) filterSites(filter []string) ([]unifi.Site, error) { + sites, err := u.GetSites() if err != nil { return nil, err } else if len(filter) < 1 || StringInSlice("all", filter) {