Add TLS support to nginx_plus, nginx_plus_api and nginx_vts (#6300)

This commit is contained in:
Daniel Nelson 2019-08-21 18:04:51 -07:00 committed by GitHub
parent 8b938f3bd4
commit 40bbd805b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 18 deletions

View File

@ -14,15 +14,16 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
type NginxPlus struct { type NginxPlus struct {
Urls []string Urls []string `toml:"urls"`
ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
client *http.Client client *http.Client
ResponseTimeout internal.Duration
} }
var sampleConfig = ` var sampleConfig = `
@ -31,6 +32,13 @@ var sampleConfig = `
# HTTP response timeout (default: 5s) # HTTP response timeout (default: 5s)
response_timeout = "5s" response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
` `
func (n *NginxPlus) SampleConfig() string { func (n *NginxPlus) SampleConfig() string {
@ -74,14 +82,20 @@ func (n *NginxPlus) Gather(acc telegraf.Accumulator) error {
} }
func (n *NginxPlus) createHttpClient() (*http.Client, error) { func (n *NginxPlus) createHttpClient() (*http.Client, error) {
if n.ResponseTimeout.Duration < time.Second { if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = time.Second * 5 n.ResponseTimeout.Duration = time.Second * 5
} }
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{}, Transport: &http.Transport{
Timeout: n.ResponseTimeout.Duration, TLSClientConfig: tlsConfig,
},
Timeout: n.ResponseTimeout.Duration,
} }
return client, nil return client, nil

View File

@ -9,17 +9,17 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
type NginxPlusApi struct { type NginxPlusApi struct {
Urls []string Urls []string `toml:"urls"`
ApiVersion int64 `toml:"api_version"`
ApiVersion int64 ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
client *http.Client client *http.Client
ResponseTimeout internal.Duration
} }
const ( const (
@ -49,6 +49,13 @@ var sampleConfig = `
# HTTP response timeout (default: 5s) # HTTP response timeout (default: 5s)
response_timeout = "5s" response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
` `
func (n *NginxPlusApi) SampleConfig() string { func (n *NginxPlusApi) SampleConfig() string {
@ -100,9 +107,16 @@ func (n *NginxPlusApi) createHttpClient() (*http.Client, error) {
n.ResponseTimeout.Duration = time.Second * 5 n.ResponseTimeout.Duration = time.Second * 5
} }
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{}, Transport: &http.Transport{
Timeout: n.ResponseTimeout.Duration, TLSClientConfig: tlsConfig,
},
Timeout: n.ResponseTimeout.Duration,
} }
return client, nil return client, nil

View File

@ -13,15 +13,16 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
type NginxVTS struct { type NginxVTS struct {
Urls []string Urls []string `toml:"urls"`
ResponseTimeout internal.Duration `toml:"response_timeout"`
tls.ClientConfig
client *http.Client client *http.Client
ResponseTimeout internal.Duration
} }
var sampleConfig = ` var sampleConfig = `
@ -30,6 +31,13 @@ var sampleConfig = `
## HTTP response timeout (default: 5s) ## HTTP response timeout (default: 5s)
response_timeout = "5s" response_timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
` `
func (n *NginxVTS) SampleConfig() string { func (n *NginxVTS) SampleConfig() string {
@ -77,9 +85,16 @@ func (n *NginxVTS) createHTTPClient() (*http.Client, error) {
n.ResponseTimeout.Duration = time.Second * 5 n.ResponseTimeout.Duration = time.Second * 5
} }
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{}, Transport: &http.Transport{
Timeout: n.ResponseTimeout.Duration, TLSClientConfig: tlsConfig,
},
Timeout: n.ResponseTimeout.Duration,
} }
return client, nil return client, nil