From 861169e8a48e3a70b46d0757ca7b573889267303 Mon Sep 17 00:00:00 2001 From: Dan Cech Date: Fri, 4 Nov 2016 11:42:31 -0400 Subject: [PATCH] added es_list function, made graphite wildcards match exactly --- ...-es-index.bash => maintenance-es-index.sh} | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) rename contrib/{maintenance-es-index.bash => maintenance-es-index.sh} (65%) diff --git a/contrib/maintenance-es-index.bash b/contrib/maintenance-es-index.sh similarity index 65% rename from contrib/maintenance-es-index.bash rename to contrib/maintenance-es-index.sh index 6eb8022210..4539300fdb 100644 --- a/contrib/maintenance-es-index.bash +++ b/contrib/maintenance-es-index.sh @@ -1,11 +1,12 @@ # this file contains some bash functions which come in handy when maintaining an ES metadata index # (which is deprecated, but anyway. see https://github.com/raintank/metrictank/blob/master/docs/metadata.md ) -# +# # first source this file: # source maintenance-es-index.sh # (note: if your shell is not bash the syntax may not be supported) -# +# # then you can call the functions like so: +# es_list some.graphite.*.pattern.*.* # es_search $(es_query some.graphite.*.pattern.*.*) # es_delete $(es_query some.graphite.*.pattern.*.*) # for i in $(cat file-with-prefixes); do es_delete "nodes.n0:$i"; done @@ -16,27 +17,37 @@ index=metrictank # show all matching documents, given a query function es_search () { - curl "http://localhost:9200/$index/metric_index/_search?q=$1&pretty=true" + size=${2:-20} + curl -s "http://localhost:9200/$index/metric_index/_search?q=$1&pretty=true&size=$size" } # delete all matching documents, given a query function es_delete () { - curl -X DELETE "http://localhost:9200/$index/metric_index/_query?q=$1" + curl -s -X DELETE "http://localhost:9200/$index/metric_index/_query?q=$1" +} + +# list series matching a graphite pattern +function es_list () { + es_search $(es_query $1) 10000 | grep name | sed -e 's/^.*: "\|",$//g' } # construct a query, given a graphite pattern # e.g. so*.*.and.so -> nodes.n0:so*+AND+nodes.n2:and+AND+nodes.n3:so # caveats: -# - foo.*.* will also match foo or foo.* or foo.*.*.* since we don't have a check yet for number of fields # - does not support {foo,bar} or [this,kindof] graphite syntax! just literal words and wildcards. function es_query () { local q IFS='.' read -ra nodes <<< "$1" for i in "${!nodes[@]}"; do - [ "${nodes[$i]}" == '*' ] && continue - new="nodes.n$i:${nodes[$i]}" - [ -n "$q" ] && q="$q+AND+" + if [ "${nodes[$i]}" == '*' ]; then + new="%2B_exists_:nodes.n$i" + else + new="%2Bnodes.n$i:%22${nodes[$i]}%22" + fi + [ -n "$q" ] && q="$q+" q="$q$new" done + i=$(expr $i + 1) + q="$q+-_exists_:nodes.n$i" echo "$q" }