should be good
This commit is contained in:
		
							parent
							
								
									e220610df7
								
							
						
					
					
						commit
						062fb797de
					
				|  | @ -106,6 +106,14 @@ | |||
|   pruneopts = "UT" | ||||
|   revision = "ac6580df4449443a05718fd7858c1f91ad5f8d20" | ||||
| 
 | ||||
| [[projects]] | ||||
|   branch = "master" | ||||
|   digest = "1:d54a8d89f95a4d2a5a24ce63cb1835ccdff337fde7776c87ceacb6fdbe4349ae" | ||||
|   name = "golift.io/config" | ||||
|   packages = ["."] | ||||
|   pruneopts = "UT" | ||||
|   revision = "fd8ffb02173aad2183e5555a03b1d1f909aca930" | ||||
| 
 | ||||
| [[projects]] | ||||
|   digest = "1:2883cea734f2766f41ff9c9d4aefccccc53e3d44f5c8b08893b9c218cf666722" | ||||
|   name = "golift.io/unifi" | ||||
|  | @ -126,14 +134,13 @@ | |||
|   analyzer-name = "dep" | ||||
|   analyzer-version = 1 | ||||
|   input-imports = [ | ||||
|     "github.com/BurntSushi/toml", | ||||
|     "github.com/influxdata/influxdb1-client/v2", | ||||
|     "github.com/prometheus/client_golang/prometheus", | ||||
|     "github.com/prometheus/client_golang/prometheus/promhttp", | ||||
|     "github.com/prometheus/common/version", | ||||
|     "github.com/spf13/pflag", | ||||
|     "golift.io/config", | ||||
|     "golift.io/unifi", | ||||
|     "gopkg.in/yaml.v2", | ||||
|   ] | ||||
|   solver-name = "gps-cdcl" | ||||
|   solver-version = 1 | ||||
|  |  | |||
|  | @ -72,16 +72,16 @@ type Controller struct { | |||
| // This is all of the data stored in the config file.
 | ||||
| // Any with explicit defaults have omitempty on json and toml tags.
 | ||||
| type Config struct { | ||||
| 	Interval   config.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"` | ||||
| 	InfxBadSSL bool            `json:"influx_insecure_ssl" toml:"influx_insecure_ssl" xml:"influx_insecure_ssl" yaml:"influx_insecure_ssl"` | ||||
| 	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"` | ||||
| 	Controller []Controller    `json:"controller,omitempty" toml:"controller,omitempty" xml:"controller" yaml:"controller"` | ||||
| 	Interval    config.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"` | ||||
| 	InfxBadSSL  bool            `json:"influx_insecure_ssl" toml:"influx_insecure_ssl" xml:"influx_insecure_ssl" yaml:"influx_insecure_ssl"` | ||||
| 	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"` | ||||
| 	Controllers []Controller    `json:"controller,omitempty" toml:"controller,omitempty" xml:"controller" yaml:"controller"` | ||||
| } | ||||
|  |  | |||
|  | @ -12,7 +12,7 @@ import ( | |||
| // This only works with controller 0 (first one) in the config.
 | ||||
| func (u *UnifiPoller) DumpJSONPayload() (err error) { | ||||
| 	u.Config.Quiet = true | ||||
| 	config := u.Config.Controller[0] | ||||
| 	config := u.Config.Controllers[0] | ||||
| 
 | ||||
| 	config.Unifi, err = unifi.NewUnifi(&unifi.Config{ | ||||
| 		User:      config.User, | ||||
|  |  | |||
|  | @ -17,7 +17,7 @@ import ( | |||
| func New() *UnifiPoller { | ||||
| 	return &UnifiPoller{ | ||||
| 		Config: &Config{ | ||||
| 			Controller: []Controller{{ | ||||
| 			Controllers: []Controller{{ | ||||
| 				Sites:     []string{"all"}, | ||||
| 				User:      defaultUnifiUser, | ||||
| 				Pass:      "", | ||||
|  | @ -65,7 +65,6 @@ func (u *UnifiPoller) Start() error { | |||
| 	if _, err := config.ParseENV(u.Config, ENVConfigPrefix); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	log.Println("START(): controller", u.Config.Controller) | ||||
| 	if u.Flag.DumpJSON != "" { | ||||
| 		return u.DumpJSONPayload() | ||||
| 	} | ||||
|  | @ -100,7 +99,7 @@ func (f *Flag) Parse(args []string) { | |||
| // 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 (u *UnifiPoller) Run() error { | ||||
| 	for _, c := range u.Config.Controller { | ||||
| 	for _, c := range u.Config.Controllers { | ||||
| 		switch err := u.GetUnifi(c); err { | ||||
| 		case nil: | ||||
| 			u.Logf("Polling UniFi Controller at %s v%s as user %s. Sites: %v", | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ func (u *UnifiPoller) GetUnifi(config Controller) error { | |||
| 		DebugLog:  u.LogDebugf, // Log debug messages.
 | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		config.Unifi = nil | ||||
| 		return fmt.Errorf("unifi controller: %v", err) | ||||
| 	} | ||||
| 
 | ||||
|  | @ -81,13 +82,8 @@ FIRST: | |||
| func (u *UnifiPoller) CollectMetrics() (metrics *metrics.Metrics, err error) { | ||||
| 	var errs []string | ||||
| 
 | ||||
| 	for _, c := range u.Config.Controller { | ||||
| 	for _, c := range u.Config.Controllers { | ||||
| 		m, err := u.collectController(c) | ||||
| 		if err != nil { | ||||
| 			errs = append(errs, err.Error()) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			u.LogErrorf("collecting metrics from %s: %v", c.URL, err) | ||||
| 			u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) | ||||
|  | @ -123,7 +119,7 @@ func (u *UnifiPoller) collectController(c Controller) (*metrics.Metrics, error) | |||
| 	if c.Unifi == nil { | ||||
| 		// Some users need to re-auth every interval because the cookie times out.
 | ||||
| 		// Sometimes we hit this path when the controller dies.
 | ||||
| 		u.Logf("Re-authenticating to UniFi Controller") | ||||
| 		u.Logf("Re-authenticating to UniFi Controller: %v", c.URL) | ||||
| 
 | ||||
| 		if err := u.GetUnifi(c); err != nil { | ||||
| 			return nil, err | ||||
|  | @ -134,23 +130,23 @@ func (u *UnifiPoller) collectController(c Controller) (*metrics.Metrics, error) | |||
| 
 | ||||
| 	// Get the sites we care about.
 | ||||
| 	if m.Sites, err = u.GetFilteredSites(c); err != nil { | ||||
| 		return m, fmt.Errorf("unifi.GetSites(): %v", err) | ||||
| 		return m, fmt.Errorf("unifi.GetSites(%v): %v", c.URL, err) | ||||
| 	} | ||||
| 
 | ||||
| 	if c.SaveIDS { | ||||
| 		m.IDSList, err = c.Unifi.GetIDS(m.Sites, time.Now().Add(u.Config.Interval.Duration), time.Now()) | ||||
| 		if err != nil { | ||||
| 			return m, fmt.Errorf("unifi.GetIDS(): %v", err) | ||||
| 			return m, fmt.Errorf("unifi.GetIDS(%v): %v", c.URL, err) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// Get all the points.
 | ||||
| 	if m.Clients, err = c.Unifi.GetClients(m.Sites); err != nil { | ||||
| 		return m, fmt.Errorf("unifi.GetClients(): %v", err) | ||||
| 		return m, fmt.Errorf("unifi.GetClients(%v): %v", c.URL, err) | ||||
| 	} | ||||
| 
 | ||||
| 	if m.Devices, err = c.Unifi.GetDevices(m.Sites); err != nil { | ||||
| 		return m, fmt.Errorf("unifi.GetDevices(): %v", err) | ||||
| 		return m, fmt.Errorf("unifi.GetDevices(%v): %v", c.URL, err) | ||||
| 	} | ||||
| 
 | ||||
| 	return u.augmentMetrics(c, m), nil | ||||
|  | @ -204,19 +200,19 @@ func (u *UnifiPoller) augmentMetrics(c Controller, metrics *metrics.Metrics) *me | |||
| // 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 (u *UnifiPoller) GetFilteredSites(config Controller) (unifi.Sites, error) { | ||||
| func (u *UnifiPoller) GetFilteredSites(c Controller) (unifi.Sites, error) { | ||||
| 	var i int | ||||
| 
 | ||||
| 	sites, err := config.Unifi.GetSites() | ||||
| 	sites, err := c.Unifi.GetSites() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} else if len(config.Sites) < 1 || StringInSlice("all", config.Sites) { | ||||
| 	} else if len(c.Sites) < 1 || StringInSlice("all", c.Sites) { | ||||
| 		return sites, nil | ||||
| 	} | ||||
| 
 | ||||
| 	for _, s := range sites { | ||||
| 		// Only include valid sites in the request filter.
 | ||||
| 		if StringInSlice(s.Name, config.Sites) { | ||||
| 		if StringInSlice(s.Name, c.Sites) { | ||||
| 			sites[i] = s | ||||
| 			i++ | ||||
| 		} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue