From 8df816e20699a07a6e4031169d127da421f6968f Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 2 May 2024 18:42:16 +0530 Subject: [PATCH] [change] Stop sending data on repeated 404 responses #105 The openwisp-monitoring agent will exit (killing both the processes for collecting and sending data) if the device has been deleted from OpenWISP Controller. The delete is determined by checking if the openwisp-config agent agent is running, which exits on repeated 404 response for checksum response. Closes #105 --- openwisp-monitoring/files/monitoring.agent | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index 2304f80..55390fc 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -190,6 +190,20 @@ send_data() { [ "$VERBOSE_MODE" -eq "1" ] && logger -s "Data not sent successfully. Retrying in $timeout seconds." \ -p daemon.warn failures=$((failures + 1)) + if [ "$response_code" = "404" ]; then + # If we get a HTTP 404 response, it could mean that the device has been deleted from OpenWISP + # Controller. We check if openwisp-config agent is running to determine if the device has been + # deleted. If openwisp-config agent is not running, the monitoring agent will also exit. + if ! pgrep -x "openwisp_config" >/dev/null; then + logger -s "Giving up and shutting down: the device may have been deleted from OpenWISP Controller" \ + -t openwisp-monitoring \ + -p daemon.err + # get process id of the process collecting data + pid=$(pgrep -f "openwisp-monitoring.*--mode collect") + kill -SIGKILL "$pid" + exit 2 + fi + fi sleep "$timeout" fi done