Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #371 from raintank/maint-script
Browse files Browse the repository at this point in the history
added es_list function, made graphite wildcards match exactly
  • Loading branch information
Dieterbe authored Nov 7, 2016
2 parents c5fdf6b + 861169e commit b67bb67
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
}

0 comments on commit b67bb67

Please sign in to comment.