Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic auth support to elasticsearch input #6122

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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