Add events

This commit is contained in:
davidnewhall2
2020-06-20 03:41:21 -07:00
parent 4dc85d25cd
commit 6214a998cf
5 changed files with 75 additions and 49 deletions
+22 -4
View File
@@ -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()
+2 -2
View File
@@ -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
)
+6
View File
@@ -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=
+19 -30
View File
@@ -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
}
+26 -13
View File
@@ -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
}