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

View File

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

View File

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

View File

@ -2,7 +2,7 @@
init () { init () {
if [ -z $INFLUXDB_API_ENDPOINT ]; then 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 exit 1
fi fi
@ -18,15 +18,17 @@ init () {
} }
report () { report () {
local METRIC=$1 local metric=$1
local VALUE=$2 local value=$2
local points
if [ "$INFLUXDB_SEND_HOSTNAME" = true ]; then if [ "$INFLUXDB_SEND_HOSTNAME" = true ]; then
local POINTS="[$VALUE,\"$HOSTNAME\"]" points="[$value,\"$HOSTNAME\"]"
else else
local POINTS="[$VALUE]" points="[$value]"
fi fi
curl -X POST $INFLUXDB_API_ENDPOINT \ curl -X POST $INFLUXDB_API_ENDPOINT \
-d "[{\"name\":\"$METRIC\",\"columns\":$__influxdb_columns,\"points\":[$POINTS]}]" -d "[{\"name\":\"$metric\",\"columns\":$__influxdb_columns,\"points\":[$points]}]"
} }
docs () { docs () {

View File

@ -2,12 +2,12 @@
init() { init() {
if [ -z $KEEN_IO_PROJECT_ID ]; then 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 exit 1
fi fi
if [ -z $KEEN_IO_WRITE_KEY ]; then 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 exit 1
fi fi
@ -22,10 +22,11 @@ init() {
} }
report () { report () {
local METRIC=$1 local metric=$1
local VALUE=$2 local value=$2
curl -s $__keen_io_api_url -H "Content-Type: application/json" \ 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 () { docs () {

View File

@ -2,15 +2,16 @@
init () { init () {
if [ -z $STATHAT_API_KEY ]; then 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 exit 1
fi fi
} }
report () { report () {
local METRIC=$1 local metric=$1
local VALUE=$2 local value=$2
curl -s -d "stat=$METRIC&ezkey=$STATHAT_API_KEY&value=$VALUE" \
curl -s -d "ezkey=$STATHAT_API_KEY&stat=$metric&value=$value" \
http://api.stathat.com/ez > /dev/null http://api.stathat.com/ez > /dev/null
} }

View File

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