code cleaup. add disk_usage reporter. add handling for docs(). various tweaks and fixes

This commit is contained in:
Patrick Stadler 2015-02-22 20:45:57 +01:00
parent 8957cb4387
commit bd037022ad
9 changed files with 114 additions and 44 deletions

View File

@ -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)
# http://stackoverflow.com/a/1369211/183097
copy_function () {
declare -F $1 > /dev/null || return 1
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
}

15
lib/utils/os.sh Normal file
View File

@ -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' ]; }

View File

@ -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."
}

19
metrics/disk_usage.sh Normal file
View File

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

View File

@ -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
fi
docs () {
echo "Percentage of used memory."
}

View File

@ -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
fi
docs () {
echo "Percentage of used swap space."
}

View File

@ -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=<ez_key>"
}

View File

@ -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)"
}

View File

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