2015-03-15 14:55:36 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2015-03-08 20:37:56 +01:00
|
|
|
# load utils
|
2015-03-15 14:55:36 +01:00
|
|
|
for util in ./lib/utils/*.sh; do
|
|
|
|
. $util
|
|
|
|
done
|
2015-03-08 20:37:56 +01:00
|
|
|
|
|
|
|
# init
|
2015-03-15 14:55:36 +01:00
|
|
|
__AVAILABLE_METRICS=
|
|
|
|
__AVAILABLE_REPORTERS=
|
2015-03-08 20:37:56 +01:00
|
|
|
|
|
|
|
main_load () {
|
2015-03-11 22:02:00 +01:00
|
|
|
# load reporter
|
2015-03-14 21:44:06 +01:00
|
|
|
for file in ./reporters/*.sh; do
|
|
|
|
local filename=$(basename $file)
|
|
|
|
local reporter=${filename%.*}
|
|
|
|
|
2015-03-15 21:04:06 +01:00
|
|
|
load_reporter_with_prefix __r_${reporter}_ $file
|
2015-03-14 21:44:06 +01:00
|
|
|
|
2015-03-15 14:55:36 +01:00
|
|
|
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
2015-03-14 21:44:06 +01:00
|
|
|
done
|
2015-03-08 20:37:56 +01:00
|
|
|
|
2015-03-21 10:55:45 +01:00
|
|
|
# load available metrics
|
2015-03-08 20:37:56 +01:00
|
|
|
for file in ./metrics/*.sh; do
|
2015-03-14 21:44:06 +01:00
|
|
|
local filename=$(basename $file)
|
|
|
|
local metric=${filename%.*}
|
2015-03-08 20:37:56 +01:00
|
|
|
|
|
|
|
# register metric
|
2015-03-15 14:55:36 +01:00
|
|
|
__AVAILABLE_METRICS=$(trim "$__AVAILABLE_METRICS $metric")
|
2015-03-08 20:37:56 +01:00
|
|
|
done
|
2015-03-21 10:55:45 +01:00
|
|
|
|
|
|
|
load_metric_with_prefix __m_${metric}_ $file
|
|
|
|
|
2015-03-08 20:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_init () {
|
2015-03-15 14:55:36 +01:00
|
|
|
# handle args
|
|
|
|
__METRICS=$(echo $1 | sed 's/,/ /g')
|
|
|
|
__REPORTER=$2
|
|
|
|
|
|
|
|
# create temp dir
|
2015-03-11 22:02:00 +01:00
|
|
|
TEMP_DIR=$(make_temp_dir)
|
|
|
|
|
|
|
|
# register trap
|
|
|
|
trap '
|
|
|
|
main_terminate
|
2015-03-15 17:18:27 +01:00
|
|
|
trap - TERM && kill -- -$$ INT TERM EXIT
|
|
|
|
' INT TERM EXIT
|
2015-03-11 22:02:00 +01:00
|
|
|
|
2015-03-15 14:55:36 +01:00
|
|
|
# check if reporter exists
|
|
|
|
if ! in_array $__REPORTER "$__AVAILABLE_REPORTERS"; then
|
|
|
|
echo "Error: reporter '$__REPORTER' is not available"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check if metrics exist
|
|
|
|
for metric in $__METRICS; do
|
2015-03-21 10:55:45 +01:00
|
|
|
metric=$(get_name $metric)
|
2015-03-15 14:55:36 +01:00
|
|
|
if ! in_array $metric "$__AVAILABLE_METRICS"; then
|
|
|
|
echo "Error: metric '$metric' is not available"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2015-03-08 20:37:56 +01:00
|
|
|
# init reporter
|
2015-03-15 14:55:36 +01:00
|
|
|
if is_function __r_${__REPORTER}_init; then
|
|
|
|
__r_${__REPORTER}_init
|
2015-03-08 20:37:56 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# init metrics
|
2015-03-15 14:55:36 +01:00
|
|
|
for metric in $__METRICS; do
|
2015-03-21 10:55:45 +01:00
|
|
|
local metric_name=$(get_name $metric)
|
|
|
|
local metric_alias=$(get_alias $metric)
|
|
|
|
|
|
|
|
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
|
|
|
|
|
|
|
if ! is_function __m_${metric_alias}_init; then
|
2015-03-08 20:37:56 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2015-03-21 10:55:45 +01:00
|
|
|
__m_${metric_alias}_init
|
2015-03-08 20:37:56 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
main_collect () {
|
|
|
|
# used by metrics to return results
|
|
|
|
report () {
|
2015-03-14 21:44:06 +01:00
|
|
|
local _r_label _r_result
|
2015-03-08 20:37:56 +01:00
|
|
|
if [ -z $2 ]; then
|
|
|
|
_r_label=$metric
|
|
|
|
_r_result="$1"
|
|
|
|
else
|
|
|
|
_r_label="$metric.$1"
|
|
|
|
_r_result="$2"
|
|
|
|
fi
|
|
|
|
if is_number $_r_result; then
|
2015-03-15 14:55:36 +01:00
|
|
|
__r_${__REPORTER}_report $_r_label $_r_result
|
2015-03-08 20:37:56 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# collect metrics
|
2015-03-15 14:55:36 +01:00
|
|
|
for metric in $__METRICS; do
|
2015-03-21 10:55:45 +01:00
|
|
|
metric=$(get_alias $metric)
|
2015-03-12 20:59:09 +01:00
|
|
|
if ! is_function __m_${metric}_collect; then
|
|
|
|
continue
|
|
|
|
fi
|
2015-03-08 20:37:56 +01:00
|
|
|
|
2015-03-15 21:04:06 +01:00
|
|
|
# fork
|
|
|
|
(while true; do
|
2015-03-21 10:55:45 +01:00
|
|
|
__m_${metric}_collect
|
|
|
|
sleep $INTERVAL
|
2015-03-15 21:04:06 +01:00
|
|
|
done) &
|
2015-03-08 20:37:56 +01:00
|
|
|
done
|
2015-03-12 20:59:09 +01:00
|
|
|
|
|
|
|
# run forever
|
2015-03-15 14:55:36 +01:00
|
|
|
sleep 2147483647 # `sleep infinity` is not portable
|
2015-03-08 20:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_terminate () {
|
|
|
|
# terminate metrics
|
2015-03-15 14:55:36 +01:00
|
|
|
for metric in $__METRICS; do
|
2015-03-21 10:55:45 +01:00
|
|
|
metric=$(get_alias $metric)
|
2015-03-08 20:37:56 +01:00
|
|
|
if ! is_function __m_${metric}_terminate; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
__m_${metric}_terminate
|
|
|
|
done
|
|
|
|
|
|
|
|
# terminate reporter
|
2015-03-15 14:55:36 +01:00
|
|
|
if is_function __r_${__REPORTER}_terminate; then
|
|
|
|
__r_${__REPORTER}_terminate
|
2015-03-08 20:37:56 +01:00
|
|
|
fi
|
2015-03-11 22:02:00 +01:00
|
|
|
|
|
|
|
# delete temporary directory
|
2015-03-12 21:17:44 +01:00
|
|
|
if [ -d $TEMP_DIR ]; then
|
2015-03-11 22:02:00 +01:00
|
|
|
rmdir $TEMP_DIR
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main_docs () {
|
2015-03-14 21:44:06 +01:00
|
|
|
echo "# Metrics"
|
2015-03-21 10:55:45 +01:00
|
|
|
|
2015-03-15 14:55:36 +01:00
|
|
|
for metric in $__AVAILABLE_METRICS; do
|
2015-03-21 10:55:45 +01:00
|
|
|
load_metric_with_prefix __m_${metric}_ ./metrics/${metric}.sh
|
|
|
|
|
2015-03-11 22:02:00 +01:00
|
|
|
if ! is_function __m_${metric}_docs; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2015-03-14 21:44:06 +01:00
|
|
|
echo
|
2015-03-11 22:02:00 +01:00
|
|
|
echo "[$metric]"
|
|
|
|
__m_${metric}_docs
|
2015-03-14 21:44:06 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "# REPORTERS"
|
2015-03-15 14:55:36 +01:00
|
|
|
for reporter in $__AVAILABLE_REPORTERS; do
|
2015-03-14 21:44:06 +01:00
|
|
|
if ! is_function __r_${reporter}_docs; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2015-03-11 22:02:00 +01:00
|
|
|
echo
|
2015-03-14 21:44:06 +01:00
|
|
|
echo "[$reporter]"
|
|
|
|
__r_${reporter}_docs
|
2015-03-11 22:02:00 +01:00
|
|
|
done
|
2015-03-08 20:37:56 +01:00
|
|
|
}
|