From f3568884aedc01afbbf586d5c3889c2f7fc649b6 Mon Sep 17 00:00:00 2001 From: Dhanus Date: Fri, 3 May 2024 17:34:03 +0530 Subject: [PATCH 1/5] [change] Random pause for every 10 successfull sent requests --- openwisp-monitoring/files/monitoring.agent | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index 2304f80..56be0db 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -118,6 +118,7 @@ handle_sigusr1() { } send_data() { + success_count=0 while true; do for file in "$TMP_DIR"/*; do if [ ! -f "$file" ]; then @@ -166,6 +167,7 @@ send_data() { # send data response_code=$($CURL_COMMAND -H "Content-Type: application/json" -d "$data" "$url") if [ "$response_code" = "200" ]; then + success_count=$((success_count + 1)) if [ "$VERBOSE_MODE" -eq "1" ]; then logger -s "Data sent successfully." \ -p daemon.info @@ -195,6 +197,11 @@ send_data() { done # retry sending same data again in next cycle [ "$failures" -eq "$MAX_RETRIES" ] && break + # pause for a while after every 10 successful request sent + if [ $((success_count % 10)) -eq 0 ]; then + pause_duration=$(/usr/sbin/openwisp-get-random-number 1 5) + sleep "$pause_duration" + fi done done } From 701b2a971e09ac59d030d04859acf796dce7a724 Mon Sep 17 00:00:00 2001 From: Dhanus Date: Fri, 3 May 2024 22:58:32 +0530 Subject: [PATCH 2/5] [chore] Fixes --- openwisp-monitoring/files/monitoring.agent | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index 56be0db..dfcadb0 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -118,7 +118,7 @@ handle_sigusr1() { } send_data() { - success_count=0 + success_count=1 while true; do for file in "$TMP_DIR"/*; do if [ ! -f "$file" ]; then @@ -197,7 +197,7 @@ send_data() { done # retry sending same data again in next cycle [ "$failures" -eq "$MAX_RETRIES" ] && break - # pause for a while after every 10 successful request sent + # pause for a while after every 10 successful request sent if [ $((success_count % 10)) -eq 0 ]; then pause_duration=$(/usr/sbin/openwisp-get-random-number 1 5) sleep "$pause_duration" From 959622c8553e616e820bcfbfaef39202c51937e4 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 10 May 2024 10:00:18 -0400 Subject: [PATCH 3/5] [chores] Minor improvements --- openwisp-monitoring/files/monitoring.agent | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index dfcadb0..2a63c6e 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -118,7 +118,7 @@ handle_sigusr1() { } send_data() { - success_count=1 + success=1 while true; do for file in "$TMP_DIR"/*; do if [ ! -f "$file" ]; then @@ -167,7 +167,7 @@ send_data() { # send data response_code=$($CURL_COMMAND -H "Content-Type: application/json" -d "$data" "$url") if [ "$response_code" = "200" ]; then - success_count=$((success_count + 1)) + success=$((success + 1)) if [ "$VERBOSE_MODE" -eq "1" ]; then logger -s "Data sent successfully." \ -p daemon.info @@ -192,13 +192,28 @@ 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 # retry sending same data again in next cycle [ "$failures" -eq "$MAX_RETRIES" ] && break - # pause for a while after every 10 successful request sent - if [ $((success_count % 10)) -eq 0 ]; then + # pause for a random time between 1 and 5 seconds every + # 10 successful requests sent to avoid overload the server + if [ $((success % 10)) -eq 0 ]; then pause_duration=$(/usr/sbin/openwisp-get-random-number 1 5) sleep "$pause_duration" fi From f71027a6f190ed36a7d93d36571247c6172204a4 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 10 May 2024 10:04:13 -0400 Subject: [PATCH 4/5] [fix] Set initial success counter to zero --- openwisp-monitoring/files/monitoring.agent | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index 2a63c6e..1fd10b4 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -118,7 +118,7 @@ handle_sigusr1() { } send_data() { - success=1 + success=0 while true; do for file in "$TMP_DIR"/*; do if [ ! -f "$file" ]; then From 0c8dd89a9adebd4c20067bcaadaec7a685fdbcfb Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 10 May 2024 10:05:03 -0400 Subject: [PATCH 5/5] [chores] Removed code from master --- openwisp-monitoring/files/monitoring.agent | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/openwisp-monitoring/files/monitoring.agent b/openwisp-monitoring/files/monitoring.agent index 1fd10b4..f816782 100755 --- a/openwisp-monitoring/files/monitoring.agent +++ b/openwisp-monitoring/files/monitoring.agent @@ -192,20 +192,6 @@ 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