enable -m <alias> / -r <alias>
This commit is contained in:
parent
b518c807d1
commit
449c1b9289
|
@ -56,5 +56,4 @@ TODO: how to write custom reporters
|
||||||
- README
|
- README
|
||||||
- config file docs
|
- config file docs
|
||||||
- load custom/contrib metrics and reporters
|
- load custom/contrib metrics and reporters
|
||||||
- enable -m <alias> / -r <alias>
|
|
||||||
- allow multiple reporters?
|
- allow multiple reporters?
|
49
lib/main.sh
49
lib/main.sh
|
@ -30,8 +30,6 @@ main_load () {
|
||||||
local filename=$(basename $file)
|
local filename=$(basename $file)
|
||||||
local reporter=${filename%.*}
|
local reporter=${filename%.*}
|
||||||
|
|
||||||
load_reporter_with_prefix __r_${reporter}_ $file
|
|
||||||
|
|
||||||
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -52,35 +50,23 @@ main_init () {
|
||||||
|
|
||||||
# create temp dir
|
# create temp dir
|
||||||
TEMP_DIR=$(make_temp_dir)
|
TEMP_DIR=$(make_temp_dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
main_collect () {
|
||||||
# check if reporter exists
|
# check if reporter exists
|
||||||
if ! in_array $__REPORTER "$__AVAILABLE_REPORTERS"; then
|
if ! in_array $(resolve_reporter $__REPORTER) "$__AVAILABLE_REPORTERS"; then
|
||||||
echo "Error: reporter '$__REPORTER' is not available"
|
echo "Error: reporter '$__REPORTER' is not available"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# check if metrics exist
|
# check if metrics exist
|
||||||
for metric in $__METRICS; do
|
for metric in $__METRICS; do
|
||||||
metric=$(get_name $metric)
|
if ! in_array $(resolve_metric $metric) "$__AVAILABLE_METRICS"; then
|
||||||
if ! in_array $metric "$__AVAILABLE_METRICS"; then
|
|
||||||
echo "Error: metric '$metric' is not available"
|
echo "Error: metric '$metric' is not available"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# init reporter
|
|
||||||
if is_function __r_${__REPORTER}_defaults; then
|
|
||||||
__r_${__REPORTER}_defaults
|
|
||||||
fi
|
|
||||||
if is_function __r_${__REPORTER}_config; then
|
|
||||||
__r_${__REPORTER}_config
|
|
||||||
fi
|
|
||||||
if is_function __r_${__REPORTER}_start; then
|
|
||||||
__r_${__REPORTER}_start
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
main_collect () {
|
|
||||||
# register trap
|
# register trap
|
||||||
trap '
|
trap '
|
||||||
trap "" 13
|
trap "" 13
|
||||||
|
@ -89,15 +75,31 @@ main_collect () {
|
||||||
kill -13 -$$
|
kill -13 -$$
|
||||||
' 13 INT TERM EXIT
|
' 13 INT TERM EXIT
|
||||||
|
|
||||||
|
# init reporter
|
||||||
|
local reporter_alias=$(resolve_reporter $__REPORTER)
|
||||||
|
local reporter_file=$(resolve_reporter $__REPORTER)
|
||||||
|
load_reporter_with_prefix __r_${reporter_alias}_ ./reporters/${reporter_file}.sh
|
||||||
|
|
||||||
|
if is_function __r_${reporter_alias}_defaults; then
|
||||||
|
__r_${reporter_alias}_defaults
|
||||||
|
fi
|
||||||
|
if is_function __r_${reporter_alias}_config; then
|
||||||
|
__r_${reporter_alias}_config
|
||||||
|
fi
|
||||||
|
if is_function __r_${reporter_alias}_start; then
|
||||||
|
__r_${reporter_alias}_start
|
||||||
|
fi
|
||||||
|
|
||||||
# collect metrics
|
# collect metrics
|
||||||
for metric in $__METRICS; do
|
for metric in $__METRICS; do
|
||||||
# run in subshell to isolate scope
|
# run in subshell to isolate scope
|
||||||
(
|
(
|
||||||
local metric_name=$(get_name $metric)
|
local metric_name=$(get_name $metric)
|
||||||
local metric_alias=$(get_alias $metric)
|
local metric_alias=$(get_alias $metric)
|
||||||
|
local metric_file=$(resolve_metric $metric)
|
||||||
|
|
||||||
# init metric
|
# init metric
|
||||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_file}.sh
|
||||||
|
|
||||||
if is_function __m_${metric_alias}_defaults; then
|
if is_function __m_${metric_alias}_defaults; then
|
||||||
__m_${metric_alias}_defaults
|
__m_${metric_alias}_defaults
|
||||||
|
@ -135,7 +137,7 @@ main_collect () {
|
||||||
_r_result="$2"
|
_r_result="$2"
|
||||||
fi
|
fi
|
||||||
if is_number $_r_result; then
|
if is_number $_r_result; then
|
||||||
__r_${__REPORTER}_report $_r_label $_r_result
|
__r_${reporter_alias}_report $_r_label $_r_result
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,9 +158,10 @@ main_collect () {
|
||||||
|
|
||||||
main_terminate () {
|
main_terminate () {
|
||||||
# stop reporter
|
# stop reporter
|
||||||
verbose "Stopping reporter '${__REPORTER}'"
|
local reporter_alias=$(get_alias $__REPORTER)
|
||||||
if is_function __r_${__REPORTER}_stop; then
|
verbose "Stopping reporter '${reporter_alias}'"
|
||||||
__r_${__REPORTER}_stop
|
if is_function __r_${reporter_alias}_stop; then
|
||||||
|
__r_${reporter_alias}_stop
|
||||||
fi
|
fi
|
||||||
|
|
||||||
verbose "Cleaning up..."
|
verbose "Cleaning up..."
|
||||||
|
|
|
@ -104,4 +104,28 @@ get_alias () {
|
||||||
_alias=$(echo $1 | awk 'BEGIN { FS=":" } { print $1 }')
|
_alias=$(echo $1 | awk 'BEGIN { FS=":" } { print $1 }')
|
||||||
fi
|
fi
|
||||||
echo $_alias
|
echo $_alias
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve_reporter () {
|
||||||
|
local _alias=$(get_alias $1)
|
||||||
|
for reporter in $__CFG_REPORTERS; do
|
||||||
|
local __alias=$(get_alias $reporter)
|
||||||
|
if [ $_alias = $__alias ]; then
|
||||||
|
echo $(get_name $reporter)
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo $_alias
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve_metric () {
|
||||||
|
local _alias=$(get_alias $1)
|
||||||
|
for metric in $__CFG_METRICS; do
|
||||||
|
local __alias=$(get_alias $metric)
|
||||||
|
if [ $_alias = $__alias ]; then
|
||||||
|
echo $(get_name $metric)
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo $_alias
|
||||||
}
|
}
|
Loading…
Reference in New Issue