Add basic auth support to elasticsearch input (#6122)

This commit is contained in:
Greg 2019-07-15 17:41:29 -06:00 committed by Daniel Nelson
parent 329179c0ff
commit 66beeb6523
2 changed files with 30 additions and 2 deletions

View File

@ -40,6 +40,10 @@ or [cluster-stats](https://www.elastic.co/guide/en/elasticsearch/reference/curre
## "breaker". Per default, all stats are gathered. ## "breaker". Per default, all stats are gathered.
# node_stats = ["jvm", "http"] # node_stats = ["jvm", "http"]
## HTTP Basic Authentication username and password.
# username = ""
# password = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"

View File

@ -119,6 +119,10 @@ const sampleConfig = `
## "breaker". Per default, all stats are gathered. ## "breaker". Per default, all stats are gathered.
# node_stats = ["jvm", "http"] # node_stats = ["jvm", "http"]
## HTTP Basic Authentication username and password.
# username = ""
# password = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"
@ -138,6 +142,8 @@ type Elasticsearch struct {
ClusterStats bool ClusterStats bool
ClusterStatsOnlyFromMaster bool ClusterStatsOnlyFromMaster bool
NodeStats []string NodeStats []string
Username string `toml:"username"`
Password string `toml:"password"`
tls.ClientConfig tls.ClientConfig
client *http.Client client *http.Client
@ -455,7 +461,16 @@ func (e *Elasticsearch) gatherClusterStats(url string, acc telegraf.Accumulator)
} }
func (e *Elasticsearch) getCatMaster(url string) (string, error) { func (e *Elasticsearch) getCatMaster(url string) (string, error) {
r, err := e.client.Get(url) req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
if e.Username != "" || e.Password != "" {
req.SetBasicAuth(e.Username, e.Password)
}
r, err := e.client.Do(req)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -478,7 +493,16 @@ func (e *Elasticsearch) getCatMaster(url string) (string, error) {
} }
func (e *Elasticsearch) gatherJsonData(url string, v interface{}) error { func (e *Elasticsearch) gatherJsonData(url string, v interface{}) error {
r, err := e.client.Get(url) req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if e.Username != "" || e.Password != "" {
req.SetBasicAuth(e.Username, e.Password)
}
r, err := e.client.Do(req)
if err != nil { if err != nil {
return err return err
} }