Created
August 3, 2017 21:22
-
-
Save inqueue/a6fc8f87e2cd426ecea63261ed2dd4c5 to your computer and use it in GitHub Desktop.
Watcher (v5.5.1): Look for indices to purge every 24h with @timestamp older than 2 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PUT _xpack/watcher/watch/purge_indices | |
{ | |
"trigger": { | |
"schedule": { | |
"interval": "24h" | |
} | |
}, | |
"input": { | |
"search": { | |
"request": { | |
"indices": [ | |
"metricbeat-*" | |
], | |
"body": { | |
"size": 0, | |
"aggs": { | |
"indices": { | |
"terms": { | |
"field": "_index" | |
}, | |
"aggs": { | |
"last_timestamp": { | |
"max": { | |
"field": "@timestamp" | |
} | |
}, | |
"aged_indices": { | |
"bucket_selector": { | |
"buckets_path": { | |
"lastTimestamp": "last_timestamp" | |
}, | |
"script": "params.lastTimestamp < System.currentTimeMillis() - 2 * 24 * 3600 * 1000" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"condition": { | |
"script": { | |
"inline": """ | |
if (ctx.payload.aggregations.indices.buckets.size() < 1) return false; return true; | |
""" | |
} | |
}, | |
"transform": { | |
"script": """ | |
def results=ctx.payload.aggregations.indices.buckets.stream().map(i -> i.key).collect(Collectors.toList()); return ['results': results]; | |
""" | |
}, | |
"actions": { | |
"delete_index": { | |
"webhook": { | |
"method": "DELETE", | |
"host": "cluster.es.tld", | |
"port": 9200, | |
"scheme": "https", | |
"path": "{{#join}}ctx.payload.results{{/join}}", | |
"auth": { | |
"basic": { | |
"username": "elastic", | |
"password": "changeme" | |
} | |
} | |
} | |
}, | |
"log_entry": { | |
"logging": { | |
"text": "[Watcher]: purging {{#join}}ctx.payload.results{{/join}}: older than 2 days" | |
} | |
} | |
} | |
} | |
POST _xpack/watcher/watch/purge_indices/_execute | |
{ | |
"action_modes": { | |
"_all": "force_simulate" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with
_rollover
: