From 3a2fff387d4879a736e936d47526826854f6208d Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Wed, 20 Feb 2019 17:27:02 +0100 Subject: [PATCH 1/9] Systemd compatibility. --- lib/main.sh | 6 +++--- lib/utils/loader.sh | 8 ++++---- metrics.sh | 3 ++- systemd/metrics.sh.service | 8 ++++++++ 4 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 systemd/metrics.sh.service diff --git a/lib/main.sh b/lib/main.sh index 11987f9..d95e067 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -1,7 +1,7 @@ #!/bin/sh # load utils -for util in ./lib/utils/*.sh; do +for util in ${DIR}/lib/utils/*.sh; do . $util done @@ -16,10 +16,10 @@ main_defaults () { DEFAULT_REPORTER=stdout fi if [ -z $CUSTOM_REPORTERS_PATH ]; then - CUSTOM_REPORTERS_PATH=./reporters/custom + CUSTOM_REPORTERS_PATH=${DIR}/reporters/custom fi if [ -z $CUSTOM_METRICS_PATH ]; then - CUSTOM_METRICS_PATH=./metrics/custom + CUSTOM_METRICS_PATH=${DIR}/metrics/custom fi } diff --git a/lib/utils/loader.sh b/lib/utils/loader.sh index b54452d..9fe62f7 100644 --- a/lib/utils/loader.sh +++ b/lib/utils/loader.sh @@ -2,7 +2,7 @@ get_available_reporters () { local result - for file in `ls ./reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${DIR}/reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local reporter=${filename%.*} result=$(echo "$result $reporter") @@ -12,7 +12,7 @@ get_available_reporters () { get_available_metrics () { local result - for file in `ls ./metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${DIR}/metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local metric=${filename%.*} # register metric @@ -26,7 +26,7 @@ load_reporter_with_prefix () { local name=$2 local file - for dir in $CUSTOM_REPORTERS_PATH ./reporters; do + for dir in $CUSTOM_REPORTERS_PATH ${DIR}/reporters; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break @@ -53,7 +53,7 @@ load_metric_with_prefix () { local name=$2 local file - for dir in $CUSTOM_METRICS_PATH ./metrics; do + for dir in $CUSTOM_METRICS_PATH ${DIR}/metrics; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break diff --git a/metrics.sh b/metrics.sh index 9032ab0..6963318 100755 --- a/metrics.sh +++ b/metrics.sh @@ -4,6 +4,7 @@ LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 +DIR=$(dirname "$(readlink -f "$0")") usage () { echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u]" @@ -90,7 +91,7 @@ while [ $# -gt 0 ]; do done # run -. ./lib/main.sh +. ${DIR}/lib/main.sh if [ $opt_do_update = true ]; then if ! command_exists git; then diff --git a/systemd/metrics.sh.service b/systemd/metrics.sh.service new file mode 100644 index 0000000..06b76cc --- /dev/null +++ b/systemd/metrics.sh.service @@ -0,0 +1,8 @@ +[Unit] +Description=Server metrics collector + +[Service] +ExecStart=/opt/metrics.sh/metrics.sh -c /etc/metrics.sh/metrics.ini + +[Install] +WantedBy=multi-user.target \ No newline at end of file From 2ed7792a20d62038938ccdfbb3363714382f552f Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Thu, 21 Feb 2019 18:17:45 +0100 Subject: [PATCH 2/9] Readme and nicer Unit --- systemd/README.md | 41 ++++++++++++++++++++++++++++++++++++++ systemd/metrics.sh.service | 4 +++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 systemd/README.md diff --git a/systemd/README.md b/systemd/README.md new file mode 100644 index 0000000..ae589b9 --- /dev/null +++ b/systemd/README.md @@ -0,0 +1,41 @@ +# Running metrics.sh as a systemd service on Linux + +Run the following commands as root: + +```sh +# Install metrics.sh at /opt/metrics.sh +$ mkdir /opt; cd /opt +$ git clone https://github.com/pstadler/metrics.sh.git +$ cd metrics.sh +# Install the service +$ cp -p $PWD/systemd/metrics.sh.service /etc/systemd/system/metrics.sh.service +$ chmod 664 /etc/systemd/system/metrics.sh.service +# Create a config file +$ mkdir /etc/metrics.sh && chmod 600 /etc/metrics.sh +$ ./metrics.sh -C > /etc/metrics.sh/metrics.ini +# At this point you should edit your config file at +# /etc/metrics.sh/metrics.ini + +# Reload systemd daemon +$ systemctl daemon-reload + +# Start service +$ systemctl start metrics.sh.service + +# If run with the default configuration where reporter is 'stdout', metrics +# will be written to /var/log/metrics.sh.log. Be aware that this file will +# grow fast. +$ tail -f /var/log/metrics.sh.log + +# Stop service +$ systemctl stop metrics.sh.service + +# Check service status +$ systemctl status metrics.sh.service + +# Automatically start service when booting and stop when shutting down +$ systemctl enable metrics.sh.service + +# Disable automatic starting/stopping +$ systemctl disable metrics.sh.service +``` diff --git a/systemd/metrics.sh.service b/systemd/metrics.sh.service index 06b76cc..2d529d7 100644 --- a/systemd/metrics.sh.service +++ b/systemd/metrics.sh.service @@ -1,5 +1,7 @@ [Unit] -Description=Server metrics collector +Description=Controls the metrics daemon "metrics.sh" +Documentation=https://github.com/pstadler/metrics.sh +After=network.target [Service] ExecStart=/opt/metrics.sh/metrics.sh -c /etc/metrics.sh/metrics.ini From 4f5ed15b98e6d284889b1759737b1d36f6a4c835 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 25 Feb 2019 13:22:53 +0100 Subject: [PATCH 3/9] Restart on exit/failure. --- systemd/metrics.sh.service | 1 + 1 file changed, 1 insertion(+) diff --git a/systemd/metrics.sh.service b/systemd/metrics.sh.service index 2d529d7..c737549 100644 --- a/systemd/metrics.sh.service +++ b/systemd/metrics.sh.service @@ -5,6 +5,7 @@ After=network.target [Service] ExecStart=/opt/metrics.sh/metrics.sh -c /etc/metrics.sh/metrics.ini +Restart=always [Install] WantedBy=multi-user.target \ No newline at end of file From 9023954f3fd17b193ae76952db02367558c70558 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 25 Feb 2019 13:23:14 +0100 Subject: [PATCH 4/9] Readme update for systemd --- systemd/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/systemd/README.md b/systemd/README.md index ae589b9..7f64997 100644 --- a/systemd/README.md +++ b/systemd/README.md @@ -23,9 +23,9 @@ $ systemctl daemon-reload $ systemctl start metrics.sh.service # If run with the default configuration where reporter is 'stdout', metrics -# will be written to /var/log/metrics.sh.log. Be aware that this file will -# grow fast. -$ tail -f /var/log/metrics.sh.log +# will be written to the journal. See the log using `journalctl -u metrics.sh` +# or follow it with: +$ journalctl -f -u metrics.sh # Stop service $ systemctl stop metrics.sh.service From 0533eb0ac882a93388115098ef5e3c3a159d44c1 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 25 Feb 2019 13:23:27 +0100 Subject: [PATCH 5/9] Typos --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index be8f0be..4460e3a 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ $ DISK_USAGE_MOUNTPOINT=/dev/vdb ./metrics.sh -m disk_usage ### Configuration files -Maintaing all these options can become a cumbersome job, but metrics.sh provides functionality for creating and reading configuration files. +Maintaining all these options can become a cumbersome job, but metrics.sh provides functionality for creating and reading configuration files. ```sh $ ./metrics.sh -C > metrics.ini # write configuration to metrics.ini @@ -152,7 +152,7 @@ defaults () {} # setting default variables start () {} # called at the beginning collect () {} # collect the actual metric stop () {} # called before exiting -docs () {} # used for priting docs and creating output for configuration +docs () {} # used for printing docs and creating output for configuration ``` Metrics run within an isolated scope. It's generally safe to create variables and helper functions within metrics. @@ -215,7 +215,7 @@ defaults () {} # setting default variables start () {} # called at the beginning report () {} # report the actual metric stop () {} # called before exiting -docs () {} # used for priting docs and creating output for configuration +docs () {} # used for printing docs and creating output for configuration ``` Below is an example script for sending metrics as JSON data to an API endpoint. Assuming this script is located at `./reporters/custom/json_api.sh`, it can be invoked by calling `./metrics.sh -r json_api`. @@ -231,7 +231,7 @@ defaults () { } # Prepare the reporter. Create helper functions to be used during collection -# if needed. Returning 1 will result in an error and exection will be stopped. +# if needed. Returning 1 will result in an error and execution will be stopped. start () { if [ -z $JSON_API_ENDPOINT ]; then echo "Error: json_api requires \$JSON_API_ENDPOINT to be specified" From 8c5131c6eff16ee73cbed347676418ddbf55b087 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 25 Feb 2019 13:54:55 +0100 Subject: [PATCH 6/9] chmod not necessary --- systemd/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/systemd/README.md b/systemd/README.md index 7f64997..ceee3a8 100644 --- a/systemd/README.md +++ b/systemd/README.md @@ -9,7 +9,6 @@ $ git clone https://github.com/pstadler/metrics.sh.git $ cd metrics.sh # Install the service $ cp -p $PWD/systemd/metrics.sh.service /etc/systemd/system/metrics.sh.service -$ chmod 664 /etc/systemd/system/metrics.sh.service # Create a config file $ mkdir /etc/metrics.sh && chmod 600 /etc/metrics.sh $ ./metrics.sh -C > /etc/metrics.sh/metrics.ini From bc23216756a89d6adde3ffa560b7dbb0f1b650e8 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 25 Feb 2019 14:13:19 +0100 Subject: [PATCH 7/9] Link to systemd instruction --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4460e3a..b04ccfb 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ $ git clone https://github.com/pstadler/metrics.sh.git ``` See this [guide](init.d/README.md) how to run metrics.sh as a service on Linux. +Or [here](systemd/README.md) for instructions to set metrics.sh up for systemd. ### Requirements From 2cca88ed8d5a92407b2396a197b377feb8f84aa8 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 11 Mar 2019 09:00:45 +0100 Subject: [PATCH 8/9] Update from review. --- lib/main.sh | 6 +++--- lib/utils/loader.sh | 8 ++++---- metrics.sh | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/main.sh b/lib/main.sh index d95e067..3fabacb 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -1,7 +1,7 @@ #!/bin/sh # load utils -for util in ${DIR}/lib/utils/*.sh; do +for util in ${PWD}/lib/utils/*.sh; do . $util done @@ -16,10 +16,10 @@ main_defaults () { DEFAULT_REPORTER=stdout fi if [ -z $CUSTOM_REPORTERS_PATH ]; then - CUSTOM_REPORTERS_PATH=${DIR}/reporters/custom + CUSTOM_REPORTERS_PATH=${PWD}/reporters/custom fi if [ -z $CUSTOM_METRICS_PATH ]; then - CUSTOM_METRICS_PATH=${DIR}/metrics/custom + CUSTOM_METRICS_PATH=${PWD}/metrics/custom fi } diff --git a/lib/utils/loader.sh b/lib/utils/loader.sh index 9fe62f7..2c021c3 100644 --- a/lib/utils/loader.sh +++ b/lib/utils/loader.sh @@ -2,7 +2,7 @@ get_available_reporters () { local result - for file in `ls ${DIR}/reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${PWD}/reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local reporter=${filename%.*} result=$(echo "$result $reporter") @@ -12,7 +12,7 @@ get_available_reporters () { get_available_metrics () { local result - for file in `ls ${DIR}/metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${PWD}/metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local metric=${filename%.*} # register metric @@ -26,7 +26,7 @@ load_reporter_with_prefix () { local name=$2 local file - for dir in $CUSTOM_REPORTERS_PATH ${DIR}/reporters; do + for dir in $CUSTOM_REPORTERS_PATH ${PWD}/reporters; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break @@ -53,7 +53,7 @@ load_metric_with_prefix () { local name=$2 local file - for dir in $CUSTOM_METRICS_PATH ${DIR}/metrics; do + for dir in $CUSTOM_METRICS_PATH ${PWD}/metrics; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break diff --git a/metrics.sh b/metrics.sh index 6963318..ff8dc40 100755 --- a/metrics.sh +++ b/metrics.sh @@ -4,7 +4,7 @@ LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 -DIR=$(dirname "$(readlink -f "$0")") +PWD=$(dirname "$0") usage () { echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u]" @@ -91,7 +91,7 @@ while [ $# -gt 0 ]; do done # run -. ${DIR}/lib/main.sh +. ${PWD}/lib/main.sh if [ $opt_do_update = true ]; then if ! command_exists git; then From 3fccd9615dcff9cae94fe91144d0dcef805b5514 Mon Sep 17 00:00:00 2001 From: Andreas Grimm Date: Mon, 11 Mar 2019 09:27:09 +0100 Subject: [PATCH 9/9] Review part 2. --- lib/main.sh | 6 +++--- lib/utils/loader.sh | 8 ++++---- metrics.sh | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/main.sh b/lib/main.sh index 3fabacb..d95e067 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -1,7 +1,7 @@ #!/bin/sh # load utils -for util in ${PWD}/lib/utils/*.sh; do +for util in ${DIR}/lib/utils/*.sh; do . $util done @@ -16,10 +16,10 @@ main_defaults () { DEFAULT_REPORTER=stdout fi if [ -z $CUSTOM_REPORTERS_PATH ]; then - CUSTOM_REPORTERS_PATH=${PWD}/reporters/custom + CUSTOM_REPORTERS_PATH=${DIR}/reporters/custom fi if [ -z $CUSTOM_METRICS_PATH ]; then - CUSTOM_METRICS_PATH=${PWD}/metrics/custom + CUSTOM_METRICS_PATH=${DIR}/metrics/custom fi } diff --git a/lib/utils/loader.sh b/lib/utils/loader.sh index 2c021c3..9fe62f7 100644 --- a/lib/utils/loader.sh +++ b/lib/utils/loader.sh @@ -2,7 +2,7 @@ get_available_reporters () { local result - for file in `ls ${PWD}/reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${DIR}/reporters/*.sh $CUSTOM_REPORTERS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local reporter=${filename%.*} result=$(echo "$result $reporter") @@ -12,7 +12,7 @@ get_available_reporters () { get_available_metrics () { local result - for file in `ls ${PWD}/metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do + for file in `ls ${DIR}/metrics/*.sh $CUSTOM_METRICS_PATH/*.sh 2> /dev/null`; do local filename=$(basename $file) local metric=${filename%.*} # register metric @@ -26,7 +26,7 @@ load_reporter_with_prefix () { local name=$2 local file - for dir in $CUSTOM_REPORTERS_PATH ${PWD}/reporters; do + for dir in $CUSTOM_REPORTERS_PATH ${DIR}/reporters; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break @@ -53,7 +53,7 @@ load_metric_with_prefix () { local name=$2 local file - for dir in $CUSTOM_METRICS_PATH ${PWD}/metrics; do + for dir in $CUSTOM_METRICS_PATH ${DIR}/metrics; do if [ -f $dir/$name.sh ]; then file=$dir/$name.sh break diff --git a/metrics.sh b/metrics.sh index ff8dc40..257ef34 100755 --- a/metrics.sh +++ b/metrics.sh @@ -4,7 +4,7 @@ LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 -PWD=$(dirname "$0") +DIR=$(dirname "$0") usage () { echo " Usage: $0 [-d] [-h] [-v] [-c] [-m] [-r] [-i] [-C] [-u]" @@ -91,7 +91,7 @@ while [ $# -gt 0 ]; do done # run -. ${PWD}/lib/main.sh +. ${DIR}/lib/main.sh if [ $opt_do_update = true ]; then if ! command_exists git; then