From 21ce9eb9c3d50fa637be2c3c45766f15dfd3fe5f Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 12 Jun 2019 22:53:52 -0700 Subject: [PATCH] Fix up the code. --- .../influxunifi/cmd/unifi-poller/README.md | 4 +- .../influxunifi/cmd/unifi-poller/jsondebug.go | 53 +++++++++---------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/integrations/influxunifi/cmd/unifi-poller/README.md b/integrations/influxunifi/cmd/unifi-poller/README.md index 2b8e8159..a9dbebf3 100644 --- a/integrations/influxunifi/cmd/unifi-poller/README.md +++ b/integrations/influxunifi/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/integrations/influxunifi/cmd/unifi-poller/jsondebug.go b/integrations/influxunifi/cmd/unifi-poller/jsondebug.go index be3f86cd..545d29a1 100644 --- a/integrations/influxunifi/cmd/unifi-poller/jsondebug.go +++ b/integrations/influxunifi/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 +}