metrics.sh/lib/utils/config.sh

131 lines
2.5 KiB
Bash
Raw Normal View History

2015-03-19 16:14:14 -04:00
#!/bin/sh
__CFG_REPORTERS=
__CFG_METRICS=
get_configured_reporters () {
echo $__CFG_REPORTERS
}
get_configured_metrics () {
echo $__CFG_METRICS
}
parse_config () {
if [ ! -f "$1" ]; then
echo "Error: unable to load config file: ${1}"
return 1
fi
RESTORE_IFS=$IFS
# dash compatibility :-(
IFS=$'
'
local _group
local _name
local _body
end_section () {
if [ -z "$_group" ]; then
return
fi
local fn_name
if [ "$_group" = "reporter" ]; then
__CFG_REPORTERS=$(trim "${__CFG_REPORTERS} ${_name}")
2015-03-19 16:14:14 -04:00
fn_name="__r_"
elif [ "$_group" = "metric" ]; then
__CFG_METRICS=$(trim "${__CFG_METRICS} ${_name}")
2015-03-19 16:14:14 -04:00
fn_name="__m_"
else
fn_name="main"
2015-03-19 16:14:14 -04:00
fi
fn_name=${fn_name}$(get_alias $_name)
2015-03-19 16:14:14 -04:00
if [ -z "$_body" ]; then
return
fi
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; }"
2015-03-19 16:14:14 -04:00
}
for line in $(cat $1); do
# handle comments / empty lines
line=$(echo $line | grep -v '^\(#\|;\)')
if [ -z "$line" ]; then
continue
fi
local _section=$(echo $line | grep '^\[.*' | sed 's/\[\(.*\)\]/\1/')
if [ -n "$_section" ]; then
end_section
unset _group _name _body
2015-03-19 16:14:14 -04:00
_group=$(echo $_section | awk '{ print $1 }')
if echo " metrics.sh metric reporter " | grep -q -v " $_group "; then
echo "Warning: unknown section in configuration file: $_section"
continue
fi
if [ "$_group" = "metrics.sh" ]; then
_group="main"
2015-03-19 16:14:14 -04:00
continue
fi
_name=$(echo $_section | awk '{ print $2 }')
2015-03-19 16:14:14 -04:00
continue
fi
_body=$(echo "${_body};${line}" | sed 's/^;//g')
done
end_section
IFS=$RESTORE_IFS
}
get_name () {
echo $1 | awk 'BEGIN { FS=":" } { print $1 }'
}
get_alias () {
local _alias=$(echo $1 | awk 'BEGIN { FS=":" } { print $2 }')
if [ -z "$_alias" ]; then
_alias=$(echo $1 | awk 'BEGIN { FS=":" } { print $1 }')
fi
echo $_alias
2015-03-21 17:03:29 -04:00
}
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
2015-03-19 16:14:14 -04:00
}