Skip to content

Commit

Permalink
[chores] Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 10, 2024
1 parent 701b2a9 commit 959622c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions openwisp-monitoring/files/monitoring.agent
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 959622c

Please sign in to comment.