added nc-backend.sh script; #2352
Costa Tsaousis (ktsaou) committed
Jun 17, 2017 at 13:54 UTC
aebfee919c485c4d70b0d04518977c95e85f8a9b
2 files changed
+142
contrib/Makefile.am
+1
@@ -21,6 +21,7 @@ dist_noinst_DATA = \
21
22
dist_noinst_SCRIPTS = \
23
debian/netdata.init \
24
+ nc-backend.sh \
25
$(NULL)
26
27
debian/changelog:
contrib/nc-backend.sh
new
+141
@@ -0,0 +1,141 @@
1
+#!/usr/bin/env bash
2
+
3
+MODE="${1}"
4
+MY_PORT="${2}"
5
+BACKEND_HOST="${3}"
6
+BACKEND_PORT="${4}"
7
+FILE="${NETDATA_NC_BACKEND_DIR-/tmp}/netdata-nc-backend-${MY_PORT}"
8
+
9
+log() {
10
+ logger --stderr --id=$$ --tag "netdata-nc-backend" "${*}"
11
+}
12
+
13
+listen_save_replay_forever() {
14
+ local file="${1}" port="${2}" real_backend_host="${3}" real_backend_port="${4}" ret delay=1 started ended
15
+
16
+ while [ 1 ]
17
+ do
18
+ log "Starting nc to listen on port ${port} and save metrics to ${file}"
19
+
20
+ started=$(date +%s)
21
+ nc -l "${port}" >>"${file}"
22
+ ret=$?
23
+ ended=$(date +%s)
24
+
25
+ log "nc stopped with return code ${ret}."
26
+
27
+ if [ -s "${file}" ]
28
+ then
29
+ if [ ! -z "${real_backend_host}" -a ! -z "${real_backend_port}" ]
30
+ then
31
+ log "Attempting to send the metrics to the real backend at ${real_backend_host}:${real_backend_port}"
32
+
33
+ nc "${real_backend_host}" "${real_backend_port}" <"${file}"
34
+ ret=$?
35
+
36
+ if [ ${ret} -eq 0 ]
37
+ then
38
+ log "Successfuly sent the metrics to ${real_backend_host}:${real_backend_port}"
39
+ mv "${file}" "${file}.old"
40
+ touch "${file}"
41
+ else
42
+ log "Failed to send the metrics to ${real_backend_host}:${real_backend_port} (nc returned ${ret}) - appending more data to ${file}"
43
+ fi
44
+ else
45
+ log "No backend configured - appending more data to ${file}"
46
+ fi
47
+ fi
48
+
49
+ # prevent a CPU hungry infinite loop
50
+ # if nc cannot listen to port
51
+ if [ $((ended - started)) -lt 5 ]
52
+ then
53
+ log "nc has been stopped too fast. Waiting 30 secs."
54
+ delay=30
55
+ else
56
+ delay=1
57
+ fi
58
+
59
+ sleep ${delay}
60
+ done
61
+}
62
+
63
+if [ "${MODE}" = "start" ]
64
+ then
65
+
66
+ # start the listener, in exclusive mode
67
+ # only one can use the same file/port at a time
68
+ {
69
+ flock -n 9
70
+ if [ $? -ne 0 ]
71
+ then
72
+ log "Cannot get exclusive lock on file ${FILE}.lock - Am I running multiple times?"
73
+ exit 2
74
+ fi
75
+
76
+ # save our PID to the lock file
77
+ echo "$$" >"${FILE}.lock"
78
+
79
+ listen_save_replay_forever "${FILE}" ${MY_PORT} ${BACKEND_HOST} ${BACKEND_PORT}
80
+ ret=$?
81
+
82
+ log "listener exited."
83
+ exit ${ret}
84
+
85
+ } 9>>"${FILE}.lock"
86
+
87
+ # we can only get here if ${FILE}.lock cannot be created
88
+ log "Cannot create file ${FILE}."
89
+ exit 3
90
+
91
+elif [ "${MODE}" = "stop" ]
92
+ then
93
+
94
+ {
95
+ flock -n 9
96
+ if [ $? -ne 0 ]
97
+ then
98
+ pid=$(<${FILE}.lock)
99
+ log "Killing process ${pid}..."
100
+ kill -TERM -${pid}
101
+ exit 0
102
+ fi
103
+
104
+ log "File ${FILE}.lock has been locked by me but it shouldn't. Is a collector running?"
105
+ exit 4
106
+
107
+ } 9<"${FILE}.lock"
108
+
109
+ log "File ${FILE}.lock does not exist. Is a collector running?"
110
+ exit 5
111
+
112
+else
113
+
114
+ cat <<EOF
115
+Usage:
116
+
117
+ "${0}" start|stop PORT [BACKEND_HOST BACKEND_PORT]
118
+
119
+ PORT The port this script will listen
120
+ (configure netdata to use this as a second backend)
121
+
122
+ BACKEND_HOST The real backend host
123
+ BACKEND_PORT The real backend port
124
+
125
+ This script can act as fallback backend for netdata.
126
+ It will receive metrics from netdata, save them to
127
+ ${FILE}
128
+ and once netdata reconnects to the real-backend, this script
129
+ will push all metrics collected to the real-backend too and
130
+ wait for a failure to happen again.
131
+
132
+ Only one netdata can connect to this script at a time.
133
+ If you need fallback for multiple netdata, run this script
134
+ multiple times with different ports.
135
+
136
+ You can run me in the background with this:
137
+
138
+ screen -d -m "${0}" start PORT [BACKEND_HOST BACKEND_PORT]
139
+EOF
140
+ exit 1
141
+fi