Skip to content

Commit

Permalink
CLI works
Browse files Browse the repository at this point in the history
  • Loading branch information
platinummonkey committed Dec 23, 2022
1 parent db9bcd5 commit 3768c53
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/datadogunifi/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (u *DatadogUnifi) Enabled() bool {
return *u.Enable
}

func (u *DatadogUnifi) DebugOutput(l poller.Logger) (bool, error) {
func (u *DatadogUnifi) DebugOutput() (bool, error) {
if u == nil {
return true, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/influxunifi/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (u *InfluxUnifi) Enabled() bool {
return !u.Disable
}

func (u *InfluxUnifi) DebugOutput(l poller.Logger) (bool, error) {
func (u *InfluxUnifi) DebugOutput() (bool, error) {
if u == nil {
return true, nil
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/influxunifi/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func (u *InfluxUnifi) Logf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
u.Collector.Logf(msg, v...)
if u.Collector != nil {
u.Collector.Logf(msg, v...)
}
}

// LogErrorf logs an error message.
Expand All @@ -24,7 +26,9 @@ func (u *InfluxUnifi) LogErrorf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
u.Collector.LogErrorf(msg, v...)
if u.Collector != nil {
u.Collector.LogErrorf(msg, v...)
}
}

// LogDebugf logs a debug message.
Expand All @@ -34,5 +38,7 @@ func (u *InfluxUnifi) LogDebugf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
u.Collector.LogDebugf(msg, v...)
if u.Collector != nil {
u.Collector.LogDebugf(msg, v...)
}
}
18 changes: 11 additions & 7 deletions pkg/inputunifi/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (u *InputUnifi) Initialize(l poller.Logger) error {
return nil
}

func (u *InputUnifi) DebugInputs(l poller.Logger) (bool, error) {
func (u *InputUnifi) DebugInput() (bool, error) {
if u == nil || u.Config == nil {
return true, nil
}
Expand All @@ -70,23 +70,27 @@ func (u *InputUnifi) DebugInputs(l poller.Logger) (bool, error) {
u.logController(&u.Default)
}

ok := true
allOK := true
var allErrors error
for i, c := range u.Controllers {
if err := u.getUnifi(u.setControllerDefaults(c)); err != nil {
u.LogErrorf("Controller %d of %d Auth or Connection Error, retrying: %v", i+1, len(u.Controllers), err)
ok = false
allOK = false
if allErrors != nil {
err = fmt.Errorf("%v: %w", err, allErrors)
allErrors = fmt.Errorf("%v: %w", err, allErrors)
} else {
allErrors = err
}
continue
}

if err := u.checkSites(c); err != nil {
u.LogErrorf("checking sites on %s: %v", c.URL, err)
ok = false
allOK = false
if allErrors != nil {
err = fmt.Errorf("%v: %w", err, allErrors)
allErrors = fmt.Errorf("%v: %w", err, allErrors)
} else {
allErrors = err
}
continue
}
Expand All @@ -95,7 +99,7 @@ func (u *InputUnifi) DebugInputs(l poller.Logger) (bool, error) {
u.logController(c)
}

return ok, allErrors
return allOK, allErrors
}

func (u *InputUnifi) logController(c *Controller) {
Expand Down
12 changes: 9 additions & 3 deletions pkg/inputunifi/updateweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ func (u *InputUnifi) Logf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
u.Logger.Logf(msg, v...)
if u.Logger != nil {
u.Logger.Logf(msg, v...)
}
}

// LogErrorf logs an error message.
Expand All @@ -201,7 +203,9 @@ func (u *InputUnifi) LogErrorf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
u.Logger.LogErrorf(msg, v...)
if u.Logger != nil {
u.Logger.LogErrorf(msg, v...)
}
}

// LogDebugf logs a debug message.
Expand All @@ -211,5 +215,7 @@ func (u *InputUnifi) LogDebugf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
u.Logger.LogDebugf(msg, v...)
if u.Logger != nil {
u.Logger.LogDebugf(msg, v...)
}
}
2 changes: 1 addition & 1 deletion pkg/lokiunifi/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (l *Loki) Enabled() bool {
return !l.Disable
}

func (l *Loki) DebugOutput(logger poller.Logger) (bool, error) {
func (l *Loki) DebugOutput() (bool, error) {
if l == nil {
return true, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mysqlunifi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (p *plugin) Enabled() bool {
return !p.Disable
}

func (p *plugin) DebugOutput(l poller.Logger) (bool, error) {
func (p *plugin) DebugOutput() (bool, error) {
if p == nil {
return true, nil
}
Expand Down
54 changes: 54 additions & 0 deletions pkg/poller/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,57 @@ func (u *UnifiPoller) PrintPasswordHash() (err error) {

return err //nolint:wrapcheck
}

func (u *UnifiPoller) DebugIO() error {
inputSync.RLock()
defer inputSync.RUnlock()
outputSync.RLock()
defer outputSync.RUnlock()

allOK := true
var allErr error

u.Logf("Checking inputs...")
totalInputs := len(inputs)
for i, input := range inputs {
u.Logf("\t(%d/%d) Checking input %s...", i+1, totalInputs, input.Name)
ok, err := input.DebugInput()
if !ok {
u.LogErrorf("\t\t %s Failed: %v", input.Name, err)
allOK = false
} else {
u.Logf("\t\t %s is OK", input.Name)
}
if err != nil {
if allErr == nil {
allErr = err
} else {
allErr = fmt.Errorf("%v: %w", err, allErr)
}
}
}

u.Logf("Checking outputs...")
totalOutputs := len(outputs)
for i, output := range outputs {
u.Logf("\t(%d/%d) Checking output %s...", i+1, totalOutputs, output.Name)
ok, err := output.DebugOutput()
if !ok {
u.LogErrorf("\t\t %s Failed: %v", output.Name, err)
allOK = false
} else {
u.Logf("\t\t %s is OK", output.Name)
}
if err != nil {
if allErr == nil {
allErr = err
} else {
allErr = fmt.Errorf("%v: %w", err, allErr)
}
}
}
if !allOK {
u.LogErrorf("No all checks passed, please fix the logged issues.")
}
return allErr
}
1 change: 1 addition & 0 deletions pkg/poller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Flags struct {
DumpJSON string
HashPW string
ShowVer bool
DebugIO bool
*pflag.FlagSet
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/poller/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Input interface {
Metrics(*Filter) (*Metrics, error) // Called every time new metrics are requested.
Events(*Filter) (*Events, error) // This is new.
RawMetrics(*Filter) ([]byte, error)
DebugInputs(Logger) (bool, error)
DebugInput() (bool, error)
}

// InputPlugin describes an input plugin's consumable interface.
Expand Down
2 changes: 1 addition & 1 deletion pkg/poller/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Collect interface {
type OutputPlugin interface {
Run(Collect) error
Enabled() bool
DebugOutput(Logger) (bool, error)
DebugOutput() (bool, error)
}

// Output defines the output data for a metric exporter like influx or prometheus.
Expand Down
10 changes: 10 additions & 0 deletions pkg/poller/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (u *UnifiPoller) Start() error {
return err
}

if u.Flags.DebugIO {
err = u.DebugIO()
if err != nil {
os.Exit(1)
}
log.Fatal("Failed debug checks")
return err
}

return u.Run()
}

Expand All @@ -63,6 +72,7 @@ func (f *Flags) Parse(args []string) {
"This option bcrypts a provided string. Useful for the webserver password. Use - to be prompted.")
f.StringVarP(&f.DumpJSON, "dumpjson", "j", "",
"This debug option prints a json payload and exits. See man page for more info.")
f.BoolVarP(&f.DebugIO, "debugio", "d", false, "Debug the Inputs and Outputs configured and exit.")
f.StringVarP(&f.ConfigFile, "config", "c", DefaultConfFile(),
"Poller config file path. Separating multiple paths with a comma will load the first config file found.")
f.BoolVarP(&f.ShowVer, "version", "v", false, "Print the version and exit.")
Expand Down
14 changes: 7 additions & 7 deletions pkg/promunifi/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -118,7 +117,7 @@ func init() { // nolint: gochecknoinits
})
}

func (u *promUnifi) DebugOutput(l poller.Logger) (bool, error) {
func (u *promUnifi) DebugOutput() (bool, error) {
if u == nil {
return true, nil
}
Expand All @@ -129,16 +128,17 @@ func (u *promUnifi) DebugOutput(l poller.Logger) (bool, error) {
return false, fmt.Errorf("invalid listen string")
}
// check the port
httpListenURL, err := url.Parse(u.HTTPListen)
if err != nil {
return false, err
parts := strings.Split(u.HTTPListen, ":")
if len(parts) != 2 {
return false, fmt.Errorf("invalid listen address: %s (must be of the form \"IP:Port\"", u.HTTPListen)
}
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%s", httpListenURL.Host, httpListenURL.Port()))

ln, err := net.Listen("tcp", u.HTTPListen)
if err != nil {
return false, err
}
_ = ln.Close()
return false, nil
return true, nil
}

func (u *promUnifi) Enabled() bool {
Expand Down
12 changes: 9 additions & 3 deletions pkg/promunifi/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func (u *promUnifi) Logf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
u.Collector.Logf(msg, v...)
if u.Collector != nil {
u.Collector.Logf(msg, v...)
}
}

// LogErrorf logs an error message.
Expand All @@ -24,7 +26,9 @@ func (u *promUnifi) LogErrorf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
u.Collector.LogErrorf(msg, v...)
if u.Collector != nil {
u.Collector.LogErrorf(msg, v...)
}
}

// LogDebugf logs a debug message.
Expand All @@ -34,5 +38,7 @@ func (u *promUnifi) LogDebugf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
u.Collector.LogDebugf(msg, v...)
if u.Collector != nil {
u.Collector.LogDebugf(msg, v...)
}
}
12 changes: 9 additions & 3 deletions pkg/webserver/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func (s *Server) Logf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
s.Collect.Logf(msg, v...)
if s.Collect != nil {
s.Collect.Logf(msg, v...)
}
}

// LogErrorf logs an error message.
Expand All @@ -22,7 +24,9 @@ func (s *Server) LogErrorf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "error"},
})
s.Collect.LogErrorf(msg, v...)
if s.Collect != nil {
s.Collect.LogErrorf(msg, v...)
}
}

// LogDebugf logs a debug message.
Expand All @@ -32,5 +36,7 @@ func (s *Server) LogDebugf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
s.Collect.LogDebugf(msg, v...)
if s.Collect != nil {
s.Collect.LogDebugf(msg, v...)
}
}
2 changes: 1 addition & 1 deletion pkg/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *Server) Run(c poller.Collect) error {
return s.Start()
}

func (s *Server) DebugOutput(l poller.Logger) (bool, error) {
func (s *Server) DebugOutput() (bool, error) {
if s == nil {
return true, nil
}
Expand Down

0 comments on commit 3768c53

Please sign in to comment.