Hello All,
I am trying to create a watcher alert for the servers that communicated to elastic in last 48 hrs but not in last 35 mins. I want to include the ctx.payload.value result in my email text but all I am getting is a blank value.
My watcher json file looks like:
{
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"metricbeat123-*"
],
"rest_total_hits_as_int": true,
"body": {
"query": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.window_period}}"
}
}
},
"aggs": {
"periods": {
"filters": {
"filters": {
"history": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.window_period}}"
}
}
},
"last_period": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.last_period}}"
}
}
}
}
},
"aggs": {
"hosts": {
"terms": {
"field": "server.name",
"size": 10000
}
}
}
}
},
"size": 0
}
}
}
},
"condition": {
"script": {
"source": "return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.size() > ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.size();",
"lang": "painless"
}
},
"actions": {
"log": {
"transform": {
"script": {
"source": "def last_period=ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.stream().map(e -> e.key).collect(Collectors.toList()); return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.stream().map(e -> e.key).filter(p -> !last_period.contains(p)).collect(Collectors.toList());",
"lang": "painless"
}
},
"logging": {
"level": "info",
"text": "Systems not responding in the last {{ctx.metadata.last_period}} minutes:{{#ctx.payload._value}}{{.}}:{{/ctx.payload._value}}"
}
},
"email_administrator": {
"email": {
"profile": "standard",
"attachments": {
"attached_data": {
"data": {
"format": "json"
}
}
},
"from": "abc@xyx.com",
"to": [
"test123@xyz.com"
],
"subject": "Encountered {{ctx.payload.hits.total}} Metricbeat Agents not reporting in last 35 mins",
"body": {
"text": "{{#ctx.payload._value}}{{.}}:{{/ctx.payload._value}}"
}
}
}
},
"metadata": {
"last_period": "35m",
"window_period": "48h"
},
"throttle_period_in_millis": 300000
}
When I do the simulate I get this result:
{
"watch_id": "_someid_",
"node": "abc123",
"state": "executed",
"user": "testuser",
"status": {
"state": {
"active": true,
"timestamp": "2021-04-09T04:11:04.692Z"
},
"last_checked": "2021-04-09T04:11:04.692Z",
"last_met_condition": "2021-04-09T04:11:04.692Z",
"actions": {
"log": {
"ack": {
"timestamp": "2021-04-09T04:11:04.692Z",
"state": "ackable"
},
"last_execution": {
"timestamp": "2021-04-09T04:11:04.692Z",
"successful": true
},
"last_successful_execution": {
"timestamp": "2021-04-09T04:11:04.692Z",
"successful": true
}
},
"email_administrator": {
"ack": {
"timestamp": "2021-04-09T04:11:04.692Z",
"state": "ackable"
},
"last_execution": {
"timestamp": "2021-04-09T04:11:04.692Z",
"successful": true
},
"last_successful_execution": {
"timestamp": "2021-04-09T04:11:04.692Z",
"successful": true
}
}
},
"execution_state": "executed",
"version": -1
},
"trigger_event": {
"type": "manual",
"triggered_time": "2021-04-09T04:11:04.692Z",
"manual": {
"schedule": {
"scheduled_time": "2021-04-09T04:11:04.692Z"
}
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"metricbeat123-*"
],
"rest_total_hits_as_int": true,
"body": {
"query": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.window_period}}"
}
}
},
"aggs": {
"periods": {
"filters": {
"filters": {
"history": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.window_period}}"
}
}
},
"last_period": {
"range": {
"@timestamp": {
"gte": "now-{{ctx.metadata.last_period}}"
}
}
}
}
},
"aggs": {
"hosts": {
"terms": {
"field": "server.name",
"size": 10000
}
}
}
}
},
"size": 0
}
}
}
},
"condition": {
"script": {
"source": "return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.size() > ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.size();",
"lang": "painless"
}
},
"metadata": {
"last_period": "35m",
"window_period": "48h",
"name": "testwatcher",
"xpack": {
"type": "json"
}
},
"result": {
"execution_time": "2021-04-09T04:11:04.692Z",
"execution_duration": 2192,
"input": {
"type": "search",
"status": "success",
"payload": {
"_shards": {
"total": 14,
"failed": 0,
"successful": 14,
"skipped": 6
},
"hits": {
"hits": [],
"total": 10000,
"max_score": null
},
"took": 2187,
"timed_out": false,
"aggregations": {
"periods": {
"meta": {},
"buckets": {
"last_period": {
"doc_count": 157223,
"hosts": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 1234,
"key": "server1"
},
{
"doc_count": 1785,
"key": "server2"
},
{
"doc_count": 1111,
"key": "server3"
},
{
"doc_count": 1254,
"key": "server4"
}
]
}
},
"history": {
"doc_count": 789564125,
"hosts": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 895412,
"key": "server1"
},
{
"doc_count": 654123,
"key": "server2"
},
{
"doc_count": 123654,
"key": "server3"
},
{
"doc_count": 789654,
"key": "server4"
},
{
"doc_count": 96324,
"key": "server5"
},
{
"doc_count": 195752,
"key": "server6"
},
{
"doc_count": 98521,
"key": "server7"
}
]
}
}
}
}
}
},
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"metricbeat123-*"
],
"rest_total_hits_as_int": true,
"body": {
"query": {
"range": {
"@timestamp": {
"gte": "now-48h"
}
}
},
"aggs": {
"periods": {
"filters": {
"filters": {
"history": {
"range": {
"@timestamp": {
"gte": "now-48h"
}
}
},
"last_period": {
"range": {
"@timestamp": {
"gte": "now-35m"
}
}
}
}
},
"aggs": {
"hosts": {
"terms": {
"field": "server.name",
"size": 10000
}
}
}
}
},
"size": 0
}
}
}
},
"condition": {
"type": "script",
"status": "success",
"met": true
},
"actions": [
{
"id": "log",
"type": "logging",
"status": "simulated",
"transform": {
"type": "script",
"status": "success",
"payload": {
"_value": [
"server4",
"server5",
"server6",
"server7"
]
}
},
"logging": {
"logged_text": "Systems not responding in the last 35 minutes:server4:server5:server6:server7"
}
},
{
"id": "email_administrator",
"type": "email",
"status": "simulated",
"email": {
"message": {
"id": "someid",
"from": "abc@xyz.com",
"sent_date": "2021-04-09T04:11:06.886335006Z",
"to": [
"test123@xyz.com"
],
"subject": "Systems not responding in the last 35m minutes",
"body": {
"text": ""
}
}
}
}
]
},
"messages": []
}
How can I get the server4, server5, server6, server7 in my email text?