mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-02-28 13:59:12 -05:00
108 lines
2.1 KiB
Bash
108 lines
2.1 KiB
Bash
|
# load utils
|
||
|
for util in ./lib/utils/*.sh; do source $util; done
|
||
|
|
||
|
# init
|
||
|
__METRICS=()
|
||
|
__TEMP_DIR=$(make_temp_dir)
|
||
|
|
||
|
# load reporter
|
||
|
|
||
|
main_load () {
|
||
|
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
|
||
|
|
||
|
# load metrics
|
||
|
for file in ./metrics/*.sh; do
|
||
|
filename=$(basename $file)
|
||
|
metric=${filename%.*}
|
||
|
|
||
|
# soruce file and copy functions
|
||
|
source $file
|
||
|
copy_function init __m_${metric}_init
|
||
|
copy_function collect __m_${metric}_collect
|
||
|
copy_function terminate __m_${metric}_terminate
|
||
|
copy_function docs __m_${metric}_docs
|
||
|
unset -f init collect terminate docs
|
||
|
|
||
|
# register metric
|
||
|
__METRICS+=($metric)
|
||
|
done
|
||
|
}
|
||
|
|
||
|
main_init () {
|
||
|
# init reporter
|
||
|
if is_function __r_${REPORTER}_init; then
|
||
|
__r_${REPORTER}_init
|
||
|
fi
|
||
|
|
||
|
# init metrics
|
||
|
for metric in ${__METRICS[@]}; do
|
||
|
if ! is_function __m_${metric}_init; then
|
||
|
continue
|
||
|
fi
|
||
|
|
||
|
__m_${metric}_init
|
||
|
done
|
||
|
}
|
||
|
|
||
|
main_docs () {
|
||
|
echo "Available metrics:"
|
||
|
for metric in ${__METRICS[@]}; do
|
||
|
if ! is_function __m_${metric}_docs; then
|
||
|
continue
|
||
|
fi
|
||
|
|
||
|
echo "[$metric]"
|
||
|
__m_${metric}_docs
|
||
|
echo
|
||
|
done
|
||
|
}
|
||
|
|
||
|
main_collect () {
|
||
|
# used by metrics to return results
|
||
|
report () {
|
||
|
local _r_result
|
||
|
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
|
||
|
__r_${REPORTER}_report $_r_label $_r_result
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# collect metrics
|
||
|
while true; do
|
||
|
for metric in ${__METRICS[@]}; do
|
||
|
if ! is_function __m_${metric}_collect; then
|
||
|
continue
|
||
|
fi
|
||
|
|
||
|
__m_${metric}_collect
|
||
|
done
|
||
|
|
||
|
sleep $INTERVAL
|
||
|
done
|
||
|
}
|
||
|
|
||
|
main_terminate () {
|
||
|
# terminate metrics
|
||
|
for metric in ${__METRICS[@]}; do
|
||
|
if ! is_function __m_${metric}_terminate; then
|
||
|
continue
|
||
|
fi
|
||
|
__m_${metric}_terminate
|
||
|
done
|
||
|
|
||
|
# terminate reporter
|
||
|
if is_function __r_${REPORTER}_terminate; then
|
||
|
__r_${REPORTER}_terminate
|
||
|
fi
|
||
|
}
|