]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/fping.plugin
uniform logging from all scripts
[netdata.git] / plugins.d / fping.plugin
index fa3b62f577d896bec214ec9a876d5adbcd96483b..32e1b711444f34584e9a3380ad6f8b4862cbe244 100755 (executable)
@@ -1,17 +1,10 @@
 #!/usr/bin/env bash
 
-me="${0}"
-
-# the frequency to send info to netdata
-# passed by netdata as the first parameter
-update_every="${1-1}"
-
-# the netdata configuration directory
-# passed by netdata as an environment variable
-NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
-
-# -----------------------------------------------------------------------------
-
+# netdata
+# real-time performance and health monitoring, done right!
+# (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
+# GPL v3+
+#
 # This plugin requires a special version of fping.
 # Get it from https://github.com/ktsaou/fping
 # and build it, like this:
@@ -29,6 +22,58 @@ NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
 # Then, create /etc/netdata/fping.conf
 # and set the configuration options given below
 
+export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
+export LC_ALL=C
+
+# -----------------------------------------------------------------------------
+
+PROGRAM_NAME="$(basename "${0}")"
+
+logdate() {
+    date "+%Y-%m-%d %H:%M:%S"
+}
+
+log() {
+    local status="${1}"
+    shift
+
+    echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
+
+}
+
+warning() {
+    log WARNING "${@}"
+}
+
+error() {
+    log ERROR "${@}"
+}
+
+info() {
+    log INFO "${@}"
+}
+
+fatal() {
+    log FATAL "${@}"
+       echo "DISABLE"
+    exit 1
+}
+
+debug=0
+debug() {
+    [ $debug -eq 1 ] && log DEBUG "${@}"
+}
+
+# -----------------------------------------------------------------------------
+
+# the frequency to send info to netdata
+# passed by netdata as the first parameter
+update_every="${1-1}"
+
+# the netdata configuration directory
+# passed by netdata as an environment variable
+NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
+
 # -----------------------------------------------------------------------------
 # configuration options
 # can be overwritten at /etc/netdata/fping.conf
@@ -48,29 +93,23 @@ ping_every="$((update_every * 1000 / 2))"
 retries=1
 
 # -----------------------------------------------------------------------------
-
 # load the configuration file
+
 if [ ! -f "${NETDATA_CONFIG_DIR}/fping.conf" ]
 then
-       echo >&2 "${me}: configuration file '${NETDATA_CONFIG_DIR}/fping.conf' not found - nothing to do."
-       echo "DISABLE"
-       exit 1
+       fatal "configuration file '${NETDATA_CONFIG_DIR}/fping.conf' not found - nothing to do."
 fi
 
 source "${NETDATA_CONFIG_DIR}/fping.conf"
 
 if [ -z "${hosts}" ]
 then
-       echo >&2 "${me}: no hosts configued in '${NETDATA_CONFIG_DIR}/fping.conf' - nothing to do."
-       echo "DISABLE"
-       exit 1
+       fatal "no hosts configued in '${NETDATA_CONFIG_DIR}/fping.conf' - nothing to do."
 fi
 
 if [ -z "${fping}" -o ! -x "${fping}" ]
 then
-       echo >&2 "${me}: command '${fping}' is not executable - cannot proceed."
-       echo "DISABLE"
-       exit 1
+       fatal "command '${fping}' is not executable - cannot proceed."
 fi
 
 # the fping options we will use
@@ -80,6 +119,4 @@ options=( -N -l -R -Q ${update_every} -p ${ping_every} -r ${retries} ${hosts} )
 exec "${fping}" "${options[@]}"
 
 # if we cannot execute fping, stop
-echo >&2 "${me}: command '${fping} ${options[@]}' failed to be executed."
-echo "DISABLE"
-exit 1
+fatal "command '${fping} ${options[@]}' failed to be executed."