Skip to content

Commit

Permalink
[enhancement] Move logger calls in subroutine #57
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
devkapilbansal committed Oct 15, 2021
1 parent bb2907c commit bab9e73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
60 changes: 32 additions & 28 deletions openwrt-openwisp-monitoring/files/monitoring.agent
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ show_version() {

echoerr() { echo "$@" 1>&2 && exit 1; }

log() {
type="$1"
level="$2"
shift 2

if [ "$type" = "-v" ] && [ "$VERBOSE_MODE" -ne "1" ]; then
return 0
fi

case "$level" in
-i) level=daemon.info;;
-w) level=daemon.warn;;
-e) level=daemon.error;;
-*) echo "Invalid message level : $level"; exit 1;;
esac

logger -s "$@" -p "$level" -t openwisp-monitoring
}

check_available_memory() {
while true; do
total=$(ubus call system info | jsonfilter -e '@.memory.total')
Expand All @@ -50,9 +69,7 @@ check_available_memory() {
if [ -f "$file" ]; then
rm "$file"
else
[ "$VERBOSE_MODE" -eq "1" ] \
&& logger -s "Not enough memory available, skipping collect data." \
-p daemon.warn
log -v -w "Not enough memory available, skipping collect data."
return 1
fi
fi
Expand All @@ -61,14 +78,11 @@ check_available_memory() {

collect_data() {
n=0
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Collecting NetJSON Monitoring data" \
-p daemon.info
[ "$VERBOSE_MODE" -eq "1" ] && log -n -i "Collecting NetJSON Monitoring data"
until [ "$n" -ge 5 ]; do
/usr/sbin/netjson-monitoring --dump "$MONITORED_INTERFACES" && break

if [ "$n" -eq 5 ]; then
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Collecting data failed!" -p daemon.err
fi
[ "$n" -eq 5 ] && log -v -e "Collecting data failed!"
n=$((n + 1))
sleep 5
done
Expand Down Expand Up @@ -103,8 +117,7 @@ save_data() {
echo "$data" >"$TMP_DIR/$filename"
# compress data
gzip "$TMP_DIR/$filename"
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Data saved temporarily" \
-p daemon.info
log -v -i "Data saved temporarily"
fi
# get process id of the process sending data
pid=$(pgrep -f "openwisp-monitoring.*--mode send")
Expand All @@ -114,17 +127,15 @@ save_data() {
}

handle_sigusr1() {
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "SIGUSR1 received! Sending data" \
-p daemon.info
log -v -i "SIGUSR1 received! Sending data"
return 0
}

send_data() {
while true; do
for file in "$TMP_DIR"/*; do
if [ ! -f "$file" ]; then
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "No data file found to send." \
-p daemon.info
log -v -i "No data file found to send"
trap handle_sigusr1 USR1
# SIGUSR1 signal received, interrupt sleep and continue sending data
sleep "$INTERVAL" &
Expand Down Expand Up @@ -153,29 +164,23 @@ send_data() {
if [ "$failures" -eq "$MAX_RETRIES" ]; then
[ -f "$RESPONSE_FILE" ] && error_message="\"$(cat "$RESPONSE_FILE")\"" || error_message='"".'
if [ "$VERBOSE_MODE" -eq "1" ]; then
logger -s "Data not sent successfully. Response code is \"$response_code\"." \
"Error message is $error_message" \
-p daemon.err
log -v -e "Data not sent successfully. Response code is \"$response_code\"." \
"Error message is $error_message"
elif [ "$FAILING" -eq "0" ]; then
FAILING=1
logger -s "Data not sent successfully. Response code is \"$response_code\"." \
log -n -e "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
"Run with verbose mode to find more."
fi
break
fi
# send data
response_code=$($CURL_COMMAND -H "Content-Type: application/json" -d "$data" "$url")
if [ "$response_code" = "200" ]; then
if [ "$VERBOSE_MODE" -eq "1" ]; then
logger -s "Data sent successfully." \
-p daemon.info
log -v -i "Data sent successfully"
elif [ "$FAILING" -eq "1" ]; then
logger -s "Data sent successfully" \
-t openwisp-monitoring \
-p daemon.info
log -n -i "Data sent successfully"
FAILING=0
[ -f "$RESPONSE_FILE" ] && rm "$RESPONSE_FILE"
fi
Expand All @@ -184,8 +189,7 @@ send_data() {
break
else
timeout=$((timeout * 2))
[ "$VERBOSE_MODE" -eq "1" ] && logger -s "Data not sent successfully. Retrying in $timeout seconds" \
-p daemon.warn
log -v -w "Data not sent successfully. Retrying in $timeout seconds"
failures=$((failures + 1))
sleep "$timeout"
fi
Expand Down
2 changes: 0 additions & 2 deletions openwrt-openwisp-monitoring/files/monitoring.init
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ start_service() {
# shellcheck disable=SC2086
procd_set_param command $PROG $interval $monitored_interfaces $verbose $required_memory --mode collect
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
[ "$verbose_mode" -eq "1" ] && procd_set_param stdout 1 && procd_set_param stderr 1
procd_close_instance

procd_open_instance "openwisp-monitoring_send_data"
# shellcheck disable=SC2086
procd_set_param command $PROG $base_url $uuid $key $verify_ssl $interval $verbose $max_retries --mode send
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
[ "$verbose_mode" -eq "1" ] && procd_set_param stdout 1 && procd_set_param stderr 1
procd_close_instance

logger -s "$PROG_NAME started" \
Expand Down

0 comments on commit bab9e73

Please sign in to comment.