implement parallel collecting. add keen_io reporter. several fixes

This commit is contained in:
Patrick Stadler 2015-03-12 20:59:09 +01:00
parent 5c65c6646f
commit 88318c64c4
6 changed files with 73 additions and 19 deletions

View File

@ -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 () {

View File

@ -26,7 +26,7 @@ else
fi
collect () {
report $(cat < $__disk_io_fifo)
report $(cat $__disk_io_fifo)
}
terminate () {

View File

@ -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

36
reporters/keen_io.sh Normal file
View File

@ -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=<write_key>"
echo "\$KEEN_IO_PROJECT_ID=<project_id>"
echo "\$KEEN_IO_EVENT_COLLECTION=$KEEN_IO_EVENT_COLLECTION"
}

View File

@ -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=<ez_key>"
echo "\$STATHAT_API_KEY=<ez_key>"
}

View File

@ -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