From bd037022ad5c9e93c35740fd4ba9dbaf3f39b06f Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Sun, 22 Feb 2015 20:45:57 +0100 Subject: [PATCH] code cleaup. add disk_usage reporter. add handling for docs(). various tweaks and fixes --- lib/{utils.sh => utils/functions.sh} | 18 +++------ lib/utils/os.sh | 15 +++++++ metrics/cpu.sh | 6 ++- metrics/disk_usage.sh | 19 +++++++++ metrics/memory.sh | 21 +++++----- metrics/swap.sh | 12 ++++-- reporters/stathat.sh | 5 +++ reporters/stdout.sh | 4 ++ sysmetricsd.sh | 58 ++++++++++++++++++++-------- 9 files changed, 114 insertions(+), 44 deletions(-) rename lib/{utils.sh => utils/functions.sh} (57%) create mode 100644 lib/utils/os.sh create mode 100644 metrics/disk_usage.sh diff --git a/lib/utils.sh b/lib/utils/functions.sh similarity index 57% rename from lib/utils.sh rename to lib/utils/functions.sh index 3407dc0..24696b6 100644 --- a/lib/utils.sh +++ b/lib/utils/functions.sh @@ -1,19 +1,11 @@ #!/bin/sh -# http://stackoverflow.com/a/1369211/183097 -copy_function () { - declare -F $1 > /dev/null || return 1 - eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)" -} - is_function () { [ "`type -t $1`" == 'function' ] } -OS_TYPE=$(case "$OSTYPE" in - (solaris*) echo solaris;; - (darwin*) echo osx;; - (linux*) echo linux;; - (bsd*) echo bsd;; - (*) echo unknown;; -esac) \ No newline at end of file +# http://stackoverflow.com/a/1369211/183097 +copy_function () { + declare -F $1 > /dev/null || return 1 + eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)" +} \ No newline at end of file diff --git a/lib/utils/os.sh b/lib/utils/os.sh new file mode 100644 index 0000000..22bbe08 --- /dev/null +++ b/lib/utils/os.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +declare -r OS_TYPE=$(case "$OSTYPE" in + (solaris*) echo solaris;; + (darwin*) echo osx;; + (linux*) echo linux;; + (bsd*) echo bsd;; + (*) echo unknown;; +esac) + +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 diff --git a/metrics/cpu.sh b/metrics/cpu.sh index bcecc9f..b6584b5 100644 --- a/metrics/cpu.sh +++ b/metrics/cpu.sh @@ -1,5 +1,9 @@ #!/bin/sh collect () { - echo $(ps aux | awk {'sum+=$3;print sum'} | tail -n 1) + echo $(ps aux | awk '{sum+=$3} END {printf "%.1f\n", sum}' | tail -n 1) +} + +docs () { + echo "CPU load percentage." } \ No newline at end of file diff --git a/metrics/disk_usage.sh b/metrics/disk_usage.sh new file mode 100644 index 0000000..ed95e59 --- /dev/null +++ b/metrics/disk_usage.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +if [ -z $DISK_USAGE_MOUNTPOINT ]; then + if is_osx; then + DISK_USAGE_MOUNTPOINT="/dev/disk1" + else + DISK_USAGE_MOUNTPOINT="/dev/vda" + fi +fi + +collect () { + echo $(df | awk -v disk_regexp="^$DISK_USAGE_MOUNTPOINT" \ + '$0 ~ disk_regexp {printf "%.1f", $5}') +} + +docs () { + echo "Disk usage percentage for a file system at a given mount point." + echo "\$DISK_USAGE_MOUNTPOINT=$DISK_USAGE_MOUNTPOINT" +} \ No newline at end of file diff --git a/metrics/memory.sh b/metrics/memory.sh index d88abd7..8d67154 100644 --- a/metrics/memory.sh +++ b/metrics/memory.sh @@ -1,21 +1,24 @@ #!/bin/sh -if [ $OS_TYPE == "osx" ]; then +if is_osx; then - # FIXME: total_memory leaks out - total_memory=$(sysctl -n hw.memsize) + declare -r __memory_os_memsize=$(sysctl -n hw.memsize) collect () { - echo $(vm_stat | awk -v total_memory=$total_memory \ - 'BEGIN {FS=" *"; pages=0} - /Pages (free|inactive|speculative)/ {pages+=$2} - END {printf "%.2f", 100 - (pages * 4096) / total_memory * 100.0}') + echo $(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}') } else collect () { - echo $(free | awk '/buffers\/cache/{printf "%.2f", $4 / ($3 + $4) * 100.0}') + echo $(free | awk '/buffers\/cache/{printf "%.1f", 100 - $4 / ($3 + $4) * 100.0}') } -fi \ No newline at end of file +fi + +docs () { + echo "Percentage of used memory." +} \ No newline at end of file diff --git a/metrics/swap.sh b/metrics/swap.sh index 97e7c81..b2e7c8c 100644 --- a/metrics/swap.sh +++ b/metrics/swap.sh @@ -1,15 +1,19 @@ #!/bin/sh -if [ $OS_TYPE == "osx" ]; then +if is_osx; then collect () { - echo $(sysctl -n vm.swapusage | awk '{printf "%.2f", $6 / $3 * 100.0}') + echo $(sysctl -n vm.swapusage | awk '{printf "%.1f", $6 / $3 * 100.0}') } else collect () { - echo $(free | awk '/Swap/{printf "%.2f", $3/$2 * 100.0;}') + echo $(free | awk '/Swap/{printf "%.1f", $3/$2 * 100.0}') } -fi \ No newline at end of file +fi + +docs () { + echo "Percentage of used swap space." +} \ No newline at end of file diff --git a/reporters/stathat.sh b/reporters/stathat.sh index 16d4784..1aec1a7 100644 --- a/reporters/stathat.sh +++ b/reporters/stathat.sh @@ -4,4 +4,9 @@ report () { METRIC=$1 VALUE=$2 curl -d "stat=$METRIC&ezkey=$API_KEY&value=$VALUE" http://api.stathat.com/ez +} + +docs () { + echo "Send data to StatHat (https://www.stathat.com)." + echo "\$API_KEY=" } \ No newline at end of file diff --git a/reporters/stdout.sh b/reporters/stdout.sh index b447793..d292643 100644 --- a/reporters/stdout.sh +++ b/reporters/stdout.sh @@ -4,4 +4,8 @@ report () { METRIC=$1 VALUE=$2 echo $METRIC: $VALUE +} + +docs () { + echo "Print to standard output (e.g. the TTY you're running the script in)" } \ No newline at end of file diff --git a/sysmetricsd.sh b/sysmetricsd.sh index d3c3852..a6ca9af 100755 --- a/sysmetricsd.sh +++ b/sysmetricsd.sh @@ -4,43 +4,67 @@ INTERVAL=2 REPORTER=stdout -# init -source ./lib/utils.sh -_METRICS=() +#init +__METRICS=() + +# load utils +for util in ./lib/utils/*.sh; do source $util; done # load reporter source ./reporters/${REPORTER}.sh -copy_function report _r_${REPORTER}_report -unset -f init report terminate +copy_function init __r_${REPORTER}_init +copy_function report __r_${REPORTER}_report +copy_function terminate __r_${REPORTER}_terminate +copy_function docs __r_${REPORTER}_docs +unset -f init report terminate docs # load metrics -for file in $(find ./metrics -type f -name '*.sh'); do - source $file +for file in ./metrics/*.sh; do filename=$(basename $file) metric=${filename%.*} - copy_function collect _m_${metric}_collect - _METRICS+=($metric) - unset -f init collect terminate + + # soruce file and copy functions + source $file + copy_function init __m_${metric}_init + copy_function collect __m_${metric}_collect + copy_function terminate __m_${metric}_terminate + copy_function docs __m_${metric}_docs + unset -f init collect terminate docs + + # register metric + __METRICS+=($metric) done # init metrics -for metric in ${_METRICS[@]}; do - if ! is_function _m_${metric}_init; then +for metric in ${__METRICS[@]}; do + if ! is_function __m_${metric}_init; then continue fi - _m_${metric}_init + __m_${metric}_init +done + +# print docs for metrics +echo "Available metrics:" +for metric in ${__METRICS[@]}; do + if ! is_function __m_${metric}_docs; then + continue + fi + + echo "[$metric]" + __m_${metric}_docs + echo done # collect metrics while true; do - for metric in ${_METRICS[@]}; do - if ! is_function _m_${metric}_collect; then + for metric in ${__METRICS[@]}; do + if ! is_function __m_${metric}_collect; then continue fi - result=$(_m_${metric}_collect) - _r_${REPORTER}_report $metric $result + result=$(__m_${metric}_collect) + __r_${REPORTER}_report $metric $result done sleep $INTERVAL