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
|
|
|
|
2015-03-21 21:19:08 +01:00
|
|
|
main_defaults () {
|
|
|
|
if [ -z $INTERVAL ]; then
|
|
|
|
INTERVAL=2
|
|
|
|
fi
|
|
|
|
if [ -z $METRICS ]; then
|
|
|
|
METRICS=cpu,disk_io,disk_usage,heartbeat,memory,network_io,swap
|
|
|
|
fi
|
|
|
|
if [ -z $REPORTER ]; then
|
|
|
|
REPORTER=stdout
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-03-08 20:37:56 +01:00
|
|
|
main_load () {
|
2015-03-21 21:19:08 +01:00
|
|
|
# set defaults
|
|
|
|
main_defaults
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-03-21 22:03:29 +01:00
|
|
|
}
|
2015-03-11 22:02:00 +01:00
|
|
|
|
2015-03-21 22:03:29 +01:00
|
|
|
main_collect () {
|
2015-03-15 14:55:36 +01:00
|
|
|
# check if reporter exists
|
2015-03-21 22:14:56 +01:00
|
|
|
if ! in_array $(get_name_for_reporter $__REPORTER) "$__AVAILABLE_REPORTERS"; then
|
2015-03-15 14:55:36 +01:00
|
|
|
echo "Error: reporter '$__REPORTER' is not available"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check if metrics exist
|
|
|
|
for metric in $__METRICS; do
|
2015-03-21 22:14:56 +01:00
|
|
|
if ! in_array $(get_name_for_metric $metric) "$__AVAILABLE_METRICS"; then
|
2015-03-15 14:55:36 +01:00
|
|
|
echo "Error: metric '$metric' is not available"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2015-03-21 16:50:35 +01:00
|
|
|
# register trap
|
|
|
|
trap '
|
|
|
|
trap "" 13
|
|
|
|
trap - INT TERM EXIT
|
|
|
|
echo Exit signal received.
|
|
|
|
kill -13 -$$
|
|
|
|
' 13 INT TERM EXIT
|
2015-03-08 20:37:56 +01:00
|
|
|
|
2015-03-21 22:03:29 +01:00
|
|
|
# init reporter
|
2015-03-21 22:14:56 +01:00
|
|
|
local reporter_name=$(get_name_for_reporter $__REPORTER)
|
|
|
|
local reporter_alias=$(get_alias $__REPORTER)
|
|
|
|
load_reporter_with_prefix __r_${reporter_alias}_ ./reporters/${reporter_name}.sh
|
2015-03-21 22:03:29 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-03-08 20:37:56 +01:00
|
|
|
# collect metrics
|
2015-03-15 14:55:36 +01:00
|
|
|
for metric in $__METRICS; do
|
2015-03-21 16:50:35 +01:00
|
|
|
# run in subshell to isolate scope
|
|
|
|
(
|
2015-03-21 22:14:56 +01:00
|
|
|
local metric_name=$(get_name_for_metric $metric)
|
2015-03-21 16:50:35 +01:00
|
|
|
local metric_alias=$(get_alias $metric)
|
|
|
|
|
|
|
|
# init metric
|
2015-03-21 22:14:56 +01:00
|
|
|
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
2015-03-21 21:19:08 +01:00
|
|
|
|
|
|
|
if is_function __m_${metric_alias}_defaults; then
|
|
|
|
__m_${metric_alias}_defaults
|
|
|
|
fi
|
|
|
|
|
2015-03-21 16:50:35 +01:00
|
|
|
if is_function __m_${metric_alias}_config; then
|
|
|
|
__m_${metric_alias}_config
|
|
|
|
fi
|
|
|
|
|
2015-03-21 21:19:08 +01:00
|
|
|
if is_function __m_${metric_alias}_start; then
|
|
|
|
__m_${metric_alias}_start
|
2015-03-21 16:50:35 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ! is_function __m_${metric_alias}_collect; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# collect metrics
|
|
|
|
trap "
|
2015-03-21 21:19:08 +01:00
|
|
|
verbose \"Stopping metric '${metric_alias}'\"
|
|
|
|
if is_function __m_${metric_alias}_stop; then
|
|
|
|
__m_${metric_alias}_stop
|
2015-03-21 16:50:35 +01:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
" 13
|
|
|
|
|
2015-03-21 21:19:08 +01:00
|
|
|
# used by metrics to return results
|
|
|
|
report () {
|
|
|
|
local _r_label _r_result
|
|
|
|
if [ -z $2 ]; then
|
|
|
|
_r_label=$metric_alias
|
|
|
|
_r_result="$1"
|
|
|
|
else
|
|
|
|
_r_label="$metric_alias.$1"
|
|
|
|
_r_result="$2"
|
|
|
|
fi
|
|
|
|
if is_number $_r_result; then
|
2015-03-21 22:03:29 +01:00
|
|
|
__r_${reporter_alias}_report $_r_label $_r_result
|
2015-03-21 21:19:08 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:50:35 +01:00
|
|
|
while true; do
|
|
|
|
__m_${metric_alias}_collect
|
|
|
|
sleep $INTERVAL
|
|
|
|
done
|
|
|
|
) &
|
2015-03-08 20:37:56 +01:00
|
|
|
done
|
2015-03-12 20:59:09 +01:00
|
|
|
|
2015-03-21 16:50:35 +01:00
|
|
|
# wait until interrupted
|
|
|
|
wait
|
|
|
|
# then wait again for processes to end
|
|
|
|
wait
|
|
|
|
|
|
|
|
main_terminate
|
2015-03-08 20:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_terminate () {
|
2015-03-21 21:19:08 +01:00
|
|
|
# stop reporter
|
2015-03-21 22:03:29 +01:00
|
|
|
local reporter_alias=$(get_alias $__REPORTER)
|
|
|
|
verbose "Stopping reporter '${reporter_alias}'"
|
|
|
|
if is_function __r_${reporter_alias}_stop; then
|
|
|
|
__r_${reporter_alias}_stop
|
2015-03-08 20:37:56 +01:00
|
|
|
fi
|
2015-03-11 22:02:00 +01:00
|
|
|
|
2015-03-21 21:19:08 +01:00
|
|
|
verbose "Cleaning up..."
|
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
|
2015-03-21 16:50:35 +01:00
|
|
|
verbose "done"
|
2015-03-11 22:02:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-21 21:19:08 +01:00
|
|
|
main_print_docs () {
|
|
|
|
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-21 21:19:08 +01:00
|
|
|
if is_function __m_${metric}_defaults; then
|
|
|
|
__m_${metric}_defaults
|
|
|
|
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-21 21:19:08 +01:00
|
|
|
if is_function __r_${reporter}_defaults; then
|
|
|
|
__r_${reporter}_defaults
|
|
|
|
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-21 21:19:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_print_config () {
|
|
|
|
echo "[metrics.sh]"
|
|
|
|
echo ";INTERVAL=$INTERVAL"
|
|
|
|
echo ";METRICS=$METRICS"
|
|
|
|
echo ";REPORTER=$REPORTER"
|
|
|
|
|
|
|
|
for metric in $__AVAILABLE_METRICS; do
|
|
|
|
load_metric_with_prefix __m_${metric}_ ./metrics/${metric}.sh
|
|
|
|
|
|
|
|
if is_function __m_${metric}_defaults; then
|
|
|
|
__m_${metric}_defaults
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo ";[metric $metric]"
|
|
|
|
if ! is_function __m_${metric}_docs; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
print_prefixed ";" "$(__m_${metric}_docs)"
|
|
|
|
done
|
|
|
|
|
|
|
|
for reporter in $__AVAILABLE_REPORTERS; do
|
|
|
|
if ! is_function __r_${reporter}_docs; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
if is_function __r_${reporter}_defaults; then
|
|
|
|
__r_${reporter}_defaults
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo ";[reporter $reporter]"
|
|
|
|
print_prefixed ";" "$(__r_${reporter}_docs)"
|
|
|
|
done
|
2015-03-08 20:37:56 +01:00
|
|
|
}
|