Update code for events
This commit is contained in:
parent
bea7e2926c
commit
035de35d84
|
|
@ -1,13 +1,5 @@
|
|||
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 (
|
||||
"os"
|
||||
"path"
|
||||
|
|
@ -44,13 +36,14 @@ type Flags struct {
|
|||
|
||||
// Metrics is a type shared by the exporting and reporting packages.
|
||||
type Metrics struct {
|
||||
TS time.Time
|
||||
unifi.Sites
|
||||
unifi.IDSList
|
||||
unifi.Clients
|
||||
*unifi.Devices
|
||||
TS time.Time
|
||||
Sites []*unifi.Site
|
||||
IDSList []*unifi.IDS
|
||||
Events []*unifi.Event
|
||||
Clients []*unifi.Client
|
||||
SitesDPI []*unifi.DPITable
|
||||
ClientsDPI []*unifi.DPITable
|
||||
*unifi.Devices
|
||||
}
|
||||
|
||||
// Config represents the core library input data.
|
||||
|
|
|
|||
|
|
@ -9,25 +9,23 @@ import (
|
|||
// 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.
|
||||
func (u *UnifiPoller) DumpJSONPayload() (err error) {
|
||||
u.Config.Quiet = true
|
||||
split := strings.SplitN(u.Flags.DumpJSON, " ", 2)
|
||||
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 {
|
||||
filter.Kind = split2[0]
|
||||
filter.Unit, _ = strconv.Atoi(split2[1])
|
||||
}
|
||||
|
||||
// Used with "other"
|
||||
if len(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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(m))
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ require (
|
|||
github.com/prometheus/common v0.10.0
|
||||
github.com/prometheus/procfs v0.1.1 // indirect
|
||||
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/sys v0.0.0-20200610111108-226ff32320da // indirect
|
||||
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/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.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 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.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=
|
||||
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=
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package poller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
|
|
@ -16,9 +15,8 @@ var (
|
|||
|
||||
// Input plugins must implement this interface.
|
||||
type Input interface {
|
||||
Initialize(Logger) error // Called once on startup to initialize the plugin.
|
||||
Metrics() (*Metrics, bool, error) // Called every time new metrics are requested.
|
||||
MetricsFrom(*Filter) (*Metrics, bool, error) // Called every time new metrics are requested.
|
||||
Initialize(Logger) error // Called once on startup to initialize the plugin.
|
||||
Metrics(*Filter) (*Metrics, error) // Called every time new metrics are requested.
|
||||
RawMetrics(*Filter) ([]byte, error)
|
||||
}
|
||||
|
||||
|
|
@ -34,10 +32,12 @@ type Filter struct {
|
|||
Type string
|
||||
Term string
|
||||
Name string
|
||||
Call string
|
||||
Tags string
|
||||
Role string
|
||||
Kind string
|
||||
Path string
|
||||
Text string
|
||||
Area int
|
||||
Item int
|
||||
Unit int
|
||||
|
|
@ -79,75 +79,43 @@ func (u *UnifiPoller) InitializeInputs() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Metrics aggregates all the measurements from all configured inputs and returns them.
|
||||
func (u *UnifiPoller) Metrics() (*Metrics, bool, error) {
|
||||
errs := []string{}
|
||||
// Metrics aggregates all the measurements from filtered inputs and returns them.
|
||||
// Passing a null filter returns everything!
|
||||
func (u *UnifiPoller) Metrics(filter *Filter) (*Metrics, error) {
|
||||
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 {
|
||||
if filter != nil && !strings.EqualFold(input.Name, filter.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
m, _, err := input.MetricsFrom(filter)
|
||||
m, err := input.Metrics(filter)
|
||||
if err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
return metrics, err
|
||||
}
|
||||
|
||||
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
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
// AppendMetrics combined the metrics from two sources.
|
||||
// AppendMetrics combines the metrics from two sources.
|
||||
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.Sites = append(existing.Sites, m.Sites...)
|
||||
existing.ClientsDPI = append(existing.ClientsDPI, m.ClientsDPI...)
|
||||
existing.Clients = append(existing.Clients, m.Clients...)
|
||||
existing.IDSList = append(existing.IDSList, m.IDSList...)
|
||||
existing.Events = append(existing.Events, m.Events...)
|
||||
|
||||
if m.Devices == nil {
|
||||
return existing
|
||||
|
|
|
|||
|
|
@ -16,8 +16,7 @@ var (
|
|||
// Collect is passed into output packages so they may collect metrics to output.
|
||||
// Output packages must implement this interface.
|
||||
type Collect interface {
|
||||
Metrics() (*Metrics, bool, error)
|
||||
MetricsFrom(*Filter) (*Metrics, bool, error)
|
||||
Metrics(*Filter) (*Metrics, error)
|
||||
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.
|
||||
func (u *UnifiPoller) Run() error {
|
||||
if u.Flags.DumpJSON != "" {
|
||||
u.Config.Quiet = true
|
||||
if err := u.InitializeInputs(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue