I'm having real trouble working out how to convert my Watcher alerts to something that will run in xpack, specifically around converting to Painless. For example, I have an alert that should put a message on Slack if the memory of anode has been over 75% for 1 hour:
PUT _xpack/watcher/watch/cluster_memory_alert
{
"trigger": {
"schedule": {
"interval": "30m"
}
},
"input": {
"search": {
"request": {
"indices": [
".monitoring-es-*"
],
"types" : [
"node_stats"
],
"body": {
"size" : 1,
"query": {
"filter": {
"range": {
"timestamp": {
"gte": "now-2h",
"lte": "now"
}
}
}
},
"aggs": {
"minutes": {
"date_histogram": {
"field": "timestamp",
"interval": "10m"
},
"aggs": {
"nodes": {
"terms": {
"field": "source_node.name",
"size": 10,
"order": {
"memory": "desc"
}
},
"aggs": {
"memory": {
"max": {
"field": "node_stats.jvm.mem.heap_used_percent"
}
}
}
}
}
}
}
}
}
}
},
"throttle_period": "2h",
"condition": {
"script": {
"id": "clusterMemoryCondition"
}
},
"actions": {
"notify-slack": {
"transform": {
"script": {
"id": "clusterMemoryTransform"
}
},
"slack": {
"message" : {
"to": [ "#monitoring" ],
"text": "Elastic Cloud Nodes with HIGH MEMORY Usage (above 75%) for the last hour:\n\n{{#ctx.payload._value}}"{{key}}" - Memory Usage is at {{memory.value}}%\n{{/ctx.payload._value}}"
}
}
}
}
}
The condition and transform scripts are:
POST /_scripts/groovy/clusterMemoryCondition
{
"script": "if (ctx.payload.hits.total < 1) return false; def rows = ctx.payload.hits.hits; if (rows[0]._source.node_stats.jvm.mem.heap_used_percent < 75) return false; if (ctx.payload.aggregations.minutes.buckets.size() < 12) return false; def last60Minutes = ctx.payload.aggregations.minutes.buckets[-12..-1]; return last60Minutes.every { it.nodes.buckets.every { s -> s.memory.value >= 75 }}"
}
And
POST /_scripts/groovy/clusterMemoryTransform
{
"script": "def latest = ctx.payload.aggregations.minutes.buckets[-1]; return latest.nodes.buckets.findAll { return it.memory && it.memory.value >= 75 };"
}
Any assistance in how to get those two scripts to work in Painless would be much appreciated