Better output formatting.

This commit is contained in:
David Newhall II 2019-06-13 00:08:46 -07:00
parent 091ba2bc68
commit cfe22aed63
4 changed files with 54 additions and 38 deletions

View File

@ -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...)
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}