diff --git a/metricbeat/module/elasticsearch/metricset.go b/metricbeat/module/elasticsearch/metricset.go index 508ac68431c0..ae36cf991367 100644 --- a/metricbeat/module/elasticsearch/metricset.go +++ b/metricbeat/module/elasticsearch/metricset.go @@ -22,6 +22,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "github.com/elastic/beats/v7/libbeat/common/productorigin" "github.com/elastic/beats/v7/metricbeat/helper" @@ -29,16 +30,13 @@ import ( "github.com/elastic/beats/v7/metricbeat/mb/parse" ) -const ( - defaultScheme = "http" - pathConfigKey = "path" -) - var ( // HostParser parses host urls for RabbitMQ management plugin HostParser = parse.URLHostParserBuilder{ - DefaultScheme: defaultScheme, - PathConfigKey: pathConfigKey, + DefaultScheme: "http", + DefaultUsername: os.Getenv("ELASTICSEARCH_READ_USERNAME"), + DefaultPassword: os.Getenv("ELASTICSEARCH_READ_PASSWORD"), + PathConfigKey: "path", }.Build() ) @@ -98,7 +96,7 @@ func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error) }{ Scope: ScopeNode, XPackEnabled: false, - ApiKey: "", + ApiKey: os.Getenv("ELASTICSEARCH_READ_API_KEY"), } if err := base.Module().UnpackConfig(&config); err != nil { return nil, err @@ -135,7 +133,7 @@ func (m *MetricSet) GetServiceURI() string { // SetServiceURI updates the URI of the Elasticsearch service being monitored by this metricset func (m *MetricSet) SetServiceURI(servicePath string) { m.servicePath = servicePath - m.HTTP.SetURI(m.GetServiceURI()) + m.SetURI(m.GetServiceURI()) } func (m *MetricSet) ShouldSkipFetch() (bool, error) { @@ -159,10 +157,9 @@ func (m *MetricSet) ShouldSkipFetch() (bool, error) { // GetMasterNodeID returns the ID of the Elasticsearch cluster's master node func (m *MetricSet) GetMasterNodeID() (string, error) { - http := m.HTTP resetURI := m.GetServiceURI() - content, err := fetchPath(http, resetURI, "_nodes/_master", "filter_path=nodes.*.name") + content, err := fetchPath(m.HTTP, resetURI, "_nodes/_master", "filter_path=nodes.*.name") if err != nil { return "", err } @@ -184,10 +181,9 @@ func (m *MetricSet) GetMasterNodeID() (string, error) { // IsMLockAllEnabled returns if the given Elasticsearch node has mlockall enabled func (m *MetricSet) IsMLockAllEnabled(nodeID string) (bool, error) { - http := m.HTTP resetURI := m.GetServiceURI() - content, err := fetchPath(http, resetURI, "_nodes/"+nodeID, "filter_path=nodes.*.process.mlockall") + content, err := fetchPath(m.HTTP, resetURI, "_nodes/"+nodeID, "filter_path=nodes.*.process.mlockall") if err != nil { return false, err }