mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-07-08 16:42:15 -04:00
improved coding standards. options parsing. verbose logging
This commit is contained in:
parent
9858beb329
commit
5c65c6646f
42
lib/main.sh
42
lib/main.sh
@ -3,11 +3,10 @@ for util in ./lib/utils/*.sh; do source $util; done
|
|||||||
|
|
||||||
# init
|
# init
|
||||||
__METRICS=()
|
__METRICS=()
|
||||||
__TEMP_DIR=$(make_temp_dir)
|
|
||||||
|
|
||||||
# load reporter
|
|
||||||
|
|
||||||
main_load () {
|
main_load () {
|
||||||
|
# load reporter
|
||||||
source ./reporters/${REPORTER}.sh
|
source ./reporters/${REPORTER}.sh
|
||||||
copy_function init __r_${REPORTER}_init
|
copy_function init __r_${REPORTER}_init
|
||||||
copy_function report __r_${REPORTER}_report
|
copy_function report __r_${REPORTER}_report
|
||||||
@ -34,6 +33,14 @@ main_load () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
main_init () {
|
main_init () {
|
||||||
|
TEMP_DIR=$(make_temp_dir)
|
||||||
|
|
||||||
|
# register trap
|
||||||
|
trap '
|
||||||
|
main_terminate
|
||||||
|
trap - SIGTERM && kill -- -$$ SIGINT SIGTERM EXIT
|
||||||
|
' SIGINT SIGTERM EXIT
|
||||||
|
|
||||||
# init reporter
|
# init reporter
|
||||||
if is_function __r_${REPORTER}_init; then
|
if is_function __r_${REPORTER}_init; then
|
||||||
__r_${REPORTER}_init
|
__r_${REPORTER}_init
|
||||||
@ -49,19 +56,6 @@ main_init () {
|
|||||||
done
|
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 () {
|
main_collect () {
|
||||||
# used by metrics to return results
|
# used by metrics to return results
|
||||||
report () {
|
report () {
|
||||||
@ -105,4 +99,22 @@ main_terminate () {
|
|||||||
if is_function __r_${REPORTER}_terminate; then
|
if is_function __r_${REPORTER}_terminate; then
|
||||||
__r_${REPORTER}_terminate
|
__r_${REPORTER}_terminate
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# delete temporary directory
|
||||||
|
if [ ! -z $TEMP_DIR ] && [ -d $TEMP_DIR ]; then
|
||||||
|
rmdir $TEMP_DIR
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
16
lib/utils/verbose.sh
Normal file
16
lib/utils/verbose.sh
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
VERBOSE_MODE=false
|
||||||
|
|
||||||
|
verbose_on () {
|
||||||
|
VERBOSE_MODE=true
|
||||||
|
}
|
||||||
|
|
||||||
|
verbose_off () {
|
||||||
|
VERBOSE_MODE=true
|
||||||
|
}
|
||||||
|
|
||||||
|
verbose () {
|
||||||
|
if [ $VERBOSE_MODE = "false" ]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
echo "$@"
|
||||||
|
}
|
@ -8,7 +8,7 @@ init () {
|
|||||||
DISK_IO_MOUNTPOINT="/dev/vda"
|
DISK_IO_MOUNTPOINT="/dev/vda"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
readonly __disk_io_fifo=$__TEMP_DIR/disk_io
|
readonly __disk_io_fifo=$TEMP_DIR/disk_io
|
||||||
mkfifo $__disk_io_fifo
|
mkfifo $__disk_io_fifo
|
||||||
__disk_io_bgproc &
|
__disk_io_bgproc &
|
||||||
}
|
}
|
||||||
@ -26,11 +26,11 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
report $(cat $__disk_io_fifo)
|
report $(cat < $__disk_io_fifo)
|
||||||
}
|
}
|
||||||
|
|
||||||
terminate () {
|
terminate () {
|
||||||
if [ ! -z $__disk_io_fifo ] && [ -f $__disk_io_fifo ]; then
|
if [ ! -z $__disk_io_fifo ] && [ -p $__disk_io_fifo ]; then
|
||||||
rm $__disk_io_fifo
|
rm $__disk_io_fifo
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,8 @@ if is_osx; then
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
collect () {
|
collect () {
|
||||||
report $(free | awk '/buffers\/cache/{printf "%.1f", 100 - $4 / ($3 + $4) * 100.0}')
|
report $(free | awk '/buffers\/cache/
|
||||||
|
{ printf "%.1f", 100 - $4 / ($3 + $4) * 100.0 }')
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ init () {
|
|||||||
|
|
||||||
if is_osx; then
|
if is_osx; then
|
||||||
__network_io_collect () {
|
__network_io_collect () {
|
||||||
netstat -bI $NETWORK_IO_INTERFACE | \
|
netstat -bI $NETWORK_IO_INTERFACE |
|
||||||
awk "/$NETWORK_IO_INTERFACE/"'{print $7" "$10; exit}'
|
awk "/$NETWORK_IO_INTERFACE/"'{ print $7" "$10 }'
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
__network_io_collect () {
|
__network_io_collect () {
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
if is_osx; then
|
if is_osx; then
|
||||||
collect () {
|
collect () {
|
||||||
report $(sysctl -n vm.swapusage | awk '{printf "%.1f", $6 / $3 * 100.0}')
|
report $(sysctl -n vm.swapusage |
|
||||||
|
awk '{ if ($3 == 0) exit; printf "%.1f", $6 / $3 * 100.0 }')
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
collect () {
|
collect () {
|
||||||
|
@ -4,7 +4,7 @@ report () {
|
|||||||
local METRIC=$1
|
local METRIC=$1
|
||||||
local VALUE=$2
|
local VALUE=$2
|
||||||
local DATE=$(iso_date)
|
local DATE=$(iso_date)
|
||||||
echo $DATE $METRIC: $VALUE >> $FILE_LOCATION
|
echo "$DATE $METRIC: $VALUE" >> $FILE_LOCATION
|
||||||
}
|
}
|
||||||
|
|
||||||
init () {
|
init () {
|
||||||
|
@ -2,16 +2,62 @@
|
|||||||
|
|
||||||
# config
|
# config
|
||||||
INTERVAL=1
|
INTERVAL=1
|
||||||
REPORTER=file
|
REPORTER=stdout
|
||||||
|
|
||||||
# register trap
|
|
||||||
trap '
|
|
||||||
main_terminate
|
|
||||||
trap - SIGTERM && kill -- -$$ SIGINT SIGTERM EXIT
|
|
||||||
' SIGINT SIGTERM EXIT
|
|
||||||
|
|
||||||
# load and start main routine
|
# handle opts
|
||||||
|
opts_spec=":dvh-:"
|
||||||
|
opt_docs=false
|
||||||
|
opt_verbose=false
|
||||||
|
|
||||||
|
usage () {
|
||||||
|
echo "usage: $0 [-d] [-h]"
|
||||||
|
}
|
||||||
|
|
||||||
|
help () {
|
||||||
|
echo "TODO"
|
||||||
|
}
|
||||||
|
|
||||||
|
while getopts "$opts_spec" opt; do
|
||||||
|
case "${opt}" in
|
||||||
|
d)
|
||||||
|
opt_docs=true
|
||||||
|
;;
|
||||||
|
v)
|
||||||
|
opt_verbose=true
|
||||||
|
;;
|
||||||
|
h)
|
||||||
|
help
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND-1))
|
||||||
|
|
||||||
|
|
||||||
|
# run
|
||||||
source ./lib/main.sh
|
source ./lib/main.sh
|
||||||
|
|
||||||
|
if [ $opt_verbose = "true" ]; then
|
||||||
|
verbose_on
|
||||||
|
verbose "Started in verbose mode"
|
||||||
|
fi
|
||||||
|
verbose "OS detected: $OS_TYPE"
|
||||||
|
|
||||||
main_load
|
main_load
|
||||||
|
verbose "Metrics loaded: ${__METRICS[@]}"
|
||||||
|
|
||||||
|
if [ "$opt_docs" = true ]; then
|
||||||
|
main_docs
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
main_init
|
main_init
|
||||||
|
verbose "Metrics initialized"
|
||||||
|
|
||||||
|
verbose "Collecting metrics every $INTERVAL second(s)"
|
||||||
main_collect
|
main_collect
|
Loading…
x
Reference in New Issue
Block a user