9
# Exit code: 2 - Problems with claiming working directory
10
# Exit code: 3 - Missing dependencies
11
# Exit code: 4 - Failure to connect to endpoint
12
-# Exit code: 5 - Unknown HTTP error message
13
-# Exit code: 6 - The CLI didn't work
14
-# Exit code: 7 - Wrong user
12
+# Exit code: 5 - The CLI didn't work
13
+# Exit code: 6 - Wrong user
14
+# Exit code: 7 - Unknown HTTP error message
15
#
16
# OK: Agent claimed successfully
17
# HTTP Status code: 204
18
# Exit code: 0
19
#
20
+# Unknown HTTP error message
21
+# HTTP Status code: 422
22
+# Exit code: 7
23
+ERROR_KEYS[7]="None"
24
+ERROR_MESSAGES[7]="Unknown HTTP error message"
25
+
26
# Error: The agent id is invalid; it does not fulfill the constraints
27
# HTTP Status code: 422
22
-# Error key: "ErrInvalidNodeID"
23
-# Error message: "invalid node id"
24
-# Exit code: 6
28
+# Exit code: 8
29
+ERROR_KEYS[8]="ErrInvalidNodeID"
30
+ERROR_MESSAGES[8]="invalid node id"
31
32
# Error: The agent hostname is invalid; it does not fulfill the constraints
33
# HTTP Status code: 422
28
-# Error key: "ErrInvalidNodeName"
29
-# Error message: "invalid node name"
30
-# Exit code: 7
31
-#
34
+# Exit code: 9
35
+ERROR_KEYS[9]="ErrInvalidNodeName"
36
+ERROR_MESSAGES[9]="invalid node name"
37
+
38
# Error: At least one of the given rooms ids is invalid; it does not fulfill the constraints
39
# HTTP Status code: 422
34
-# Error key: "ErrInvalidRoomID"
35
-# Error message: "invalid room id"
36
-# Exit code: 8
37
-#
40
+# Exit code: 10
41
+ERROR_KEYS[10]="ErrInvalidRoomID"
42
+ERROR_MESSAGES[10]="invalid room id"
43
+
44
# Error: Invalid public key; the public key is empty or not present
45
# HTTP Status code: 422
40
-# Error key: "ErrInvalidPublicKey"
41
-# Error message: "invalid public key"
42
-# Exit code: 9
46
+# Exit code: 11
47
+ERROR_KEYS[11]="ErrInvalidPublicKey"
48
+ERROR_MESSAGES[11]="invalid public key"
49
#
50
# Error: Expired, missing or invalid token
51
# HTTP Status code: 403
46
-# Error key: "ErrForbidden"
47
-# Error message: "token expired" | "token not found" | "invalid token"
48
-# Exit code: 10
49
-#
52
+# Exit code: 12
53
+ERROR_KEYS[12]="ErrForbidden"
54
+ERROR_MESSAGES[12]="token expired/token not found/invalid token"
55
+
56
# Error: Duplicate agent id; an agent with the same id is already registered in the cloud
57
# HTTP Status code: 409
52
-# Error key: "ErrAlreadyClaimed"
53
-# Error message: "already claimed"
54
-# Exit code: 11
55
-#
58
+# Exit code: 13
59
+ERROR_KEYS[13]="ErrAlreadyClaimed"
60
+ERROR_MESSAGES[13]="already claimed"
61
+
62
# Error: The node claiming process is still in progress.
63
# HTTP Status code: 102
58
-# Error key: "ErrProcessingClaim"
59
-# Error message: "processing claiming"
60
-# Exit code: 12
61
-#
64
+# Exit code: 14
65
+ERROR_KEYS[14]="ErrProcessingClaim"
66
+ERROR_MESSAGES[14]="processing claiming"
67
+
68
# Error: Internal server error. Any other unexpected error (DB problems, etc.)
69
# HTTP Status code: 500
64
-# Error key: "ErrInternalServerError"
65
-# Error message: "Internal Server Error"
66
-# Exit code: 13
67
-#
70
+# Exit code: 15
71
+ERROR_KEYS[15]="ErrInternalServerError"
72
+ERROR_MESSAGES[15]="Internal Server Error"
73
+
74
# Error: There was a timout processing the claim.
75
# HTTP Status code: 504
70
-# Error key: "ErrGatewayTimeout"
71
-# Error message: "Gateway Timeout"
72
-# Exit code: 14
73
-#
76
+# Exit code: 16
77
+ERROR_KEYS[16]="ErrGatewayTimeout"
78
+ERROR_MESSAGES[16]="Gateway Timeout"
79
+
80
# Error: The service cannot handle the claiming request at this time.
81
# HTTP Status code: 503
76
-# Error key: "ErrServiceUnavailable"
77
-# Error message: "Service Unavailable"
78
-# Exit code: 15
82
+# Exit code: 17
83
+ERROR_KEYS[17]="ErrServiceUnavailable"
84
+ERROR_MESSAGES[17]="Service Unavailable"
85
86
+get_config_value() {
87
+ conf_file="${1}"
88
+ section="${2}"
89
+ key_name="${3}"
90
+ config_result=$(@sbindir_POST@/netdatacli 2>/dev/null read-config "$conf_file|$section|$key_name"; exit $?)
91
+ # shellcheck disable=SC2181
92
+ if [ "$?" != "0" ]; then
93
+ echo >&2 "cli failed, assume netdata is not running and query the on-disk config"
94
+ config_result=$(@sbindir_POST@/netdata 2>/dev/null -W get2 "$conf_file" "$section" "$key_name" unknown_default)
95
+ fi
96
+ echo "$config_result"
97
+}
98
if command -v curl >/dev/null 2>&1 ; then
99
URLTOOL="curl"
100
elif command -v wget >/dev/null 2>&1 ; then
108
exit 3
109
fi
110
111
+# shellcheck disable=SC2050
112
+if [ "@enable_cloud_POST@" = "no" ]; then
113
+ echo >&2 "This agent was built with --disable-cloud and cannot be claimed"
114
+ exit 3
115
+fi
116
+# shellcheck disable=SC2050
117
+if [ "@can_enable_aclk_POST@" != "yes" ]; then
118
+ echo >&2 "This agent was built without the dependencies for Cloud and cannot be claimed"
119
+ exit 3
120
+fi
121
122
# -----------------------------------------------------------------------------
123
# defaults to allow running this script by hand
124
97
-[ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
125
+[ -z "${NETDATA_VARLIB_DIR}" ] && NETDATA_VARLIB_DIR="@varlibdir_POST@"
126
MACHINE_GUID_FILE="@registrydir_POST@/netdata.public.unique.id"
99
-CLAIMING_DIR="${NETDATA_USER_CONFIG_DIR}/claim.d"
127
+CLAIMING_DIR="${NETDATA_VARLIB_DIR}/cloud.d"
128
TOKEN="unknown"
101
-URL_BASE="https://netdata.cloud"
129
+URL_BASE=$(get_config_value cloud global "cloud base url")
130
+[ -z "$URL_BASE" ] && URL_BASE="https://app.netdata.cloud" # Cover post-install with --dont-start
131
ID="unknown"
132
ROOMS=""
133
[ -z "$HOSTNAME" ] && HOSTNAME=$(hostname)
135
VERBOSE=0
136
INSECURE=0
137
RELOAD=1
109
-NETDATA_USER=netdata
138
+NETDATA_USER=$(get_config_value netdata global "run as user")
139
[ -z "$EUID" ] && EUID="$(id -u)"
140
112
-CONF_USER=$(grep '^[ #]*run as user[ ]*=' "${NETDATA_USER_CONFIG_DIR}/netdata.conf" 2>/dev/null)
113
-if [ -n "$CONF_USER" ]; then
114
- NETDATA_USER=$(echo "$CONF_USER" | sed 's/^[^=]*=[ \t]*//' | sed 's/[ \t]*$//')
115
-fi
116
-
141
142
# get the MACHINE_GUID by default
143
if [ -r "${MACHINE_GUID_FILE}" ]; then
176
177
if [ "$EUID" != "0" ] && [ "$(whoami)" != "$NETDATA_USER" ]; then
178
echo >&2 "This script must be run by the $NETDATA_USER user account"
155
- exit 7
179
+ exit 6
180
fi
181
182
# if curl not installed give warning SOCKS can't be used
303
cat "${CLAIMING_DIR}/tmpout.txt"
304
fi
305
282
-HTTP_STATUS_CODE=$(grep "HTTP" "${CLAIMING_DIR}/tmpout.txt" | awk -F " " '{print $2}')
306
+ERROR_KEY=$(grep "\"errorMsgKey\":" "${CLAIMING_DIR}/tmpout.txt" | awk -F "errorMsgKey\":\"" '{print $2}' | awk -F "\"" '{print $1}')
307
+case ${ERROR_KEY} in
308
+ "ErrInvalidNodeID") EXIT_CODE=8 ;;
309
+ "ErrInvalidNodeName") EXIT_CODE=9 ;;
310
+ "ErrInvalidRoomID") EXIT_CODE=10 ;;
311
+ "ErrInvalidPublicKey") EXIT_CODE=11 ;;
312
+ "ErrForbidden") EXIT_CODE=12 ;;
313
+ "ErrAlreadyClaimed") EXIT_CODE=13 ;;
314
+ "ErrProcessingClaim") EXIT_CODE=14 ;;
315
+ "ErrInternalServerError") EXIT_CODE=15 ;;
316
+ "ErrGatewayTimeout") EXIT_CODE=16 ;;
317
+ "ErrServiceUnavailable") EXIT_CODE=17 ;;
318
+ *) EXIT_CODE=7 ;;
319
+esac
320
321
+HTTP_STATUS_CODE=$(grep "HTTP" "${CLAIMING_DIR}/tmpout.txt" | awk -F " " '{print $2}')
322
if [ "${HTTP_STATUS_CODE}" = "204" ] ; then
323
+ EXIT_CODE=0
324
+fi
325
+
326
+if [ "${HTTP_STATUS_CODE}" = "204" ] || [ "${ERROR_KEY}" = "ErrAlreadyClaimed" ] ; then
327
rm -f "${CLAIMING_DIR}/tmpout.txt"
328
echo -n "${ID}" >"${CLAIMING_DIR}/claimed_id" || (echo >&2 "Claiming failed"; set -e; exit 2)
329
rm -f "${CLAIMING_DIR}/token" || (echo >&2 "Claiming failed"; set -e; exit 2)
330
+
331
+ # Rewrite the cloud.conf on the disk
332
+ cat > "$CLAIMING_DIR/cloud.conf" <<HERE_DOC
333
+[global]
334
+ enabled = yes
335
+ cloud base url = $URL_BASE
336
+HERE_DOC
337
if [ "$EUID" == "0" ]; then
338
chown -R "${NETDATA_USER}:${NETDATA_USER}" ${CLAIMING_DIR} || (echo >&2 "Claiming failed"; set -e; exit 2)
339
fi
340
if [ "${RELOAD}" == "0" ] ; then
292
- exit 0
341
+ exit $EXIT_CODE
342
+ fi
343
+
344
+ if [ -z "${PROXY}" ]; then
345
+ PROXYMSG=""
346
+ else
347
+ PROXYMSG="You have attempted to claim this node through a proxy - please update your the proxy setting in your netdata.conf to ${PROXY}. "
348
fi
294
- netdatacli reload-claiming-state && echo >&2 "Node was successfully claimed." && exit 0
295
- echo "The claim was successful but the agent could not be notified ($?)- it requires a restart to connect to the cloud"
296
- exit 6
349
+ # Update cloud.conf in the agent memory
350
+ @sbindir_POST@/netdatacli write-config 'cloud|global|enabled|yes' && \
351
+ @sbindir_POST@/netdatacli write-config "cloud|global|cloud base url|$URL_BASE" && \
352
+ @sbindir_POST@/netdatacli reload-claiming-state && \
353
+ if [ "${HTTP_STATUS_CODE}" = "204" ] ; then
354
+ echo >&2 "${PROXYMSG}Node was successfully claimed."
355
+ else
356
+ echo >&2 "The agent cloud base url is set to the url provided."
357
+ echo >&2 "The cloud may have different credentials already registered for this agent ID and it cannot be reclaimed under different credentials for security reasons. If you are unable to connect use -id=\$(uuidgen) to overwrite this agent ID with a fresh value if the original credentials cannot be restored."
358
+ echo >&2 "${PROXYMSG}Failed to claim node with the following error message:\"${ERROR_MESSAGES[$EXIT_CODE]}\""
359
+ fi && exit $EXIT_CODE
360
+
361
+ if [ "${ERROR_KEY}" = "ErrAlreadyClaimed" ] ; then
362
+ echo >&2 "The cloud may have different credentials already registered for this agent ID and it cannot be reclaimed under different credentials for security reasons. If you are unable to connect use -id=\$(uuidgen) to overwrite this agent ID with a fresh value if the original credentials cannot be restored."
363
+ echo >&2 "${PROXYMSG}Failed to claim node with the following error message:\"${ERROR_MESSAGES[$EXIT_CODE]}\""
364
+ exit $EXIT_CODE
365
+ fi
366
+ echo >&2 "${PROXYMSG}The claim was successful but the agent could not be notified ($?)- it requires a restart to connect to the cloud."
367
+ exit 5
368
fi
369
299
-ERROR_MESSAGE=$(grep "\"errorMsgKey\":" "${CLAIMING_DIR}/tmpout.txt" | awk -F "errorMsgKey\":\"" '{print $2}' | awk -F "\"" '{print $1}')
300
-case ${ERROR_MESSAGE} in
301
- "ErrInvalidNodeID") EXIT_CODE=6 ;;
302
- "ErrInvalidNodeName") EXIT_CODE=7 ;;
303
- "ErrInvalidRoomID") EXIT_CODE=8 ;;
304
- "ErrInvalidPublicKey") EXIT_CODE=9 ;;
305
- "ErrForbidden") EXIT_CODE=10 ;;
306
- "ErrAlreadyClaimed") EXIT_CODE=11 ;;
307
- "ErrProcessingClaim") EXIT_CODE=12 ;;
308
- "ErrInternalServerError") EXIT_CODE=13 ;;
309
- "ErrGatewayTimeout") EXIT_CODE=14 ;;
310
- "ErrServiceUnavailable") EXIT_CODE=15 ;;
311
- *) EXIT_CODE=5 ;;
312
-esac
313
-echo >&2 "Failed to claim node."
370
+echo >&2 "Failed to claim node with the following error message:\"${ERROR_MESSAGES[$EXIT_CODE]}\""
371
+if [ "${VERBOSE}" == 1 ]; then
372
+ echo >&2 "Error key was:\"${ERROR_KEYS[$EXIT_CODE]}\""
373
+fi
374
rm -f "${CLAIMING_DIR}/tmpout.txt"
375
exit $EXIT_CODE