From aacc884ed616d410a0da8ecfe20dbd72cc673dc8 Mon Sep 17 00:00:00 2001 From: DN2 Date: Fri, 11 Jan 2019 21:08:58 -0800 Subject: [PATCH] Move flags to config file. --- cmd/unifi-poller/README.md | 14 ++------- cmd/unifi-poller/config.go | 24 ++++++++++------ cmd/unifi-poller/main.go | 59 +++++++++++++++++++------------------- up.conf.example | 27 ++++++++++++----- 4 files changed, 67 insertions(+), 57 deletions(-) diff --git a/cmd/unifi-poller/README.md b/cmd/unifi-poller/README.md index 95dd06ee..e28fbeee 100644 --- a/cmd/unifi-poller/README.md +++ b/cmd/unifi-poller/README.md @@ -9,25 +9,15 @@ unifi-poller(1) -- Utility to poll Unifi Metrics and drop them into InfluxDB * This application polls a Unifi Controller API for Client and Device Metrics. * The metrics are then stored in an InfluxDB instance. +* See the example configuration file for help configuring this application. ## OPTIONS -`unifi-poller [-c ] [-D] [-q] [-s] [-h] [-v]` +`unifi-poller [-c ] [-h] [-v]` -c, --config Provide a configuration file (instead of the default). - -D, --debug - Turns on line numbers, microsecond logging, and a per-device log. - - -q, --quiet - Turns off per-device log and per-interval log. Logs only errors. - Recommend using -D with this setting for better error logging. - - -s, --verify-ssl - If your Unifi controller has a valid SSL certificate, you can enable - this option to validate it. Otherwise, any SSL certificate is valid. - -v, --version Display version and exit. diff --git a/cmd/unifi-poller/config.go b/cmd/unifi-poller/config.go index 1eec3702..cd20c8fa 100644 --- a/cmd/unifi-poller/config.go +++ b/cmd/unifi-poller/config.go @@ -3,23 +3,29 @@ package main import "time" // Version is loosely followed. -var Version = "v0.2.1" +var Version = "v0.3.0" const ( // App defaults in case they're missing from the config. - defaultConfFile = "/usr/local/etc/unifi-poller/up.conf" - defaultInterval = 30 * time.Second - defaultInfxDb = "unifi" - defaultInfxUser = "unifi" - defaultInfxPass = "unifi" - defaultInfxURL = "http://127.0.0.1:8086" - defaultUnifUser = "influx" - defaultUnifURL = "https://127.0.0.1:8443" + defaultConfFile = "/usr/local/etc/unifi-poller/up.conf" + defaultInterval = 30 * time.Second + defaultInfxDb = "unifi" + defaultInfxUser = "unifi" + defaultInfxPass = "unifi" + defaultInfxURL = "http://127.0.0.1:8086" + defaultUnifUser = "influx" + defaultUnifURL = "https://127.0.0.1:8443" + defaultVerifySSL = false + defaultDebug = false + defaultQuiet = false ) // Config represents the data needed to poll a controller and report to influxdb. type Config struct { Interval Dur `json:"interval" toml:"interval" xml:"interval" yaml:"interval"` + Debug bool `json:"debug" toml:"debug" xml:"debug" yaml:"debug"` + Quiet bool `json:"quiet" toml:"quiet" xml:"quiet" yaml:"quiet"` + VerifySSL bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` InfluxURL string `json:"influx_url" toml:"influx_url" xml:"influx_url" yaml:"influx_url"` InfluxUser string `json:"influx_user" toml:"influx_user" xml:"influx_user" yaml:"influx_user"` InfluxPass string `json:"influx_pass" toml:"influx_pass" xml:"influx_pass" yaml:"influx_pass"` diff --git a/cmd/unifi-poller/main.go b/cmd/unifi-poller/main.go index 8c3ced1f..f749fc1e 100644 --- a/cmd/unifi-poller/main.go +++ b/cmd/unifi-poller/main.go @@ -15,37 +15,22 @@ import ( ) func main() { - flg.Usage = func() { - fmt.Println("Usage: unifi-poller [--config=filepath] [--debug] [--version]") - flg.PrintDefaults() - } - configFile := flg.StringP("config", "c", defaultConfFile, "Poller Config File (TOML Format)") - flg.BoolVarP(&unidev.Debug, "debug", "D", false, "Turn on the Spam (default false)") - quiet := flg.BoolP("quiet", "q", false, "Do not print logs on every poll, only errors") - version := flg.BoolP("version", "v", false, "Print the version and exit") - verifySSL := flg.BoolP("verify-ssl", "s", false, "If your controller has a valid SSL cert, require it with this flag") - - if flg.Parse(); *version { - fmt.Println("unifi-poller version:", Version) - os.Exit(0) // don't run anything else. - } + configFile := parseFlags() log.Println("Unifi-Poller Starting Up! PID:", os.Getpid()) - if log.SetFlags(0); unidev.Debug { + config, err := GetConfig(configFile) + if err != nil { + flg.Usage() + log.Fatalf("Config Error '%v': %v", configFile, err) + } else if log.SetFlags(0); config.Debug { log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) log.Println("Debug Logging Enabled") } - config, err := GetConfig(*configFile) - if err != nil { - flg.Usage() - log.Fatalf("Config Error '%v': %v", *configFile, err) - } - log.Println("Loaded Configuration:", *configFile) + log.Println("Loaded Configuration:", configFile) // Create an authenticated session to the Unifi Controller. - unifi, err := unidev.AuthController(config.UnifiUser, config.UnifiPass, config.UnifiBase, *verifySSL) + unifi, err := unidev.AuthController(config.UnifiUser, config.UnifiPass, config.UnifiBase, config.VerifySSL) if err != nil { log.Fatalln("Unifi Controller Error:", err) - } - if !*quiet { + } else if !config.Quiet { log.Println("Authenticated to Unifi Controller @", config.UnifiBase, "as user", config.UnifiUser) } infdb, err := influx.NewHTTPClient(influx.HTTPConfig{ @@ -55,16 +40,29 @@ func main() { }) if err != nil { log.Fatalln("InfluxDB Error:", err) - } - if *quiet { - // Do it this way allows debug error logs (line numbers, etc) + } else if config.Quiet { + // Doing it this way allows debug error logs (line numbers, etc) unidev.Debug = false } else { log.Println("Logging Unifi Metrics to InfluXDB @", config.InfluxURL, "as user", config.InfluxUser) log.Println("Polling Unifi Controller, interval:", config.Interval.value) } log.Println("Everyting checks out! Beginning Poller Routine.") - config.PollUnifiController(infdb, unifi, *quiet) + config.PollUnifiController(infdb, unifi, config.Quiet) +} + +func parseFlags() string { + flg.Usage = func() { + fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") + flg.PrintDefaults() + } + configFile := flg.StringP("config", "c", defaultConfFile, "Poller Config File (TOML Format)") + version := flg.BoolP("version", "v", false, "Print the version and exit") + if flg.Parse(); *version { + fmt.Println("unifi-poller version:", Version) + os.Exit(0) // don't run anything else. + } + return *configFile } // GetConfig parses and returns our configuration data. @@ -78,6 +76,9 @@ func GetConfig(configFile string) (Config, error) { UnifiUser: defaultUnifUser, UnifiPass: os.Getenv("UNIFI_PASSWORD"), UnifiBase: defaultUnifURL, + VerifySSL: defaultVerifySSL, + Debug: defaultDebug, + Quiet: defaultQuiet, Interval: Dur{value: defaultInterval}, } if buf, err := ioutil.ReadFile(configFile); err != nil { @@ -118,7 +119,7 @@ func (c *Config) PollUnifiController(infdb influx.Client, unifi *unidev.AuthedRe continue } if !quiet { - log.Println("Logged client state. Clients:", len(clients), "- Devices:", len(devices)) + log.Println("Logged Unifi States. Clients:", len(clients), "- Devices:", len(devices)) } } } diff --git a/up.conf.example b/up.conf.example index 8cdc6583..30f411f8 100644 --- a/up.conf.example +++ b/up.conf.example @@ -1,19 +1,32 @@ # unifi-poller primary configuration file. # # copy this file to: /usr/local/etc/unifi-poller/up.conf # +# commented lines are defaults, uncomment to change. # ########################################################## # The Unifi Controller only updates traffic stats about every 30 seconds. # Setting this to something lower may lead to "zeros" in your data. You've been warned. -interval = "30s" +#interval = "30s" + +# Turns on line numbers, microsecond logging, and a per-device log. +#debug = false + +# Turns off per-device log and per-interval log. Logs only errors. +# Recommend using debug with this setting for better error logging. +#quiet = false # InfluxDB does not require auth by default, so the user/password are probably unimportant. -influx_url = "http://127.0.0.1:8086" -influx_user = "unifi" -influx_pass = "unifi" +#influx_url = "http://127.0.0.1:8086" +#influx_user = "unifi" +#influx_pass = "unifi" # Be sure to create this database. -influx_db = "unifi" +#influx_db = "unifi" # Make a read-only user in the Unifi Admin Settings. -unifi_user = "influxdb" +#unifi_user = "influxdb" +# You may also set env variable UNIFI_PASSWORD instead of putting this in the config. unifi_pass = "4BB9345C-2341-48D7-99F5-E01B583FF77F" -unifi_url = "https://127.0.0.1:8443" +#unifi_url = "https://127.0.0.1:8443" + +# If your Unifi controller has a valid SSL certificate, you can enable +# this option to validate it. Otherwise, any SSL certificate is valid. +#verify_ssl = false