basic script flow implemented

This commit is contained in:
Patrick Stadler 2015-02-21 18:18:27 +01:00
parent ae2ec619d3
commit 5d3a3fcdb1
8 changed files with 71 additions and 7 deletions

View File

@ -1,2 +0,0 @@
#!/bin/sh
# curl -d "stat=$METRIC&ezkey=$API_KEY&value=$VALUE" http://api.stathat.com/ez

View File

@ -1,2 +1,5 @@
#!/bin/sh
echo $(ps aux | awk {'sum+=$3;print sum'} | tail -n 1)
collect () {
echo $(ps aux | awk {'sum+=$3;print sum'} | tail -n 1)
}

View File

@ -1,2 +1,5 @@
#!/bin/sh
echo $(free | awk '/buffers\/cache/{print $4/($3+$4) * 100.0;}')
collect () {
echo $(free | awk '/buffers\/cache/{print $4/($3+$4) * 100.0;}')
}

View File

@ -1,2 +1,5 @@
#!/bin/sh
echo $(free | awk '/Swap/{print $3/$2 * 100.0;}')
collect () {
echo $(free | awk '/Swap/{print $3/$2 * 100.0;}')
}

7
reporters/stathat.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/sh
report () {
METRIC=$1
VALUE=$2
curl -d "stat=$METRIC&ezkey=$API_KEY&value=$VALUE" http://api.stathat.com/ez
}

7
reporters/stdout.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/sh
report () {
METRIC=$1
VALUE=$2
echo $METRIC: $VALUE
}

View File

@ -1,5 +1,43 @@
#!/bin/sh
for script in $(find ./metrics -type f -name '*.sh'); do
sh $script
# config
INTERVAL=2
REPORTER=stdout
# init
source utils.sh
_METRICS=()
# load reporter
source ./reporters/${REPORTER}.sh
copy_function report _r_${REPORTER}_report
unset -f report
# load metrics
for file in $(find ./metrics -type f -name '*.sh'); do
source $file
filename=$(basename $file)
metric=${filename%.*}
copy_function collect _m_${metric}_collect
_METRICS+=($metric)
unset -f collect
unset -f init
done
# init metrics
for metric in ${_METRICS[@]}; do
[ "`type -t _m_${metric}_init`" != 'function' ] && continue
echo init metric "$metric"
_m_${metric}_init
done
# collect metrics
while true; do
for metric in ${_METRICS[@]}; do
[ "`type -t _m_${metric}_collect`" != 'function' ] && continue
result=$(_m_${metric}_collect)
_r_${REPORTER}_report $metric $result
done
sleep $INTERVAL
done

5
utils.sh Normal file
View File

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