From c4246b9ab072004ff946710f633af340616253db Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Sun, 22 Mar 2015 13:55:14 +0100 Subject: [PATCH] add --update option. minor fixes --- README.md | 8 +++++--- lib/utils/functions.sh | 6 +++++- lib/utils/helpers.sh | 2 +- metrics.sh | 32 ++++++++++++++++++++++++-------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9941cd5..fafe5b5 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ metrics.sh is a lightweight metrics collection and fowarding utility implemented ``` $ ./metrics.sh --help - - Usage: ./metrics.sh [-d] [-h] [-v] [-c] [-m] [-r] [-i] + + Usage: ./metrics.sh [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u] Options: @@ -16,6 +16,8 @@ $ ./metrics.sh --help -r, --reporter use specified reporter (default: stdout) -i, --interval collect metrics every n seconds (default: 2) -v, --verbose enable verbose mode + -C, --print-config print output to be used in a config file + -u, --update pull the latest version (requires git) -d, --docs show documentation -h, --help show this text ``` @@ -28,7 +30,7 @@ $ git clone git@github.com:pstadler/metrics.sh.git ### Requirements -metrics.sh has been tested on Ubuntu 14.04 and Mac OS X but is supposed to run on most Unix-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.sh has been tested on Ubuntu 14.04 and Mac OS X but is supposed to run on most Unix-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 diff --git a/lib/utils/functions.sh b/lib/utils/functions.sh index 23f30e7..11b7ee9 100644 --- a/lib/utils/functions.sh +++ b/lib/utils/functions.sh @@ -3,4 +3,8 @@ # this is bad, but `declare -f -F` is not portable is_function () { type $1 2> /dev/null | grep -q 'function$' -} \ No newline at end of file +} + +command_exists () { + type $1 > /dev/null 2>&1 +} diff --git a/lib/utils/helpers.sh b/lib/utils/helpers.sh index eb0431c..b94d2b5 100644 --- a/lib/utils/helpers.sh +++ b/lib/utils/helpers.sh @@ -25,4 +25,4 @@ unique_id () { print_prefixed () { printf "$2" | sed -e "s/^/$1/g" -} \ No newline at end of file +} diff --git a/metrics.sh b/metrics.sh index 0fc3006..ccfad71 100755 --- a/metrics.sh +++ b/metrics.sh @@ -6,7 +6,7 @@ LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 usage () { - echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C]" + echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u]" } help () { @@ -20,8 +20,9 @@ help () { echo " -r, --reporter use specified reporter (default: stdout)" echo " -i, --interval collect metrics every n seconds (default: 2)" echo " -v, --verbose enable verbose mode" - echo " -d, --docs show documentation" echo " -C, --print-config print output to be used in a config file" + echo " -u, --update pull the latest version (requires git)" + echo " -d, --docs show documentation" echo " -h, --help show this text" echo } @@ -31,9 +32,10 @@ opt_config_file= opt_metrics= opt_reporter= opt_interval= -opt_docs=false opt_verbose=false opt_print_config=false +opt_do_update=false +opt_docs=false while [ $# -gt 0 ]; do case $1 in @@ -57,18 +59,22 @@ while [ $# -gt 0 ]; do opt_interval=$1 ;; - -v|-verbose) + -v|--verbose) opt_verbose=true ;; - -d|--docs) - opt_docs=true - ;; - -C|--print-config) opt_print_config=true ;; + -u|--update) + opt_do_update=true + ;; + + -d|--docs) + opt_docs=true + ;; + -h|--help) help exit @@ -86,6 +92,16 @@ done # run . ./lib/main.sh +if [ $opt_do_update = true ]; then + if ! command_exists git; then + echo "Error: --update requires `git` to be in the PATH" + 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"