increase portability by getting rid of declare
This commit is contained in:
parent
7a43a2e21c
commit
6f8e87208c
25
lib/main.sh
25
lib/main.sh
|
@ -15,13 +15,7 @@ main_load () {
|
||||||
local filename=$(basename $file)
|
local filename=$(basename $file)
|
||||||
local reporter=${filename%.*}
|
local reporter=${filename%.*}
|
||||||
|
|
||||||
# source reporter and copy functions
|
load_reporter_with_prefix __r_${reporter}_ $file
|
||||||
. $file
|
|
||||||
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
|
|
||||||
|
|
||||||
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
||||||
done
|
done
|
||||||
|
@ -31,13 +25,7 @@ main_load () {
|
||||||
local filename=$(basename $file)
|
local filename=$(basename $file)
|
||||||
local metric=${filename%.*}
|
local metric=${filename%.*}
|
||||||
|
|
||||||
# soruce metric and copy functions
|
load_metric_with_prefix __m_${metric}_ $file
|
||||||
. $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
|
# register metric
|
||||||
__AVAILABLE_METRICS=$(trim "$__AVAILABLE_METRICS $metric")
|
__AVAILABLE_METRICS=$(trim "$__AVAILABLE_METRICS $metric")
|
||||||
|
@ -109,14 +97,11 @@ main_collect () {
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fork () {
|
# fork
|
||||||
while true; do
|
(while true; do
|
||||||
__m_${metric}_collect
|
__m_${metric}_collect
|
||||||
sleep $INTERVAL
|
sleep $INTERVAL
|
||||||
done
|
done) &
|
||||||
}
|
|
||||||
fork &
|
|
||||||
unset -f fork
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# run forever
|
# run forever
|
||||||
|
|
|
@ -1,34 +1,6 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# this is bad, but `declare -f -F` is not portable
|
||||||
is_function () {
|
is_function () {
|
||||||
declare -f -F $1 > /dev/null; return $?
|
type $1 2> /dev/null | grep -q 'function$'
|
||||||
}
|
}
|
||||||
|
|
||||||
# http://stackoverflow.com/a/1369211/183097
|
|
||||||
copy_function () {
|
|
||||||
is_function $1 || return 1
|
|
||||||
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
|
|
||||||
}
|
|
||||||
|
|
||||||
# if `type declare | grep -q 'shell builtin$'`; then
|
|
||||||
# is_function () {
|
|
||||||
# declare -f -F $1 > /dev/null; return $?
|
|
||||||
# }
|
|
||||||
|
|
||||||
# # http://stackoverflow.com/a/1369211/183097
|
|
||||||
# copy_function () {
|
|
||||||
# is_function $1 || return 1
|
|
||||||
# eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
|
|
||||||
# }
|
|
||||||
|
|
||||||
# # `declare` is non-posix, provide alternative
|
|
||||||
# else
|
|
||||||
# is_function () {
|
|
||||||
# type $1 | grep -q 'shell function$'
|
|
||||||
# }
|
|
||||||
|
|
||||||
# copy_function () {
|
|
||||||
# is_function $1 || return 1
|
|
||||||
# echo "${2}() { ${1} }"
|
|
||||||
# }
|
|
||||||
# fi
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
load_metric_with_prefix () {
|
||||||
|
local prefix=$1
|
||||||
|
local file=$2
|
||||||
|
|
||||||
|
local content=$(sed \
|
||||||
|
-e "s/^\s*\(init[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(collect[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(terminate[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(docs[ ]*()[ ]*{\)/${prefix}\1/" $file)
|
||||||
|
|
||||||
|
eval "$content"
|
||||||
|
}
|
||||||
|
|
||||||
|
load_reporter_with_prefix () {
|
||||||
|
local prefix=$1
|
||||||
|
local file=$2
|
||||||
|
local content=$(sed \
|
||||||
|
-e "s/^\s*\(init[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(report[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(terminate[ ]*()[ ]*{\)/${prefix}\1/" \
|
||||||
|
-e "s/^\s*\(docs[ ]*()[ ]*{\)/${prefix}\1/" $file)
|
||||||
|
|
||||||
|
eval "$content"
|
||||||
|
}
|
|
@ -13,8 +13,7 @@ init () {
|
||||||
|
|
||||||
if is_osx; then
|
if is_osx; then
|
||||||
__network_io_collect () {
|
__network_io_collect () {
|
||||||
netstat -bI $NETWORK_IO_INTERFACE |
|
netstat -b -I $NETWORK_IO_INTERFACE | awk '{ print $7" "$10 }' | tail -n 1
|
||||||
awk "/$NETWORK_IO_INTERFACE/"'{ print $7" "$10 }'
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
__network_io_collect () {
|
__network_io_collect () {
|
||||||
|
@ -29,12 +28,14 @@ __network_io_calc_kBps() {
|
||||||
}
|
}
|
||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
local sample=( $(__network_io_collect) )
|
local sample=$(__network_io_collect)
|
||||||
if [ ! -z $__network_io_sample ]; then
|
if [ ! -z "$__network_io_sample" ]; then
|
||||||
report "in" $(__network_io_calc_kBps ${sample[0]} ${__network_io_sample[0]})
|
report "in" $(__network_io_calc_kBps $(echo $sample | awk '{print $1}') \
|
||||||
report "out" $(__network_io_calc_kBps ${sample[1]} ${__network_io_sample[1]})
|
$(echo $__network_io_sample | awk '{print $1}'))
|
||||||
|
report "out" $(__network_io_calc_kBps $(echo $sample | awk '{print $2}') \
|
||||||
|
$(echo $__network_io_sample | awk '{print $2}'))
|
||||||
fi
|
fi
|
||||||
__network_io_sample=( "${sample[@]}" )
|
__network_io_sample="$sample"
|
||||||
}
|
}
|
||||||
|
|
||||||
docs () {
|
docs () {
|
||||||
|
|
Loading…
Reference in New Issue