Skip to content

Commit

Permalink
Add basic auth support to elasticsearch input (#6122)
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored and danielnelson committed Jul 15, 2019
1 parent 329179c commit 66beeb6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions plugins/inputs/elasticsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ or [cluster-stats](https://www.elastic.co/guide/en/elasticsearch/reference/curre
## "breaker". Per default, all stats are gathered.
# node_stats = ["jvm", "http"]

## HTTP Basic Authentication username and password.
# username = ""
# password = ""

## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
Expand Down
28 changes: 26 additions & 2 deletions plugins/inputs/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ const sampleConfig = `
## "breaker". Per default, all stats are gathered.
# node_stats = ["jvm", "http"]
## HTTP Basic Authentication username and password.
# username = ""
# password = ""
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
Expand All @@ -138,6 +142,8 @@ type Elasticsearch struct {
ClusterStats bool
ClusterStatsOnlyFromMaster bool
NodeStats []string
Username string `toml:"username"`
Password string `toml:"password"`
tls.ClientConfig

client *http.Client
Expand Down Expand Up @@ -455,7 +461,16 @@ func (e *Elasticsearch) gatherClusterStats(url string, acc telegraf.Accumulator)
}

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 {
return "", err
}
Expand All @@ -478,7 +493,16 @@ func (e *Elasticsearch) getCatMaster(url string) (string, 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 {
return err
}
Expand Down

0 comments on commit 66beeb6

Please sign in to comment.