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

Fix Elasticsearch plugin. #950

Merged
merged 3 commits into from Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 53 additions & 19 deletions mackerel-plugin-elasticsearch/lib/elasticsearch.go
@@ -1,6 +1,7 @@
package mpelasticsearch

import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
Expand All @@ -25,42 +26,45 @@ var metricPlace = map[string][]string{
"total_refresh": {"indices", "refresh", "total"},
"total_flush": {"indices", "flush", "total"},
"total_warmer": {"indices", "warmer", "total"},
"total_percolate": {"indices", "percolate", "total"},
"total_suggest": {"indices", "suggest", "total"},
"total_percolate": {"indices", "percolate", "total"}, // MISSINGv7 = no value after v7.0 (at least)
"total_suggest": {"indices", "suggest", "total"}, // MISSINGv7
"docs_count": {"indices", "docs", "count"},
"docs_deleted": {"indices", "docs", "deleted"},
"fielddata_size": {"indices", "fielddata", "memory_size_in_bytes"},
"filter_cache_size": {"indices", "filter_cache", "memory_size_in_bytes"},
"filter_cache_size": {"indices", "filter_cache", "memory_size_in_bytes"}, // MISSINGv7
"segments_size": {"indices", "segments", "memory_in_bytes"},
"segments_index_writer_size": {"indices", "segments", "index_writer_memory_in_bytes"},
"segments_version_map_size": {"indices", "segments", "version_map_memory_in_bytes"},
"segments_fixed_bit_set_size": {"indices", "segments", "fixed_bit_set_memory_in_bytes"},
"evictions_fielddata": {"indices", "fielddata", "evictions"},
"evictions_filter_cache": {"indices", "filter_cache", "evictions"},
"evictions_filter_cache": {"indices", "filter_cache", "evictions"}, // MISSINGv7
"heap_used": {"jvm", "mem", "heap_used_in_bytes"},
"heap_max": {"jvm", "mem", "heap_max_in_bytes"},
"threads_generic": {"thread_pool", "generic", "threads"},
"threads_index": {"thread_pool", "index", "threads"},
"threads_snapshot_data": {"thread_pool", "snapshot_data", "threads"},
"threads_index": {"thread_pool", "index", "threads"}, // MISSINGv7
"threads_snapshot_data": {"thread_pool", "snapshot_data", "threads"}, // MISSINGv7
"threads_get": {"thread_pool", "get", "threads"},
"threads_bench": {"thread_pool", "bench", "threads"},
"threads_bench": {"thread_pool", "bench", "threads"}, // MISSINGv7
"threads_snapshot": {"thread_pool", "snapshot", "threads"},
"threads_merge": {"thread_pool", "merge", "threads"},
"threads_suggest": {"thread_pool", "suggest", "threads"},
"threads_bulk": {"thread_pool", "bulk", "threads"},
"threads_optimize": {"thread_pool", "optimize", "threads"},
"threads_merge": {"thread_pool", "merge", "threads"}, // MISSINGv7
"threads_suggest": {"thread_pool", "suggest", "threads"}, // MISSINGv7
"threads_bulk": {"thread_pool", "bulk", "threads"}, // MISSINGv7
"threads_optimize": {"thread_pool", "optimize", "threads"}, // MISSINGv7
"threads_warmer": {"thread_pool", "warmer", "threads"},
"threads_flush": {"thread_pool", "flush", "threads"},
"threads_search": {"thread_pool", "search", "threads"},
"threads_percolate": {"thread_pool", "percolate", "threads"},
"threads_percolate": {"thread_pool", "percolate", "threads"}, // MISSINGv7
"threads_refresh": {"thread_pool", "refresh", "threads"},
"threads_management": {"thread_pool", "management", "threads"},
"threads_fetch_shard_started": {"thread_pool", "fetch_shard_started", "threads"},
"threads_fetch_shard_store": {"thread_pool", "fetch_shard_store", "threads"},
"threads_listener": {"thread_pool", "listener", "threads"},
"threads_listener": {"thread_pool", "listener", "threads"}, // MISSINGv8
"count_rx": {"transport", "rx_count"},
"count_tx": {"transport", "tx_count"},
"open_file_descriptors": {"process", "open_file_descriptors"},
"compilations": {"script", "compilations"},
"cache_evictions": {"script", "cache_evictions"},
"compilation_limit_triggered": {"script", "compilation_limit_triggered"},
}

func getFloatValue(s map[string]interface{}, keys []string) (float64, error) {
Expand Down Expand Up @@ -89,9 +93,13 @@ func getFloatValue(s map[string]interface{}, keys []string) (float64, error) {

// ElasticsearchPlugin mackerel plugin for Elasticsearch
type ElasticsearchPlugin struct {
URI string
Prefix string
LabelPrefix string
URI string
Prefix string
LabelPrefix string
Insecure bool
User string
Password string
SuppressMissingError bool
}

// FetchMetrics interface for mackerelplugin
Expand All @@ -101,8 +109,15 @@ func (p ElasticsearchPlugin) FetchMetrics() (map[string]float64, error) {
return nil, err
}
req.Header.Set("User-Agent", "mackerel-plugin-elasticsearch")

resp, err := http.DefaultClient.Do(req)
if p.User != "" && p.Password != "" {
req.SetBasicAuth(p.User, p.Password)
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: p.Insecure},
},
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +145,9 @@ func (p ElasticsearchPlugin) FetchMetrics() (map[string]float64, error) {
for k, v := range metricPlace {
val, err := getFloatValue(node, v)
if err != nil {
logger.Errorf("Failed to find '%s': %s", k, err)
if !p.SuppressMissingError {
logger.Errorf("Failed to find '%s': %s", k, err)
}
continue
}

Expand Down Expand Up @@ -243,6 +260,15 @@ func (p ElasticsearchPlugin) GraphDefinition() map[string]mp.Graphs {
{Name: "open_file_descriptors", Label: "Open File Descriptors"},
},
},
p.Prefix + ".script": {
Label: (p.LabelPrefix + " Script"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "compilations", Label: "Compilations", Diff: true},
{Name: "cache_evictions", Label: "Cache Evictions", Diff: true},
{Name: "compilation_limit_triggered", Label: "Compilation Limit Triggered", Diff: true},
},
},
}

return graphdef
Expand All @@ -256,6 +282,10 @@ func Do() {
optPrefix := flag.String("metric-key-prefix", "elasticsearch", "Metric key prefix")
optLabelPrefix := flag.String("metric-label-prefix", "", "Metric Label prefix")
optTempfile := flag.String("tempfile", "", "Temp file name")
optInsecure := flag.Bool("insecure", false, "Skip TLS certificate verification")
optUser := flag.String("user", "", "Basic auth user")
optPassword := flag.String("password", "", "Basic auth password")
optSuppressMissingError := flag.Bool("suppress-missing-error", false, "Suppress ERROR for missing values")
flag.Parse()

var elasticsearch ElasticsearchPlugin
Expand All @@ -266,6 +296,10 @@ func Do() {
} else {
elasticsearch.LabelPrefix = *optLabelPrefix
}
elasticsearch.Insecure = *optInsecure
elasticsearch.User = *optUser
elasticsearch.Password = *optPassword
elasticsearch.SuppressMissingError = *optSuppressMissingError

helper := mp.NewMackerelPlugin(elasticsearch)
if *optTempfile != "" {
Expand Down
14 changes: 8 additions & 6 deletions mackerel-plugin-elasticsearch/lib/elasticsearch_test.go
Expand Up @@ -31,6 +31,7 @@ func TestGraphDefinition(t *testing.T) {
assert.EqualValues(t, "threads_fetch_shard_started", graphdef["elasticsearch.thread_pool.threads"].Metrics[16].Name)
assert.EqualValues(t, "threads_fetch_shard_store", graphdef["elasticsearch.thread_pool.threads"].Metrics[17].Name)
assert.EqualValues(t, "threads_listener", graphdef["elasticsearch.thread_pool.threads"].Metrics[18].Name)
assert.EqualValues(t, "compilation_limit_triggered", graphdef["elasticsearch.script"].Metrics[2].Name)
}

func TestFetchMetrics(t *testing.T) {
Expand All @@ -44,10 +45,11 @@ func TestFetchMetrics(t *testing.T) {
t.Fatal(err)
}

assert.EqualValues(t, 6991, stat["http_opened"])
assert.EqualValues(t, 1, stat["threads_generic"])
assert.EqualValues(t, 0, stat["threads_merge"])
assert.EqualValues(t, 2, stat["threads_fetch_shard_started"])
assert.EqualValues(t, 3, stat["threads_fetch_shard_store"])
assert.EqualValues(t, 1, stat["threads_listener"])
assert.EqualValues(t, 37, stat["http_opened"])
assert.EqualValues(t, 8, stat["threads_generic"])
assert.EqualValues(t, 13, stat["threads_search"])
assert.EqualValues(t, 0, stat["threads_fetch_shard_started"])
assert.EqualValues(t, 0, stat["threads_fetch_shard_store"])
assert.EqualValues(t, 331, stat["open_file_descriptors"])
assert.EqualValues(t, 1, stat["compilations"])
}