diff --git a/examples/MANUAL.md b/examples/MANUAL.md index 0840f9a1..ea45d132 100644 --- a/examples/MANUAL.md +++ b/examples/MANUAL.md @@ -106,10 +106,10 @@ is provided so the application can be easily adapted to any environment. measurements at /metrics for collection by prometheus. Enabling this mode disables InfluxDB usage entirely. This is BETA. - http_listen default: 0.0.0.0:61317 + http_listen default: 0.0.0.0:9130 This option controls the IP and port the http listener uses when the mode is set to prometheus. This setting has no effect when other modes - are in use. + are in use. Metrics become available at the /metrics URI. influx_url default: http://127.0.0.1:8086 This is the URL where the Influx web server is available. diff --git a/main.go b/main.go index c9ec4f3c..f98e65c4 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( // Keep it simple. func main() { - if err := poller.Start(); err != nil { + if err := poller.New().Start(); err != nil { log.Fatalln("[ERROR]", err) } } diff --git a/poller/config.go b/poller/config.go index 1fdb0927..0f12ff08 100644 --- a/poller/config.go +++ b/poller/config.go @@ -32,7 +32,7 @@ const ( defaultInfluxURL = "http://127.0.0.1:8086" defaultUnifiUser = "influx" defaultUnifiURL = "https://127.0.0.1:8443" - defaultHTTPListen = ":61317" + defaultHTTPListen = "0.0.0.0:9130" ) // ENVConfigPrefix is the prefix appended to an env variable tag diff --git a/poller/prometheus.go b/poller/prometheus.go index 3cfb626f..c811ef47 100644 --- a/poller/prometheus.go +++ b/poller/prometheus.go @@ -12,7 +12,6 @@ import ( // HTTP at /metrics for prometheus collection. // This is run by Prometheus as CollectFn. func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) { - u.LastCheck = time.Now() m, err := u.CollectMetrics() if err != nil { u.LogErrorf("collecting metrics: %v", err) @@ -21,6 +20,7 @@ func (u *UnifiPoller) ExportMetrics() (*metrics.Metrics, error) { u.LogError(err, "re-authenticating") return nil, err } + if m, err = u.CollectMetrics(); err != nil { u.LogErrorf("collecting metrics: %v", err) return nil, err diff --git a/poller/start.go b/poller/start.go index 2d647ae8..9e808d5c 100644 --- a/poller/start.go +++ b/poller/start.go @@ -18,43 +18,57 @@ import ( "golift.io/unifi" ) -// Start begins the application from a CLI. -// Parses flags, parses config and executes Run(). -func Start() error { - log.SetFlags(log.LstdFlags) - up := &UnifiPoller{ - Flag: &Flag{}, +// New returns a new poller struct preloaded with default values. +// No need to call this if you call Start.c +func New() *UnifiPoller { + return &UnifiPoller{ Config: &Config{ - // Preload our defaults. InfluxURL: defaultInfluxURL, InfluxUser: defaultInfluxUser, InfluxPass: defaultInfluxPass, InfluxDB: defaultInfluxDB, UnifiUser: defaultUnifiUser, - UnifiPass: os.Getenv("UNIFI_PASSWORD"), // deprecated name. + UnifiPass: defaultUnifiUser, UnifiBase: defaultUnifiURL, Interval: Duration{defaultInterval}, Sites: []string{"all"}, HTTPListen: defaultHTTPListen, Namespace: appName, - }} + }, Flag: &Flag{}, + } +} + +// Start begins the application from a CLI. +// Parses flags, parses config and executes Run(). +func (u *UnifiPoller) Start() error { + log.SetFlags(log.LstdFlags) + up := New() up.Flag.Parse(os.Args[1:]) + if up.Flag.ShowVer { fmt.Printf("%s v%s\n", appName, Version) return nil // don't run anything else w/ version request. } + if up.Flag.DumpJSON == "" { // do not print this when dumping JSON. up.Logf("Loading Configuration File: %s", up.Flag.ConfigFile) } + // Parse config file. if err := up.Config.ParseFile(up.Flag.ConfigFile); err != nil { up.Flag.Usage() return err } + // Update Config with ENV variable overrides. if err := up.Config.ParseENV(); err != nil { return err } + + if up.Flag.DumpJSON != "" { + return up.DumpJSONPayload() + } + return up.Run() } @@ -65,6 +79,7 @@ func (f *Flag) Parse(args []string) { fmt.Printf("Usage: %s [--config=/path/to/up.conf] [--version]", appName) f.PrintDefaults() } + f.StringVarP(&f.DumpJSON, "dumpjson", "j", "", "This debug option prints a json payload and exits. See man page for more info.") f.StringVarP(&f.ConfigFile, "config", "c", DefaultConfFile, "Poller config file path.") @@ -74,17 +89,16 @@ func (f *Flag) Parse(args []string) { // Run invokes all the application logic and routines. func (u *UnifiPoller) Run() (err error) { - if u.Flag.DumpJSON != "" { - return u.DumpJSONPayload() - } if u.Config.Debug { log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) u.LogDebugf("Debug Logging Enabled") } + log.Printf("[INFO] UniFi Poller v%v Starting Up! PID: %d", Version, os.Getpid()) if err = u.GetUnifi(); err != nil { return err } + u.Logf("Polling UniFi Controller at %s v%s as user %s. Sites: %v", u.Config.UnifiBase, u.Unifi.ServerVersion, u.Config.UnifiUser, u.Config.Sites) @@ -149,5 +163,6 @@ func (u *UnifiPoller) GetUnifi() (err error) { return fmt.Errorf("unifi controller: %v", err) } u.LogDebugf("Authenticated with controller successfully") + return u.CheckSites() }