metrics.sh/metrics.sh

167 lines
3.1 KiB
Bash
Raw Normal View History

2015-02-19 14:15:38 -05:00
#!/bin/sh
2015-03-12 16:31:45 -04:00
# env
2015-03-14 08:32:26 -04:00
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
2019-03-11 04:27:09 -04:00
DIR=$(dirname "$0")
2015-03-08 12:51:15 -04:00
usage () {
2015-03-22 08:55:14 -04:00
echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u]"
}
help () {
echo
usage
echo
echo " Options: "
echo
2015-03-19 16:14:14 -04:00
echo " -c, --config <file> path to config file"
2015-03-17 15:22:26 -04:00
echo " -m, --metrics <metrics> comma-separated list of metrics to collect"
echo " -r, --reporter <reporter> use specified reporter (default: stdout)"
echo " -i, --interval <seconds> collect metrics every n seconds (default: 2)"
echo " -v, --verbose enable verbose mode"
echo " -C, --print-config print output to be used in a config file"
2015-03-22 08:55:14 -04:00
echo " -u, --update pull the latest version (requires git)"
echo " -d, --docs show documentation"
2015-03-17 15:22:26 -04:00
echo " -h, --help show this text"
echo
}
2015-03-15 16:14:44 -04:00
# handle opts
opt_config_file=
opt_metrics=
opt_reporter=
opt_interval=
2015-03-15 16:14:44 -04:00
opt_verbose=false
opt_print_config=false
2015-03-22 08:55:14 -04:00
opt_do_update=false
opt_docs=false
2015-03-15 16:14:44 -04:00
while [ $# -gt 0 ]; do
case $1 in
2015-03-19 16:14:14 -04:00
-c|--config)
shift
opt_config_file=$1
2015-03-19 16:14:14 -04:00
;;
-m|--metrics)
shift
opt_metrics=$1
;;
-r|--reporter)
shift
opt_reporter=$1
;;
-i|--interval)
shift
opt_interval=$1
;;
2015-03-22 08:55:14 -04:00
-v|--verbose)
opt_verbose=true
;;
-C|--print-config)
opt_print_config=true
;;
2015-03-22 08:55:14 -04:00
-u|--update)
opt_do_update=true
;;
-d|--docs)
opt_docs=true
;;
-h|--help)
help
exit
2015-03-14 09:59:17 -04:00
;;
*)
usage
exit 1
;;
esac
shift
done
# run
2019-03-11 04:27:09 -04:00
. ${DIR}/lib/main.sh
2015-03-22 08:55:14 -04:00
if [ $opt_do_update = true ]; then
2015-12-02 04:20:16 -05:00
if ! command_exists git; then
2015-12-02 04:11:10 -05:00
echo "Error: --update requires 'git' to be in the PATH"
2015-03-22 08:55:14 -04:00
exit 1
fi
echo "Fetching latest version..."
git pull https://github.com/pstadler/metrics.sh.git master
exit $?
fi
if [ $opt_verbose = true ]; then
verbose_on
verbose "Started in verbose mode"
verbose "OS detected: $OS_TYPE"
fi
main_load
verbose "Available metrics: $__AVAILABLE_METRICS"
verbose "Available reporters: $__AVAILABLE_REPORTERS"
2015-03-15 16:14:44 -04:00
if [ $opt_docs = true ]; then
main_print_docs
exit
fi
if [ $opt_print_config = true ]; then
main_print_config
exit
fi
if [ -n "$opt_config_file" ]; then
verbose "Loading configuration file: $opt_config_file"
2015-03-19 16:14:14 -04:00
2015-12-02 04:16:06 -05:00
parse_config "$opt_config_file"
2015-03-19 16:14:14 -04:00
if [ $? -ne 0 ]; then
exit 1
fi
configured_reporters=$(get_configured_reporters)
if [ -n "$configured_reporters" ]; then
REPORTER=$configured_reporters
fi
configured_metrics=$(get_configured_metrics)
if [ -n "$configured_metrics" ]; then
METRICS=$configured_metrics
fi
fi
# --reporter always wins
if [ -n "$opt_reporter" ]; then
REPORTER=$opt_reporter
fi
# --metrics always wins
if [ -n "$opt_metrics" ]; then
METRICS=$opt_metrics
fi
# --interval always wins
if [ -n "$opt_interval" ]; then
INTERVAL=$opt_interval
fi
main_init "$METRICS" "$REPORTER"
verbose "Using metrics: $__METRICS"
verbose "Using reporter: $__REPORTER"
2015-03-22 13:13:41 -04:00
echo "metrics.sh started with PID: $$"
verbose "Collecting metrics every $INTERVAL second(s)"
2015-12-02 04:11:10 -05:00
main_collect