Fix up the code.

This commit is contained in:
David Newhall II 2019-06-12 22:53:52 -07:00
parent c256edc2b5
commit 21ce9eb9c3
2 changed files with 27 additions and 30 deletions

View File

@ -24,9 +24,9 @@ unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in In
-j, --dumpjson <filter>
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

View File

@ -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,6 +51,14 @@ 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)
if err := dumpJSON(path, "Device", s, controller); err != nil {
return err
}
}
return nil
}
func dumpJSON(path, what string, site unifi.Site, controller *unifi.Unifi) error {
req, err := controller.UniReq(path, "")
if err != nil {
return err
@ -71,13 +67,14 @@ func (c *Config) DumpDeviceJSON(sites []unifi.Site, controller *unifi.Unifi) err
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
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.Fprintf(os.Stderr, "Dumping %s JSON for site %s (%s)\n", what, site.Desc, site.Name)
fmt.Println(string(body))
}
return nil
}