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

added es_list function, made graphite wildcards match exactly #371

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do i understand correctly that this checks for the absence of the "next word we don't need" ? e.g. for foo.* it checks for absence of 3rd node? that's neat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was the idea.

echo "$q"
}