diff --git a/plugins/inputs/elasticsearch/README.md b/plugins/inputs/elasticsearch/README.md index 5698cc7f0826e..65869ae94a540 100644 --- a/plugins/inputs/elasticsearch/README.md +++ b/plugins/inputs/elasticsearch/README.md @@ -46,6 +46,17 @@ or [cluster-stats](https://www.elastic.co/guide/en/elasticsearch/reference/curre # insecure_skip_verify = false ``` +### Status mappings + +When reporting health (green/yellow/red), additional field `status_code` +is reported. Field contains mapping from status:string to status_code:int +with following rules: + +* `green` - 1 +* `yellow` - 2 +* `red` - 3 +* `unknown` - 0 + ### Measurements & Fields: field data circuit breaker measurement names: diff --git a/plugins/inputs/elasticsearch/elasticsearch.go b/plugins/inputs/elasticsearch/elasticsearch.go index f5ddef5fb892d..32e283df781de 100644 --- a/plugins/inputs/elasticsearch/elasticsearch.go +++ b/plugins/inputs/elasticsearch/elasticsearch.go @@ -143,6 +143,19 @@ func NewElasticsearch() *Elasticsearch { } } +// perform status mapping +func mapHealthStatusToCode(s string) int { + switch strings.ToLower(s) { + case "green": + return 1 + case "yellow": + return 2 + case "red": + return 3 + } + return 0 +} + // SampleConfig returns sample configuration for this plugin. func (e *Elasticsearch) SampleConfig() string { return sampleConfig @@ -311,6 +324,7 @@ func (e *Elasticsearch) gatherClusterHealth(url string, acc telegraf.Accumulator measurementTime := time.Now() clusterFields := map[string]interface{}{ "status": healthStats.Status, + "status_code": mapHealthStatusToCode(healthStats.Status), "timed_out": healthStats.TimedOut, "number_of_nodes": healthStats.NumberOfNodes, "number_of_data_nodes": healthStats.NumberOfDataNodes, @@ -330,6 +344,7 @@ func (e *Elasticsearch) gatherClusterHealth(url string, acc telegraf.Accumulator for name, health := range healthStats.Indices { indexFields := map[string]interface{}{ "status": health.Status, + "status_code": mapHealthStatusToCode(health.Status), "number_of_shards": health.NumberOfShards, "number_of_replicas": health.NumberOfReplicas, "active_primary_shards": health.ActivePrimaryShards, diff --git a/plugins/inputs/elasticsearch/testdata_test.go b/plugins/inputs/elasticsearch/testdata_test.go index 6e487ff3f65ed..8229fd66ecd66 100644 --- a/plugins/inputs/elasticsearch/testdata_test.go +++ b/plugins/inputs/elasticsearch/testdata_test.go @@ -54,6 +54,7 @@ const clusterHealthResponseWithIndices = ` var clusterHealthExpected = map[string]interface{}{ "status": "green", + "status_code": 1, "timed_out": false, "number_of_nodes": 3, "number_of_data_nodes": 3, @@ -66,6 +67,7 @@ var clusterHealthExpected = map[string]interface{}{ var v1IndexExpected = map[string]interface{}{ "status": "green", + "status_code": 1, "number_of_shards": 10, "number_of_replicas": 1, "active_primary_shards": 10, @@ -77,6 +79,7 @@ var v1IndexExpected = map[string]interface{}{ var v2IndexExpected = map[string]interface{}{ "status": "red", + "status_code": 3, "number_of_shards": 10, "number_of_replicas": 1, "active_primary_shards": 0,