From c54c9d73b7ca4c65b4e26f0e0a71c2e7243c342e Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Sun, 8 Mar 2015 17:51:15 +0100 Subject: [PATCH] implement disk_io metric on osx --- README.md | 14 ++++++++++ lib/utils/os.sh | 11 +++++++- metrics/disk_io.sh | 64 +++++++++++++++++++++++++++------------------- sysmetricsd.sh | 23 ++++++++++++----- 4 files changed, 77 insertions(+), 35 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..01d8c1d --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +## TODO + +- [] README +- [] option parsing +- [] allow multiple reporters +- [] implement log reporter +- [] better docs, including reporters (--docs) +- [] config file support +- [] config file auto-generation +- [] disk_io metric +- [] load custom metrics +- [] async metrics +- [] extended metric labels? +- [] same metric multiple times (e.g. disk_usage for multiple devices) \ No newline at end of file diff --git a/lib/utils/os.sh b/lib/utils/os.sh index 22bbe08..155e93b 100644 --- a/lib/utils/os.sh +++ b/lib/utils/os.sh @@ -12,4 +12,13 @@ is_solaris () { [ $OS_TYPE == 'solaris' ]; } is_osx () { [ $OS_TYPE == 'osx' ]; } is_linux () { [ $OS_TYPE == 'solaris' ]; } is_bsd () { [ $OS_TYPE == 'bsd']; } -is_unknown () { [ $OS_TYPE == 'unknown' ]; } \ No newline at end of file +is_unknown () { [ $OS_TYPE == 'unknown' ]; } + + +make_temp_dir () { + if is_osx; then + mktemp -d -t 'sysmetrics' + else + mktemp -d + fi +} \ No newline at end of file diff --git a/metrics/disk_io.sh b/metrics/disk_io.sh index 3c05a50..80b1b22 100644 --- a/metrics/disk_io.sh +++ b/metrics/disk_io.sh @@ -1,31 +1,41 @@ -# #!/bin/sh +#!/bin/sh -# if [ -z $DISK_IO_MOUNTPOINT ]; then -# if is_osx; then -# DISK_IO_MOUNTPOINT="disk0" -# else -# DISK_IO_MOUNTPOINT="/dev/vda" -# fi -# fi +if [ -z $DISK_IO_MOUNTPOINT ]; then + if is_osx; then + DISK_IO_MOUNTPOINT="disk0" + else + DISK_IO_MOUNTPOINT="/dev/vda" + fi +fi -# if is_osx; then -# __disk_io_bgproc () { -# iostat -K -d -c 99999 -w $INTERVAL $DISK_IO_MOUNTPOINT | while read line; do -# echo $line | awk '{print $3}' > ./foobar -# done -# } -# else -# __disk_io_bgproc () { -# echo $(iostat -y -d 1 $DISK_IO_MOUNTPOINT) -# } -# fi +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 + done + } +else + __disk_io_bgproc () { + echo $(iostat -y -d 1 $DISK_IO_MOUNTPOINT) + } +fi -# init () { -# mkfifo ./foobar -# #exec 3<> ./foobar -# __disk_io_bgproc > ./foobar & -# } +__disk_io_fifo=$__TEMP_DIR/disk_io -# collect () { -# cat ./foobar -# } \ No newline at end of file +init () { + __disk_io_bgproc & + mkfifo $__disk_io_fifo +} + +collect () { + report $(cat $__disk_io_fifo) +} + +terminate () { + rm $__disk_io_fifo +} + +docs () { + echo "Disk I/O in MB/s." + echo "\$DISK_IO_MOUNTPOINT=$DISK_IO_MOUNTPOINT" +} \ No newline at end of file diff --git a/sysmetricsd.sh b/sysmetricsd.sh index 84ff1cc..79d50c7 100755 --- a/sysmetricsd.sh +++ b/sysmetricsd.sh @@ -4,12 +4,24 @@ INTERVAL=1 REPORTER=stdout -#init -__METRICS=() - # load utils for util in ./lib/utils/*.sh; do source $util; done +# init +__METRICS=() +__TEMP_DIR=$(make_temp_dir) + +# register trap +trap ' + for metric in ${__METRICS[@]}; do + if ! is_function __m_${metric}_terminate; then + continue + fi + __m_${metric}_terminate + done + trap - SIGTERM && kill -- -$$ SIGINT SIGTERM EXIT +' SIGINT SIGTERM EXIT + # load reporter source ./reporters/${REPORTER}.sh copy_function init __r_${REPORTER}_init @@ -79,7 +91,4 @@ while true; do done sleep $INTERVAL -done - -# trap 'kill $(jobs -pr)' SIGINT SIGTERM EXIT -# trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT \ No newline at end of file +done \ No newline at end of file