I want to get a list of processes that utilize more than 1% CPU. So I created a nested bucket aggregation query which ends in an avg aggregation:
GET metricbeat-*/_search
{
"aggs": {
"host": {
"terms": {
"field": "agent.hostname"
},
"aggs": {
"user": {
"terms": {
"field": "user.name"
},
"aggs": {
"process": {
"terms": {
"field": "process.name"
},
"aggs": {
"pid": {
"terms": {
"field": "process.pid"
},
"aggs": {
"cpu": {
"avg": {
"field": "system.process.cpu.total.pct"
}
},
"cpu_bucket_selector": {
"bucket_selector": {
"buckets_path": {
"avg_cpu": "cpu"
},
"script": "params.avg_cpu > 0.01"
}
}
}
}
}
}
}
}
}
}
},
"size": 0,
"stored_fields": [
"*"
],
"script_fields": {},
"docvalue_fields": [
{
"field": "@timestamp",
"format": "date_time"
}
],
"query": {
"bool": {
"must": [],
"filter": [
{
"match_all": {}
},
{
"range": {
"@timestamp": {
"gte": "now-5m/s"
}
}
}
],
"should": [],
"must_not": []
}
}
}
Values below 1% are filtered but this also leads to empty "pid" buckets in the results list. I tried to define the bucket selector at the very first parent aggregation like this:
GET metricbeat-*/_search
{
"aggs": {
"host": {
"terms": {
"field": "agent.hostname"
},
"aggs": {
"user": {
"terms": {
"field": "user.name"
},
"aggs": {
"process": {
"terms": {
"field": "process.name"
},
"aggs": {
"pid": {
"terms": {
"field": "process.pid"
},
"aggs": {
"cpu": {
"avg": {
"field": "system.process.cpu.total.pct"
}
}
}
}
}
}
}
}
}
},
"cpu_bucket_selector": {
"bucket_selector": {
"buckets_path": {
"avg_cpu": "host>user>process>pid>cpu"
},
"script": "params.avg_cpu > 0.01"
}
}
},
"size": 0,
"stored_fields": [
"*"
],
"script_fields": {},
"docvalue_fields": [
{
"field": "@timestamp",
"format": "date_time"
}
],
"query": {
"bool": {
"must": [],
"filter": [
{
"match_all": {}
},
{
"range": {
"@timestamp": {
"gte": "now-5m/s"
}
}
}
],
"should": [],
"must_not": []
}
}
}
But this gives me an error:
{
"error" : {
"root_cause" : [
{
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: bucket_selector aggregation [cpu_bucket_selector] must be declared inside of another aggregation;"
}
],
"type" : "action_request_validation_exception",
"reason" : "Validation Failed: 1: bucket_selector aggregation [cpu_bucket_selector] must be declared inside of another aggregation;"
},
"status" : 400
}
Whats the issue and am I even on the right path?