From 6214a998cfbab9d2c41620e04bba32459303db68 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sat, 20 Jun 2020 03:41:06 -0700 Subject: [PATCH 01/18] Add events --- integrations/inputunifi/collector.go | 26 ++++++++++++--- integrations/inputunifi/go.mod | 4 +-- integrations/inputunifi/go.sum | 6 ++++ integrations/inputunifi/input.go | 49 +++++++++++----------------- integrations/inputunifi/interface.go | 39 ++++++++++++++-------- 5 files changed, 75 insertions(+), 49 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index fe3060fe..159e5191 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -105,8 +105,15 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { } } + if c.SaveEvents != nil && *c.SaveEvents { + m.Events, err = c.Unifi.GetEvents(m.Sites, time.Now().Add(time.Minute)) + if err != nil { + return nil, errors.Wrapf(err, "unifi.GetEvents(%s)", c.URL) + } + } + if c.SaveIDS != nil && *c.SaveIDS { - m.IDSList, err = c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute), time.Now()) + m.IDSList, err = c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute)) if err != nil { return nil, errors.Wrapf(err, "unifi.GetIDS(%s)", c.URL) } @@ -155,6 +162,17 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *pol devices[r.Mac] = r.Name } + for i := range metrics.Events { + if *c.HashPII { + metrics.Events[i].DestIPGeo = unifi.IPGeo{} + metrics.Events[i].SourceIPGeo = unifi.IPGeo{} + metrics.Events[i].Host = RedactMacPII(metrics.Events[i].Host, c.HashPII) + metrics.Events[i].Hostname = RedactMacPII(metrics.Events[i].Hostname, c.HashPII) + metrics.Events[i].DstMAC = RedactMacPII(metrics.Events[i].DstMAC, c.HashPII) + metrics.Events[i].SrcMAC = RedactMacPII(metrics.Events[i].SrcMAC, c.HashPII) + } + } + // These come blank, so set them here. for i, client := range metrics.Clients { if devices[client.Mac] = client.Name; client.Name == "" { @@ -191,7 +209,7 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *pol // RedactNamePII converts a name string to an md5 hash (first 24 chars only). // Useful for maskiing out personally identifying information. func RedactNamePII(pii string, hash *bool) string { - if hash == nil || !*hash { + if hash == nil || !*hash || pii == "" { return pii } @@ -203,7 +221,7 @@ func RedactNamePII(pii string, hash *bool) string { // RedactMacPII converts a MAC address to an md5 hashed version (first 14 chars only). // Useful for maskiing out personally identifying information. func RedactMacPII(pii string, hash *bool) (output string) { - if hash == nil || !*hash { + if hash == nil || !*hash || pii == "" { return pii } @@ -215,7 +233,7 @@ func RedactMacPII(pii string, hash *bool) (output string) { // getFilteredSites returns a list of sites to fetch data for. // Omits requested but unconfigured sites. Grabs the full list from the // controller and returns the sites provided in the config file. -func (u *InputUnifi) getFilteredSites(c *Controller) (unifi.Sites, error) { +func (u *InputUnifi) getFilteredSites(c *Controller) ([]*unifi.Site, error) { u.RLock() defer u.RUnlock() diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index aee2f49f..a10ae25d 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -5,6 +5,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 github.com/prometheus/procfs v0.1.2 // indirect - github.com/unifi-poller/poller v0.0.6 - github.com/unifi-poller/unifi v0.0.5 + github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9 + github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 64aed6f0..6ae35f2e 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -102,8 +102,14 @@ github.com/unifi-poller/poller v0.0.5 h1:qnofARTx0JveNc9PqGJmNUs7xsjwFqCWpS8pwDS github.com/unifi-poller/poller v0.0.5/go.mod h1:45TyAHk+xYF4KoFKaaZyjMmSdhbq2I1pLniYWfOXdHs= github.com/unifi-poller/poller v0.0.6 h1:KhOWUeYI029Nn/4NOIk/yblQ3tEd9QhS+u/8/S9ZoDs= github.com/unifi-poller/poller v0.0.6/go.mod h1:RkRJ4pAc2dAN8Xu9+VOumeE3BdN5QDQ3PC+jBx8hWW0= +github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9 h1:BYNOSvttR91a0F+ttG2ecD3DenVI9p9oi1W7m36zH98= +github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9/go.mod h1:q2tufLljemUR/blyhjIj+T0sdAOZo0kdlv3h79kEMgU= github.com/unifi-poller/unifi v0.0.4 h1:NDTxHTdF0MAt1y1RU8J+MSqdYlO0CEIqlrktcj3y/og= github.com/unifi-poller/unifi v0.0.4/go.mod h1:bTUtctrf56aapjKH+L+98eThBaVFbQXw5iNGZI0g/+E= +github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5 h1:DI1Dtq+2CY1t0Woesz7CRkbNrUT5GSX0h8mDsP3pgcw= +github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= +github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd h1:jrwuiY1AdoPi+b+R8zjt/e8h8ZqULNB9izcyQnf3pSw= +github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5 h1:Izeun32YxcQOeKZUXY0Sy4ltKYFuYxWGcN9JS6xkIJU= github.com/unifi-poller/unifi v0.0.5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/integrations/inputunifi/input.go b/integrations/inputunifi/input.go index 2a3ccbcc..7cdf95c9 100644 --- a/integrations/inputunifi/input.go +++ b/integrations/inputunifi/input.go @@ -3,9 +3,7 @@ package inputunifi import ( - "fmt" "io/ioutil" - "os" "strings" "sync" @@ -33,16 +31,17 @@ type InputUnifi struct { // Controller represents the configuration for a UniFi Controller. // Each polled controller may have its own configuration. type Controller struct { - VerifySSL *bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` - 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"` - HashPII *bool `json:"hash_pii" toml:"hash_pii" xml:"hash_pii" yaml:"hash_pii"` - SaveSites *bool `json:"save_sites" toml:"save_sites" xml:"save_sites" yaml:"save_sites"` - User string `json:"user" toml:"user" xml:"user" yaml:"user"` - Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"` - URL string `json:"url" toml:"url" xml:"url" yaml:"url"` - Sites []string `json:"sites,omitempty" toml:"sites,omitempty" xml:"site" yaml:"sites"` - Unifi *unifi.Unifi `json:"-" toml:"-" xml:"-" yaml:"-"` + VerifySSL *bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` + 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"` + SaveDPI *bool `json:"save_dpi" toml:"save_dpi" xml:"save_dpi" yaml:"save_dpi"` + HashPII *bool `json:"hash_pii" toml:"hash_pii" xml:"hash_pii" yaml:"hash_pii"` + SaveSites *bool `json:"save_sites" toml:"save_sites" xml:"save_sites" yaml:"save_sites"` + User string `json:"user" toml:"user" xml:"user" yaml:"user"` + Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"` + URL string `json:"url" toml:"url" xml:"url" yaml:"url"` + Sites []string `json:"sites,omitempty" toml:"sites,omitempty" xml:"site" yaml:"sites"` + Unifi *unifi.Unifi `json:"-" toml:"-" xml:"-" yaml:"-"` } // Config contains our configuration data. @@ -143,24 +142,6 @@ FIRST: return nil } -func (u *InputUnifi) dumpSitesJSON(c *Controller, path, name string, sites unifi.Sites) ([]byte, error) { - allJSON := []byte{} - - for _, s := range sites { - apiPath := fmt.Sprintf(path, s.Name) - _, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping %s: '%s' JSON for site: %s (%s):\n", name, apiPath, s.Desc, s.Name) - - body, err := c.Unifi.GetJSON(apiPath) - if err != nil { - return allJSON, err - } - - allJSON = append(allJSON, body...) - } - - return allJSON, nil -} - func (u *InputUnifi) getPassFromFile(filename string) string { b, err := ioutil.ReadFile(filename) if err != nil { @@ -196,6 +177,10 @@ func (u *InputUnifi) setDefaults(c *Controller) { c.SaveIDS = &f } + if c.SaveEvents == nil { + c.SaveEvents = &f + } + if c.URL == "" { c.URL = defaultURL } @@ -241,6 +226,10 @@ func (u *InputUnifi) setControllerDefaults(c *Controller) *Controller { c.SaveIDS = u.Default.SaveIDS } + if c.SaveEvents == nil { + c.SaveEvents = u.Default.SaveEvents + } + if c.URL == "" { c.URL = u.Default.URL } diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index dab0185e..2c668338 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -71,18 +71,14 @@ func (u *InputUnifi) logController(c *Controller) { u.Logf(" => Verify SSL: %v", *c.VerifySSL) u.Logf(" => Save DPI: %v", *c.SaveDPI) u.Logf(" => Save IDS: %v", *c.SaveIDS) + u.Logf(" => Save Events: %v", *c.SaveEvents) u.Logf(" => Save Sites: %v", *c.SaveSites) } // Metrics grabs all the measurements from a UniFi controller and returns them. -func (u *InputUnifi) Metrics() (*poller.Metrics, bool, error) { - return u.MetricsFrom(nil) -} - -// MetricsFrom grabs all the measurements from a UniFi controller and returns them. -func (u *InputUnifi) MetricsFrom(filter *poller.Filter) (*poller.Metrics, bool, error) { +func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { if u.Disable { - return nil, false, nil + return nil, nil } metrics := &poller.Metrics{} @@ -95,27 +91,26 @@ func (u *InputUnifi) MetricsFrom(filter *poller.Filter) (*poller.Metrics, bool, m, err := u.collectController(c) if err != nil { - return metrics, false, err + return metrics, err } metrics = poller.AppendMetrics(metrics, m) } if filter == nil || len(metrics.Clients) != 0 { - return metrics, true, nil + return metrics, nil } if !u.Dynamic { - return nil, false, errDynamicLookupsDisabled + return nil, errDynamicLookupsDisabled } // Attempt a dynamic metrics fetch from an unconfigured controller. - m, err := u.dynamicController(filter.Path) - - return m, err == nil && m != nil, err + return u.dynamicController(filter.Path) } // RawMetrics returns API output from the first configured unifi controller. +// Adjust filter.Unit to pull from a controller other than the first. func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { if l := len(u.Controllers); filter.Unit >= l { return nil, errors.Wrapf(errControllerNumNotFound, "%d controller(s) configured, '%d'", l, filter.Unit) @@ -151,3 +146,21 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { return []byte{}, errNoFilterKindProvided } } + +func (u *InputUnifi) dumpSitesJSON(c *Controller, path, name string, sites []*unifi.Site) ([]byte, error) { + allJSON := []byte{} + + for _, s := range sites { + apiPath := fmt.Sprintf(path, s.Name) + _, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping %s: '%s' JSON for site: %s (%s):\n", name, apiPath, s.Desc, s.Name) + + body, err := c.Unifi.GetJSON(apiPath) + if err != nil { + return allJSON, err + } + + allJSON = append(allJSON, body...) + } + + return allJSON, nil +} From a103be56d8c288d984087ffa38148adc37e5dc3b Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 02:40:56 -0700 Subject: [PATCH 02/18] Add ad-hoc Events method --- integrations/inputunifi/collector.go | 11 +++--- integrations/inputunifi/go.mod | 5 +-- integrations/inputunifi/go.sum | 36 +++--------------- integrations/inputunifi/interface.go | 57 ++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 41 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 159e5191..f0849650 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -134,7 +134,7 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { // augmentMetrics is our middleware layer between collecting metrics and writing them. // This is where we can manipuate the returned data or make arbitrary decisions. // This function currently adds parent device names to client metrics. -func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *poller.Metrics { +func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *poller.Metrics { // nolint: funlen if metrics == nil || metrics.Devices == nil || metrics.Clients == nil { return metrics } @@ -162,12 +162,13 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *pol devices[r.Mac] = r.Name } - for i := range metrics.Events { - if *c.HashPII { + if *c.HashPII { + for i := range metrics.Events { + // metrics.Events[i].Msg <-- not sure what to do here. metrics.Events[i].DestIPGeo = unifi.IPGeo{} metrics.Events[i].SourceIPGeo = unifi.IPGeo{} - metrics.Events[i].Host = RedactMacPII(metrics.Events[i].Host, c.HashPII) - metrics.Events[i].Hostname = RedactMacPII(metrics.Events[i].Hostname, c.HashPII) + metrics.Events[i].Host = RedactNamePII(metrics.Events[i].Host, c.HashPII) + metrics.Events[i].Hostname = RedactNamePII(metrics.Events[i].Hostname, c.HashPII) metrics.Events[i].DstMAC = RedactMacPII(metrics.Events[i].DstMAC, c.HashPII) metrics.Events[i].SrcMAC = RedactMacPII(metrics.Events[i].SrcMAC, c.HashPII) } diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index a10ae25d..5e36eeb5 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -4,7 +4,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 - github.com/prometheus/procfs v0.1.2 // indirect - github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9 - github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd + github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b + github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 6ae35f2e..23205549 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -28,8 +28,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= @@ -59,15 +57,12 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.4.0 h1:YVIb/fVcOTMSqtqZWSKnHpSLBxu8DKgxq8z6RuBZwqI= -github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_golang v1.6.0 h1:YVPodQOcK15POxhgARIvnDRVpLcuK8mglnMrWfyrw6A= github.com/prometheus/client_golang v1.6.0/go.mod h1:ZLOG9ck3JLRdB5MgO8f+lLTe83AXG6ro35rLTxvnIl4= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -76,19 +71,14 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1: github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= github.com/prometheus/common v0.10.0 h1:RyRA7RzGXQZiW+tGMr7sxa85G1z0yOpM1qq5c8lNawc= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8= -github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.1.1 h1:/ZKcW+ixpq2dOl4yeH4qvACNXnkiDCp5e/F5Tq07X7o= github.com/prometheus/procfs v0.1.1/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= -github.com/prometheus/procfs v0.1.2 h1:DS2izEFqEVp1gPg5yvEF/YzS6Ajo8YdKRJjQKiQm59A= -github.com/prometheus/procfs v0.1.2/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -98,20 +88,13 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/unifi-poller/poller v0.0.5 h1:qnofARTx0JveNc9PqGJmNUs7xsjwFqCWpS8pwDSjW78= -github.com/unifi-poller/poller v0.0.5/go.mod h1:45TyAHk+xYF4KoFKaaZyjMmSdhbq2I1pLniYWfOXdHs= -github.com/unifi-poller/poller v0.0.6 h1:KhOWUeYI029Nn/4NOIk/yblQ3tEd9QhS+u/8/S9ZoDs= -github.com/unifi-poller/poller v0.0.6/go.mod h1:RkRJ4pAc2dAN8Xu9+VOumeE3BdN5QDQ3PC+jBx8hWW0= -github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9 h1:BYNOSvttR91a0F+ttG2ecD3DenVI9p9oi1W7m36zH98= -github.com/unifi-poller/poller v0.0.8-0.20200619104117-a5e263a36ac9/go.mod h1:q2tufLljemUR/blyhjIj+T0sdAOZo0kdlv3h79kEMgU= -github.com/unifi-poller/unifi v0.0.4 h1:NDTxHTdF0MAt1y1RU8J+MSqdYlO0CEIqlrktcj3y/og= -github.com/unifi-poller/unifi v0.0.4/go.mod h1:bTUtctrf56aapjKH+L+98eThBaVFbQXw5iNGZI0g/+E= -github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5 h1:DI1Dtq+2CY1t0Woesz7CRkbNrUT5GSX0h8mDsP3pgcw= -github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= -github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd h1:jrwuiY1AdoPi+b+R8zjt/e8h8ZqULNB9izcyQnf3pSw= +github.com/unifi-poller/poller v0.0.8-0.20200621084019-8e9b2f223849 h1:/xyUMrTm5Yc+Tobmmd4FCZjzhIJDPMQEmnHaAp4tTz4= +github.com/unifi-poller/poller v0.0.8-0.20200621084019-8e9b2f223849/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= +github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b h1:AJKt/ZIDtlEOWxwpL/a7pcrWmqdJaaNE9odNn4JSRj0= +github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= -github.com/unifi-poller/unifi v0.0.5 h1:Izeun32YxcQOeKZUXY0Sy4ltKYFuYxWGcN9JS6xkIJU= -github.com/unifi-poller/unifi v0.0.5/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= +github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= +github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -124,8 +107,6 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM= golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -140,9 +121,6 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38= @@ -180,8 +158,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 2c668338..0f162b96 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/pkg/errors" "github.com/unifi-poller/poller" @@ -75,6 +76,56 @@ func (u *InputUnifi) logController(c *Controller) { u.Logf(" => Save Sites: %v", *c.SaveSites) } +// Events allows you to pull only events (and IDS) from the UniFi Controller. +// This does not respect HashPII, but it may in the future! +// Use Filter.Dur to set a search duration into the past; 1 minute default. +// Set Filter.True to true to disable IDS collection. +func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { + if u.Disable { + return nil, nil + } + + events := &poller.Events{} + + for _, c := range u.Controllers { + if filter != nil && filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { + continue + } + + if filter == nil || filter.Dur == 0 { + filter = &poller.Filter{Dur: time.Minute} + } + + // Get the sites we care about. + sites, err := u.getFilteredSites(c) + if err != nil { + return events, errors.Wrap(err, "unifi.GetSites()") + } + + e, err := c.Unifi.GetEvents(sites, time.Now().Add(-filter.Dur)) + if err != nil { + return events, errors.Wrap(err, "unifi.GetEvents()") + } + + for _, l := range e { + events.Logs = append(events.Logs, l) + } + + if !filter.True { + i, err := c.Unifi.GetIDS(sites, time.Now().Add(-filter.Dur)) + if err != nil { + return events, errors.Wrap(err, "unifi.GetIDS()") + } + + for _, l := range i { + events.Logs = append(events.Logs, l) + } + } + } + + return events, nil +} + // Metrics grabs all the measurements from a UniFi controller and returns them. func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { if u.Disable { @@ -85,7 +136,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { // Check if the request is for an existing, configured controller (or all controllers) for _, c := range u.Controllers { - if filter != nil && !strings.EqualFold(c.URL, filter.Path) { + if filter != nil && filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { continue } @@ -97,7 +148,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { metrics = poller.AppendMetrics(metrics, m) } - if filter == nil || len(metrics.Clients) != 0 { + if filter == nil || filter.Path == "" || len(metrics.Clients) != 0 { return metrics, nil } @@ -109,7 +160,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { return u.dynamicController(filter.Path) } -// RawMetrics returns API output from the first configured unifi controller. +// RawMetrics returns API output from the first configured UniFi controller. // Adjust filter.Unit to pull from a controller other than the first. func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { if l := len(u.Controllers); filter.Unit >= l { From 1656c205ed3374001b9321cf7c7e2336b665ace6 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 03:26:14 -0700 Subject: [PATCH 03/18] overload the filter --- integrations/inputunifi/collector.go | 22 +++++++++++----------- integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 ++ integrations/inputunifi/interface.go | 23 ++++++++++++++++------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index f0849650..a4326264 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -40,28 +40,28 @@ func (u *InputUnifi) newDynamicCntrlr(url string) (bool, *Controller) { return true, u.dynamic[url] } -func (u *InputUnifi) dynamicController(url string) (*poller.Metrics, error) { - if !strings.HasPrefix(url, "http") { +func (u *InputUnifi) dynamicController(filter *poller.Filter) (*poller.Metrics, error) { + if !strings.HasPrefix(filter.Path, "http") { return nil, errScrapeFilterMatchFailed } - new, c := u.newDynamicCntrlr(url) + new, c := u.newDynamicCntrlr(filter.Path) if new { - u.Logf("Authenticating to Dynamic UniFi Controller: %s", url) + u.Logf("Authenticating to Dynamic UniFi Controller: %s", filter.Path) if err := u.getUnifi(c); err != nil { u.logController(c) - return nil, errors.Wrapf(err, "authenticating to %s", url) + return nil, errors.Wrapf(err, "authenticating to %s", filter.Path) } u.logController(c) } - return u.collectController(c) + return u.collectController(c, filter) } -func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { +func (u *InputUnifi) collectController(c *Controller, filter *poller.Filter) (*poller.Metrics, error) { if u.isNill(c) { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) @@ -70,7 +70,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { } } - metrics, err := u.pollController(c) + metrics, err := u.pollController(c, filter) if err != nil { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) @@ -82,7 +82,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { return metrics, err } -func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { +func (u *InputUnifi) pollController(c *Controller, filter *poller.Filter) (*poller.Metrics, error) { var err error u.RLock() @@ -105,14 +105,14 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { } } - if c.SaveEvents != nil && *c.SaveEvents { + if !filter.Skip && c.SaveEvents != nil && *c.SaveEvents { m.Events, err = c.Unifi.GetEvents(m.Sites, time.Now().Add(time.Minute)) if err != nil { return nil, errors.Wrapf(err, "unifi.GetEvents(%s)", c.URL) } } - if c.SaveIDS != nil && *c.SaveIDS { + if !filter.Skip && c.SaveIDS != nil && *c.SaveIDS { m.IDSList, err = c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute)) if err != nil { return nil, errors.Wrapf(err, "unifi.GetIDS(%s)", c.URL) diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index 5e36eeb5..9fb672c5 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -4,6 +4,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 - github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b + github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 23205549..c4b4155f 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -92,6 +92,8 @@ github.com/unifi-poller/poller v0.0.8-0.20200621084019-8e9b2f223849 h1:/xyUMrTm5 github.com/unifi-poller/poller v0.0.8-0.20200621084019-8e9b2f223849/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b h1:AJKt/ZIDtlEOWxwpL/a7pcrWmqdJaaNE9odNn4JSRj0= github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= +github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece h1:EsyR6cKuwAKzddS4gsKDugfN+OEHCm7bhNOvEfBCWWA= +github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 0f162b96..e9a70c64 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -79,7 +79,7 @@ func (u *InputUnifi) logController(c *Controller) { // Events allows you to pull only events (and IDS) from the UniFi Controller. // This does not respect HashPII, but it may in the future! // Use Filter.Dur to set a search duration into the past; 1 minute default. -// Set Filter.True to true to disable IDS collection. +// Set Filter.Skip to true to disable IDS collection. func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { if u.Disable { return nil, nil @@ -88,7 +88,9 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { events := &poller.Events{} for _, c := range u.Controllers { - if filter != nil && filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { + if filter != nil && filter.Path != "" && + !strings.EqualFold(c.URL, filter.Path) { + // continue only if we have a filter path and it doesn't match. continue } @@ -111,7 +113,7 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { events.Logs = append(events.Logs, l) } - if !filter.True { + if !filter.Skip { i, err := c.Unifi.GetIDS(sites, time.Now().Add(-filter.Dur)) if err != nil { return events, errors.Wrap(err, "unifi.GetIDS()") @@ -127,6 +129,8 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { } // Metrics grabs all the measurements from a UniFi controller and returns them. +// Set Filter.Path to a controller URL for a specific controller (or get them all). +// Set Filter.Skip to true to Skip Events and IDS collection (Prometheus does this). func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { if u.Disable { return nil, nil @@ -134,13 +138,18 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { metrics := &poller.Metrics{} + if filter == nil { + filter = &poller.Filter{} + } + // Check if the request is for an existing, configured controller (or all controllers) for _, c := range u.Controllers { - if filter != nil && filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { + if filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { + // continue only if we have a filter path and it doesn't match. continue } - m, err := u.collectController(c) + m, err := u.collectController(c, filter) if err != nil { return metrics, err } @@ -148,7 +157,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { metrics = poller.AppendMetrics(metrics, m) } - if filter == nil || filter.Path == "" || len(metrics.Clients) != 0 { + if filter.Path == "" || len(metrics.Clients) != 0 { return metrics, nil } @@ -157,7 +166,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { } // Attempt a dynamic metrics fetch from an unconfigured controller. - return u.dynamicController(filter.Path) + return u.dynamicController(filter) } // RawMetrics returns API output from the first configured UniFi controller. From bc23ac89c800736071aa2c64c45fbb6406dbb896 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 03:46:28 -0700 Subject: [PATCH 04/18] return a []interface of events. --- integrations/inputunifi/collector.go | 44 ++++++++++++++++++---------- integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 ++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index a4326264..01daf0db 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -106,17 +106,25 @@ func (u *InputUnifi) pollController(c *Controller, filter *poller.Filter) (*poll } if !filter.Skip && c.SaveEvents != nil && *c.SaveEvents { - m.Events, err = c.Unifi.GetEvents(m.Sites, time.Now().Add(time.Minute)) + e, err := c.Unifi.GetEvents(m.Sites, time.Now().Add(time.Minute)) if err != nil { return nil, errors.Wrapf(err, "unifi.GetEvents(%s)", c.URL) } + + for _, l := range e { + m.Events = append(m.Events, redactEvent(l, c.HashPII)) + } } if !filter.Skip && c.SaveIDS != nil && *c.SaveIDS { - m.IDSList, err = c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute)) + e, err := c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute)) if err != nil { return nil, errors.Wrapf(err, "unifi.GetIDS(%s)", c.URL) } + + for _, l := range e { + m.Events = append(m.Events, l) + } } // Get all the points. @@ -134,7 +142,7 @@ func (u *InputUnifi) pollController(c *Controller, filter *poller.Filter) (*poll // augmentMetrics is our middleware layer between collecting metrics and writing them. // This is where we can manipuate the returned data or make arbitrary decisions. // This function currently adds parent device names to client metrics. -func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *poller.Metrics { // nolint: funlen +func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *poller.Metrics { if metrics == nil || metrics.Devices == nil || metrics.Clients == nil { return metrics } @@ -162,18 +170,6 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *pol devices[r.Mac] = r.Name } - if *c.HashPII { - for i := range metrics.Events { - // metrics.Events[i].Msg <-- not sure what to do here. - metrics.Events[i].DestIPGeo = unifi.IPGeo{} - metrics.Events[i].SourceIPGeo = unifi.IPGeo{} - metrics.Events[i].Host = RedactNamePII(metrics.Events[i].Host, c.HashPII) - metrics.Events[i].Hostname = RedactNamePII(metrics.Events[i].Hostname, c.HashPII) - metrics.Events[i].DstMAC = RedactMacPII(metrics.Events[i].DstMAC, c.HashPII) - metrics.Events[i].SrcMAC = RedactMacPII(metrics.Events[i].SrcMAC, c.HashPII) - } - } - // These come blank, so set them here. for i, client := range metrics.Clients { if devices[client.Mac] = client.Name; client.Name == "" { @@ -207,6 +203,24 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *pol return metrics } +// redactEvent attempts to mask personally identying information from log messages. +// This currently misses the "msg" value entirely and leaks PII information. +func redactEvent(e *unifi.Event, hash *bool) *unifi.Event { + if !*hash { + return e + } + + // metrics.Events[i].Msg <-- not sure what to do here. + e.DestIPGeo = unifi.IPGeo{} + e.SourceIPGeo = unifi.IPGeo{} + e.Host = RedactNamePII(e.Host, hash) + e.Hostname = RedactNamePII(e.Hostname, hash) + e.DstMAC = RedactMacPII(e.DstMAC, hash) + e.SrcMAC = RedactMacPII(e.SrcMAC, hash) + + return e +} + // RedactNamePII converts a name string to an md5 hash (first 24 chars only). // Useful for maskiing out personally identifying information. func RedactNamePII(pii string, hash *bool) string { diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index 9fb672c5..fab5f989 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -4,6 +4,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 - github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece + github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6 github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index c4b4155f..90193dec 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -94,6 +94,8 @@ github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b h1:AJKt/ZIDt github.com/unifi-poller/poller v0.0.8-0.20200621091816-fd5c7abd9f4b/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece h1:EsyR6cKuwAKzddS4gsKDugfN+OEHCm7bhNOvEfBCWWA= github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= +github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6 h1:V19WgXwjXxGY75Mn8Hc5Whl3+BC71YSGatRvKVRh9pA= +github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= From 9ab151bc1a8771fcacecca4ee3fc13d2a7c61471 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 04:16:57 -0700 Subject: [PATCH 05/18] Remove events from metrics --- integrations/inputunifi/collector.go | 22 ------------------- integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 ++ integrations/inputunifi/interface.go | 32 +++++++++++++--------------- 4 files changed, 18 insertions(+), 40 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 01daf0db..047e6f5d 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -105,28 +105,6 @@ func (u *InputUnifi) pollController(c *Controller, filter *poller.Filter) (*poll } } - if !filter.Skip && c.SaveEvents != nil && *c.SaveEvents { - e, err := c.Unifi.GetEvents(m.Sites, time.Now().Add(time.Minute)) - if err != nil { - return nil, errors.Wrapf(err, "unifi.GetEvents(%s)", c.URL) - } - - for _, l := range e { - m.Events = append(m.Events, redactEvent(l, c.HashPII)) - } - } - - if !filter.Skip && c.SaveIDS != nil && *c.SaveIDS { - e, err := c.Unifi.GetIDS(m.Sites, time.Now().Add(time.Minute)) - if err != nil { - return nil, errors.Wrapf(err, "unifi.GetIDS(%s)", c.URL) - } - - for _, l := range e { - m.Events = append(m.Events, l) - } - } - // Get all the points. if m.Clients, err = c.Unifi.GetClients(m.Sites); err != nil { return nil, errors.Wrapf(err, "unifi.GetClients(%s)", c.URL) diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index fab5f989..1754cda0 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -4,6 +4,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 - github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6 + github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10 github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 90193dec..008928ac 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -96,6 +96,8 @@ github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece h1:EsyR6cKuw github.com/unifi-poller/poller v0.0.8-0.20200621101255-6d0d0b288ece/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6 h1:V19WgXwjXxGY75Mn8Hc5Whl3+BC71YSGatRvKVRh9pA= github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= +github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10 h1:1rGP4ISFpBj9xjJDXNak7EdaQtyoy3MwMZzo2+W1PLo= +github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index e9a70c64..0ce2d3ab 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -77,9 +77,8 @@ func (u *InputUnifi) logController(c *Controller) { } // Events allows you to pull only events (and IDS) from the UniFi Controller. -// This does not respect HashPII, but it may in the future! +// This does not fully respect HashPII, but it may in the future! // Use Filter.Dur to set a search duration into the past; 1 minute default. -// Set Filter.Skip to true to disable IDS collection. func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { if u.Disable { return nil, nil @@ -88,32 +87,32 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { events := &poller.Events{} for _, c := range u.Controllers { - if filter != nil && filter.Path != "" && - !strings.EqualFold(c.URL, filter.Path) { - // continue only if we have a filter path and it doesn't match. - continue - } - if filter == nil || filter.Dur == 0 { filter = &poller.Filter{Dur: time.Minute} } + if filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { + continue + } + // Get the sites we care about. sites, err := u.getFilteredSites(c) if err != nil { return events, errors.Wrap(err, "unifi.GetSites()") } - e, err := c.Unifi.GetEvents(sites, time.Now().Add(-filter.Dur)) - if err != nil { - return events, errors.Wrap(err, "unifi.GetEvents()") + if *c.SaveEvents { + e, err := c.Unifi.GetEvents(sites, time.Now().Add(-filter.Dur)) + if err != nil { + return events, errors.Wrap(err, "unifi.GetEvents()") + } + + for _, l := range e { + events.Logs = append(events.Logs, redactEvent(l, c.HashPII)) + } } - for _, l := range e { - events.Logs = append(events.Logs, l) - } - - if !filter.Skip { + if *c.SaveIDS { i, err := c.Unifi.GetIDS(sites, time.Now().Add(-filter.Dur)) if err != nil { return events, errors.Wrap(err, "unifi.GetIDS()") @@ -130,7 +129,6 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { // Metrics grabs all the measurements from a UniFi controller and returns them. // Set Filter.Path to a controller URL for a specific controller (or get them all). -// Set Filter.Skip to true to Skip Events and IDS collection (Prometheus does this). func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { if u.Disable { return nil, nil From 51d3e457b9df06399fedcaed6d276277acc9c060 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 04:22:52 -0700 Subject: [PATCH 06/18] fixes --- integrations/inputunifi/collector.go | 8 ++++---- integrations/inputunifi/interface.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 047e6f5d..2acb5442 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -58,10 +58,10 @@ func (u *InputUnifi) dynamicController(filter *poller.Filter) (*poller.Metrics, u.logController(c) } - return u.collectController(c, filter) + return u.collectController(c) } -func (u *InputUnifi) collectController(c *Controller, filter *poller.Filter) (*poller.Metrics, error) { +func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { if u.isNill(c) { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) @@ -70,7 +70,7 @@ func (u *InputUnifi) collectController(c *Controller, filter *poller.Filter) (*p } } - metrics, err := u.pollController(c, filter) + metrics, err := u.pollController(c) if err != nil { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) @@ -82,7 +82,7 @@ func (u *InputUnifi) collectController(c *Controller, filter *poller.Filter) (*p return metrics, err } -func (u *InputUnifi) pollController(c *Controller, filter *poller.Filter) (*poller.Metrics, error) { +func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { var err error u.RLock() diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 0ce2d3ab..5a143005 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -147,7 +147,7 @@ func (u *InputUnifi) Metrics(filter *poller.Filter) (*poller.Metrics, error) { continue } - m, err := u.collectController(c, filter) + m, err := u.collectController(c) if err != nil { return metrics, err } From 5f5b4c4f6a41e222e8d4d2419248c0a7deac0a9a Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 04:43:46 -0700 Subject: [PATCH 07/18] movable pieces --- integrations/inputunifi/collector.go | 31 +++++++++++++++++++++ integrations/inputunifi/interface.go | 40 +++++++--------------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 2acb5442..22de75c2 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -82,6 +82,37 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { return metrics, err } +func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]interface{}, error) { + var ( + l interface{} + logs []interface{} + ) + + // Get the sites we care about. + sites, err := u.getFilteredSites(c) + if err != nil { + return nil, errors.Wrap(err, "unifi.GetSites()") + } + + if *c.SaveEvents { + if l, err = c.Unifi.GetEvents(sites, from); err != nil { + return nil, errors.Wrap(err, "unifi.GetEvents()") + } + + logs = append(logs, l) + } + + if *c.SaveIDS { + if l, err = c.Unifi.GetIDS(sites, from); err != nil { + return nil, errors.Wrap(err, "unifi.GetIDS()") + } + + logs = append(logs, l) + } + + return logs, nil +} + func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { var err error diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 5a143005..6688cc3a 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -78,53 +78,33 @@ func (u *InputUnifi) logController(c *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! // Use Filter.Dur to set a search duration into the past; 1 minute default. func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { if u.Disable { return nil, nil } - events := &poller.Events{} + logs := []interface{}{} + + if filter == nil || filter.Dur == 0 { + filter = &poller.Filter{Dur: time.Minute} + } for _, c := range u.Controllers { - if filter == nil || filter.Dur == 0 { - filter = &poller.Filter{Dur: time.Minute} - } - if filter.Path != "" && !strings.EqualFold(c.URL, filter.Path) { continue } - // Get the sites we care about. - sites, err := u.getFilteredSites(c) + events, err := u.collectControllerEvents(c, time.Now().Add(-filter.Dur)) if err != nil { - return events, errors.Wrap(err, "unifi.GetSites()") + return nil, err } - if *c.SaveEvents { - e, err := c.Unifi.GetEvents(sites, time.Now().Add(-filter.Dur)) - if err != nil { - return events, errors.Wrap(err, "unifi.GetEvents()") - } - - for _, l := range e { - events.Logs = append(events.Logs, redactEvent(l, c.HashPII)) - } - } - - if *c.SaveIDS { - i, err := c.Unifi.GetIDS(sites, time.Now().Add(-filter.Dur)) - if err != nil { - return events, errors.Wrap(err, "unifi.GetIDS()") - } - - for _, l := range i { - events.Logs = append(events.Logs, l) - } - } + logs = append(logs, events...) } - return events, nil + return &poller.Events{Logs: logs}, nil } // Metrics grabs all the measurements from a UniFi controller and returns them. From 6d9dd6d4cb095bf3aeaa26bd3eb13ea0f77207b2 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 12:22:01 -0700 Subject: [PATCH 08/18] fixes --- integrations/inputunifi/collector.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 22de75c2..7666b3b2 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -83,10 +83,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { } func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]interface{}, error) { - var ( - l interface{} - logs []interface{} - ) + logs := []interface{}{} // Get the sites we care about. sites, err := u.getFilteredSites(c) @@ -95,19 +92,25 @@ func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]i } if *c.SaveEvents { - if l, err = c.Unifi.GetEvents(sites, from); err != nil { + events, err := c.Unifi.GetEvents(sites, from) + if err != nil { return nil, errors.Wrap(err, "unifi.GetEvents()") } - logs = append(logs, l) + for _, e := range events { + logs = append(logs, redactEvent(e, c.HashPII)) + } } if *c.SaveIDS { - if l, err = c.Unifi.GetIDS(sites, from); err != nil { + events, err := c.Unifi.GetIDS(sites, from) + if err != nil { return nil, errors.Wrap(err, "unifi.GetIDS()") } - logs = append(logs, l) + for _, e := range events { + logs = append(logs, e) + } } return logs, nil From 90bebbf165e3a0c1de365914796d568d50049f05 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 16:04:47 -0700 Subject: [PATCH 09/18] satisfy new interface --- integrations/inputunifi/collector.go | 64 +++++++++++++++++----------- integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 + integrations/inputunifi/input.go | 10 +++++ 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 7666b3b2..4855b3c2 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -122,9 +122,10 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { u.RLock() defer u.RUnlock() - m := &poller.Metrics{TS: time.Now()} // At this point, it's the Current Check. + m := &Metrics{TS: time.Now()} // At this point, it's the Current Check. // Get the sites we care about. + if m.Sites, err = u.getFilteredSites(c); err != nil { return nil, errors.Wrap(err, "unifi.GetSites()") } @@ -154,65 +155,78 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { // augmentMetrics is our middleware layer between collecting metrics and writing them. // This is where we can manipuate the returned data or make arbitrary decisions. // This function currently adds parent device names to client metrics. -func (u *InputUnifi) augmentMetrics(c *Controller, metrics *poller.Metrics) *poller.Metrics { - if metrics == nil || metrics.Devices == nil || metrics.Clients == nil { - return metrics +func (u *InputUnifi) augmentMetrics(c *Controller, metrics *Metrics) *poller.Metrics { + if metrics == nil { + return nil } + m := &poller.Metrics{TS: metrics.TS} devices := make(map[string]string) bssdIDs := make(map[string]string) - for _, r := range metrics.UAPs { + for _, r := range metrics.Devices.UAPs { devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) for _, v := range r.VapTable { bssdIDs[v.Bssid] = fmt.Sprintf("%s %s %s:", r.Name, v.Radio, v.RadioName) } } - for _, r := range metrics.USGs { + for _, r := range metrics.Devices.USGs { devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) } - for _, r := range metrics.USWs { + for _, r := range metrics.Devices.USWs { devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) } - for _, r := range metrics.UDMs { + for _, r := range metrics.Devices.UDMs { devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) } // These come blank, so set them here. - for i, client := range metrics.Clients { + for _, client := range metrics.Clients { if devices[client.Mac] = client.Name; client.Name == "" { devices[client.Mac] = client.Hostname } - metrics.Clients[i].Mac = RedactMacPII(metrics.Clients[i].Mac, c.HashPII) - metrics.Clients[i].Name = RedactNamePII(metrics.Clients[i].Name, c.HashPII) - metrics.Clients[i].Hostname = RedactNamePII(metrics.Clients[i].Hostname, c.HashPII) - metrics.Clients[i].SwName = devices[client.SwMac] - metrics.Clients[i].ApName = devices[client.ApMac] - metrics.Clients[i].GwName = devices[client.GwMac] - metrics.Clients[i].RadioDescription = bssdIDs[metrics.Clients[i].Bssid] + metrics.Clients[i].RadioProto + client.Mac = RedactMacPII(client.Mac, c.HashPII) + client.Name = RedactNamePII(client.Name, c.HashPII) + client.Hostname = RedactNamePII(client.Hostname, c.HashPII) + client.SwName = devices[client.SwMac] + client.ApName = devices[client.ApMac] + client.GwName = devices[client.GwMac] + client.RadioDescription = bssdIDs[client.Bssid] + client.RadioProto + m.Clients = append(m.Clients, client) } - for i := range metrics.ClientsDPI { + for _, client := range metrics.ClientsDPI { // Name on Client DPI data also comes blank, find it based on MAC address. - metrics.ClientsDPI[i].Name = devices[metrics.ClientsDPI[i].MAC] - if metrics.ClientsDPI[i].Name == "" { - metrics.ClientsDPI[i].Name = metrics.ClientsDPI[i].MAC + client.Name = devices[client.MAC] + if client.Name == "" { + client.Name = client.MAC } - metrics.ClientsDPI[i].Name = RedactNamePII(metrics.ClientsDPI[i].Name, c.HashPII) - metrics.ClientsDPI[i].MAC = RedactMacPII(metrics.ClientsDPI[i].MAC, c.HashPII) + client.Name = RedactNamePII(client.Name, c.HashPII) + client.MAC = RedactMacPII(client.MAC, c.HashPII) + m.ClientsDPI = append(m.ClientsDPI, client) } - if !*c.SaveSites { - metrics.Sites = nil + if *c.SaveSites { + for _, site := range metrics.Sites { + m.Sites = append(m.Sites, site) + } + + for _, site := range metrics.SitesDPI { + m.SitesDPI = append(m.SitesDPI, site) + } } - return metrics + return m } // redactEvent attempts to mask personally identying information from log messages. diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index 1754cda0..24d6b1c5 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -4,6 +4,6 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 - github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10 + github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46 github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 008928ac..64e38613 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -98,6 +98,8 @@ github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6 h1:V19WgXwjX github.com/unifi-poller/poller v0.0.8-0.20200621103717-5f3d60890ed6/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10 h1:1rGP4ISFpBj9xjJDXNak7EdaQtyoy3MwMZzo2+W1PLo= github.com/unifi-poller/poller v0.0.8-0.20200621110949-33f1a1454d10/go.mod h1:+Ppksi2wBCrByJke0B0lTutxFtKfv1zx6L1haALBrN4= +github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46 h1:OhbVj3VVgbpUMQFSwD0NszDsbEL7DdbTcJuU+p9DwIM= +github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46/go.mod h1:pJ/MeYaakLOOpbyc7s4zeZ92UzNK/rir5jkA7t5jIjo= github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= diff --git a/integrations/inputunifi/input.go b/integrations/inputunifi/input.go index 7cdf95c9..4d429ded 100644 --- a/integrations/inputunifi/input.go +++ b/integrations/inputunifi/input.go @@ -5,6 +5,7 @@ package inputunifi import ( "io/ioutil" "strings" + "time" "sync" @@ -53,6 +54,15 @@ type Config struct { Controllers []*Controller `json:"controllers" toml:"controller" xml:"controller" yaml:"controllers"` } +type Metrics struct { + TS time.Time + Sites []*unifi.Site + Clients []*unifi.Client + SitesDPI []*unifi.DPITable + ClientsDPI []*unifi.DPITable + Devices *unifi.Devices +} + func init() { // nolint: gochecknoinits u := &InputUnifi{} From 3c58f883f9ae909364fc251fa8295d3a2441d0f8 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Sun, 21 Jun 2020 18:34:32 -0700 Subject: [PATCH 10/18] break up method --- integrations/inputunifi/collector.go | 62 +++++++++++++++------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 4855b3c2..af57d258 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -125,7 +125,6 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { m := &Metrics{TS: time.Now()} // At this point, it's the Current Check. // Get the sites we care about. - if m.Sites, err = u.getFilteredSites(c); err != nil { return nil, errors.Wrap(err, "unifi.GetSites()") } @@ -160,33 +159,7 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *Metrics) *poller.Met return nil } - m := &poller.Metrics{TS: metrics.TS} - devices := make(map[string]string) - bssdIDs := make(map[string]string) - - for _, r := range metrics.Devices.UAPs { - devices[r.Mac] = r.Name - m.Devices = append(m.Devices, r) - - for _, v := range r.VapTable { - bssdIDs[v.Bssid] = fmt.Sprintf("%s %s %s:", r.Name, v.Radio, v.RadioName) - } - } - - for _, r := range metrics.Devices.USGs { - devices[r.Mac] = r.Name - m.Devices = append(m.Devices, r) - } - - for _, r := range metrics.Devices.USWs { - devices[r.Mac] = r.Name - m.Devices = append(m.Devices, r) - } - - for _, r := range metrics.Devices.UDMs { - devices[r.Mac] = r.Name - m.Devices = append(m.Devices, r) - } + m, devices, bssdIDs := extractDevices(metrics) // These come blank, so set them here. for _, client := range metrics.Clients { @@ -229,6 +202,39 @@ func (u *InputUnifi) augmentMetrics(c *Controller, metrics *Metrics) *poller.Met return m } +// this is a helper function for augmentMetrics. +func extractDevices(metrics *Metrics) (*poller.Metrics, map[string]string, map[string]string) { + m := &poller.Metrics{TS: metrics.TS} + devices := make(map[string]string) + bssdIDs := make(map[string]string) + + for _, r := range metrics.Devices.UAPs { + devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) + + for _, v := range r.VapTable { + bssdIDs[v.Bssid] = fmt.Sprintf("%s %s %s:", r.Name, v.Radio, v.RadioName) + } + } + + for _, r := range metrics.Devices.USGs { + devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) + } + + for _, r := range metrics.Devices.USWs { + devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) + } + + for _, r := range metrics.Devices.UDMs { + devices[r.Mac] = r.Name + m.Devices = append(m.Devices, r) + } + + return m, devices, bssdIDs +} + // redactEvent attempts to mask personally identying information from log messages. // This currently misses the "msg" value entirely and leaks PII information. func redactEvent(e *unifi.Event, hash *bool) *unifi.Event { From f73bed4142d82f6cf5a3623203264ab81968b372 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 22 Jun 2020 00:40:35 -0700 Subject: [PATCH 11/18] go back 1 hour --- integrations/inputunifi/collector.go | 6 +++--- integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 ++ integrations/inputunifi/interface.go | 8 +++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index af57d258..9db5ca6d 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -82,7 +82,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { return metrics, err } -func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]interface{}, error) { +func (u *InputUnifi) collectControllerEvents(c *Controller) ([]interface{}, error) { logs := []interface{}{} // Get the sites we care about. @@ -92,7 +92,7 @@ func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]i } if *c.SaveEvents { - events, err := c.Unifi.GetEvents(sites, from) + events, err := c.Unifi.GetEvents(sites, time.Hour) if err != nil { return nil, errors.Wrap(err, "unifi.GetEvents()") } @@ -103,7 +103,7 @@ func (u *InputUnifi) collectControllerEvents(c *Controller, from time.Time) ([]i } if *c.SaveIDS { - events, err := c.Unifi.GetIDS(sites, from) + events, err := c.Unifi.GetIDS(sites, time.Now().Add(-time.Hour)) if err != nil { return nil, errors.Wrap(err, "unifi.GetIDS()") } diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index 24d6b1c5..955ff399 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -5,5 +5,5 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46 - github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 + github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 64e38613..2d165cc8 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -103,6 +103,8 @@ github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46/go.mod h1:pJ github.com/unifi-poller/unifi v0.0.5-0.20200620103801-b927287ea1cd/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD656pLSWgxFwhDhHe/zfAXrm7F39Zn4R+si8= github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= +github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6 h1:KzQDaEhDxtDzoyfBPXzM/pwpD76+4Y74Y66XMhKt2pI= +github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 6688cc3a..35222039 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "strings" - "time" "github.com/pkg/errors" "github.com/unifi-poller/poller" @@ -79,7 +78,6 @@ func (u *InputUnifi) logController(c *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! -// Use Filter.Dur to set a search duration into the past; 1 minute default. func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { if u.Disable { return nil, nil @@ -87,8 +85,8 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { logs := []interface{}{} - if filter == nil || filter.Dur == 0 { - filter = &poller.Filter{Dur: time.Minute} + if filter == nil { + filter = &poller.Filter{} } for _, c := range u.Controllers { @@ -96,7 +94,7 @@ func (u *InputUnifi) Events(filter *poller.Filter) (*poller.Events, error) { continue } - events, err := u.collectControllerEvents(c, time.Now().Add(-filter.Dur)) + events, err := u.collectControllerEvents(c) if err != nil { return nil, err } From 2d2985addce30693640a05743848b79b50a4e2d4 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 22 Jun 2020 01:10:00 -0700 Subject: [PATCH 12/18] allow not printing --- integrations/inputunifi/interface.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 35222039..09333e69 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -172,23 +172,25 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { switch filter.Kind { case "d", "device", "devices": - return u.dumpSitesJSON(c, unifi.APIDevicePath, "Devices", sites) + return u.dumpSitesJSON(c, unifi.APIDevicePath, "Devices", sites, !filter.Skip) case "client", "clients", "c": - return u.dumpSitesJSON(c, unifi.APIClientPath, "Clients", sites) + return u.dumpSitesJSON(c, unifi.APIClientPath, "Clients", sites, !filter.Skip) case "other", "o": - _, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping Path '%s':\n", filter.Path) - return c.Unifi.GetJSON(filter.Path) + return u.dumpSitesJSON(c, filter.Path, "Path", sites, !filter.Skip) default: return []byte{}, errNoFilterKindProvided } } -func (u *InputUnifi) dumpSitesJSON(c *Controller, path, name string, sites []*unifi.Site) ([]byte, error) { +func (u *InputUnifi) dumpSitesJSON(c *Controller, path, name string, sites []*unifi.Site, print bool) ([]byte, error) { allJSON := []byte{} for _, s := range sites { apiPath := fmt.Sprintf(path, s.Name) - _, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping %s: '%s' JSON for site: %s (%s):\n", name, apiPath, s.Desc, s.Name) + + if print { + _, _ = fmt.Fprintf(os.Stderr, "[INFO] Printing %s: '%s' JSON for site: %s (%s):\n", name, apiPath, s.Desc, s.Name) + } body, err := c.Unifi.GetJSON(apiPath) if err != nil { From e973f4b193333109793f329cf95ca4e2ee45c42c Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 22 Jun 2020 01:15:39 -0700 Subject: [PATCH 13/18] just dont log --- integrations/inputunifi/interface.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 09333e69..1fd021a0 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -4,7 +4,6 @@ package inputunifi import ( "fmt" - "os" "strings" "github.com/pkg/errors" @@ -172,27 +171,21 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { switch filter.Kind { case "d", "device", "devices": - return u.dumpSitesJSON(c, unifi.APIDevicePath, "Devices", sites, !filter.Skip) + return u.dumpSitesJSON(c, unifi.APIDevicePath, sites) case "client", "clients", "c": - return u.dumpSitesJSON(c, unifi.APIClientPath, "Clients", sites, !filter.Skip) + return u.dumpSitesJSON(c, unifi.APIClientPath, sites) case "other", "o": - return u.dumpSitesJSON(c, filter.Path, "Path", sites, !filter.Skip) + return c.Unifi.GetJSON(filter.Path) default: return []byte{}, errNoFilterKindProvided } } -func (u *InputUnifi) dumpSitesJSON(c *Controller, path, name string, sites []*unifi.Site, print bool) ([]byte, error) { +func (u *InputUnifi) dumpSitesJSON(c *Controller, path string, sites []*unifi.Site) ([]byte, error) { allJSON := []byte{} for _, s := range sites { - apiPath := fmt.Sprintf(path, s.Name) - - if print { - _, _ = fmt.Fprintf(os.Stderr, "[INFO] Printing %s: '%s' JSON for site: %s (%s):\n", name, apiPath, s.Desc, s.Name) - } - - body, err := c.Unifi.GetJSON(apiPath) + body, err := c.Unifi.GetJSON(fmt.Sprintf(path, s.Name)) if err != nil { return allJSON, err } From ab5d2038fb70cfa02c5bad1a3a1239dc42a98f87 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 22 Jun 2020 01:25:44 -0700 Subject: [PATCH 14/18] just make it debug --- integrations/inputunifi/interface.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 1fd021a0..9810db44 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -171,9 +171,9 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { switch filter.Kind { case "d", "device", "devices": - return u.dumpSitesJSON(c, unifi.APIDevicePath, sites) + return u.getSitesJSON(c, unifi.APIDevicePath, sites) case "client", "clients", "c": - return u.dumpSitesJSON(c, unifi.APIClientPath, sites) + return u.getSitesJSON(c, unifi.APIClientPath, sites) case "other", "o": return c.Unifi.GetJSON(filter.Path) default: @@ -181,11 +181,14 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) { } } -func (u *InputUnifi) dumpSitesJSON(c *Controller, path string, sites []*unifi.Site) ([]byte, error) { +func (u *InputUnifi) getSitesJSON(c *Controller, path string, sites []*unifi.Site) ([]byte, error) { allJSON := []byte{} for _, s := range sites { - body, err := c.Unifi.GetJSON(fmt.Sprintf(path, s.Name)) + apiPath := fmt.Sprintf(path, s.Name) + u.LogDebugf("Returning Path '%s' for site: %s (%s):\n", apiPath, s.Desc, s.Name) + + body, err := c.Unifi.GetJSON(apiPath) if err != nil { return allJSON, err } From 37f2f953f8797b5fa9d7975bae61afa89142a210 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Tue, 23 Jun 2020 00:32:37 -0700 Subject: [PATCH 15/18] meh --- integrations/inputunifi/collector.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 9db5ca6d..b4eedd98 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -117,34 +117,33 @@ func (u *InputUnifi) collectControllerEvents(c *Controller) ([]interface{}, erro } func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { - var err error - u.RLock() defer u.RUnlock() - m := &Metrics{TS: time.Now()} // At this point, it's the Current Check. - // Get the sites we care about. - if m.Sites, err = u.getFilteredSites(c); err != nil { + sites, err := u.getFilteredSites(c) + if err != nil { return nil, errors.Wrap(err, "unifi.GetSites()") } + m := &Metrics{TS: time.Now(), Sites: sites} + if c.SaveDPI != nil && *c.SaveDPI { - if m.SitesDPI, err = c.Unifi.GetSiteDPI(m.Sites); err != nil { + if m.SitesDPI, err = c.Unifi.GetSiteDPI(sites); err != nil { return nil, errors.Wrapf(err, "unifi.GetSiteDPI(%s)", c.URL) } - if m.ClientsDPI, err = c.Unifi.GetClientsDPI(m.Sites); err != nil { + if m.ClientsDPI, err = c.Unifi.GetClientsDPI(sites); err != nil { return nil, errors.Wrapf(err, "unifi.GetClientsDPI(%s)", c.URL) } } // Get all the points. - if m.Clients, err = c.Unifi.GetClients(m.Sites); err != nil { + if m.Clients, err = c.Unifi.GetClients(sites); err != nil { return nil, errors.Wrapf(err, "unifi.GetClients(%s)", c.URL) } - if m.Devices, err = c.Unifi.GetDevices(m.Sites); err != nil { + if m.Devices, err = c.Unifi.GetDevices(sites); err != nil { return nil, errors.Wrapf(err, "unifi.GetDevices(%s)", c.URL) } From 23fdaf41bed9c2c5c12221fc819f7dbaa89041d7 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Wed, 24 Jun 2020 03:11:58 -0700 Subject: [PATCH 16/18] add inputname const --- integrations/inputunifi/collector.go | 3 ++- integrations/inputunifi/input.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index b4eedd98..3ea4c342 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -152,7 +152,8 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { // augmentMetrics is our middleware layer between collecting metrics and writing them. // This is where we can manipuate the returned data or make arbitrary decisions. -// This function currently adds parent device names to client metrics. +// This method currently adds parent device names to client metrics and hashes PII. +// This method also converts our local *Metrics type into a slice of interfaces for poller. func (u *InputUnifi) augmentMetrics(c *Controller, metrics *Metrics) *poller.Metrics { if metrics == nil { return nil diff --git a/integrations/inputunifi/input.go b/integrations/inputunifi/input.go index 4d429ded..612ee064 100644 --- a/integrations/inputunifi/input.go +++ b/integrations/inputunifi/input.go @@ -15,6 +15,7 @@ import ( ) const ( + PluginName = "unifi" // PluginName is the name of this input plugin. defaultURL = "https://127.0.0.1:8443" defaultUser = "unifipoller" defaultPass = "unifipoller" @@ -67,7 +68,7 @@ func init() { // nolint: gochecknoinits u := &InputUnifi{} poller.NewInput(&poller.InputPlugin{ - Name: "unifi", + Name: PluginName, Input: u, // this library implements poller.Input interface for Metrics(). Config: u, // Defines our config data interface. }) From 79ede658828773cd545dde29c9512240e3113c12 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Thu, 25 Jun 2020 02:31:01 -0700 Subject: [PATCH 17/18] add anomalies and alarms --- integrations/inputunifi/collector.go | 22 ++++++++++++++++++++++ integrations/inputunifi/go.mod | 2 +- integrations/inputunifi/go.sum | 2 ++ integrations/inputunifi/input.go | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/integrations/inputunifi/collector.go b/integrations/inputunifi/collector.go index 3ea4c342..2d6d76fb 100644 --- a/integrations/inputunifi/collector.go +++ b/integrations/inputunifi/collector.go @@ -91,6 +91,28 @@ func (u *InputUnifi) collectControllerEvents(c *Controller) ([]interface{}, erro return nil, errors.Wrap(err, "unifi.GetSites()") } + if *c.SaveAnomal { + anom, err := c.Unifi.GetAnomalies(sites, time.Now().Add(-time.Hour)) + if err != nil { + return nil, errors.Wrap(err, "unifi.GetAnomalies()") + } + + for _, a := range anom { + logs = append(logs, a) + } + } + + if *c.SaveAlarms { + alarms, err := c.Unifi.GetAlarms(sites) + if err != nil { + return nil, errors.Wrap(err, "unifi.GetAlarms()") + } + + for _, a := range alarms { + logs = append(logs, a) + } + } + if *c.SaveEvents { events, err := c.Unifi.GetEvents(sites, time.Hour) if err != nil { diff --git a/integrations/inputunifi/go.mod b/integrations/inputunifi/go.mod index 955ff399..1718ea14 100644 --- a/integrations/inputunifi/go.mod +++ b/integrations/inputunifi/go.mod @@ -5,5 +5,5 @@ go 1.14 require ( github.com/pkg/errors v0.9.1 github.com/unifi-poller/poller v0.0.8-0.20200621214016-5d1ed3324a46 - github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6 + github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37 ) diff --git a/integrations/inputunifi/go.sum b/integrations/inputunifi/go.sum index 2d165cc8..5a7d916b 100644 --- a/integrations/inputunifi/go.sum +++ b/integrations/inputunifi/go.sum @@ -105,6 +105,8 @@ github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106 h1:eKErSqWD65 github.com/unifi-poller/unifi v0.0.5-0.20200621075746-253ccae7e106/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6 h1:KzQDaEhDxtDzoyfBPXzM/pwpD76+4Y74Y66XMhKt2pI= github.com/unifi-poller/unifi v0.0.5-0.20200622073824-4a29471d80f6/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= +github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37 h1:T2y8JWkjZd1vz2ZKu4vmmAk9s6PUwupuTldwhfww5xY= +github.com/unifi-poller/unifi v0.0.6-0.20200625090439-421046871a37/go.mod h1:L1kMRH2buZhB31vZnRC1im7Tk/4uD3ET4biwl2faYy8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/integrations/inputunifi/input.go b/integrations/inputunifi/input.go index 612ee064..d1723355 100644 --- a/integrations/inputunifi/input.go +++ b/integrations/inputunifi/input.go @@ -34,6 +34,8 @@ type InputUnifi struct { // Each polled controller may have its own configuration. type Controller struct { VerifySSL *bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` + 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"` SaveDPI *bool `json:"save_dpi" toml:"save_dpi" xml:"save_dpi" yaml:"save_dpi"` @@ -192,6 +194,14 @@ func (u *InputUnifi) setDefaults(c *Controller) { c.SaveEvents = &f } + if c.SaveAlarms == nil { + c.SaveAlarms = &f + } + + if c.SaveAnomal == nil { + c.SaveAnomal = &f + } + if c.URL == "" { c.URL = defaultURL } @@ -241,6 +251,14 @@ func (u *InputUnifi) setControllerDefaults(c *Controller) *Controller { c.SaveEvents = u.Default.SaveEvents } + if c.SaveAlarms == nil { + c.SaveAlarms = u.Default.SaveAlarms + } + + if c.SaveAnomal == nil { + c.SaveAnomal = u.Default.SaveAnomal + } + if c.URL == "" { c.URL = u.Default.URL } From 57470c307339c8ddd88cbb27a7c24387b8aa68ef Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Thu, 25 Jun 2020 22:26:30 -0700 Subject: [PATCH 18/18] fix up logging --- integrations/inputunifi/input.go | 4 +++- integrations/inputunifi/interface.go | 28 ++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/integrations/inputunifi/input.go b/integrations/inputunifi/input.go index d1723355..9129aa27 100644 --- a/integrations/inputunifi/input.go +++ b/integrations/inputunifi/input.go @@ -67,7 +67,9 @@ type Metrics struct { } func init() { // nolint: gochecknoinits - u := &InputUnifi{} + u := &InputUnifi{ + dynamic: make(map[string]*Controller), + } poller.NewInput(&poller.InputPlugin{ Name: PluginName, diff --git a/integrations/inputunifi/interface.go b/integrations/inputunifi/interface.go index 9810db44..b7df17da 100644 --- a/integrations/inputunifi/interface.go +++ b/integrations/inputunifi/interface.go @@ -24,8 +24,8 @@ func (u *InputUnifi) Initialize(l poller.Logger) error { u.Config = &Config{Disable: true} } - if u.Disable { - l.Logf("UniFi input plugin disabled or missing configuration!") + if u.Logger = l; u.Disable { + u.Logf("UniFi input plugin disabled or missing configuration!") return nil } @@ -34,12 +34,10 @@ func (u *InputUnifi) Initialize(l poller.Logger) error { } if len(u.Controllers) == 0 { - l.Logf("No controllers configured. Polling dynamic controllers only!") + u.Logf("No controllers configured. Polling dynamic controllers only! Defaults:") + u.logController(&u.Default) } - u.dynamic = make(map[string]*Controller) - u.Logger = l - for i, c := range u.Controllers { switch err := u.getUnifi(u.setControllerDefaults(c)); err { case nil: @@ -47,9 +45,9 @@ func (u *InputUnifi) Initialize(l poller.Logger) error { u.LogErrorf("checking sites on %s: %v", c.URL, err) } - u.Logf("Configured UniFi Controller %d:", i+1) + u.Logf("Configured UniFi Controller %d of %d:", i+1, len(u.Controllers)) default: - u.LogErrorf("Controller %d Auth or Connection Error, retrying: %v", i+1, err) + u.LogErrorf("Controller %d of %d Auth or Connection Error, retrying: %v", i+1, len(u.Controllers), err) } u.logController(c) @@ -59,19 +57,17 @@ func (u *InputUnifi) Initialize(l poller.Logger) error { } func (u *InputUnifi) logController(c *Controller) { - u.Logf(" => URL: %s", c.URL) + u.Logf(" => URL: %s (verify SSL: %v)", c.URL, *c.VerifySSL) if c.Unifi != nil { - u.Logf(" => Version: %s", c.Unifi.ServerVersion) + u.Logf(" => Version: %s (%s)", c.Unifi.ServerVersion, c.Unifi.UUID) } u.Logf(" => Username: %s (has password: %v)", c.User, c.Pass != "") - u.Logf(" => Hash PII: %v", *c.HashPII) - u.Logf(" => Verify SSL: %v", *c.VerifySSL) - u.Logf(" => Save DPI: %v", *c.SaveDPI) - u.Logf(" => Save IDS: %v", *c.SaveIDS) - u.Logf(" => Save Events: %v", *c.SaveEvents) - u.Logf(" => Save Sites: %v", *c.SaveSites) + u.Logf(" => Hash PII / Poll Sites: %v / %s", *c.HashPII, strings.Join(c.Sites, ", ")) + u.Logf(" => Save Sites / Save DPI: %v / %v (metrics)", *c.SaveSites, *c.SaveDPI) + u.Logf(" => Save Events / Save IDS: %v / %v (logs)", *c.SaveEvents, *c.SaveIDS) + u.Logf(" => Save Alarms / Anomalies: %v / %v (logs)", *c.SaveAlarms, *c.SaveAnomal) } // Events allows you to pull only events (and IDS) from the UniFi Controller.