Skip to content

Commit

Permalink
[enhancement] Improve curl response message #50
Browse files Browse the repository at this point in the history
Closes #50
  • Loading branch information
devkapilbansal committed Sep 1, 2021
1 parent 1dc4f33 commit f0b77c1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions openwrt-openwisp-monitoring/files/monitoring.agent
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ check_available_memory(){
if [ "$available" -ge "$required" ]; then
echo "0"
else
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Not enough memory available" \
-p daemon.err
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Not enough memory available, skipping collect data." \
-p daemon.warn
echo "1"
fi
}
Expand Down Expand Up @@ -84,7 +84,7 @@ set_url_and_curl(){

URL="$BASE_URL/api/v1/monitoring/device/$UUID/?key=$KEY"

CURL_COMMAND="curl -s -w "%{http_code}""
CURL_COMMAND="curl -s -w "%{http_code}" --output "$TMP_DIR"/response.txt"
[ "$VERIFY_SSL" -eq "0" ] && CURL_COMMAND="$CURL_COMMAND -k"
[ "$VERBOSE_MODE" -eq "1" ] && CURL_COMMAND="$CURL_COMMAND -v"
MAX_RETRIES=${MAX_RETRIES:-5}
Expand Down Expand Up @@ -135,6 +135,10 @@ send_data() {
trap "" USR1
basefilename=${file##*/}
filename=${basefilename%.*}
# remove previous saved response if exist
if [ "$filename" = "response" ]; then
rm "$TMP_DIR"/response.txt 2>/dev/null && continue
fi
# extra zeroes are added for nanoseconds precision
url="$URL&time=$filename.000000"
# retry sending data in case of failure
Expand All @@ -145,12 +149,15 @@ send_data() {
while true
do
if [ "$failures" -eq "$MAX_RETRIES" ]; then
[ -f "$TMP_DIR"/response.txt ] && error_message="$(cat "$TMP_DIR"/response.txt)" || error_message='"".'
if [ "$VERBOSE_MODE" -eq "1" ]; then
logger -s "Data not sent successfully. Response received is $response_code" \
logger -s "Data not sent successfully. Response code is \"$response_code\"." \
"Error message is $error_message" \
-p daemon.err
elif [ "$FAILING" -eq "0" ]; then
FAILING=1
logger -s "Data not sent successfully. Response received is $response_code" \
logger -s "Data not sent successfully. Response code is \"$response_code\"." \
"Error message is $error_message" \
"Run with verbose mode to find more." \
-t openwisp_monitoring \
-p daemon.err
Expand All @@ -169,9 +176,10 @@ send_data() {
-t openwisp_monitoring \
-p daemon.info
FAILING=0
[ -f "$TMP_DIR"/response.txt ] && rm "$TMP_DIR"/response.txt
fi
# remove saved data
rm "$file"
[ -f "$file" ] && rm "$file"
break
else
timeout=$((timeout*2))
Expand Down

0 comments on commit f0b77c1

Please sign in to comment.