Move flags to config file.
This commit is contained in:
		
							parent
							
								
									bc5516dbb3
								
							
						
					
					
						commit
						f0ac1429a2
					
				| 
						 | 
					@ -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.
 | 
					* This application polls a Unifi Controller API for Client and Device Metrics.
 | 
				
			||||||
* The metrics are then stored in an InfluxDB instance.
 | 
					* The metrics are then stored in an InfluxDB instance.
 | 
				
			||||||
 | 
					* See the example configuration file for help configuring this application.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## OPTIONS
 | 
					## OPTIONS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`unifi-poller [-c <config file>] [-D] [-q] [-s] [-h] [-v]`
 | 
					`unifi-poller [-c <config file>] [-h] [-v]`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    -c, --config <file_path>
 | 
					    -c, --config <file_path>
 | 
				
			||||||
        Provide a configuration file (instead of the default).
 | 
					        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
 | 
					    -v, --version
 | 
				
			||||||
        Display version and exit.
 | 
					        Display version and exit.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,7 @@ package main
 | 
				
			||||||
import "time"
 | 
					import "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Version is loosely followed.
 | 
					// Version is loosely followed.
 | 
				
			||||||
var Version = "v0.2.1"
 | 
					var Version = "v0.3.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	// App defaults in case they're missing from the config.
 | 
						// App defaults in case they're missing from the config.
 | 
				
			||||||
| 
						 | 
					@ -15,11 +15,17 @@ const (
 | 
				
			||||||
	defaultInfxURL   = "http://127.0.0.1:8086"
 | 
						defaultInfxURL   = "http://127.0.0.1:8086"
 | 
				
			||||||
	defaultUnifUser  = "influx"
 | 
						defaultUnifUser  = "influx"
 | 
				
			||||||
	defaultUnifURL   = "https://127.0.0.1:8443"
 | 
						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.
 | 
					// Config represents the data needed to poll a controller and report to influxdb.
 | 
				
			||||||
type Config struct {
 | 
					type Config struct {
 | 
				
			||||||
	Interval   Dur    `json:"interval" toml:"interval" xml:"interval" yaml:"interval"`
 | 
						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"`
 | 
						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"`
 | 
						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"`
 | 
						InfluxPass string `json:"influx_pass" toml:"influx_pass" xml:"influx_pass" yaml:"influx_pass"`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,37 +15,22 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	flg.Usage = func() {
 | 
						configFile := parseFlags()
 | 
				
			||||||
		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.
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	log.Println("Unifi-Poller Starting Up! PID:", os.Getpid())
 | 
						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.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
 | 
				
			||||||
		log.Println("Debug Logging Enabled")
 | 
							log.Println("Debug Logging Enabled")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config, err := GetConfig(*configFile)
 | 
						log.Println("Loaded Configuration:", configFile)
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		flg.Usage()
 | 
					 | 
				
			||||||
		log.Fatalf("Config Error '%v': %v", *configFile, err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	log.Println("Loaded Configuration:", *configFile)
 | 
					 | 
				
			||||||
	// Create an authenticated session to the Unifi Controller.
 | 
						// 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 {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalln("Unifi Controller Error:", err)
 | 
							log.Fatalln("Unifi Controller Error:", err)
 | 
				
			||||||
	}
 | 
						} else if !config.Quiet {
 | 
				
			||||||
	if !*quiet {
 | 
					 | 
				
			||||||
		log.Println("Authenticated to Unifi Controller @", config.UnifiBase, "as user", config.UnifiUser)
 | 
							log.Println("Authenticated to Unifi Controller @", config.UnifiBase, "as user", config.UnifiUser)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	infdb, err := influx.NewHTTPClient(influx.HTTPConfig{
 | 
						infdb, err := influx.NewHTTPClient(influx.HTTPConfig{
 | 
				
			||||||
| 
						 | 
					@ -55,16 +40,29 @@ func main() {
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalln("InfluxDB Error:", err)
 | 
							log.Fatalln("InfluxDB Error:", err)
 | 
				
			||||||
	}
 | 
						} else if config.Quiet {
 | 
				
			||||||
	if *quiet {
 | 
							// Doing it this way allows debug error logs (line numbers, etc)
 | 
				
			||||||
		// Do it this way allows debug error logs (line numbers, etc)
 | 
					 | 
				
			||||||
		unidev.Debug = false
 | 
							unidev.Debug = false
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		log.Println("Logging Unifi Metrics to InfluXDB @", config.InfluxURL, "as user", config.InfluxUser)
 | 
							log.Println("Logging Unifi Metrics to InfluXDB @", config.InfluxURL, "as user", config.InfluxUser)
 | 
				
			||||||
		log.Println("Polling Unifi Controller, interval:", config.Interval.value)
 | 
							log.Println("Polling Unifi Controller, interval:", config.Interval.value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	log.Println("Everyting checks out! Beginning Poller Routine.")
 | 
						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.
 | 
					// GetConfig parses and returns our configuration data.
 | 
				
			||||||
| 
						 | 
					@ -78,6 +76,9 @@ func GetConfig(configFile string) (Config, error) {
 | 
				
			||||||
		UnifiUser:  defaultUnifUser,
 | 
							UnifiUser:  defaultUnifUser,
 | 
				
			||||||
		UnifiPass:  os.Getenv("UNIFI_PASSWORD"),
 | 
							UnifiPass:  os.Getenv("UNIFI_PASSWORD"),
 | 
				
			||||||
		UnifiBase:  defaultUnifURL,
 | 
							UnifiBase:  defaultUnifURL,
 | 
				
			||||||
 | 
							VerifySSL:  defaultVerifySSL,
 | 
				
			||||||
 | 
							Debug:      defaultDebug,
 | 
				
			||||||
 | 
							Quiet:      defaultQuiet,
 | 
				
			||||||
		Interval:   Dur{value: defaultInterval},
 | 
							Interval:   Dur{value: defaultInterval},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if buf, err := ioutil.ReadFile(configFile); err != nil {
 | 
						if buf, err := ioutil.ReadFile(configFile); err != nil {
 | 
				
			||||||
| 
						 | 
					@ -118,7 +119,7 @@ func (c *Config) PollUnifiController(infdb influx.Client, unifi *unidev.AuthedRe
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if !quiet {
 | 
							if !quiet {
 | 
				
			||||||
			log.Println("Logged client state. Clients:", len(clients), "- Devices:", len(devices))
 | 
								log.Println("Logged Unifi States. Clients:", len(clients), "- Devices:", len(devices))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,19 +1,32 @@
 | 
				
			||||||
# unifi-poller primary configuration file.               #
 | 
					# unifi-poller primary configuration file.               #
 | 
				
			||||||
# copy this file to: /usr/local/etc/unifi-poller/up.conf #
 | 
					# 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.
 | 
					# 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.
 | 
					# 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.
 | 
					# InfluxDB does not require auth by default, so the user/password are probably unimportant.
 | 
				
			||||||
influx_url = "http://127.0.0.1:8086"
 | 
					#influx_url = "http://127.0.0.1:8086"
 | 
				
			||||||
influx_user = "unifi"
 | 
					#influx_user = "unifi"
 | 
				
			||||||
influx_pass = "unifi"
 | 
					#influx_pass = "unifi"
 | 
				
			||||||
# Be sure to create this database.
 | 
					# Be sure to create this database.
 | 
				
			||||||
influx_db = "unifi"
 | 
					#influx_db = "unifi"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Make a read-only user in the Unifi Admin Settings.
 | 
					# 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_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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue