Skip to content

Instantly share code, notes, and snippets.

@tamsky
Last active June 23, 2016 08:33
Show Gist options
  • Save tamsky/5cdf8f36aea09dd797bc to your computer and use it in GitHub Desktop.
Save tamsky/5cdf8f36aea09dd797bc to your computer and use it in GitHub Desktop.
#!/bin/bash
# Required binaries: jq, date, curl, sed
# Prometheus text file format is described here:
# http://prometheus.io/docs/instrumenting/exposition_formats/
# Returns epoch + made up milliseconds timestamp
get_timestamp() {
date "+%s000"
}
# Args:
# metric name - What to call the metric in Prometheus. *Required*
# metric_value - Value to report to Prometheus. *Required*
# type - The Prometheus metric type. Default: counter
# help - Helpful description. Optional.
# timestamp - Timestamp of when data was collected. Optional.
emit_metric() {
metric_name=$1
metric_value=$2
type=${3:-counter}
help=${4}
timestamp=${5}
if [ -z "$timestamp" ]; then
timestamp=$(get_timestamp)
fi
{
if [ -n "$help" ] ; then
echo "# HELP $metric_name $help"
fi
echo "# TYPE $metric_name $type"
echo "$metric_name $metric_value $timestamp"
echo
} >> $OUTPUT.$$
}
OUTPUT="/var/spool/your_org/prometheus/elasticsearch_cluster_stats.prom"
METRIC_PREFIX="elasticsearch_cluster"
############################
## MAIN PROGRAM begins here:
############################
CLUSTER_HEALTH_JSON=$(curl --max-time 30 -s '0:9200/_cluster/health?local=true')
TIMESTAMP=$(date "+%s000")
declare -a KEYS
KEYS=( $(echo ${CLUSTER_HEALTH_JSON=$} | jq --raw-output '.|keys|.[]') )
# for each value in the json
for i in ${KEYS[*]} ; do
[[ $i == "cluster_name" ]] && continue
emit_metric "${METRIC_PREFIX}_$i" \
$(echo ${CLUSTER_HEALTH_JSON=$} | jq --raw-output ".$i") \
gauge \
"value from _cluster/health/" \
${TIMESTAMP}
done
# Coerce known string-valued metrics to integer-values
sed -i -e 's/ false / 0 /' \
-e 's/ true / 1 /' \
-e 's/ green / 0 /' \
-e 's/ yellow / 1 /' \
-e 's/ red / 2 /' \
$OUTPUT.$$
mv ${OUTPUT}.$$ ${OUTPUT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment