mirror of
https://github.com/pstadler/metrics.sh.git
synced 2025-02-28 13:59:12 -05:00
code cleaup. add disk_usage reporter. add handling for docs(). various tweaks and fixes
This commit is contained in:
parent
8957cb4387
commit
bd037022ad
@ -1,19 +1,11 @@
|
|||||||
#!/bin/sh
|
#!/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 () {
|
is_function () {
|
||||||
[ "`type -t $1`" == 'function' ]
|
[ "`type -t $1`" == 'function' ]
|
||||||
}
|
}
|
||||||
|
|
||||||
OS_TYPE=$(case "$OSTYPE" in
|
# http://stackoverflow.com/a/1369211/183097
|
||||||
(solaris*) echo solaris;;
|
copy_function () {
|
||||||
(darwin*) echo osx;;
|
declare -F $1 > /dev/null || return 1
|
||||||
(linux*) echo linux;;
|
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
|
||||||
(bsd*) echo bsd;;
|
}
|
||||||
(*) echo unknown;;
|
|
||||||
esac)
|
|
15
lib/utils/os.sh
Normal file
15
lib/utils/os.sh
Normal 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' ]; }
|
@ -1,5 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
collect () {
|
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
19
metrics/disk_usage.sh
Normal 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"
|
||||||
|
}
|
@ -1,21 +1,24 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
if [ $OS_TYPE == "osx" ]; then
|
if is_osx; then
|
||||||
|
|
||||||
# FIXME: total_memory leaks out
|
declare -r __memory_os_memsize=$(sysctl -n hw.memsize)
|
||||||
total_memory=$(sysctl -n hw.memsize)
|
|
||||||
|
|
||||||
collect () {
|
collect () {
|
||||||
echo $(vm_stat | awk -v total_memory=$total_memory \
|
echo $(vm_stat | awk -v total_memory=$__memory_os_memsize \
|
||||||
'BEGIN {FS=" *"; pages=0}
|
'BEGIN {FS=" *"; pages=0}
|
||||||
/Pages (free|inactive|speculative)/ {pages+=$2}
|
/Pages (free|inactive|speculative)/ {pages+=$2}
|
||||||
END {printf "%.2f", 100 - (pages * 4096) / total_memory * 100.0}')
|
END {printf "%.1f", 100 - (pages * 4096) / total_memory * 100.0}')
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
collect () {
|
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."
|
||||||
|
}
|
@ -1,15 +1,19 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
if [ $OS_TYPE == "osx" ]; then
|
if is_osx; then
|
||||||
|
|
||||||
collect () {
|
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
|
else
|
||||||
|
|
||||||
collect () {
|
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."
|
||||||
|
}
|
@ -4,4 +4,9 @@ report () {
|
|||||||
METRIC=$1
|
METRIC=$1
|
||||||
VALUE=$2
|
VALUE=$2
|
||||||
curl -d "stat=$METRIC&ezkey=$API_KEY&value=$VALUE" http://api.stathat.com/ez
|
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>"
|
||||||
}
|
}
|
@ -4,4 +4,8 @@ report () {
|
|||||||
METRIC=$1
|
METRIC=$1
|
||||||
VALUE=$2
|
VALUE=$2
|
||||||
echo $METRIC: $VALUE
|
echo $METRIC: $VALUE
|
||||||
|
}
|
||||||
|
|
||||||
|
docs () {
|
||||||
|
echo "Print to standard output (e.g. the TTY you're running the script in)"
|
||||||
}
|
}
|
@ -4,43 +4,67 @@
|
|||||||
INTERVAL=2
|
INTERVAL=2
|
||||||
REPORTER=stdout
|
REPORTER=stdout
|
||||||
|
|
||||||
# init
|
#init
|
||||||
source ./lib/utils.sh
|
__METRICS=()
|
||||||
_METRICS=()
|
|
||||||
|
# load utils
|
||||||
|
for util in ./lib/utils/*.sh; do source $util; done
|
||||||
|
|
||||||
# load reporter
|
# load reporter
|
||||||
source ./reporters/${REPORTER}.sh
|
source ./reporters/${REPORTER}.sh
|
||||||
copy_function report _r_${REPORTER}_report
|
copy_function init __r_${REPORTER}_init
|
||||||
unset -f init report terminate
|
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
|
# load metrics
|
||||||
for file in $(find ./metrics -type f -name '*.sh'); do
|
for file in ./metrics/*.sh; do
|
||||||
source $file
|
|
||||||
filename=$(basename $file)
|
filename=$(basename $file)
|
||||||
metric=${filename%.*}
|
metric=${filename%.*}
|
||||||
copy_function collect _m_${metric}_collect
|
|
||||||
_METRICS+=($metric)
|
# soruce file and copy functions
|
||||||
unset -f init collect terminate
|
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
|
done
|
||||||
|
|
||||||
# init metrics
|
# init metrics
|
||||||
for metric in ${_METRICS[@]}; do
|
for metric in ${__METRICS[@]}; do
|
||||||
if ! is_function _m_${metric}_init; then
|
if ! is_function __m_${metric}_init; then
|
||||||
continue
|
continue
|
||||||
fi
|
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
|
done
|
||||||
|
|
||||||
# collect metrics
|
# collect metrics
|
||||||
while true; do
|
while true; do
|
||||||
for metric in ${_METRICS[@]}; do
|
for metric in ${__METRICS[@]}; do
|
||||||
if ! is_function _m_${metric}_collect; then
|
if ! is_function __m_${metric}_collect; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
result=$(_m_${metric}_collect)
|
result=$(__m_${metric}_collect)
|
||||||
_r_${REPORTER}_report $metric $result
|
__r_${REPORTER}_report $metric $result
|
||||||
done
|
done
|
||||||
|
|
||||||
sleep $INTERVAL
|
sleep $INTERVAL
|
||||||
|
Loading…
x
Reference in New Issue
Block a user