summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-06-17 13:54:28 +0300
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-06-17 13:54:28 +0300
commitaebfee919c485c4d70b0d04518977c95e85f8a9b (patch)
treed3a4220efeaadfc272d82a1c355824db8ba57e6d /contrib
parent8a45529c3c0811a9e1bb8ade09d9c66bac1cc714 (diff)
added nc-backend.sh script; #2352
Diffstat (limited to 'contrib')
-rw-r--r--contrib/Makefile.am1
-rwxr-xr-xcontrib/nc-backend.sh141
2 files changed, 142 insertions, 0 deletions
diff --git a/contrib/Makefile.am b/contrib/Makefile.am
index b58b40e265..15e9c00089 100644
--- a/contrib/Makefile.am
+++ b/contrib/Makefile.am
@@ -21,6 +21,7 @@ dist_noinst_DATA = \
dist_noinst_SCRIPTS = \
debian/netdata.init \
+ nc-backend.sh \
$(NULL)
debian/changelog:
diff --git a/contrib/nc-backend.sh b/contrib/nc-backend.sh
new file mode 100755
index 0000000000..2ad427fbb1
--- /dev/null
+++ b/contrib/nc-backend.sh
@@ -0,0 +1,141 @@
+#!/usr/bin/env bash
+
+MODE="${1}"
+MY_PORT="${2}"
+BACKEND_HOST="${3}"
+BACKEND_PORT="${4}"
+FILE="${NETDATA_NC_BACKEND_DIR-/tmp}/netdata-nc-backend-${MY_PORT}"
+
+log() {
+ logger --stderr --id=$$ --tag "netdata-nc-backend" "${*}"
+}
+
+listen_save_replay_forever() {
+ local file="${1}" port="${2}" real_backend_host="${3}" real_backend_port="${4}" ret delay=1 started ended
+
+ while [ 1 ]
+ do
+ log "Starting nc to listen on port ${port} and save metrics to ${file}"
+
+ started=$(date +%s)
+ nc -l "${port}" >>"${file}"
+ ret=$?
+ ended=$(date +%s)
+
+ log "nc stopped with return code ${ret}."
+
+ if [ -s "${file}" ]
+ then
+ if [ ! -z "${real_backend_host}" -a ! -z "${real_backend_port}" ]
+ then
+ log "Attempting to send the metrics to the real backend at ${real_backend_host}:${real_backend_port}"
+
+ nc "${real_backend_host}" "${real_backend_port}" <"${file}"
+ ret=$?
+
+ if [ ${ret} -eq 0 ]
+ then
+ log "Successfuly sent the metrics to ${real_backend_host}:${real_backend_port}"
+ mv "${file}" "${file}.old"
+ touch "${file}"
+ else
+ log "Failed to send the metrics to ${real_backend_host}:${real_backend_port} (nc returned ${ret}) - appending more data to ${file}"
+ fi
+ else
+ log "No backend configured - appending more data to ${file}"
+ fi
+ fi
+
+ # prevent a CPU hungry infinite loop
+ # if nc cannot listen to port
+ if [ $((ended - started)) -lt 5 ]
+ then
+ log "nc has been stopped too fast. Waiting 30 secs."
+ delay=30
+ else
+ delay=1
+ fi
+
+ sleep ${delay}
+ done
+}
+
+if [ "${MODE}" = "start" ]
+ then
+
+ # start the listener, in exclusive mode
+ # only one can use the same file/port at a time
+ {
+ flock -n 9
+ if [ $? -ne 0 ]
+ then
+ log "Cannot get exclusive lock on file ${FILE}.lock - Am I running multiple times?"
+ exit 2
+ fi
+
+ # save our PID to the lock file
+ echo "$$" >"${FILE}.lock"
+
+ listen_save_replay_forever "${FILE}" ${MY_PORT} ${BACKEND_HOST} ${BACKEND_PORT}
+ ret=$?
+
+ log "listener exited."
+ exit ${ret}
+
+ } 9>>"${FILE}.lock"
+
+ # we can only get here if ${FILE}.lock cannot be created
+ log "Cannot create file ${FILE}."
+ exit 3
+
+elif [ "${MODE}" = "stop" ]
+ then
+
+ {
+ flock -n 9
+ if [ $? -ne 0 ]
+ then
+ pid=$(<${FILE}.lock)
+ log "Killing process ${pid}..."
+ kill -TERM -${pid}
+ exit 0
+ fi
+
+ log "File ${FILE}.lock has been locked by me but it shouldn't. Is a collector running?"
+ exit 4
+
+ } 9<"${FILE}.lock"
+
+ log "File ${FILE}.lock does not exist. Is a collector running?"
+ exit 5
+
+else
+
+ cat <<EOF
+Usage:
+
+ "${0}" start|stop PORT [BACKEND_HOST BACKEND_PORT]
+
+ PORT The port this script will listen
+ (configure netdata to use this as a second backend)
+
+ BACKEND_HOST The real backend host
+ BACKEND_PORT The real backend port
+
+ This script can act as fallback backend for netdata.
+ It will receive metrics from netdata, save them to
+ ${FILE}
+ and once netdata reconnects to the real-backend, this script
+ will push all metrics collected to the real-backend too and
+ wait for a failure to happen again.
+
+ Only one netdata can connect to this script at a time.
+ If you need fallback for multiple netdata, run this script
+ multiple times with different ports.
+
+ You can run me in the background with this:
+
+ screen -d -m "${0}" start PORT [BACKEND_HOST BACKEND_PORT]
+EOF
+ exit 1
+fi