mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-11-08 21:24:54 -05:00
add defaults() fn. add --print-config. config loader now fails on error
This commit is contained in:
133
lib/main.sh
133
lib/main.sh
@@ -9,7 +9,22 @@ done
|
||||
__AVAILABLE_METRICS=
|
||||
__AVAILABLE_REPORTERS=
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
main_load () {
|
||||
# set defaults
|
||||
main_defaults
|
||||
|
||||
# load reporter
|
||||
for file in ./reporters/*.sh; do
|
||||
local filename=$(basename $file)
|
||||
@@ -54,11 +69,14 @@ main_init () {
|
||||
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}_init; then
|
||||
__r_${__REPORTER}_init
|
||||
if is_function __r_${__REPORTER}_start; then
|
||||
__r_${__REPORTER}_start
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -77,6 +95,35 @@ main_collect () {
|
||||
(
|
||||
local metric_name=$(get_name $metric)
|
||||
local metric_alias=$(get_alias $metric)
|
||||
|
||||
# init metric
|
||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
||||
|
||||
if is_function __m_${metric_alias}_defaults; then
|
||||
__m_${metric_alias}_defaults
|
||||
fi
|
||||
|
||||
if is_function __m_${metric_alias}_config; then
|
||||
__m_${metric_alias}_config
|
||||
fi
|
||||
|
||||
if is_function __m_${metric_alias}_start; then
|
||||
__m_${metric_alias}_start
|
||||
fi
|
||||
|
||||
if ! is_function __m_${metric_alias}_collect; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# collect metrics
|
||||
trap "
|
||||
verbose \"Stopping metric '${metric_alias}'\"
|
||||
if is_function __m_${metric_alias}_stop; then
|
||||
__m_${metric_alias}_stop
|
||||
fi
|
||||
exit 0
|
||||
" 13
|
||||
|
||||
# used by metrics to return results
|
||||
report () {
|
||||
local _r_label _r_result
|
||||
@@ -92,30 +139,6 @@ main_collect () {
|
||||
fi
|
||||
}
|
||||
|
||||
# init metric
|
||||
if is_function __m_${metric_alias}_config; then
|
||||
__m_${metric_alias}_config
|
||||
fi
|
||||
|
||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
||||
|
||||
if is_function __m_${metric_alias}_init; then
|
||||
__m_${metric_alias}_init
|
||||
fi
|
||||
|
||||
if ! is_function __m_${metric_alias}_collect; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# collect metrics
|
||||
trap "
|
||||
if is_function __m_${metric_alias}_terminate; then
|
||||
verbose 'Stopping metric ${metric_alias}'
|
||||
__m_${metric_alias}_terminate
|
||||
fi
|
||||
exit 0
|
||||
" 13
|
||||
|
||||
while true; do
|
||||
__m_${metric_alias}_collect
|
||||
sleep $INTERVAL
|
||||
@@ -132,13 +155,13 @@ main_collect () {
|
||||
}
|
||||
|
||||
main_terminate () {
|
||||
# terminate reporter
|
||||
if is_function __r_${__REPORTER}_terminate; then
|
||||
verbose "Stopping reporter ${__REPORTER}"
|
||||
__r_${__REPORTER}_terminate
|
||||
# stop reporter
|
||||
verbose "Stopping reporter '${__REPORTER}'"
|
||||
if is_function __r_${__REPORTER}_stop; then
|
||||
__r_${__REPORTER}_stop
|
||||
fi
|
||||
|
||||
verbose -n "Cleaning up..."
|
||||
verbose "Cleaning up..."
|
||||
# delete temporary directory
|
||||
if [ -d $TEMP_DIR ]; then
|
||||
rmdir $TEMP_DIR
|
||||
@@ -146,8 +169,8 @@ main_terminate () {
|
||||
verbose "done"
|
||||
}
|
||||
|
||||
main_docs () {
|
||||
echo "# Metrics"
|
||||
main_print_docs () {
|
||||
echo "# METRICS"
|
||||
|
||||
for metric in $__AVAILABLE_METRICS; do
|
||||
load_metric_with_prefix __m_${metric}_ ./metrics/${metric}.sh
|
||||
@@ -156,6 +179,10 @@ main_docs () {
|
||||
continue
|
||||
fi
|
||||
|
||||
if is_function __m_${metric}_defaults; then
|
||||
__m_${metric}_defaults
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "[$metric]"
|
||||
__m_${metric}_docs
|
||||
@@ -168,8 +195,48 @@ main_docs () {
|
||||
continue
|
||||
fi
|
||||
|
||||
if is_function __r_${reporter}_defaults; then
|
||||
__r_${reporter}_defaults
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "[$reporter]"
|
||||
__r_${reporter}_docs
|
||||
done
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -48,8 +48,13 @@ parse_config () {
|
||||
return
|
||||
fi
|
||||
|
||||
#echo "${fn_name}_config () { ${_body}; }"
|
||||
eval "${fn_name}_config () { ${_body}; }"
|
||||
if ! eval "$_body" > /dev/null 2>&1; then
|
||||
echo "Error parsing config section: $_name: $_body"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#echo "${fn_name}_config () { $_body; }"
|
||||
eval "${fn_name}_config () { $_body; }"
|
||||
}
|
||||
|
||||
for line in $(cat $1); do
|
||||
|
||||
@@ -9,8 +9,6 @@ iso_date () {
|
||||
}
|
||||
|
||||
in_array () {
|
||||
local item=$1
|
||||
local arr=$2
|
||||
echo " $2 " | grep -q " $1 "
|
||||
}
|
||||
|
||||
@@ -23,4 +21,8 @@ unique_id () {
|
||||
LC_ALL=C
|
||||
echo __u_$(cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c 10)
|
||||
LC_ALL=$RESTORE_LC_ALL
|
||||
}
|
||||
|
||||
print_prefixed () {
|
||||
printf "$2" | sed -e "s/^/$1/g"
|
||||
}
|
||||
@@ -6,9 +6,10 @@ load_reporter_with_prefix () {
|
||||
local content
|
||||
|
||||
content=$(sed \
|
||||
-e 's/^[[:space:]]*\(init[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(report[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(terminate[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(stop[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(docs[ ]*()[ ]*{\)/'"$prefix"'\1/' $file)
|
||||
|
||||
eval "$content"
|
||||
@@ -21,9 +22,10 @@ load_metric_with_prefix () {
|
||||
|
||||
# dash will error if this variable is defined as `local`
|
||||
content=$(sed \
|
||||
-e 's/^[[:space:]]*\(init[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(collect[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(terminate[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(stop[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||
-e 's/^[[:space:]]*\(docs[ ]*()[ ]*{\)/'"$prefix"'\1/' $file)
|
||||
|
||||
eval "$content"
|
||||
|
||||
Reference in New Issue
Block a user