overload the filter

This commit is contained in:
davidnewhall2 2020-06-21 03:26:14 -07:00
parent a103be56d8
commit 1656c205ed
4 changed files with 30 additions and 19 deletions

View File

@ -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)

View File

@ -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
)

View File

@ -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=

View File

@ -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.