Call it mode.
This commit is contained in:
		
							parent
							
								
									e650f1f412
								
							
						
					
					
						commit
						df46550a4f
					
				|  | @ -83,9 +83,14 @@ is provided so the application can be easily adapted to any environment. | ||||||
|         errors will be logged. Using this with debug=true adds line numbers to |         errors will be logged. Using this with debug=true adds line numbers to | ||||||
|         any error logs. |         any error logs. | ||||||
| 
 | 
 | ||||||
|     lambda         default: false |     mode           default: "influx" | ||||||
|         * Until reports of astonishing success with this mode, it is EXPERIMENTAL. |         * Value: influx | ||||||
|         Setting this to true will invoke a run-once mode where the application |         This default mode runs this application as a daemon. It will poll | ||||||
|  |         the controller at the configured interval. Providing an invalid value | ||||||
|  |         will run in this default mode. | ||||||
|  | 
 | ||||||
|  |         * Value: influxlambda  -  (the only other available option right now) | ||||||
|  |         Setting this value will invoke a run-once mode where the application | ||||||
|         immediately polls the controller and reports the metrics to InfluxDB. |         immediately polls the controller and reports the metrics to InfluxDB. | ||||||
|         Then it exits. This mode is useful in an AWS Lambda or a crontab where |         Then it exits. This mode is useful in an AWS Lambda or a crontab where | ||||||
|         the execution timings are controlled. This mode may also be adapted |         the execution timings are controlled. This mode may also be adapted | ||||||
|  |  | ||||||
|  | @ -20,10 +20,16 @@ | ||||||
| # Recommend enabling debug with this setting for better error logging. | # Recommend enabling debug with this setting for better error logging. | ||||||
| #quiet = false | #quiet = false | ||||||
| 
 | 
 | ||||||
| # Lambda mode makes the application exit after collecting and reporting metrics one time. | # Which mode to run this application in. The default mode is "influx". Providing | ||||||
| # This mode requires an external process like an AWS Lambda or a simple crontab to keep | # an invalid mode will also result in "influx". In this default mode the application | ||||||
| # the timings accurate on UniFi Poller run intervals. | # runs as a daemon and polls the controller at the configured interval. | ||||||
| #lambda = false | # | ||||||
|  | # There is only one other option at this time: "influxlambda" | ||||||
|  | # | ||||||
|  | # Lambda mode makes the application exit after collecting and reporting metrics | ||||||
|  | # to InfluxDB one time. This mode requires an external process like an AWS Lambda | ||||||
|  | # or a simple crontab to keep the timings accurate on UniFi Poller run intervals. | ||||||
|  | #mode = "influx" | ||||||
| 
 | 
 | ||||||
| # If the poller experiences an error from the UniFi controller or from InfluxDB | # If the poller experiences an error from the UniFi controller or from InfluxDB | ||||||
| # it will exit. If you do not want it to exit, change max_errors to -1. You can | # it will exit. If you do not want it to exit, change max_errors to -1. You can | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ | ||||||
|  "interval": "30s", |  "interval": "30s", | ||||||
|  "debug": false, |  "debug": false, | ||||||
|  "quiet": false, |  "quiet": false, | ||||||
|  "lambda": false, |  "mode": "influx", | ||||||
|  "max_errors": 0, |  "max_errors": 0, | ||||||
|  "influx_url": "http://127.0.0.1:8086", |  "influx_url": "http://127.0.0.1:8086", | ||||||
|  "influx_user": "unifi", |  "influx_user": "unifi", | ||||||
|  |  | ||||||
|  | @ -36,11 +36,17 @@ | ||||||
|   <quiet>false</quiet> |   <quiet>false</quiet> | ||||||
| 
 | 
 | ||||||
|   <!-- |   <!-- | ||||||
|   # Lambda mode makes the application exit after collecting and reporting metrics one time. |   # Which mode to run this application in. The default mode is "influx". Providing | ||||||
|   # This mode requires an external process like an AWS Lambda or a simple crontab to keep |   # an invalid mode will also result in "influx". In this default mode the application | ||||||
|   # the timings accurate on UniFi Poller run intervals. |   # runs as a daemon and polls the controller at the configured interval. | ||||||
|  |   # | ||||||
|  |   # There is only one other option at this time: "influxlambda" | ||||||
|  |   # | ||||||
|  |   # Lambda mode makes the application exit after collecting and reporting metrics | ||||||
|  |   # to InfluxDB one time. This mode requires an external process like an AWS Lambda | ||||||
|  |   # or a simple crontab to keep the timings accurate on UniFi Poller run intervals. | ||||||
|   --> |   --> | ||||||
|   <lambda>false</lambda> |   <mode>influx</mode> | ||||||
| 
 | 
 | ||||||
|   <!-- |   <!-- | ||||||
|   # If the poller experiences an error from the UniFi controller or from InfluxDB |   # If the poller experiences an error from the UniFi controller or from InfluxDB | ||||||
|  |  | ||||||
|  | @ -21,10 +21,16 @@ debug: false | ||||||
| # Recommend enabling debug with this setting for better error logging. | # Recommend enabling debug with this setting for better error logging. | ||||||
| quiet: false | quiet: false | ||||||
| 
 | 
 | ||||||
| # Lambda mode makes the application exit after collecting and reporting metrics one time. | # Which mode to run this application in. The default mode is "influx". Providing | ||||||
| # This mode requires an external process like an AWS Lambda or a simple crontab to keep | # an invalid mode will also result in "influx". In this default mode the application | ||||||
| # the timings accurate on UniFi Poller run intervals. | # runs as a daemon and polls the controller at the configured interval. | ||||||
| lambda: false | # | ||||||
|  | # There is only one other option at this time: "influxlambda" | ||||||
|  | # | ||||||
|  | # Lambda mode makes the application exit after collecting and reporting metrics | ||||||
|  | # to InfluxDB one time. This mode requires an external process like an AWS Lambda | ||||||
|  | # or a simple crontab to keep the timings accurate on UniFi Poller run intervals. | ||||||
|  | mode: "influx" | ||||||
| 
 | 
 | ||||||
| # If the poller experiences an error from the UniFi controller or from InfluxDB | # If the poller experiences an error from the UniFi controller or from InfluxDB | ||||||
| # it will exit. If you do not want it to exit, change max_errors to -1. You can | # it will exit. If you do not want it to exit, change max_errors to -1. You can | ||||||
|  |  | ||||||
|  | @ -56,7 +56,7 @@ type Config struct { | ||||||
| 	Debug      bool     `json:"debug" toml:"debug" xml:"debug" yaml:"debug"` | 	Debug      bool     `json:"debug" toml:"debug" xml:"debug" yaml:"debug"` | ||||||
| 	Quiet      bool     `json:"quiet,_omitempty" toml:"quiet,_omitempty" xml:"quiet" yaml:"quiet"` | 	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"` | 	VerifySSL  bool     `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` | ||||||
| 	Lambda     bool     `json:"lambda" toml:"lambda" xml:"lambda" yaml:"lambda"` | 	Mode       string   `json:"mode" toml:"mode" xml:"mode" yaml:"mode"` | ||||||
| 	InfluxURL  string   `json:"influx_url,_omitempty" toml:"influx_url,_omitempty" xml:"influx_url" yaml:"influx_url"` | 	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"` | 	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"` | 	InfluxPass string   `json:"influx_pass,_omitempty" toml:"influx_pass,_omitempty" xml:"influx_pass" yaml:"influx_pass"` | ||||||
|  |  | ||||||
|  | @ -96,8 +96,8 @@ func (u *UnifiPoller) Run() (err error) { | ||||||
| 	if err = u.GetInfluxDB(); err != nil { | 	if err = u.GetInfluxDB(); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	switch { | 	switch strings.ToLower(u.Mode) { | ||||||
| 	case u.Lambda: | 	case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda": | ||||||
| 		u.LogDebugf("Lambda Mode Enabled") | 		u.LogDebugf("Lambda Mode Enabled") | ||||||
| 		return u.CollectAndReport() | 		return u.CollectAndReport() | ||||||
| 	default: | 	default: | ||||||
|  | @ -128,13 +128,12 @@ func (u *UnifiPoller) GetUnifi() (err error) { | ||||||
| 	} | 	} | ||||||
| 	u.Unifi.ErrorLog = u.LogErrorf // Log all errors.
 | 	u.Unifi.ErrorLog = u.LogErrorf // Log all errors.
 | ||||||
| 	u.Unifi.DebugLog = u.LogDebugf // Log debug messages.
 | 	u.Unifi.DebugLog = u.LogDebugf // Log debug messages.
 | ||||||
| 	// this may fail? but we'll try one more time with u.CheckSites below.
 |  | ||||||
| 	v, err := u.GetServer() | 	v, err := u.GetServer() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		v.ServerVersion = "unknown" | 		return err | ||||||
| 	} | 	} | ||||||
| 	u.Logf("Authenticated to UniFi Controller at %s version %s as user %s", u.UnifiBase, v.ServerVersion, u.UnifiUser) | 	u.Logf("Authenticated to UniFi Controller at %s version %s as user %s", u.UnifiBase, v.ServerVersion, u.UnifiUser) | ||||||
| 	if err = u.CheckSites(); err != nil { | 	if err := u.CheckSites(); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	u.Logf("Polling UniFi Controller Sites: %v", u.Sites) | 	u.Logf("Polling UniFi Controller Sites: %v", u.Sites) | ||||||
|  |  | ||||||
|  | @ -11,7 +11,11 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // CheckSites makes sure the list of provided sites exists on the controller.
 | // CheckSites makes sure the list of provided sites exists on the controller.
 | ||||||
|  | // This does not run in Lambda (run-once) mode.
 | ||||||
| func (u *UnifiPoller) CheckSites() error { | func (u *UnifiPoller) CheckSites() error { | ||||||
|  | 	if strings.Contains(strings.ToLower(u.Mode), "lambda") { | ||||||
|  | 		return nil // Skip this in lambda mode.
 | ||||||
|  | 	} | ||||||
| 	sites, err := u.GetSites() | 	sites, err := u.GetSites() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue