At the end, I am using the monitor configuration like this.
However, I am not sure how to access the bucket key and bucket doc_count without using the syntax like {{ctx.results.0.aggregations.response.buckets.1.key}} and {{ctx.results.0.aggregations.response.buckets.1.doc_count}}
After the filter, can we just access the filtered bucket to get the key and value?
PUT _plugins/_alerting/monitors/PdbUC38BMQQOgIcx73Sx
{
"type": "monitor",
"name": "alert-demo-888",
"monitor_type": "bucket_level_monitor",
"enabled": true,
"schedule": {
"period": {
"interval": 1,
"unit": "MINUTES"
}
},
"inputs": [
{
"search": {
"indices": [
"opensearch_dashboards_sample_data_logs"
],
"query": {
"size": 1,
"query": {
"match_all": {}
},
"aggs": {
"response": {
"terms": {
"field": "response.keyword",
"size": 10
}
}
}
}
}
}
],
"triggers": [
{
"bucket_level_trigger": {
"name": "trigger-demo-404",
"severity": "1",
"condition": {
"buckets_path": {
"_count": "_count"
},
"parent_bucket_path": "response",
"script": {
"source": """
params._count > 10
""",
"lang": "painless"
},
"filter": {
"include": "404"
}
},
"actions": [
{
"name": "test-action",
"destination_id": "3yt6-34BSFe9yDgKGlUf",
"message_template": {
"source": """
{
"Monitor": "{{ctx.monitor.name}",
"Severity": "{{ctx.trigger.severity}}",
"Period start": "{{ctx.periodStart}}",
"Period end": "{{ctx.periodEnd}}",
"error_code": "{{ctx.results.0.aggregations.response.buckets.1.key}}",
"count": "{{ctx.results.0.aggregations.response.buckets.1.doc_count}} "
}
""",
"lang": "mustache"
},
"throttle_enabled": false,
"throttle": {
"value": 10,
"unit": "MINUTES"
},
"action_execution_policy": {
"action_execution_scope": {
"per_alert": {
"actionable_alerts": [
"DEDUPED",
"NEW"
]
}
}
},
"subject_template": {
"source": "alert-demo-404",
"lang": "mustache"
}
}
]
}
}
]
}
It would be more straightforward if I can access the bucket “xNZYDH8BMQQOgIcxOnYR”
{
"monitor_name" : "alert-demo-888",
"period_start" : 1645179524989,
"period_end" : 1645179584989,
"error" : null,
"input_results" : {
"results" : [
{
"_shards" : {
"total" : 1,
"failed" : 0,
"successful" : 1,
"skipped" : 0
},
"hits" : {
"hits" : [
{
"_index" : "opensearch_dashboards_sample_data_logs",
"_type" : "_doc",
"_source" : {
"referer" : "http://twitter.com/success/wendy-lawrence",
"request" : "/opensearch/opensearch-1.0.0.deb",
"agent" : "Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 Firefox/6.0a1",
"extension" : "deb",
"memory" : null,
"ip" : "223.87.60.27",
"index" : "opensearch_dashboards_sample_data_logs",
"message" : "223.87.60.27 - - [2018-07-22T00:39:02.912Z] \"GET /opensearch/opensearch-1.0.0.deb_1 HTTP/1.1\" 200 6219 \"-\" \"Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 Firefox/6.0a1\"",
"url" : "https://artifacts.opensearch.org/downloads/opensearch/opensearch-1.0.0.deb_1",
"tags" : [
"success",
"info"
],
"geo" : {
"srcdest" : "IN:US",
"src" : "IN",
"coordinates" : {
"lon" : -88.8454325,
"lat" : 39.41042861
},
"dest" : "US"
},
"utc_time" : "2022-02-06T00:39:02.912Z",
"bytes" : 6219,
"machine" : {
"os" : "win 8",
"ram" : 8589934592
},
"response" : 200,
"clientip" : "223.87.60.27",
"host" : "artifacts.opensearch.org",
"event" : {
"dataset" : "sample_web_logs"
},
"phpmemory" : null,
"timestamp" : "2022-02-06T00:39:02.912Z"
},
"_id" : "mSsZ-34BSFe9yDgKQR44",
"_score" : 1.0
}
],
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : 1.0
},
"took" : 3,
"timed_out" : false,
"aggregations" : {
"xNZYDH8BMQQOgIcxOnYR" : {
"parent_bucket_path" : "response",
"bucket_indices" : [
1
]
},
"response" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"doc_count" : 12832,
"key" : "200"
},
{
"doc_count" : 801,
"key" : "404"
},
{
"doc_count" : 441,
"key" : "503"
}
]
}
}
}
],
"error" : null
},
"trigger_results" : {
"xNZYDH8BMQQOgIcxOnYR" : {
"name" : "trigger-demo-404",
"agg_result_buckets" : {
"404" : {
"agg_alert_content" : {
"parent_bucket_path" : "response",
"bucket_keys" : [
"404"
],
"bucket" : {
"doc_count" : 801,
"key" : "404"
}
}
}
},
"action_results" : {
"404" : {
"xdZYDH8BMQQOgIcxOnYR" : {
"id" : "xdZYDH8BMQQOgIcxOnYR",
"name" : "test-action",
"output" : {
"subject" : "alert-demo-404",
"message" : """
{
"Monitor": "",
"Period start": "2022-02-18T10:18:44.989Z",
"Period end": "2022-02-18T10:19:44.989Z",
"error_code": "404",
"count": "801 "
}
""",
"messageId" : ""
},
"throttled" : false,
"executionTime" : 1645179585511,
"error" : null
}
}
},
"error" : null
}
}
}