Skip to content

Commit

Permalink
Merge pull request #486 from unpoller/debug-cli
Browse files Browse the repository at this point in the history
Adds DebugIO option to CLI
  • Loading branch information
platinummonkey committed Dec 23, 2022
2 parents a5f858c + 3768c53 commit ae9ae5f
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 18 deletions.
17 changes: 17 additions & 0 deletions pkg/datadogunifi/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package datadogunifi

import (
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -203,6 +204,22 @@ func (u *DatadogUnifi) Enabled() bool {
return *u.Enable
}

func (u *DatadogUnifi) DebugOutput() (bool, error) {
if u == nil {
return true, nil
}
if !u.Enabled() {
return true, nil
}
u.setConfigDefaults()
var err error
u.datadog, err = statsd.New(u.Address, u.options...)
if err != nil {
return false, fmt.Errorf("Error configuration Datadog agent reporting: %+v", err)
}
return true, nil
}

// Run runs a ticker to poll the unifi server and update Datadog.
func (u *DatadogUnifi) Run(c poller.Collect) error {
u.Collector = c
Expand Down
46 changes: 46 additions & 0 deletions pkg/influxunifi/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/unpoller/unifi"
"github.com/unpoller/unpoller/pkg/poller"
"github.com/unpoller/unpoller/pkg/webserver"
"golang.org/x/net/context"
"golift.io/cnfg"
)

Expand Down Expand Up @@ -142,6 +143,51 @@ func (u *InfluxUnifi) Enabled() bool {
return !u.Disable
}

func (u *InfluxUnifi) DebugOutput() (bool, error) {
if u == nil {
return true, nil
}
if !u.Enabled() {
return true, nil
}
u.setConfigDefaults()
_, err := url.Parse(u.Config.URL)
if err != nil {
return false, fmt.Errorf("invalid influx URL: %v", err)
}

if u.IsVersion2 {
// we're a version 2
tlsConfig := &tls.Config{InsecureSkipVerify: !u.VerifySSL} // nolint: gosec
serverOptions := influx.DefaultOptions().SetTLSConfig(tlsConfig).SetBatchSize(u.BatchSize)
u.influxV2 = influx.NewClientWithOptions(u.URL, u.AuthToken, serverOptions)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
ok, err := u.influxV2.Ping(ctx)
if err != nil {
return false, err
}
if !ok {
return false, fmt.Errorf("unsuccessful ping to influxdb2")
}
} else {
u.influxV1, err = influxV1.NewHTTPClient(influxV1.HTTPConfig{
Addr: u.URL,
Username: u.User,
Password: u.Pass,
TLSConfig: &tls.Config{InsecureSkipVerify: !u.VerifySSL}, // nolint: gosec
})
if err != nil {
return false, fmt.Errorf("making client: %w", err)
}
_, _, err = u.influxV1.Ping(time.Second * 2)
if err != nil {
return false, fmt.Errorf("unsuccessful ping to influxdb1")
}
}
return true, nil
}

// Run runs a ticker to poll the unifi server and update influxdb.
func (u *InfluxUnifi) Run(c poller.Collect) error {
u.Collector = c
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...)
}
}
45 changes: 45 additions & 0 deletions pkg/inputunifi/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,51 @@ func (u *InputUnifi) Initialize(l poller.Logger) error {
return nil
}

func (u *InputUnifi) DebugInput() (bool, error) {
if u == nil || u.Config == nil {
return true, nil
}
if u.setDefaults(&u.Default); len(u.Controllers) == 0 && !u.Dynamic {
u.Controllers = []*Controller{&u.Default}
}

if len(u.Controllers) == 0 {
u.Logf("No controllers configured. Polling dynamic controllers only! Defaults:")
u.logController(&u.Default)
}

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)
allOK = false
if allErrors != nil {
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)
allOK = false
if allErrors != nil {
allErrors = fmt.Errorf("%v: %w", err, allErrors)
} else {
allErrors = err
}
continue
}

u.Logf("Valid UniFi Controller %d of %d:", i+1, len(u.Controllers))
u.logController(c)
}

return allOK, allErrors
}

func (u *InputUnifi) logController(c *Controller) {
u.Logf(" => URL: %s (verify SSL: %v)", c.URL, *c.VerifySSL)

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...)
}
}
12 changes: 9 additions & 3 deletions pkg/lokiunifi/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func (l *Loki) Logf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "info"},
})
l.Collect.Logf(msg, v...)
if l.Collect != nil {
l.Collect.Logf(msg, v...)
}
}

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

// LogDebugf logs a debug message.
Expand All @@ -34,5 +38,7 @@ func (l *Loki) LogDebugf(msg string, v ...any) {
Msg: fmt.Sprintf(msg, v...),
Tags: map[string]string{"type": "debug"},
})
l.Collect.LogDebugf(msg, v...)
if l.Collect != nil {
l.Collect.LogDebugf(msg, v...)
}
}
22 changes: 20 additions & 2 deletions pkg/lokiunifi/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ func (l *Loki) Enabled() bool {
return !l.Disable
}

func (l *Loki) DebugOutput() (bool, error) {
if l == nil {
return true, nil
}
if !l.Enabled() {
return true, nil
}
if err := l.ValidateConfig(); err != nil {
return false, err
}
return true, nil
}

// Run is fired from the poller library after the Config is unmarshalled.
func (l *Loki) Run(collect poller.Collect) error {
l.Collect = collect
Expand All @@ -85,7 +98,10 @@ func (l *Loki) Run(collect poller.Collect) error {
}
l.Logf("Loki enabled")

l.ValidateConfig()
if err := l.ValidateConfig(); err != nil {
l.LogErrorf("invalid loki config")
return err
}

fake := *l.Config
fake.Password = strconv.FormatBool(fake.Password != "")
Expand All @@ -99,7 +115,7 @@ func (l *Loki) Run(collect poller.Collect) error {

// ValidateConfig sets initial "last" update time. Also creates an http client,
// makes sure URL is sane, and sets interval within min/max limits.
func (l *Loki) ValidateConfig() {
func (l *Loki) ValidateConfig() error {
if l.Interval.Duration > maxInterval {
l.Interval.Duration = maxInterval
} else if l.Interval.Duration < minInterval {
Expand All @@ -110,6 +126,7 @@ func (l *Loki) ValidateConfig() {
pass, err := os.ReadFile(strings.TrimPrefix(l.Password, "file://"))
if err != nil {
l.LogErrorf("Reading Loki Password File: %v", err)
return fmt.Errorf("error reading password file")
}

l.Password = strings.TrimSpace(string(pass))
Expand All @@ -118,6 +135,7 @@ func (l *Loki) ValidateConfig() {
l.last = time.Now().Add(-l.Interval.Duration)
l.client = l.httpClient()
l.URL = strings.TrimRight(l.URL, "/") // gets a path appended to it later.
return nil
}

// PollController runs forever, polling UniFi for events and pushing them to Loki.
Expand Down
2 changes: 1 addition & 1 deletion pkg/lokiunifi/report_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *Report) IDS(event *unifi.IDS, logs *Logs) {
}

r.Counts[typeIDS]++ // increase counter and append new log line.

logs.Streams = append(logs.Streams, LogStream{
Entries: [][]string{{strconv.FormatInt(event.Datetime.UnixNano(), 10), event.Msg}},
Labels: CleanLabels(map[string]string{
Expand Down
13 changes: 13 additions & 0 deletions pkg/mysqlunifi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func (p *plugin) Enabled() bool {
return !p.Disable
}

func (p *plugin) DebugOutput() (bool, error) {
if p == nil {
return true, nil
}
if !p.Enabled() {
return true, nil
}
if err := p.validateConfig(); err != nil {
return false, err
}
return true, nil
}

// Run gets called by poller core code. Return when the plugin stops working or has an error.
// In other words, don't run your code in a go routine, it already is.
func (p *plugin) Run(c poller.Collect) error {
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
1 change: 1 addition & 0 deletions pkg/poller/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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)
DebugInput() (bool, error)
}

// InputPlugin describes an input plugin's consumable interface.
Expand Down

0 comments on commit ae9ae5f

Please sign in to comment.