clean up code. load all reporters on init

This commit is contained in:
Patrick Stadler 2015-03-14 21:44:06 +01:00
parent 1d30530199
commit ca25c6d7fe
7 changed files with 62 additions and 37 deletions

View File

@ -3,23 +3,31 @@ for util in ./lib/utils/*.sh; do source $util; done
# init
__METRICS=()
__REPORTERS=()
main_load () {
# load reporter
source ./reporters/${REPORTER}.sh
copy_function init __r_${REPORTER}_init
copy_function report __r_${REPORTER}_report
copy_function terminate __r_${REPORTER}_terminate
copy_function docs __r_${REPORTER}_docs
unset -f init report terminate docs
for file in ./reporters/*.sh; do
local filename=$(basename $file)
local reporter=${filename%.*}
# source reporter and copy functions
source $file
copy_function init __r_${reporter}_init
copy_function report __r_${reporter}_report
copy_function terminate __r_${reporter}_terminate
copy_function docs __r_${reporter}_docs
unset -f init report terminate docs
__REPORTERS+=($reporter)
done
# load metrics
for file in ./metrics/*.sh; do
filename=$(basename $file)
metric=${filename%.*}
local filename=$(basename $file)
local metric=${filename%.*}
# soruce file and copy functions
# soruce metric and copy functions
source $file
copy_function init __m_${metric}_init
copy_function collect __m_${metric}_collect
@ -59,7 +67,7 @@ main_init () {
main_collect () {
# used by metrics to return results
report () {
local _r_result
local _r_label _r_result
if [ -z $2 ]; then
_r_label=$metric
_r_result="$1"
@ -112,14 +120,26 @@ main_terminate () {
}
main_docs () {
echo "Available metrics:"
echo "# Metrics"
for metric in ${__METRICS[@]}; do
if ! is_function __m_${metric}_docs; then
continue
fi
echo
echo "[$metric]"
__m_${metric}_docs
done
echo
echo "# REPORTERS"
for reporter in ${__REPORTERS[@]}; do
if ! is_function __r_${reporter}_docs; then
continue
fi
echo
echo "[$reporter]"
__r_${reporter}_docs
done
}

View File

@ -10,12 +10,12 @@ LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
# handle opts
opts_spec=":dhv:r:i:"
opts_spec=":dhvr:i:"
opt_docs=false
opt_verbose=false
usage () {
echo "Usage: $0 [-d] [-h] [-v] [-r repoter] [-i interval]"
echo "Usage: $0 [-d] [-h] [-v] [-r reporter] [-i interval]"
}
help () {

View File

@ -2,7 +2,7 @@
init () {
if [ -z $FILE_LOCATION ]; then
echo "Missing configuration: \$FILE_LOCATION"
echo "Error: file reporter requires \$FILE_LOCATION to be specified"
exit 1
fi
@ -12,10 +12,10 @@ init () {
}
report () {
local METRIC=$1
local VALUE=$2
local DATE=$(iso_date)
echo "$DATE $METRIC: $VALUE" >> $FILE_LOCATION
local metric=$1
local value=$2
local datetime=$(iso_date)
echo "$datetime $metric: $value" >> $FILE_LOCATION
}
docs () {

View File

@ -2,7 +2,7 @@
init () {
if [ -z $INFLUXDB_API_ENDPOINT ]; then
echo "Error: influxdb requires \$INFLUXDB_API_ENDPOINT to be set"
echo "Error: influxdb requires \$INFLUXDB_API_ENDPOINT to be specified"
exit 1
fi
@ -18,15 +18,17 @@ init () {
}
report () {
local METRIC=$1
local VALUE=$2
local metric=$1
local value=$2
local points
if [ "$INFLUXDB_SEND_HOSTNAME" = true ]; then
local POINTS="[$VALUE,\"$HOSTNAME\"]"
points="[$value,\"$HOSTNAME\"]"
else
local POINTS="[$VALUE]"
points="[$value]"
fi
curl -X POST $INFLUXDB_API_ENDPOINT \
-d "[{\"name\":\"$METRIC\",\"columns\":$__influxdb_columns,\"points\":[$POINTS]}]"
-d "[{\"name\":\"$metric\",\"columns\":$__influxdb_columns,\"points\":[$points]}]"
}
docs () {

View File

@ -2,12 +2,12 @@
init() {
if [ -z $KEEN_IO_PROJECT_ID ]; then
echo "Error: keen_io requires \$KEEN_IO_PROJECT_ID to be set"
echo "Error: keen_io requires \$KEEN_IO_PROJECT_ID to be specified"
exit 1
fi
if [ -z $KEEN_IO_WRITE_KEY ]; then
echo "Error: keen_io requires \$KEEN_IO_WRITE_KEY to be set"
echo "Error: keen_io requires \$KEEN_IO_WRITE_KEY to be specified"
exit 1
fi
@ -22,10 +22,11 @@ init() {
}
report () {
local METRIC=$1
local VALUE=$2
local metric=$1
local value=$2
curl -s $__keen_io_api_url -H "Content-Type: application/json" \
-d "{\"metric\": \"$METRIC\", \"value\": $VALUE}" > /dev/null
-d "{\"metric\":\"$metric\",\"value\":$value}" > /dev/null
}
docs () {

View File

@ -2,15 +2,16 @@
init () {
if [ -z $STATHAT_API_KEY ]; then
echo "Error: stathat requires \$STATHAT_API_KEY to be set"
echo "Error: stathat requires \$STATHAT_API_KEY to be specified"
exit 1
fi
}
report () {
local METRIC=$1
local VALUE=$2
curl -s -d "stat=$METRIC&ezkey=$STATHAT_API_KEY&value=$VALUE" \
local metric=$1
local value=$2
curl -s -d "ezkey=$STATHAT_API_KEY&stat=$metric&value=$value" \
http://api.stathat.com/ez > /dev/null
}

View File

@ -1,9 +1,10 @@
#!/bin/sh
report () {
local METRIC=$1
local VALUE=$2
echo $METRIC: $VALUE
local metric=$1
local value=$2
echo $metric: $value
}
docs () {