diff --git a/lib/main.sh b/lib/main.sh index 8171516..86c1543 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -73,17 +73,22 @@ main_collect () { } # collect metrics - while true; do - for metric in ${__METRICS[@]}; do - if ! is_function __m_${metric}_collect; then - continue - fi + for metric in ${__METRICS[@]}; do + if ! is_function __m_${metric}_collect; then + continue + fi + fork () { __m_${metric}_collect - done - - sleep $INTERVAL + sleep $INTERVAL + fork + } + fork & + unset -f fork done + + # run forever + tail -f /dev/null # `sleep infinity` is not portable } main_terminate () { diff --git a/metrics/disk_io.sh b/metrics/disk_io.sh index b5d3215..809e13f 100644 --- a/metrics/disk_io.sh +++ b/metrics/disk_io.sh @@ -26,7 +26,7 @@ else fi collect () { - report $(cat < $__disk_io_fifo) + report $(cat $__disk_io_fifo) } terminate () { diff --git a/metrics/swap.sh b/metrics/swap.sh index 2c14c66..640a885 100644 --- a/metrics/swap.sh +++ b/metrics/swap.sh @@ -3,11 +3,12 @@ if is_osx; then collect () { report $(sysctl -n vm.swapusage | - awk '{ if ($3 == 0) exit; printf "%.1f", $6 / $3 * 100.0 }') + awk '{ if (int($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/{ if (int($2) == 0) exit; printf "%.1f", $3 / $2 * 100.0 }') } fi diff --git a/reporters/keen_io.sh b/reporters/keen_io.sh new file mode 100644 index 0000000..65016fb --- /dev/null +++ b/reporters/keen_io.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +init() { + if [ -z $KEEN_IO_PROJECT_ID ]; then + echo "Error: keen_io requires \$KEEN_IO_PROJECT_ID to be set" + exit 1 + fi + + if [ -z $KEEN_IO_WRITE_KEY ]; then + echo "Error: keen_io requires \$KEEN_IO_WRITE_KEY to be set" + exit 1 + fi + + if [ -z $KEEN_IO_EVENT_COLLECTION ]; then + KEEN_IO_EVENT_COLLECTION=$HOSTNAME + fi + + __keen_io_api_url="https://api.keen.io/3.0" + __keen_io_api_url+="/projects/$KEEN_IO_PROJECT_ID" + __keen_io_api_url+="/events/$KEEN_IO_EVENT_COLLECTION" + __keen_io_api_url+="?api_key=$KEEN_IO_WRITE_KEY" +} + +report () { + METRIC=$1 + VALUE=$2 + curl -s $__keen_io_api_url -H "Content-Type: application/json" \ + -d "{\"metric\": \"$METRIC\", \"value\": $VALUE}" > /dev/null +} + +docs () { + echo "Send data to Keen IO (https://keen.io)." + echo "\$KEEN_IO_WRITE_KEY=" + echo "\$KEEN_IO_PROJECT_ID=" + echo "\$KEEN_IO_EVENT_COLLECTION=$KEEN_IO_EVENT_COLLECTION" +} \ No newline at end of file diff --git a/reporters/stathat.sh b/reporters/stathat.sh index e584153..677467a 100644 --- a/reporters/stathat.sh +++ b/reporters/stathat.sh @@ -1,12 +1,20 @@ #!/bin/sh +init () { + if [ -z $STATHAT_API_KEY ]; then + echo "Error: stathat requires \$STATHAT_API_KEY to be set" + exit 1 + fi +} + report () { METRIC=$1 VALUE=$2 - curl -d "stat=$METRIC&ezkey=$STATHAT_API_KEY&value=$VALUE" http://api.stathat.com/ez + curl -s -d "stat=$METRIC&ezkey=$STATHAT_API_KEY&value=$VALUE" \ + http://api.stathat.com/ez > /dev/null } docs () { echo "Send data to StatHat (https://www.stathat.com)." - echo "\$API_KEY=" + echo "\$STATHAT_API_KEY=" } \ No newline at end of file diff --git a/sysmetricsd.sh b/sysmetricsd.sh index b6b97d8..de7cfef 100755 --- a/sysmetricsd.sh +++ b/sysmetricsd.sh @@ -2,16 +2,16 @@ # config INTERVAL=1 -REPORTER=stdout +REPORTER=stdout # TODO: handle multiple reporters # handle opts -opts_spec=":dvh-:" +opts_spec=":dhvr:" opt_docs=false opt_verbose=false usage () { - echo "usage: $0 [-d] [-h]" + echo "usage: $0 [-d] [-h] [-v] [-r]" } help () { @@ -23,13 +23,16 @@ while getopts "$opts_spec" opt; do d) opt_docs=true ;; - v) - opt_verbose=true - ;; h) help exit ;; + v) + opt_verbose=true + ;; + r) + REPORTER=$OPTARG + ;; *) usage exit 1 @@ -50,6 +53,7 @@ verbose "OS detected: $OS_TYPE" main_load verbose "Metrics loaded: ${__METRICS[@]}" +verbose "Reporters loaded: ${REPORTER}" if [ "$opt_docs" = true ]; then main_docs