Check return status of execution of anonymous statistics script (#11188)
* check return of execution of anonymous statistics script * break check into two parts
Emmanuel Vasilakis committed
May 31, 2021 at 10:42 UTC
1d493f5f85d1cfae858f4ecc0aec423135618411
2 files changed
+12
-6
daemon/analytics.c
+9
-4
@@ -876,10 +876,15 @@ void send_statistics(const char *action, const char *action_result, const char *
876
877
FILE *fp = mypopen(command_to_run, &command_pid);
878
if (fp) {
879
- char buffer[100 + 1];
880
- while (fgets(buffer, 100, fp) != NULL)
881
- ;
882
- mypclose(fp, command_pid);
879
+ char buffer[4 + 1];
880
+ char *s = fgets(buffer, 4, fp);
881
+ int exit_code = mypclose(fp, command_pid);
882
+ if (exit_code)
883
+ error("Execution of anonymous statistics script returned %s.", strerror(exit_code));
884
+ if (s && strncmp(buffer, "200", 3))
885
+ error("Execution of anonymous statistics script returned http code %s.", buffer);
886
+ } else {
887
+ error("Failed to run anonymous statistics script %s.", as_script);
888
}
889
freez(command_to_run);
890
}
daemon/anonymous-statistics.sh.in
+3
-2
@@ -148,12 +148,13 @@ EOF
148
149
# send the anonymous statistics to the Netdata PostHog
150
if [ -n "$(command -v curl 2> /dev/null)" ]; then
151
- curl -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" https://posthog.netdata.cloud/capture/ > /dev/null 2>&1
151
+ curl --silent -o /dev/null --write-out '%{http_code}' -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" https://posthog.netdata.cloud/capture/
152
else
153
wget -q -O - --no-check-certificate \
154
+ --server-response \
155
--method POST \
156
--timeout=1 \
157
--header 'Content-Type: application/json' \
158
--body-data "${REQ_BODY}" \
158
- 'https://posthog.netdata.cloud/capture/' > /dev/null 2>&1
159
+ 'https://posthog.netdata.cloud/capture/' 2>&1 | awk '/^ HTTP/{print $2}'
160
fi