load custom reporters. update README
This commit is contained in:
parent
f61ca57450
commit
1fd9771e91
|
@ -0,0 +1,2 @@
|
||||||
|
/metrics/custom/*.sh
|
||||||
|
/reporters/custom/*.sh
|
|
@ -1,6 +1,6 @@
|
||||||
# metrics.sh
|
# metrics.sh
|
||||||
|
|
||||||
metrics.sh is a lightweight metrics collection and fowarding utility implemented in portable POSIX compliant shell scripts. A transparent interface based on hooks enables writing custom metric collectors and reporters in an elegant way.
|
metrics.sh is a lightweight metrics collection and fowarding utility implemented in portable POSIX compliant shell scripts. A transparent interface based on hooks enables writing custom collectors and reporters in an elegant way.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ $ git clone git@github.com:pstadler/metrics.sh.git
|
||||||
|
|
||||||
### Requirements
|
### Requirements
|
||||||
|
|
||||||
metrics.sh has been tested on Ubuntu 14.04 and Mac OS X but is supposed to run on most *NIX-like operating systems. Some of the provided metrics require [procfs](http://en.wikipedia.org/wiki/Procfs) to be available.
|
metrics.sh has been tested on Ubuntu 14.04 and Mac OS X but is supposed to run on most *NIX-like operating systems. Some of the provided metrics require [procfs](http://en.wikipedia.org/wiki/Procfs) to be available when running on *NIX. POSIX compliancy means that metrics.sh works with minimalistic command interpreters such as [dash](http://manpages.ubuntu.com/manpages/trusty/en/man1/dash.1.html). Built-in metrics do **not** require root privileges.
|
||||||
|
|
||||||
## Metrics
|
## Metrics
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
|
|
||||||
SCRIPT=<SCRIPT>
|
SCRIPT=<SCRIPT>
|
||||||
RUNAS=<USER>
|
RUNAS=<USER>
|
||||||
ARGS=<ARGS>
|
CONFIG_FILE=<CONFIG_FILE>
|
||||||
|
ARGS="-c $CONFIG_FILE"
|
||||||
NAME=metrics.sh
|
NAME=metrics.sh
|
||||||
|
|
||||||
PIDFILE=/var/run/$NAME.pid
|
PIDFILE=/var/run/$NAME.pid
|
||||||
|
@ -25,7 +26,7 @@ start() {
|
||||||
echo 'Service already running' >&2
|
echo 'Service already running' >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
echo 'Starting service…' >&2
|
echo 'Starting service...' >&2
|
||||||
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
|
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
|
||||||
su -c "$CMD $ARGS" $RUNAS > "$PIDFILE"
|
su -c "$CMD $ARGS" $RUNAS > "$PIDFILE"
|
||||||
echo 'Service started' >&2
|
echo 'Service started' >&2
|
||||||
|
@ -36,7 +37,7 @@ stop() {
|
||||||
echo 'Service not running' >&2
|
echo 'Service not running' >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
echo 'Stopping service…' >&2
|
echo 'Stopping service...' >&2
|
||||||
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
|
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
|
||||||
echo 'Service stopped' >&2
|
echo 'Service stopped' >&2
|
||||||
}
|
}
|
||||||
|
|
91
lib/main.sh
91
lib/main.sh
|
@ -5,19 +5,21 @@ for util in ./lib/utils/*.sh; do
|
||||||
. $util
|
. $util
|
||||||
done
|
done
|
||||||
|
|
||||||
# init
|
|
||||||
__AVAILABLE_METRICS=
|
|
||||||
__AVAILABLE_REPORTERS=
|
|
||||||
|
|
||||||
main_defaults () {
|
main_defaults () {
|
||||||
if [ -z $INTERVAL ]; then
|
if [ -z $INTERVAL ]; then
|
||||||
INTERVAL=2
|
INTERVAL=2
|
||||||
fi
|
fi
|
||||||
if [ -z $METRICS ]; then
|
if [ -z $DEFAULT_METRICS ]; then
|
||||||
METRICS=cpu,disk_io,disk_usage,heartbeat,memory,network_io,swap
|
DEFAULT_METRICS=cpu,memory,swap,network_io,disk_io,disk_usage
|
||||||
fi
|
fi
|
||||||
if [ -z $REPORTER ]; then
|
if [ -z $DEFAULT_REPORTER ]; then
|
||||||
REPORTER=stdout
|
DEFAULT_REPORTER=stdout
|
||||||
|
fi
|
||||||
|
if [ -z $CUSTOM_REPORTERS_PATH ]; then
|
||||||
|
CUSTOM_REPORTERS_PATH=./reporters/custom
|
||||||
|
fi
|
||||||
|
if [ -z $CUSTOM_METRICS_PATH ]; then
|
||||||
|
CUSTOM_METRICS_PATH=./metrics/custom
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,28 +27,23 @@ main_load () {
|
||||||
# set defaults
|
# set defaults
|
||||||
main_defaults
|
main_defaults
|
||||||
|
|
||||||
# load reporter
|
__AVAILABLE_REPORTERS=$(get_available_reporters)
|
||||||
for file in ./reporters/*.sh; do
|
__AVAILABLE_METRICS=$(get_available_metrics)
|
||||||
local filename=$(basename $file)
|
|
||||||
local reporter=${filename%.*}
|
|
||||||
|
|
||||||
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
|
||||||
done
|
|
||||||
|
|
||||||
# load available metrics
|
|
||||||
for file in ./metrics/*.sh; do
|
|
||||||
local filename=$(basename $file)
|
|
||||||
local metric=${filename%.*}
|
|
||||||
|
|
||||||
# register metric
|
|
||||||
__AVAILABLE_METRICS=$(trim "$__AVAILABLE_METRICS $metric")
|
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main_init () {
|
main_init () {
|
||||||
# handle args
|
local metrics="$1"
|
||||||
__METRICS=$(echo $1 | sed 's/,/ /g')
|
local reporter="$2"
|
||||||
__REPORTER=$2
|
|
||||||
|
if [ -z "$metrics" ]; then
|
||||||
|
metrics=$DEFAULT_METRICS
|
||||||
|
fi
|
||||||
|
if [ -z "$reporter" ]; then
|
||||||
|
reporter=$DEFAULT_REPORTER
|
||||||
|
fi
|
||||||
|
|
||||||
|
__METRICS=$(echo $metrics| sed 's/,/ /g')
|
||||||
|
__REPORTER=$reporter
|
||||||
|
|
||||||
# create temp dir
|
# create temp dir
|
||||||
TEMP_DIR=$(make_temp_dir)
|
TEMP_DIR=$(make_temp_dir)
|
||||||
|
@ -78,14 +75,17 @@ main_collect () {
|
||||||
# init reporter
|
# init reporter
|
||||||
local reporter_name=$(get_name_for_reporter $__REPORTER)
|
local reporter_name=$(get_name_for_reporter $__REPORTER)
|
||||||
local reporter_alias=$(get_alias $__REPORTER)
|
local reporter_alias=$(get_alias $__REPORTER)
|
||||||
load_reporter_with_prefix __r_${reporter_alias}_ ./reporters/${reporter_name}.sh
|
load_reporter_with_prefix __r_${reporter_alias}_ ${reporter_name}
|
||||||
|
|
||||||
if is_function __r_${reporter_alias}_defaults; then
|
if is_function __r_${reporter_alias}_defaults; then
|
||||||
__r_${reporter_alias}_defaults
|
__r_${reporter_alias}_defaults
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if is_function __r_${reporter_alias}_config; then
|
if is_function __r_${reporter_alias}_config; then
|
||||||
__r_${reporter_alias}_config
|
__r_${reporter_alias}_config
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
verbose "Starting reporter '${reporter_alias}'"
|
||||||
if is_function __r_${reporter_alias}_start; then
|
if is_function __r_${reporter_alias}_start; then
|
||||||
__r_${reporter_alias}_start
|
__r_${reporter_alias}_start
|
||||||
fi
|
fi
|
||||||
|
@ -98,7 +98,7 @@ main_collect () {
|
||||||
local metric_alias=$(get_alias $metric)
|
local metric_alias=$(get_alias $metric)
|
||||||
|
|
||||||
# init metric
|
# init metric
|
||||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
load_metric_with_prefix __m_${metric_alias}_ ${metric_name}
|
||||||
|
|
||||||
if is_function __m_${metric_alias}_defaults; then
|
if is_function __m_${metric_alias}_defaults; then
|
||||||
__m_${metric_alias}_defaults
|
__m_${metric_alias}_defaults
|
||||||
|
@ -108,11 +108,13 @@ main_collect () {
|
||||||
__m_${metric_alias}_config
|
__m_${metric_alias}_config
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
verbose "Starting metric '${metric_alias}'"
|
||||||
if is_function __m_${metric_alias}_start; then
|
if is_function __m_${metric_alias}_start; then
|
||||||
__m_${metric_alias}_start
|
__m_${metric_alias}_start
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! is_function __m_${metric_alias}_collect; then
|
if ! is_function __m_${metric_alias}_collect; then
|
||||||
|
verbose "No collect() function found for '${metric_alias}'"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -172,10 +174,19 @@ main_terminate () {
|
||||||
}
|
}
|
||||||
|
|
||||||
main_print_docs () {
|
main_print_docs () {
|
||||||
|
echo "# GLOBAL"
|
||||||
|
echo
|
||||||
|
echo "INTERVAL=$INTERVAL"
|
||||||
|
echo "DEFAULT_REPORTER=$DEFAULT_REPORTER"
|
||||||
|
echo "DEFAULT_METRICS=$DEFAULT_METRICS"
|
||||||
|
echo "CUSTOM_REPORTERS_PATH=$CUSTOM_REPORTERS_PATH"
|
||||||
|
echo "CUSTOM_METRICS_PATH=$CUSTOM_METRICS_PATH"
|
||||||
|
|
||||||
|
echo
|
||||||
echo "# METRICS"
|
echo "# METRICS"
|
||||||
|
|
||||||
for metric in $__AVAILABLE_METRICS; do
|
for metric in $__AVAILABLE_METRICS; do
|
||||||
load_metric_with_prefix __m_${metric}_ ./metrics/${metric}.sh
|
load_metric_with_prefix __m_${metric}_ ${metric}
|
||||||
|
|
||||||
if ! is_function __m_${metric}_docs; then
|
if ! is_function __m_${metric}_docs; then
|
||||||
continue
|
continue
|
||||||
|
@ -193,7 +204,7 @@ main_print_docs () {
|
||||||
echo
|
echo
|
||||||
echo "# REPORTERS"
|
echo "# REPORTERS"
|
||||||
for reporter in $__AVAILABLE_REPORTERS; do
|
for reporter in $__AVAILABLE_REPORTERS; do
|
||||||
load_reporter_with_prefix __r_${reporter}_ ./reporters/${reporter}.sh
|
load_reporter_with_prefix __r_${reporter}_ ${reporter}
|
||||||
|
|
||||||
if ! is_function __r_${reporter}_docs; then
|
if ! is_function __r_${reporter}_docs; then
|
||||||
continue
|
continue
|
||||||
|
@ -212,11 +223,13 @@ main_print_docs () {
|
||||||
main_print_config () {
|
main_print_config () {
|
||||||
echo "[metrics.sh]"
|
echo "[metrics.sh]"
|
||||||
echo ";INTERVAL=$INTERVAL"
|
echo ";INTERVAL=$INTERVAL"
|
||||||
echo ";METRICS=$METRICS"
|
echo ";DEFAULT_REPORTER=$DEFAULT_REPORTER"
|
||||||
echo ";REPORTER=$REPORTER"
|
echo ";DEFAULT_METRICS=$DEFAULT_METRICS"
|
||||||
|
echo ";CUSTOM_REPORTERS_PATH=$CUSTOM_REPORTERS_PATH"
|
||||||
|
echo ";CUSTOM_METRICS_PATH=$CUSTOM_METRICS_PATH"
|
||||||
|
|
||||||
for metric in $__AVAILABLE_METRICS; do
|
for metric in $__AVAILABLE_METRICS; do
|
||||||
load_metric_with_prefix __m_${metric}_ ./metrics/${metric}.sh
|
load_metric_with_prefix __m_${metric}_ ${metric}
|
||||||
|
|
||||||
if is_function __m_${metric}_defaults; then
|
if is_function __m_${metric}_defaults; then
|
||||||
__m_${metric}_defaults
|
__m_${metric}_defaults
|
||||||
|
@ -231,11 +244,7 @@ main_print_config () {
|
||||||
done
|
done
|
||||||
|
|
||||||
for reporter in $__AVAILABLE_REPORTERS; do
|
for reporter in $__AVAILABLE_REPORTERS; do
|
||||||
load_reporter_with_prefix __r_${reporter}_ ./reporters/${reporter}.sh
|
load_reporter_with_prefix __r_${reporter}_ ${reporter}
|
||||||
|
|
||||||
if ! is_function __r_${reporter}_docs; then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
if is_function __r_${reporter}_defaults; then
|
if is_function __r_${reporter}_defaults; then
|
||||||
__r_${reporter}_defaults
|
__r_${reporter}_defaults
|
||||||
|
@ -243,6 +252,8 @@ main_print_config () {
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo ";[reporter $reporter]"
|
echo ";[reporter $reporter]"
|
||||||
print_prefixed ";" "$(__r_${reporter}_docs)"
|
if is_function __r_${reporter}_docs; then
|
||||||
|
print_prefixed ";" "$(__r_${reporter}_docs)"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
|
@ -1,10 +1,43 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
get_available_reporters () {
|
||||||
|
local result
|
||||||
|
for file in `ls ./reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2>/dev/null`; do
|
||||||
|
local filename=$(basename $file)
|
||||||
|
local reporter=${filename%.*}
|
||||||
|
result=$(echo "$result $reporter")
|
||||||
|
done
|
||||||
|
echo $result
|
||||||
|
}
|
||||||
|
|
||||||
|
get_available_metrics () {
|
||||||
|
local result
|
||||||
|
for file in `ls ./metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2>/dev/null`; do
|
||||||
|
local filename=$(basename $file)
|
||||||
|
local metric=${filename%.*}
|
||||||
|
# register metric
|
||||||
|
result=$(trim "$result $metric")
|
||||||
|
done
|
||||||
|
echo $result
|
||||||
|
}
|
||||||
|
|
||||||
load_reporter_with_prefix () {
|
load_reporter_with_prefix () {
|
||||||
local prefix=$1
|
local prefix=$1
|
||||||
local file=$2
|
local name=$2
|
||||||
local content
|
|
||||||
|
|
||||||
|
local file
|
||||||
|
for dir in $CUSTOM_REPORTERS_PATH ./reporters; do
|
||||||
|
if [ -f $dir/$name.sh ]; then
|
||||||
|
file=$dir/$name.sh
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z $file ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local content
|
||||||
content=$(sed \
|
content=$(sed \
|
||||||
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||||
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||||
|
@ -17,10 +50,21 @@ load_reporter_with_prefix () {
|
||||||
|
|
||||||
load_metric_with_prefix () {
|
load_metric_with_prefix () {
|
||||||
local prefix=$1
|
local prefix=$1
|
||||||
local file=$2
|
local name=$2
|
||||||
local content
|
|
||||||
|
|
||||||
# dash will error if this variable is defined as `local`
|
local file
|
||||||
|
for dir in $CUSTOM_METRICS_PATH ./metrics; do
|
||||||
|
if [ -f $dir/$name.sh ]; then
|
||||||
|
file=$dir/$name.sh
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z $file ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local content
|
||||||
content=$(sed \
|
content=$(sed \
|
||||||
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
-e 's/^[[:space:]]*\(defaults[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||||
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
-e 's/^[[:space:]]*\(start[ ]*()[ ]*{\)/'"$prefix"'\1/' \
|
||||||
|
|
Loading…
Reference in New Issue