diff --git a/lib/main.sh b/lib/main.sh index 2fbf7a7..8171516 100644 --- a/lib/main.sh +++ b/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 } \ No newline at end of file diff --git a/lib/utils/verbose.sh b/lib/utils/verbose.sh new file mode 100644 index 0000000..bb7e35f --- /dev/null +++ b/lib/utils/verbose.sh @@ -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 "$@" +} \ No newline at end of file diff --git a/metrics/cpu.sh b/metrics/cpu.sh index 59c4615..fc9b80d 100644 --- a/metrics/cpu.sh +++ b/metrics/cpu.sh @@ -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 () { diff --git a/metrics/disk_io.sh b/metrics/disk_io.sh index 1284bfb..b5d3215 100644 --- a/metrics/disk_io.sh +++ b/metrics/disk_io.sh @@ -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 } diff --git a/metrics/disk_usage.sh b/metrics/disk_usage.sh index 3205124..323fc50 100644 --- a/metrics/disk_usage.sh +++ b/metrics/disk_usage.sh @@ -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 () { diff --git a/metrics/memory.sh b/metrics/memory.sh index 234b10b..6cf3bc0 100644 --- a/metrics/memory.sh +++ b/metrics/memory.sh @@ -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 diff --git a/metrics/network_io.sh b/metrics/network_io.sh index 4c2eb75..a3f9d75 100644 --- a/metrics/network_io.sh +++ b/metrics/network_io.sh @@ -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 () { diff --git a/metrics/swap.sh b/metrics/swap.sh index 909ae74..2c14c66 100644 --- a/metrics/swap.sh +++ b/metrics/swap.sh @@ -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 diff --git a/reporters/file.sh b/reporters/file.sh index dec4f17..39a2c09 100644 --- a/reporters/file.sh +++ b/reporters/file.sh @@ -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 () { diff --git a/sysmetricsd.sh b/sysmetricsd.sh index eb8e473..b6b97d8 100755 --- a/sysmetricsd.sh +++ b/sysmetricsd.sh @@ -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 \ No newline at end of file