update per PR suggestions

This commit is contained in:
Cody Lee 2022-12-05 16:06:30 -06:00
parent 8273aafaf4
commit a148f669da
No known key found for this signature in database
2 changed files with 56 additions and 44 deletions

View File

@ -8,6 +8,8 @@ This is meant for InfluxDB users 1.8+ and 2.x series.
### InfluxDB 1.8+, 2.x ### InfluxDB 1.8+, 2.x
Note the use of `auth_token` to enable this mode.
```yaml ```yaml
influxdb: influxdb:
disable: false disable: false
@ -15,7 +17,7 @@ influxdb:
interval: "2m" interval: "2m"
# the influxdb url to post data # the influxdb url to post data
url: http://somehost:1234 url: http://somehost:1234
# the secret auth token # the secret auth token, this enables InfluxDB 1.8, 2.x compatibility.
auth_token: somesecret auth_token: somesecret
# the influxdb org # the influxdb org
org: my-org org: my-org
@ -27,6 +29,8 @@ influxdb:
### InfluxDB pre 1.8 ### InfluxDB pre 1.8
Note the lack of `auth_token` to enable this mode.
```yaml ```yaml
influxdb: influxdb:
disable: false disable: false

View File

@ -35,24 +35,30 @@ const (
// Config defines the data needed to store metrics in InfluxDB. // Config defines the data needed to store metrics in InfluxDB.
type Config struct { type Config struct {
Interval cnfg.Duration `json:"interval,omitempty" toml:"interval,omitempty" xml:"interval" yaml:"interval"` Interval cnfg.Duration `json:"interval,omitempty" toml:"interval,omitempty" xml:"interval" yaml:"interval"`
Version2 bool `json:"version2,omitempty" toml:"version2,omitempty" xml:"version2" yaml:"version2"`
// V1 // Pass controls the influxdb v1 password to write metrics with
User string `json:"user,omitempty" toml:"user,omitempty" xml:"user" yaml:"user"`
Pass string `json:"pass,omitempty" toml:"pass,omitempty" xml:"pass" yaml:"pass"` Pass string `json:"pass,omitempty" toml:"pass,omitempty" xml:"pass" yaml:"pass"`
DB string `json:"db,omitempty" toml:"db,omitempty" xml:"db" yaml:"db"` // User controls the influxdb v1 user to write metrics with
User string `json:"user,omitempty" toml:"user,omitempty" xml:"user" yaml:"user"`
// DB controls the influxdb v1 database to write metrics to
DB string `json:"db,omitempty" toml:"db,omitempty" xml:"db" yaml:"db"`
// V2 features // AuthToken is the secret for v2 influxdb
AuthToken string `json:"auth_token,omitempty" toml:"auth_token,omitempty" xml:"auth_token" yaml:"auth_token"` AuthToken string `json:"auth_token,omitempty" toml:"auth_token,omitempty" xml:"auth_token" yaml:"auth_token"`
Org string `json:"org,omitempty" toml:"org,omitempty" xml:"org" yaml:"org"` // Org is the influx org to put metrics under for v2 influxdb
Bucket string `json:"bucket,omitempty" toml:"bucket,omitempty" xml:"bucket" yaml:"bucket"` Org string `json:"org,omitempty" toml:"org,omitempty" xml:"org" yaml:"org"`
BatchSize uint `json:"batch_size,omitempty" toml:"batch_size,omitempty" xml:"batch_size" yaml:"batch_size"` // Bucket is the influx bucket to put metrics under for v2 influxdb
Bucket string `json:"bucket,omitempty" toml:"bucket,omitempty" xml:"bucket" yaml:"bucket"`
// BatchSize controls the async batch size for v2 influxdb client mode
BatchSize uint `json:"batch_size,omitempty" toml:"batch_size,omitempty" xml:"batch_size" yaml:"batch_size"`
// Common // URL details which influxdb url to use to report metrics to.
URL string `json:"url,omitempty" toml:"url,omitempty" xml:"url" yaml:"url"` URL string `json:"url,omitempty" toml:"url,omitempty" xml:"url" yaml:"url"`
// Disable when true will disable the influxdb output.
Disable bool `json:"disable" toml:"disable" xml:"disable,attr" yaml:"disable"` Disable bool `json:"disable" toml:"disable" xml:"disable,attr" yaml:"disable"`
// VerifySSL when true will require ssl verification.
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"`
// Save data for dead ports? ie. ports that are down or disabled. // DeadPorts when true will save data for dead ports, for example ports that are down or disabled.
DeadPorts bool `json:"dead_ports" toml:"dead_ports" xml:"dead_ports" yaml:"dead_ports"` DeadPorts bool `json:"dead_ports" toml:"dead_ports" xml:"dead_ports" yaml:"dead_ports"`
} }
@ -63,10 +69,11 @@ type InfluxDB struct {
// InfluxUnifi is returned by New() after you provide a Config. // InfluxUnifi is returned by New() after you provide a Config.
type InfluxUnifi struct { type InfluxUnifi struct {
Collector poller.Collect Collector poller.Collect
influxV1 influxV1.Client influxV1 influxV1.Client
influxV2 influx.Client influxV2 influx.Client
LastCheck time.Time LastCheck time.Time
IsVersion2 bool
*InfluxDB *InfluxDB
} }
@ -130,7 +137,7 @@ func (u *InfluxUnifi) Run(c poller.Collect) error {
u.setConfigDefaults() u.setConfigDefaults()
if u.Config.Version2 { if u.IsVersion2 {
// we're a version 2 // we're a version 2
tlsConfig := &tls.Config{InsecureSkipVerify: !u.VerifySSL} // nolint: gosec tlsConfig := &tls.Config{InsecureSkipVerify: !u.VerifySSL} // nolint: gosec
serverOptions := influx.DefaultOptions().SetTLSConfig(tlsConfig).SetBatchSize(u.BatchSize) serverOptions := influx.DefaultOptions().SetTLSConfig(tlsConfig).SetBatchSize(u.BatchSize)
@ -165,36 +172,37 @@ func (u *InfluxUnifi) setConfigDefaults() {
u.AuthToken = u.getPassFromFile(strings.TrimPrefix(u.AuthToken, "file://")) u.AuthToken = u.getPassFromFile(strings.TrimPrefix(u.AuthToken, "file://"))
} }
if u.AuthToken == "" { if u.AuthToken != "" {
u.AuthToken = "anonymous" // Version >= 1.8 influx
} u.IsVersion2 = true
if u.Org == "" {
u.Org = defaultInfluxOrg
}
if u.Org == "" { if u.Bucket == "" {
u.Org = defaultInfluxOrg u.Bucket = defaultInfluxBucket
} }
if u.Bucket == "" { if u.BatchSize == 0 {
u.Bucket = defaultInfluxBucket u.BatchSize = 20
} }
} else {
// Version < 1.8 influx
if u.User == "" {
u.User = defaultInfluxUser
}
if u.BatchSize == 0 { if strings.HasPrefix(u.Pass, "file://") {
u.BatchSize = 20 u.Pass = u.getPassFromFile(strings.TrimPrefix(u.Pass, "file://"))
} }
if u.User == "" { if u.Pass == "" {
u.User = defaultInfluxUser u.Pass = defaultInfluxUser
} }
if strings.HasPrefix(u.Pass, "file://") { if u.DB == "" {
u.Pass = u.getPassFromFile(strings.TrimPrefix(u.Pass, "file://")) u.DB = defaultInfluxDB
} }
if u.Pass == "" {
u.Pass = defaultInfluxUser
}
if u.DB == "" {
u.DB = defaultInfluxDB
} }
if u.Interval.Duration == 0 { if u.Interval.Duration == 0 {
@ -220,7 +228,7 @@ func (u *InfluxUnifi) getPassFromFile(filename string) string {
// Returns an error if influxdb calls fail, otherwise returns a report. // Returns an error if influxdb calls fail, otherwise returns a report.
func (u *InfluxUnifi) ReportMetrics(m *poller.Metrics, e *poller.Events) (*Report, error) { func (u *InfluxUnifi) ReportMetrics(m *poller.Metrics, e *poller.Events) (*Report, error) {
r := &Report{ r := &Report{
UseV2: u.Config.Version2, UseV2: u.IsVersion2,
Metrics: m, Metrics: m,
Events: e, Events: e,
ch: make(chan *metric), ch: make(chan *metric),
@ -229,7 +237,7 @@ func (u *InfluxUnifi) ReportMetrics(m *poller.Metrics, e *poller.Events) (*Repor
} }
defer close(r.ch) defer close(r.ch)
if u.Config.Version2 { if u.IsVersion2 {
// Make a new Influx Points Batcher. // Make a new Influx Points Batcher.
r.writer = u.influxV2.WriteAPI(u.Org, u.Bucket) r.writer = u.influxV2.WriteAPI(u.Org, u.Bucket)
@ -273,7 +281,7 @@ func (u *InfluxUnifi) collect(r report, ch chan *metric) {
m.TS = r.metrics().TS m.TS = r.metrics().TS
} }
if u.Config.Version2 { if u.IsVersion2 {
pt := influx.NewPoint(m.Table, m.Tags, m.Fields, m.TS) pt := influx.NewPoint(m.Table, m.Tags, m.Fields, m.TS)
r.batchV2(m, pt) r.batchV2(m, pt)
} else { } else {