auto fix lint rules
This commit is contained in:
		
							parent
							
								
									45a9e0d1eb
								
							
						
					
					
						commit
						9bdc6e8d0f
					
				|  | @ -140,7 +140,7 @@ func (u *DatadogUnifi) setConfigDefaults() { | |||
| 		u.options = append(u.options, statsd.WithNamespace(*u.Namespace)) | ||||
| 	} | ||||
| 
 | ||||
| 	if u.Tags != nil && len(u.Tags) > 0 { | ||||
| 	if len(u.Tags) > 0 { | ||||
| 		u.options = append(u.options, statsd.WithTags(u.Tags)) | ||||
| 	} | ||||
| 
 | ||||
|  | @ -378,7 +378,7 @@ func (u *DatadogUnifi) switchExport(r report, v any) { //nolint:cyclop | |||
| 	case *unifi.Event: | ||||
| 		u.batchEvent(r, v) | ||||
| 	case *unifi.IDS: | ||||
| 		u.batchIDS(r, v) | ||||
| 		u.batchIDs(r, v) | ||||
| 	case *unifi.Alarm: | ||||
| 		u.batchAlarms(r, v) | ||||
| 	case *unifi.Anomaly: | ||||
|  |  | |||
|  | @ -11,11 +11,11 @@ import ( | |||
| // These constants are used as names for printed/logged counters.
 | ||||
| const ( | ||||
| 	eventT = item("Event") | ||||
| 	idsT   = item("IDS") | ||||
| 	idsT   = item("IDs") | ||||
| ) | ||||
| 
 | ||||
| // batchIDS generates intrusion detection datapoints for Datadog.
 | ||||
| func (u *DatadogUnifi) batchIDS(r report, i *unifi.IDS) { // nolint:dupl
 | ||||
| // batchIDs generates intrusion detection datapoints for Datadog.
 | ||||
| func (u *DatadogUnifi) batchIDs(r report, i *unifi.IDS) { // nolint:dupl
 | ||||
| 	if time.Since(i.Datetime) > u.Interval.Duration+time.Second { | ||||
| 		return // The event is older than our interval, ignore it.
 | ||||
| 	} | ||||
|  |  | |||
|  | @ -9,11 +9,11 @@ import ( | |||
| // These constants are used as names for printed/logged counters.
 | ||||
| const ( | ||||
| 	eventT = item("Event") | ||||
| 	idsT   = item("IDS") | ||||
| 	idsT   = item("IDs") | ||||
| ) | ||||
| 
 | ||||
| // batchIDS generates intrusion detection datapoints for InfluxDB.
 | ||||
| func (u *InfluxUnifi) batchIDS(r report, i *unifi.IDS) { // nolint:dupl
 | ||||
| // batchIDs generates intrusion detection datapoints for InfluxDB.
 | ||||
| func (u *InfluxUnifi) batchIDs(r report, i *unifi.IDS) { // nolint:dupl
 | ||||
| 	if time.Since(i.Datetime) > u.Interval.Duration+time.Second { | ||||
| 		return // The event is older than our interval, ignore it.
 | ||||
| 	} | ||||
|  |  | |||
|  | @ -103,11 +103,11 @@ func (u *InfluxUnifi) PollController() { | |||
| 	interval := u.Interval.Round(time.Second) | ||||
| 	ticker := time.NewTicker(interval) | ||||
| 	version := "1" | ||||
| 	 | ||||
| 
 | ||||
| 	if u.IsVersion2 { | ||||
| 		version = "2" | ||||
| 	} | ||||
| 	 | ||||
| 
 | ||||
| 	u.Logf("Poller->InfluxDB started, version: %s, interval: %v, dp: %v, db: %s, url: %s, bucket: %s, org: %s", | ||||
| 		version, interval, u.DeadPorts, u.DB, u.URL, u.Bucket, u.Org) | ||||
| 
 | ||||
|  | @ -120,7 +120,7 @@ func (u *InfluxUnifi) Poll(interval time.Duration) { | |||
| 	metrics, err := u.Collector.Metrics(&poller.Filter{Name: "unifi"}) | ||||
| 	if err != nil { | ||||
| 		u.LogErrorf("metric fetch for InfluxDB failed: %v", err) | ||||
| 		 | ||||
| 
 | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
|  | @ -146,11 +146,11 @@ func (u *InfluxUnifi) Enabled() bool { | |||
| 	if u == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	 | ||||
| 
 | ||||
| 	if u.Config == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	 | ||||
| 
 | ||||
| 	return !u.Disable | ||||
| } | ||||
| 
 | ||||
|  | @ -158,13 +158,13 @@ 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) | ||||
|  | @ -175,7 +175,7 @@ func (u *InfluxUnifi) DebugOutput() (bool, error) { | |||
| 		tlsConfig := &tls.Config{InsecureSkipVerify: !u.VerifySSL} // nolint: gosec
 | ||||
| 		serverOptions := influx.DefaultOptions().SetTLSConfig(tlsConfig).SetBatchSize(u.BatchSize) | ||||
| 		u.InfluxV2Client = influx.NewClientWithOptions(u.URL, u.AuthToken, serverOptions) | ||||
| 		 | ||||
| 
 | ||||
| 		ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) | ||||
| 		defer cancel() | ||||
| 
 | ||||
|  | @ -210,7 +210,7 @@ func (u *InfluxUnifi) DebugOutput() (bool, error) { | |||
| // Run runs a ticker to poll the unifi server and update influxdb.
 | ||||
| func (u *InfluxUnifi) Run(c poller.Collect) error { | ||||
| 	u.Collector = c | ||||
| 	 | ||||
| 
 | ||||
| 	if !u.Enabled() { | ||||
| 		u.LogDebugf("InfluxDB config missing (or disabled), InfluxDB output disabled!") | ||||
| 
 | ||||
|  | @ -452,7 +452,7 @@ func (u *InfluxUnifi) switchExport(r report, v any) { //nolint:cyclop | |||
| 	case *unifi.Event: | ||||
| 		u.batchEvent(r, v) | ||||
| 	case *unifi.IDS: | ||||
| 		u.batchIDS(r, v) | ||||
| 		u.batchIDs(r, v) | ||||
| 	case *unifi.Alarm: | ||||
| 		u.batchAlarms(r, v) | ||||
| 	case *unifi.Anomaly: | ||||
|  |  | |||
|  | @ -12,7 +12,7 @@ import ( | |||
| 
 | ||||
| func (u *InputUnifi) collectControllerEvents(c *Controller) ([]any, error) { | ||||
| 	u.LogDebugf("Collecting controller events: %s (%s)", c.URL, c.ID) | ||||
| 	 | ||||
| 
 | ||||
| 	if u.isNill(c) { | ||||
| 		u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) | ||||
| 
 | ||||
|  | @ -34,7 +34,7 @@ func (u *InputUnifi) collectControllerEvents(c *Controller) ([]any, error) { | |||
| 
 | ||||
| 	type caller func([]any, []*unifi.Site, *Controller) ([]any, error) | ||||
| 
 | ||||
| 	for _, call := range []caller{u.collectIDS, u.collectAnomalies, u.collectAlarms, u.collectEvents} { | ||||
| 	for _, call := range []caller{u.collectIDs, u.collectAnomalies, u.collectAlarms, u.collectEvents} { | ||||
| 		if newLogs, err = call(logs, sites, c); err != nil { | ||||
| 			return logs, err | ||||
| 		} | ||||
|  | @ -123,9 +123,9 @@ func (u *InputUnifi) collectEvents(logs []any, sites []*unifi.Site, c *Controlle | |||
| 	return logs, nil | ||||
| } | ||||
| 
 | ||||
| func (u *InputUnifi) collectIDS(logs []any, sites []*unifi.Site, c *Controller) ([]any, error) { | ||||
| 	if *c.SaveIDS { | ||||
| 		u.LogDebugf("Collecting controller IDS data: %s (%s)", c.URL, c.ID) | ||||
| func (u *InputUnifi) collectIDs(logs []any, sites []*unifi.Site, c *Controller) ([]any, error) { | ||||
| 	if *c.SaveIDs { | ||||
| 		u.LogDebugf("Collecting controller IDs data: %s (%s)", c.URL, c.ID) | ||||
| 
 | ||||
| 		for _, s := range sites { | ||||
| 			events, err := c.Unifi.GetIDSSite(s) | ||||
|  | @ -159,7 +159,7 @@ func redactEvent(e *unifi.Event, hash *bool, dropPII *bool) *unifi.Event { | |||
| 	// metrics.Events[i].Msg <-- not sure what to do here.
 | ||||
| 	e.DestIPGeo = unifi.IPGeo{} | ||||
| 	e.SourceIPGeo = unifi.IPGeo{} | ||||
| 	 | ||||
| 
 | ||||
| 	if *dropPII { | ||||
| 		e.Host = "" | ||||
| 		e.Hostname = "" | ||||
|  |  | |||
|  | @ -38,7 +38,7 @@ type Controller struct { | |||
| 	SaveAnomal *bool        `json:"save_anomalies" toml:"save_anomalies" xml:"save_anomalies" yaml:"save_anomalies"` | ||||
| 	SaveAlarms *bool        `json:"save_alarms"    toml:"save_alarms"    xml:"save_alarms"    yaml:"save_alarms"` | ||||
| 	SaveEvents *bool        `json:"save_events"    toml:"save_events"    xml:"save_events"    yaml:"save_events"` | ||||
| 	SaveIDS    *bool        `json:"save_ids"       toml:"save_ids"       xml:"save_ids"       yaml:"save_ids"` | ||||
| 	SaveIDs    *bool        `json:"save_ids"       toml:"save_ids"       xml:"save_ids"       yaml:"save_ids"` | ||||
| 	SaveDPI    *bool        `json:"save_dpi"       toml:"save_dpi"       xml:"save_dpi"       yaml:"save_dpi"` | ||||
| 	SaveRogue  *bool        `json:"save_rogue"     toml:"save_rogue"     xml:"save_rogue"     yaml:"save_rogue"` | ||||
| 	HashPII    *bool        `json:"hash_pii"       toml:"hash_pii"       xml:"hash_pii"       yaml:"hash_pii"` | ||||
|  | @ -132,7 +132,7 @@ func (u *InputUnifi) getUnifi(c *Controller) error { | |||
| 	}) | ||||
| 	if err != nil { | ||||
| 		c.Unifi = nil | ||||
| 		 | ||||
| 
 | ||||
| 		return fmt.Errorf("unifi controller: %w", err) | ||||
| 	} | ||||
| 
 | ||||
|  | @ -231,8 +231,8 @@ func (u *InputUnifi) setDefaults(c *Controller) { //nolint:cyclop | |||
| 		c.SaveRogue = &f | ||||
| 	} | ||||
| 
 | ||||
| 	if c.SaveIDS == nil { | ||||
| 		c.SaveIDS = &f | ||||
| 	if c.SaveIDs == nil { | ||||
| 		c.SaveIDs = &f | ||||
| 	} | ||||
| 
 | ||||
| 	if c.SaveEvents == nil { | ||||
|  | @ -296,8 +296,8 @@ func (u *InputUnifi) setControllerDefaults(c *Controller) *Controller { //nolint | |||
| 		c.SaveDPI = u.Default.SaveDPI | ||||
| 	} | ||||
| 
 | ||||
| 	if c.SaveIDS == nil { | ||||
| 		c.SaveIDS = u.Default.SaveIDS | ||||
| 	if c.SaveIDs == nil { | ||||
| 		c.SaveIDs = u.Default.SaveIDs | ||||
| 	} | ||||
| 
 | ||||
| 	if c.SaveRogue == nil { | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ func (u *InputUnifi) Initialize(l poller.Logger) error { | |||
| 
 | ||||
| 	if u.Logger = l; u.Disable { | ||||
| 		u.Logf("UniFi input plugin disabled or missing configuration!") | ||||
| 		 | ||||
| 
 | ||||
| 		return nil | ||||
| 	} | ||||
| 
 | ||||
|  | @ -63,7 +63,7 @@ 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} | ||||
| 	} | ||||
|  | @ -80,7 +80,7 @@ func (u *InputUnifi) DebugInput() (bool, 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 { | ||||
|  | @ -94,7 +94,7 @@ func (u *InputUnifi) DebugInput() (bool, error) { | |||
| 
 | ||||
| 		if err := u.checkSites(c); err != nil { | ||||
| 			u.LogErrorf("checking sites on %s: %v", c.URL, err) | ||||
| 			 | ||||
| 
 | ||||
| 			allOK = false | ||||
| 
 | ||||
| 			if allErrors != nil { | ||||
|  | @ -127,12 +127,12 @@ func (u *InputUnifi) logController(c *Controller) { | |||
| 	u.Logf("   => Username: %s (has password: %v)", c.User, c.Pass != "") | ||||
| 	u.Logf("   => Hash PII %v / Drop PII %v / Poll Sites: %s", *c.HashPII, *c.DropPII, strings.Join(c.Sites, ", ")) | ||||
| 	u.Logf("   => Save Sites %v / Save DPI %v (metrics)", *c.SaveSites, *c.SaveDPI) | ||||
| 	u.Logf("   => Save Events %v / Save IDS %v (logs)", *c.SaveEvents, *c.SaveIDS) | ||||
| 	u.Logf("   => Save Events %v / Save IDs %v (logs)", *c.SaveEvents, *c.SaveIDs) | ||||
| 	u.Logf("   => Save Alarms %v / Anomalies %v (logs)", *c.SaveAlarms, *c.SaveAnomal) | ||||
| 	u.Logf("   => Save Rogue APs: %v", *c.SaveRogue) | ||||
| } | ||||
| 
 | ||||
| // Events allows you to pull only events (and IDS) from the UniFi Controller.
 | ||||
| // Events allows you to pull only events (and IDs) from the UniFi Controller.
 | ||||
| // This does not fully respect HashPII, but it may in the future!
 | ||||
| // Use Filter.Path to pick a specific controller, otherwise poll them all!
 | ||||
| func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { | ||||
|  |  | |||
|  | @ -44,7 +44,7 @@ func formatControllers(controllers []*Controller) []*Controller { | |||
| 			SaveAlarms: c.SaveAlarms, | ||||
| 			SaveRogue:  c.SaveRogue, | ||||
| 			SaveEvents: c.SaveEvents, | ||||
| 			SaveIDS:    c.SaveIDS, | ||||
| 			SaveIDs:    c.SaveIDs, | ||||
| 			SaveDPI:    c.SaveDPI, | ||||
| 			HashPII:    c.HashPII, | ||||
| 			DropPII:    c.DropPII, | ||||
|  | @ -191,7 +191,7 @@ func (u *InputUnifi) Logf(msg string, v ...any) { | |||
| 		Msg:  fmt.Sprintf(msg, v...), | ||||
| 		Tags: map[string]string{"type": "info"}, | ||||
| 	}) | ||||
| 	 | ||||
| 
 | ||||
| 	if u.Logger != nil { | ||||
| 		u.Logger.Logf(msg, v...) | ||||
| 	} | ||||
|  | @ -204,7 +204,7 @@ func (u *InputUnifi) LogErrorf(msg string, v ...any) { | |||
| 		Msg:  fmt.Sprintf(msg, v...), | ||||
| 		Tags: map[string]string{"type": "error"}, | ||||
| 	}) | ||||
| 	 | ||||
| 
 | ||||
| 	if u.Logger != nil { | ||||
| 		u.Logger.LogErrorf(msg, v...) | ||||
| 	} | ||||
|  | @ -217,7 +217,7 @@ func (u *InputUnifi) LogDebugf(msg string, v ...any) { | |||
| 		Msg:  fmt.Sprintf(msg, v...), | ||||
| 		Tags: map[string]string{"type": "debug"}, | ||||
| 	}) | ||||
| 	 | ||||
| 
 | ||||
| 	if u.Logger != nil { | ||||
| 		u.Logger.LogDebugf(msg, v...) | ||||
| 	} | ||||
|  |  | |||
|  | @ -48,7 +48,7 @@ func (r *Report) ProcessEventLogs(events *poller.Events) *Logs { | |||
| 	for _, e := range events.Logs { | ||||
| 		switch event := e.(type) { | ||||
| 		case *unifi.IDS: | ||||
| 			r.IDS(event, logs) | ||||
| 			r.IDs(event, logs) | ||||
| 		case *unifi.Event: | ||||
| 			r.Event(event, logs) | ||||
| 		case *unifi.Alarm: | ||||
|  | @ -65,7 +65,7 @@ func (r *Report) ProcessEventLogs(events *poller.Events) *Logs { | |||
| 
 | ||||
| func (r *Report) String() string { | ||||
| 	return fmt.Sprintf("%s: %d, %s: %d, %s: %d, %s: %d, Dur: %v", | ||||
| 		typeEvent, r.Counts[typeEvent], typeIDS, r.Counts[typeIDS], | ||||
| 		typeEvent, r.Counts[typeEvent], typeIDs, r.Counts[typeIDs], | ||||
| 		typeAlarm, r.Counts[typeAlarm], typeAnomaly, r.Counts[typeAnomaly], | ||||
| 		time.Since(r.Start).Round(time.Millisecond)) | ||||
| } | ||||
|  |  | |||
|  | @ -6,15 +6,15 @@ import ( | |||
| 	"github.com/unpoller/unifi" | ||||
| ) | ||||
| 
 | ||||
| const typeIDS = "IDS" | ||||
| const typeIDs = "IDs" | ||||
| 
 | ||||
| // event stores a structured event Event for batch sending to Loki.
 | ||||
| func (r *Report) IDS(event *unifi.IDS, logs *Logs) { | ||||
| func (r *Report) IDs(event *unifi.IDS, logs *Logs) { | ||||
| 	if event.Datetime.Before(r.Oldest) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	r.Counts[typeIDS]++ // increase counter and append new log line.
 | ||||
| 	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}}, | ||||
|  |  | |||
|  | @ -24,7 +24,7 @@ func NewTestSetup(t *testing.T) *TestRig { | |||
| 		SaveAnomal: &enabled, | ||||
| 		SaveAlarms: &enabled, | ||||
| 		SaveEvents: &enabled, | ||||
| 		SaveIDS:    &enabled, | ||||
| 		SaveIDs:    &enabled, | ||||
| 		SaveDPI:    &enabled, | ||||
| 		SaveRogue:  &enabled, | ||||
| 		SaveSites:  &enabled, | ||||
|  | @ -42,7 +42,7 @@ func NewTestSetup(t *testing.T) *TestRig { | |||
| 		Name:  "unifi", | ||||
| 		Input: in, | ||||
| 	}) | ||||
| 	 | ||||
| 
 | ||||
| 	return &TestRig{ | ||||
| 		MockServer: srv, | ||||
| 		Collector:  testCollector, | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue