poller

package module
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2020 License: MIT Imports: 15 Imported by: 6

README

poller

UniFi Poller Core

This module ties the inputs together with the outputs.

Aggregates metrics on request. Provides CLI app and args parsing.

Ideal

This library has no notion of "UniFi" or controllers, or Influx, or Prometheus. This library simply provides an input interface and an output interface. Each interface uses an []interface{} type, so any type of data can be used. That is to say, you could write input and output plugins that work with, say, Cisco gear, or any other network (or even non-network) data. The existing plugins should provide ample example of how to use this library, but at some point the godoc will improve.

Features

  • Automatically unmarshal's plugin config structs from config file and/or env variables.
  • Initializes all "imported" plugins on startup.
  • Provides input plugins a Logger, requires an interface for Metrics and Events retrieval.
  • Provides Output plugins an interface to retrieve Metrics and Events, and a Logger.
  • Provides automatic aggregation of Metrics and Events from multiple sources.

Documentation

Overview

Package poller provides the CLI interface to setup unifi-poller.

Index

Constants

View Source
const (
	// AppName is the name of the application.
	AppName = "unifi-poller"
	// ENVConfigPrefix is the prefix appended to an env variable tag name.
	ENVConfigPrefix = "UP"
)
View Source
const DefaultConfFile = "/config/unifi-poller.conf,/etc/unifi-poller/up.conf"

DefaultConfFile is where to find config if --config is not prvided.

View Source
const DefaultObjPath = "/usr/lib/unifi-poller"

DefaultObjPath is the path to look for shared object libraries (plugins).

Variables

This section is empty.

Functions

func NewInput

func NewInput(i *InputPlugin)

NewInput creates a metric input. This should be called by input plugins init() functions.

func NewOutput

func NewOutput(o *Output)

NewOutput should be called by each output package's init function.

Types

type Collect

type Collect interface {
	Logger
	Metrics(*Filter) (*Metrics, error)
	Events(*Filter) (*Events, error)
	// These get used by the webserver output plugin.
	Poller() Poller
	Inputs() []string
	Outputs() []string
}

Collect is passed into output packages so they may collect metrics to output.

type Config

type Config struct {
	*Poller `json:"poller" toml:"poller" xml:"poller" yaml:"poller"`
}

Config represents the core library input data.

type Events added in v0.0.8

type Events struct {
	Logs []interface{}
}

Events defines the type for log entries.

type Filter

type Filter struct {
	Type string
	Term string
	Name string
	Role string
	Kind string
	Path string
	Text string
	Unit int
	Pass bool
	Skip bool
	Time time.Time
	Dur  time.Duration
}

Filter is used for metrics filters. Many fields for lots of expansion.

type Flags

type Flags struct {
	ConfigFile string
	DumpJSON   string
	HashPW     string
	ShowVer    bool
	*pflag.FlagSet
}

Flags represents the CLI args available and their settings.

func (*Flags) Parse

func (f *Flags) Parse(args []string)

Parse turns CLI arguments into data structures. Called by Start() on startup.

type Input

type Input interface {
	Initialize(Logger) error           // Called once on startup to initialize the plugin.
	Metrics(*Filter) (*Metrics, error) // Called every time new metrics are requested.
	Events(*Filter) (*Events, error)   // This is new.
	RawMetrics(*Filter) ([]byte, error)
}

Input plugins must implement this interface.

type InputPlugin

type InputPlugin struct {
	Name   string
	Config interface{} // Each config is passed into an unmarshaller later.
	Input
}

InputPlugin describes an input plugin's consumable interface.

type Logger

type Logger interface {
	Logf(m string, v ...interface{})
	LogErrorf(m string, v ...interface{})
	LogDebugf(m string, v ...interface{})
}

Logger is passed into input packages so they may write logs.

type Metrics

type Metrics struct {
	TS         time.Time
	Sites      []interface{}
	Clients    []interface{}
	SitesDPI   []interface{}
	ClientsDPI []interface{}
	Devices    []interface{}
}

Metrics is a type shared by the exporting and reporting packages.

func AppendMetrics

func AppendMetrics(existing *Metrics, m *Metrics) *Metrics

AppendMetrics combines the metrics from two sources.

type Output

type Output struct {
	Name   string
	Config interface{}         // Each config is passed into an unmarshaller later.
	Method func(Collect) error // Called on startup for each configured output.
}

Output defines the output data for a metric exporter like influx or prometheus. Output packages should call NewOutput with this struct in init().

type Poller

type Poller struct {
	Plugins []string `json:"plugins" toml:"plugins" xml:"plugin" yaml:"plugins"`
	Debug   bool     `json:"debug" toml:"debug" xml:"debug,attr" yaml:"debug"`
	Quiet   bool     `json:"quiet" toml:"quiet" xml:"quiet,attr" yaml:"quiet"`
}

Poller is the global config values.

type UnifiPoller

type UnifiPoller struct {
	Flags *Flags
	*Config
}

UnifiPoller contains the application startup data, and auth info for UniFi & Influx.

func New

func New() *UnifiPoller

New returns a new poller struct.

func (*UnifiPoller) Events added in v0.0.8

func (u *UnifiPoller) Events(filter *Filter) (*Events, error)

Events aggregates log messages (events) from one or more sources.

func (*UnifiPoller) InitializeInputs

func (u *UnifiPoller) InitializeInputs() error

InitializeInputs runs the passed-in initializer method for each input plugin.

func (*UnifiPoller) InitializeOutputs

func (u *UnifiPoller) InitializeOutputs() error

InitializeOutputs runs all the configured output plugins. If none exist, or they all exit an error is returned.

func (*UnifiPoller) Inputs added in v0.0.8

func (u *UnifiPoller) Inputs() (names []string)

Inputs allows output plugins to see the list of loaded input plugins.

func (*UnifiPoller) LoadPlugins

func (u *UnifiPoller) LoadPlugins() error

LoadPlugins reads-in dynamic shared libraries. Not used very often, if at all.

func (*UnifiPoller) LogDebugf

func (u *UnifiPoller) LogDebugf(m string, v ...interface{})

LogDebugf prints a debug log entry if debug is true and quite is false.

func (*UnifiPoller) LogErrorf

func (u *UnifiPoller) LogErrorf(m string, v ...interface{})

LogErrorf prints an error log entry.

func (*UnifiPoller) Logf

func (u *UnifiPoller) Logf(m string, v ...interface{})

Logf prints a log entry if quiet is false.

func (*UnifiPoller) Metrics

func (u *UnifiPoller) Metrics(filter *Filter) (*Metrics, error)

Metrics aggregates all the measurements from filtered inputs and returns them. Passing a null filter returns everything!

func (*UnifiPoller) Outputs added in v0.0.8

func (u *UnifiPoller) Outputs() (names []string)

Outputs allows other output plugins to see the list of loaded output plugins.

func (*UnifiPoller) ParseConfigs

func (u *UnifiPoller) ParseConfigs() error

ParseConfigs parses the poller config and the config for each registered output plugin.

func (*UnifiPoller) Poller added in v0.0.8

func (u *UnifiPoller) Poller() Poller

Poller returns the poller config.

func (*UnifiPoller) PrintPasswordHash added in v0.0.8

func (u *UnifiPoller) PrintPasswordHash() (err error)

PrintPasswordHash prints a bcrypt'd password. Useful for the web server.

func (*UnifiPoller) PrintRawMetrics added in v0.0.8

func (u *UnifiPoller) PrintRawMetrics() (err error)

PrintRawMetrics 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 (*UnifiPoller) Run

func (u *UnifiPoller) Run() error

Run picks a mode and executes the associated functions. This will do one of three things: 1. Start the collector routine that polls unifi and reports to influx on an interval. (default) 2. Run the collector one time and report the metrics to influxdb. (lambda) 3. Start a web server and wait for Prometheus to poll the application for metrics.

func (*UnifiPoller) Start

func (u *UnifiPoller) Start() error

Start begins the application from a CLI. Parses cli flags, parses config file, parses env vars, sets up logging, then: - dumps a json payload OR - executes Run().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL