poller

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 22 Imported by: 3

Documentation

Overview

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

Index

Constants

View Source
const DefaultConfFile = "/etc/unifi-poller/up.conf"

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

View Source
const ENVConfigPrefix = "UP_"

ENVConfigPrefix is the prefix appended to an env variable tag name before retrieving the value from the OS.

Variables

View Source
var Version = "development"

Version is injected by the Makefile

Functions

func StringInSlice

func StringInSlice(str string, slice []string) bool

StringInSlice returns true if a string is in a slice.

Types

type Config

type Config struct {
	Interval   Duration `json:"interval,omitempty" toml:"interval,omitempty" xml:"interval" yaml:"interval"`
	Debug      bool     `json:"debug" toml:"debug" xml:"debug" yaml:"debug"`
	Quiet      bool     `json:"quiet,omitempty" toml:"quiet,omitempty" xml:"quiet" yaml:"quiet"`
	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"`
	ReAuth     bool     `json:"reauthenticate" toml:"reauthenticate" xml:"reauthenticate" yaml:"reauthenticate"`
	InfxBadSSL bool     `json:"influx_insecure_ssl" toml:"influx_insecure_ssl" xml:"influx_insecure_ssl" yaml:"influx_insecure_ssl"`
	SaveSites  bool     `json:"save_sites,omitempty" toml:"save_sites,omitempty" xml:"save_sites" yaml:"save_sites"`
	Mode       string   `json:"mode" toml:"mode" xml:"mode" yaml:"mode"`
	HTTPListen string   `json:"http_listen" toml:"http_listen" xml:"http_listen" yaml:"http_listen"`
	Namespace  string   `json:"namespace" toml:"namespace" xml:"namespace" yaml:"namespace"`
	InfluxURL  string   `json:"influx_url,omitempty" toml:"influx_url,omitempty" xml:"influx_url" yaml:"influx_url"`
	InfluxUser string   `json:"influx_user,omitempty" toml:"influx_user,omitempty" xml:"influx_user" yaml:"influx_user"`
	InfluxPass string   `json:"influx_pass,omitempty" toml:"influx_pass,omitempty" xml:"influx_pass" yaml:"influx_pass"`
	InfluxDB   string   `json:"influx_db,omitempty" toml:"influx_db,omitempty" xml:"influx_db" yaml:"influx_db"`
	UnifiUser  string   `json:"unifi_user,omitempty" toml:"unifi_user,omitempty" xml:"unifi_user" yaml:"unifi_user"`
	UnifiPass  string   `json:"unifi_pass,omitempty" toml:"unifi_pass,omitempty" xml:"unifi_pass" yaml:"unifi_pass"`
	UnifiBase  string   `json:"unifi_url,omitempty" toml:"unifi_url,omitempty" xml:"unifi_url" yaml:"unifi_url"`
	Sites      []string `json:"sites,omitempty" toml:"sites,omitempty" xml:"sites" yaml:"sites"`
}

Config represents the data needed to poll a controller and report to influxdb. This is all of the data stored in the config file. Any with explicit defaults have omitempty on json and toml tags.

func (*Config) ParseENV

func (c *Config) ParseENV() error

ParseENV copies environment variables into configuration values. This is useful for Docker users that find it easier to pass ENV variables than a specific configuration file. Uses reflection to find struct tags. This method uses the json struct tag member to match environment variables. Use a custom tag name by changing "json" below, but that's overkill for this app.

func (*Config) ParseFile

func (c *Config) ParseFile(configFile string) error

ParseFile parses and returns our configuration data.

type Duration

type Duration struct{ time.Duration }

Duration is used to UnmarshalTOML into a time.Duration value.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(data []byte) (err error)

UnmarshalText parses a duration type from a config file.

type Flag

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

Flag represents the CLI args available and their settings.

func (*Flag) Parse

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

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

type UnifiPoller

type UnifiPoller struct {
	Influx     *influxunifi.InfluxUnifi
	Unifi      *unifi.Unifi
	Flag       *Flag
	Config     *Config
	LastCheck  time.Time
	sync.Mutex // locks the Unifi struct member when re-authing to unifi.
}

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

func New

func New() *UnifiPoller

New returns a new poller struct preloaded with default values. No need to call this if you call Start.c

func (*UnifiPoller) AugmentMetrics

func (u *UnifiPoller) AugmentMetrics(metrics *metrics.Metrics)

AugmentMetrics is our middleware layer between collecting metrics and writing them. This is where we can manipuate the returned data or make arbitrary decisions. This function currently adds parent device names to client metrics.

func (*UnifiPoller) CheckSites

func (u *UnifiPoller) CheckSites() error

CheckSites makes sure the list of provided sites exists on the controller. This does not run in Lambda (run-once) mode.

func (*UnifiPoller) CollectAndProcess

func (u *UnifiPoller) CollectAndProcess() error

CollectAndProcess collects measurements and then reports them to InfluxDB Can be called once or in a ticker loop. This function and all the ones below handle their own logging. An error is returned so the calling function may determine if there was a read or write error and act on it. This is currently called in two places in this library. One returns an error, one does not.

func (*UnifiPoller) CollectMetrics

func (u *UnifiPoller) CollectMetrics() (*metrics.Metrics, error)

CollectMetrics grabs all the measurements from a UniFi controller and returns them.

func (*UnifiPoller) DumpJSONPayload

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

DumpJSONPayload prints raw json from the UniFi Controller.

func (*UnifiPoller) ExportMetrics

func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error)

ExportMetrics updates the internal metrics provided via HTTP at /metrics for prometheus collection. This is run by Prometheus as CollectFn.

func (*UnifiPoller) GetFilteredSites

func (u *UnifiPoller) GetFilteredSites() (unifi.Sites, error)

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

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

GetInfluxDB returns an InfluxDB interface.

func (*UnifiPoller) GetUnifi

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

GetUnifi returns a UniFi controller interface.

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

func (u *UnifiPoller) LogExportReport(report *promunifi.Report)

LogExportReport is called after prometheus exports metrics. This is run by Prometheus as LoggingFn

func (*UnifiPoller) LogInfluxReport

func (u *UnifiPoller) LogInfluxReport(r *influxunifi.Report)

LogInfluxReport writes a log message after exporting to influxdb.

func (*UnifiPoller) Logf

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

Logf prints a log entry if quiet is false.

func (*UnifiPoller) PollController

func (u *UnifiPoller) PollController()

PollController runs forever, polling UniFi and pushing to InfluxDB This is started by Run() or RunBoth() after everything checks out.

func (*UnifiPoller) PrintRawAPIJSON

func (u *UnifiPoller) PrintRawAPIJSON(apiPath string) error

PrintRawAPIJSON prints the raw json for a user-provided path on a UniFi Controller.

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

func (u *UnifiPoller) RunPrometheus() error

RunPrometheus starts the web server and registers the collector.

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