mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-02-28 13:59:12 -05: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
|
||||
__METRICS=()
|
||||
__TEMP_DIR=$(make_temp_dir)
|
||||
|
||||
# load reporter
|
||||
|
||||
main_load () {
|
||||
# load reporter
|
||||
source ./reporters/${REPORTER}.sh
|
||||
copy_function init __r_${REPORTER}_init
|
||||
copy_function report __r_${REPORTER}_report
|
||||
@ -34,6 +33,14 @@ main_load () {
|
||||
}
|
||||
|
||||
main_init () {
|
||||
TEMP_DIR=$(make_temp_dir)
|
||||
|
||||
# register trap
|
||||
trap '
|
||||
main_terminate
|
||||
trap - SIGTERM && kill -- -$$ SIGINT SIGTERM EXIT
|
||||
' SIGINT SIGTERM EXIT
|
||||
|
||||
# init reporter
|
||||
if is_function __r_${REPORTER}_init; then
|
||||
__r_${REPORTER}_init
|
||||
@ -49,19 +56,6 @@ main_init () {
|
||||
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 () {
|
||||
# used by metrics to return results
|
||||
report () {
|
||||
@ -105,4 +99,22 @@ main_terminate () {
|
||||
if is_function __r_${REPORTER}_terminate; then
|
||||
__r_${REPORTER}_terminate
|
||||
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 "$@"
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
collect () {
|
||||
report $(ps aux | awk '{sum+=$3} END {printf "%.1f\n", sum}' | tail -n 1)
|
||||
report $(ps aux | awk '{ sum+=$3 } END { printf "%.1f\n", sum }' | tail -n 1)
|
||||
}
|
||||
|
||||
docs () {
|
||||
|
@ -8,7 +8,7 @@ init () {
|
||||
DISK_IO_MOUNTPOINT="/dev/vda"
|
||||
fi
|
||||
fi
|
||||
readonly __disk_io_fifo=$__TEMP_DIR/disk_io
|
||||
readonly __disk_io_fifo=$TEMP_DIR/disk_io
|
||||
mkfifo $__disk_io_fifo
|
||||
__disk_io_bgproc &
|
||||
}
|
||||
@ -16,7 +16,7 @@ init () {
|
||||
if is_osx; then
|
||||
__disk_io_bgproc () {
|
||||
iostat -K -d -w $INTERVAL $DISK_IO_MOUNTPOINT | while read line; do
|
||||
echo $line | awk '{print $3}' > $__disk_io_fifo
|
||||
echo $line | awk '{ print $3 }' > $__disk_io_fifo
|
||||
done
|
||||
}
|
||||
else
|
||||
@ -26,11 +26,11 @@ else
|
||||
fi
|
||||
|
||||
collect () {
|
||||
report $(cat $__disk_io_fifo)
|
||||
report $(cat < $__disk_io_fifo)
|
||||
}
|
||||
|
||||
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
|
||||
fi
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ init () {
|
||||
|
||||
collect () {
|
||||
report $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \
|
||||
'$0 ~ disk_regexp {printf "%.1f", $5}')
|
||||
'$0 ~ disk_regexp { printf "%.1f", $5 }')
|
||||
}
|
||||
|
||||
docs () {
|
||||
|
@ -7,13 +7,14 @@ if is_osx; then
|
||||
|
||||
collect () {
|
||||
report $(vm_stat | awk -v total_memory=$__memory_os_memsize \
|
||||
'BEGIN {FS=" *"; pages=0}
|
||||
/Pages (free|inactive|speculative)/ {pages+=$2}
|
||||
END {printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0}')
|
||||
'BEGIN { FS=" *"; pages=0 }
|
||||
/Pages (free|inactive|speculative)/ { pages+=$2 }
|
||||
END { printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0 }')
|
||||
}
|
||||
else
|
||||
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
|
||||
|
||||
|
@ -13,19 +13,19 @@ init () {
|
||||
|
||||
if is_osx; then
|
||||
__network_io_collect () {
|
||||
netstat -bI $NETWORK_IO_INTERFACE | \
|
||||
awk "/$NETWORK_IO_INTERFACE/"'{print $7" "$10; exit}'
|
||||
netstat -bI $NETWORK_IO_INTERFACE |
|
||||
awk "/$NETWORK_IO_INTERFACE/"'{ print $7" "$10 }'
|
||||
}
|
||||
else
|
||||
__network_io_collect () {
|
||||
cat /proc/net/dev | awk -v iface_regex="$NETWORK_IO_INTERFACE:" \
|
||||
'$0 ~ iface_regex {print $2" "$10}'
|
||||
'$0 ~ iface_regex { print $2" "$10 }'
|
||||
}
|
||||
fi
|
||||
|
||||
__network_io_calc_kBps() {
|
||||
echo $1 $2 | awk -v divisor=$__network_io_divisor \
|
||||
'{printf "%.2f", ($1 - $2) / divisor}'
|
||||
'{ printf "%.2f", ($1 - $2) / divisor }'
|
||||
}
|
||||
|
||||
collect () {
|
||||
|
@ -2,11 +2,12 @@
|
||||
|
||||
if is_osx; then
|
||||
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
|
||||
collect () {
|
||||
report $(free | awk '/Swap/{printf "%.1f", $3/$2 * 100.0}')
|
||||
report $(free | awk '/Swap/{ printf "%.1f", $3/$2 * 100.0 }')
|
||||
}
|
||||
fi
|
||||
|
||||
|
@ -4,7 +4,7 @@ report () {
|
||||
local METRIC=$1
|
||||
local VALUE=$2
|
||||
local DATE=$(iso_date)
|
||||
echo $DATE $METRIC: $VALUE >> $FILE_LOCATION
|
||||
echo "$DATE $METRIC: $VALUE" >> $FILE_LOCATION
|
||||
}
|
||||
|
||||
init () {
|
||||
|
@ -2,16 +2,62 @@
|
||||
|
||||
# config
|
||||
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
|
||||
|
||||
if [ $opt_verbose = "true" ]; then
|
||||
verbose_on
|
||||
verbose "Started in verbose mode"
|
||||
fi
|
||||
verbose "OS detected: $OS_TYPE"
|
||||
|
||||
main_load
|
||||
verbose "Metrics loaded: ${__METRICS[@]}"
|
||||
|
||||
if [ "$opt_docs" = true ]; then
|
||||
main_docs
|
||||
exit
|
||||
fi
|
||||
|
||||
main_init
|
||||
verbose "Metrics initialized"
|
||||
|
||||
verbose "Collecting metrics every $INTERVAL second(s)"
|
||||
main_collect
|
Loading…
x
Reference in New Issue
Block a user