Merge pull request #7 from grimeyCH/systemd-compatibility

Systemd compatibility
This commit is contained in:
Patrick Stadler 2019-03-11 12:34:55 +01:00 committed by GitHub
commit 4f28c79906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 12 deletions

View File

@ -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
@ -95,7 +96,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 +153,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 +216,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 +232,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"

View File

@ -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
}

View File

@ -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

View File

@ -4,6 +4,7 @@
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
DIR=$(dirname "$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

40
systemd/README.md Normal file
View File

@ -0,0 +1,40 @@
# 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
# 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 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
# 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
```

View File

@ -0,0 +1,11 @@
[Unit]
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
Restart=always
[Install]
WantedBy=multi-user.target