Update code for events
This commit is contained in:
parent
bea7e2926c
commit
035de35d84
|
|
@ -1,13 +1,5 @@
|
||||||
package poller
|
package poller
|
||||||
|
|
||||||
/*
|
|
||||||
I consider this file the pinacle example of how to allow a Go application to be configured from a file.
|
|
||||||
You can put your configuration into any file format: XML, YAML, JSON, TOML, and you can override any
|
|
||||||
struct member using an environment variable. The Duration type is also supported. All of the Config{}
|
|
||||||
and Duration{} types and methods are reusable in other projects. Just adjust the data in the struct to
|
|
||||||
meet your app's needs. See the New() procedure and Start() method in start.go for example usage.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -44,13 +36,14 @@ type Flags struct {
|
||||||
|
|
||||||
// Metrics is a type shared by the exporting and reporting packages.
|
// Metrics is a type shared by the exporting and reporting packages.
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
TS time.Time
|
TS time.Time
|
||||||
unifi.Sites
|
Sites []*unifi.Site
|
||||||
unifi.IDSList
|
IDSList []*unifi.IDS
|
||||||
unifi.Clients
|
Events []*unifi.Event
|
||||||
*unifi.Devices
|
Clients []*unifi.Client
|
||||||
SitesDPI []*unifi.DPITable
|
SitesDPI []*unifi.DPITable
|
||||||
ClientsDPI []*unifi.DPITable
|
ClientsDPI []*unifi.DPITable
|
||||||
|
*unifi.Devices
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config represents the core library input data.
|
// Config represents the core library input data.
|
||||||
|
|
|
||||||
|
|
@ -9,25 +9,23 @@ import (
|
||||||
// DumpJSONPayload prints raw json from the UniFi Controller. This is currently
|
// DumpJSONPayload prints raw json from the UniFi Controller. This is currently
|
||||||
// tied into the -j CLI arg, and is probably not very useful outside that context.
|
// tied into the -j CLI arg, and is probably not very useful outside that context.
|
||||||
func (u *UnifiPoller) DumpJSONPayload() (err error) {
|
func (u *UnifiPoller) DumpJSONPayload() (err error) {
|
||||||
u.Config.Quiet = true
|
|
||||||
split := strings.SplitN(u.Flags.DumpJSON, " ", 2)
|
split := strings.SplitN(u.Flags.DumpJSON, " ", 2)
|
||||||
filter := &Filter{Kind: split[0]}
|
filter := &Filter{Kind: split[0]}
|
||||||
|
|
||||||
|
// Allows you to grab a controller other than 0 from config.
|
||||||
if split2 := strings.Split(filter.Kind, ":"); len(split2) > 1 {
|
if split2 := strings.Split(filter.Kind, ":"); len(split2) > 1 {
|
||||||
filter.Kind = split2[0]
|
filter.Kind = split2[0]
|
||||||
filter.Unit, _ = strconv.Atoi(split2[1])
|
filter.Unit, _ = strconv.Atoi(split2[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used with "other"
|
||||||
if len(split) > 1 {
|
if len(split) > 1 {
|
||||||
filter.Path = split[1]
|
filter.Path = split[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As of now we only have one input plugin, so target that [0].
|
||||||
m, err := inputs[0].RawMetrics(filter)
|
m, err := inputs[0].RawMetrics(filter)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(string(m))
|
fmt.Println(string(m))
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ require (
|
||||||
github.com/prometheus/common v0.10.0
|
github.com/prometheus/common v0.10.0
|
||||||
github.com/prometheus/procfs v0.1.1 // indirect
|
github.com/prometheus/procfs v0.1.1 // indirect
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/unifi-poller/unifi v0.0.4
|
github.com/unifi-poller/unifi v0.0.5-0.20200619092006-d24c776a42f5
|
||||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
|
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
|
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
|
||||||
golift.io/cnfg v0.0.5
|
golift.io/cnfg v0.0.5
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,8 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
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.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
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/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 v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||||
github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM=
|
github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM=
|
||||||
|
|
@ -106,6 +108,8 @@ github.com/unifi-poller/unifi v0.0.3 h1:6pmjW7MuEEDKKvYoxjL3EZlaLOgmhYyxJBAg4X7G
|
||||||
github.com/unifi-poller/unifi v0.0.3/go.mod h1:DagVD/I+VMnVUHmTT4Fi76lPI+DHbuMwwtMIzanwMxM=
|
github.com/unifi-poller/unifi v0.0.3/go.mod h1:DagVD/I+VMnVUHmTT4Fi76lPI+DHbuMwwtMIzanwMxM=
|
||||||
github.com/unifi-poller/unifi v0.0.4 h1:NDTxHTdF0MAt1y1RU8J+MSqdYlO0CEIqlrktcj3y/og=
|
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.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=
|
||||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
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/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=
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package poller
|
package poller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -16,9 +15,8 @@ var (
|
||||||
|
|
||||||
// Input plugins must implement this interface.
|
// Input plugins must implement this interface.
|
||||||
type Input interface {
|
type Input interface {
|
||||||
Initialize(Logger) error // Called once on startup to initialize the plugin.
|
Initialize(Logger) error // Called once on startup to initialize the plugin.
|
||||||
Metrics() (*Metrics, bool, error) // Called every time new metrics are requested.
|
Metrics(*Filter) (*Metrics, error) // Called every time new metrics are requested.
|
||||||
MetricsFrom(*Filter) (*Metrics, bool, error) // Called every time new metrics are requested.
|
|
||||||
RawMetrics(*Filter) ([]byte, error)
|
RawMetrics(*Filter) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,10 +32,12 @@ type Filter struct {
|
||||||
Type string
|
Type string
|
||||||
Term string
|
Term string
|
||||||
Name string
|
Name string
|
||||||
|
Call string
|
||||||
Tags string
|
Tags string
|
||||||
Role string
|
Role string
|
||||||
Kind string
|
Kind string
|
||||||
Path string
|
Path string
|
||||||
|
Text string
|
||||||
Area int
|
Area int
|
||||||
Item int
|
Item int
|
||||||
Unit int
|
Unit int
|
||||||
|
|
@ -79,75 +79,43 @@ func (u *UnifiPoller) InitializeInputs() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metrics aggregates all the measurements from all configured inputs and returns them.
|
// Metrics aggregates all the measurements from filtered inputs and returns them.
|
||||||
func (u *UnifiPoller) Metrics() (*Metrics, bool, error) {
|
// Passing a null filter returns everything!
|
||||||
errs := []string{}
|
func (u *UnifiPoller) Metrics(filter *Filter) (*Metrics, error) {
|
||||||
metrics := &Metrics{}
|
metrics := &Metrics{}
|
||||||
ok := false
|
|
||||||
|
|
||||||
for _, input := range inputs {
|
|
||||||
m, _, err := input.Metrics()
|
|
||||||
if err != nil {
|
|
||||||
errs = append(errs, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if m == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = true
|
|
||||||
metrics = AppendMetrics(metrics, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if len(errs) > 0 {
|
|
||||||
err = fmt.Errorf(strings.Join(errs, ", ")) // nolint: goerr113
|
|
||||||
}
|
|
||||||
|
|
||||||
return metrics, ok, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// MetricsFrom aggregates all the measurements from filtered inputs and returns them.
|
|
||||||
func (u *UnifiPoller) MetricsFrom(filter *Filter) (*Metrics, bool, error) {
|
|
||||||
errs := []string{}
|
|
||||||
metrics := &Metrics{}
|
|
||||||
ok := false
|
|
||||||
|
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
if filter != nil && !strings.EqualFold(input.Name, filter.Name) {
|
if filter != nil && !strings.EqualFold(input.Name, filter.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
m, _, err := input.MetricsFrom(filter)
|
m, err := input.Metrics(filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err.Error())
|
return metrics, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if m == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = true
|
|
||||||
metrics = AppendMetrics(metrics, m)
|
metrics = AppendMetrics(metrics, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
return metrics, nil
|
||||||
|
|
||||||
if len(errs) > 0 {
|
|
||||||
err = fmt.Errorf(strings.Join(errs, ", ")) // nolint: goerr113
|
|
||||||
}
|
|
||||||
|
|
||||||
return metrics, ok, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendMetrics combined the metrics from two sources.
|
// AppendMetrics combines the metrics from two sources.
|
||||||
func AppendMetrics(existing *Metrics, m *Metrics) *Metrics {
|
func AppendMetrics(existing *Metrics, m *Metrics) *Metrics {
|
||||||
|
if existing == nil {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
if m == nil {
|
||||||
|
return existing
|
||||||
|
}
|
||||||
|
|
||||||
existing.SitesDPI = append(existing.SitesDPI, m.SitesDPI...)
|
existing.SitesDPI = append(existing.SitesDPI, m.SitesDPI...)
|
||||||
existing.Sites = append(existing.Sites, m.Sites...)
|
existing.Sites = append(existing.Sites, m.Sites...)
|
||||||
existing.ClientsDPI = append(existing.ClientsDPI, m.ClientsDPI...)
|
existing.ClientsDPI = append(existing.ClientsDPI, m.ClientsDPI...)
|
||||||
existing.Clients = append(existing.Clients, m.Clients...)
|
existing.Clients = append(existing.Clients, m.Clients...)
|
||||||
existing.IDSList = append(existing.IDSList, m.IDSList...)
|
existing.IDSList = append(existing.IDSList, m.IDSList...)
|
||||||
|
existing.Events = append(existing.Events, m.Events...)
|
||||||
|
|
||||||
if m.Devices == nil {
|
if m.Devices == nil {
|
||||||
return existing
|
return existing
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@ var (
|
||||||
// Collect is passed into output packages so they may collect metrics to output.
|
// Collect is passed into output packages so they may collect metrics to output.
|
||||||
// Output packages must implement this interface.
|
// Output packages must implement this interface.
|
||||||
type Collect interface {
|
type Collect interface {
|
||||||
Metrics() (*Metrics, bool, error)
|
Metrics(*Filter) (*Metrics, error)
|
||||||
MetricsFrom(*Filter) (*Metrics, bool, error)
|
|
||||||
Logger
|
Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ func (f *Flags) Parse(args []string) {
|
||||||
// 3. Start a web server and wait for Prometheus to poll the application for metrics.
|
// 3. Start a web server and wait for Prometheus to poll the application for metrics.
|
||||||
func (u *UnifiPoller) Run() error {
|
func (u *UnifiPoller) Run() error {
|
||||||
if u.Flags.DumpJSON != "" {
|
if u.Flags.DumpJSON != "" {
|
||||||
|
u.Config.Quiet = true
|
||||||
if err := u.InitializeInputs(); err != nil {
|
if err := u.InitializeInputs(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue