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

Allow Elasticsearch Auth and TLS #2308

Merged
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
40 changes: 29 additions & 11 deletions cmd/scollector/collectors/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,47 @@ func init() {
if instance.Port == 0 {
instance.Port = 9200
}
url := fmt.Sprintf("http://%v:%v", instance.Host, instance.Port)
instanceName := fmt.Sprintf("%v_%v", instance.Host, instance.Port)
if instance.Scheme == "" {
instance.Scheme = "http"
}
if instance.Name == "" {
instance.Name = fmt.Sprintf("%v_%v", instance.Host, instance.Port)
}
if instance.Disable {
slog.Infof("Elastic instance %v is disabled. Skipping.", instance.Name)
continue
}
var creds string
if instance.User != "" || instance.Password != "" {
creds = fmt.Sprintf("%v:%v@", instance.User, instance.Password)
} else {
creds = ""
}
url := fmt.Sprintf("%v://%v%v:%v", instance.Scheme, creds, instance.Host, instance.Port)
if instance.IndexInterval != "" {
indexInterval, err = time.ParseDuration(instance.IndexInterval)
if err != nil {
panic(fmt.Errorf("Failed to parse IndexInterval: %v, err: %v", instance.IndexInterval, err))
}
slog.Infof("Using IndexInterval: %v for %v", indexInterval, instanceName)
slog.Infof("Using IndexInterval: %v for %v", indexInterval, instance.Name)
} else {
slog.Infof("Using default IndexInterval: %v for %v", indexInterval, instanceName)
slog.Infof("Using default IndexInterval: %v for %v", indexInterval, instance.Name)
}
if instance.ClusterInterval != "" {
clusterInterval, err = time.ParseDuration(instance.ClusterInterval)
if err != nil {
panic(fmt.Errorf("Failed to parse ClusterInterval: %v, err: %v", instance.ClusterInterval, err))
}
slog.Infof("Using ClusterInterval: %v for %v", clusterInterval, instanceName)
slog.Infof("Using ClusterInterval: %v for %v", clusterInterval, instance.Name)
} else {
slog.Infof("Using default ClusterInterval: %v for %v", clusterInterval, instanceName)
slog.Infof("Using default ClusterInterval: %v for %v", clusterInterval, instance.Name)
}
// for legacy reasons, keep localhost:9200 named elasticsearch / elasticsearch-indices
var name string
if instanceName == "localhost_9200" {
if instance.Name == "localhost_9200" {
name = "elasticsearch"
} else {
name = fmt.Sprintf("elasticsearch-%v", instanceName)
name = fmt.Sprintf("elasticsearch-%v", instance.Name)
}
collectors = append(collectors, &IntervalCollector{
F: func() (opentsdb.MultiDataPoint, error) {
Expand All @@ -83,10 +98,10 @@ func init() {
Enable: enableURL(url),
})
// keep legacy collector name if localhost_9200
if instanceName == "localhost_9200" {
if instance.Name == "localhost_9200" {
name = "elasticsearch-indices"
} else {
name = fmt.Sprintf("elasticsearch-indices-%v", instanceName)
name = fmt.Sprintf("elasticsearch-indices-%v", instance.Name)
}
collectors = append(collectors, &IntervalCollector{
F: func() (opentsdb.MultiDataPoint, error) {
Expand Down Expand Up @@ -309,14 +324,17 @@ func esSkipIndex(index string) bool {
}

func esReq(instance conf.Elastic, path, query string, v interface{}) error {
up := url.UserPassword(instance.User, instance.Password)
u := &url.URL{
Scheme: "http",
Scheme: instance.Scheme,
User: up,
Host: fmt.Sprintf("%v:%v", instance.Host, instance.Port),
Path: path,
RawQuery: query,
}
resp, err := http.Get(u.String())
if err != nil {
slog.Errorf("Error querying Elasticsearch: %v", err)
return nil
}
defer resp.Body.Close()
Expand Down
1 change: 1 addition & 0 deletions cmd/scollector/collectors/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func enableURL(url string, regexes ...string) func() bool {
resp.Body.Close()
}()
if resp.StatusCode != 200 {
slog.Infof("URL not enabled: %v; HTTP Response not 200, but %v", url, resp.StatusCode)
return false
}
if len(res) == 0 {
Expand Down
5 changes: 5 additions & 0 deletions cmd/scollector/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,9 @@ type Elastic struct {
Port uint16 // default is 9200
ClusterInterval string // default is DefaultFreq
IndexInterval string // default is 15 mins
User string // default is empty
Password string // default is empty
Disable bool // default is false.
Name string // default is host_port
Scheme string // default is http
}
30 changes: 17 additions & 13 deletions cmd/scollector/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,19 +414,23 @@ ConnectionString and Role, which are the same as using sqlplus.

By default Elastic nodes are auto-detected on localhost:9200, but if you have a
node running on another network interface, a non-standard port or even multiple
nodes running on the same host you can use the Elastic configuration:

[[Elastic]]
Host = "192.168.1.1"
Port = 9201
ClusterInterval = "10s"
IndexInterval = "1m"

[[Elastic]]
Host = "192.168.1.1"
Port = 9202
ClusterInterval = "10s"
IndexInterval = "1m"
nodes running on the same host you can use the Elastic configuration. Also lets
you specify basic auth credentials and using TLS by setting the Scheme to https:

[[Elastic]]
Host = "192.168.1.1"
Port = 9201
ClusterInterval = "10s"
IndexInterval = "1m"
User = "user"
Password = "pass"
Scheme = "https"

[[Elastic]]
Host = "192.168.1.1"
Port = 9202
ClusterInterval = "10s"
IndexInterval = "1m"

Windows

Expand Down