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
|
# 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 "$@"
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
collect () {
|
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 () {
|
docs () {
|
||||||
|
@ -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 &
|
||||||
}
|
}
|
||||||
@ -16,7 +16,7 @@ init () {
|
|||||||
if is_osx; then
|
if is_osx; then
|
||||||
__disk_io_bgproc () {
|
__disk_io_bgproc () {
|
||||||
iostat -K -d -w $INTERVAL $DISK_IO_MOUNTPOINT | while read line; do
|
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
|
done
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -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
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ init () {
|
|||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
report $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \
|
report $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \
|
||||||
'$0 ~ disk_regexp {printf "%.1f", $5}')
|
'$0 ~ disk_regexp { printf "%.1f", $5 }')
|
||||||
}
|
}
|
||||||
|
|
||||||
docs () {
|
docs () {
|
||||||
|
@ -7,13 +7,14 @@ if is_osx; then
|
|||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
report $(vm_stat | awk -v total_memory=$__memory_os_memsize \
|
report $(vm_stat | awk -v total_memory=$__memory_os_memsize \
|
||||||
'BEGIN {FS=" *"; pages=0}
|
'BEGIN { FS=" *"; pages=0 }
|
||||||
/Pages (free|inactive|speculative)/ {pages+=$2}
|
/Pages (free|inactive|speculative)/ { pages+=$2 }
|
||||||
END {printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0}')
|
END { printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0 }')
|
||||||
}
|
}
|
||||||
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,19 +13,19 @@ 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 () {
|
||||||
cat /proc/net/dev | awk -v iface_regex="$NETWORK_IO_INTERFACE:" \
|
cat /proc/net/dev | awk -v iface_regex="$NETWORK_IO_INTERFACE:" \
|
||||||
'$0 ~ iface_regex {print $2" "$10}'
|
'$0 ~ iface_regex { print $2" "$10 }'
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
__network_io_calc_kBps() {
|
__network_io_calc_kBps() {
|
||||||
echo $1 $2 | awk -v divisor=$__network_io_divisor \
|
echo $1 $2 | awk -v divisor=$__network_io_divisor \
|
||||||
'{printf "%.2f", ($1 - $2) / divisor}'
|
'{ printf "%.2f", ($1 - $2) / divisor }'
|
||||||
}
|
}
|
||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
|
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
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 () {
|
||||||
report $(free | awk '/Swap/{printf "%.1f", $3/$2 * 100.0}')
|
report $(free | awk '/Swap/{ printf "%.1f", $3/$2 * 100.0 }')
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -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