From bab9e736ac438101b1cb7bbb6dfd28a9938209f2 Mon Sep 17 00:00:00 2001 From: Kapil Bansal Date: Thu, 14 Oct 2021 03:19:42 +0530 Subject: [PATCH] [enhancement] Move logger calls in subroutine #57 Closes #57 --- .../files/monitoring.agent | 60 ++++++++++--------- .../files/monitoring.init | 2 - 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/openwrt-openwisp-monitoring/files/monitoring.agent b/openwrt-openwisp-monitoring/files/monitoring.agent index 8517880..91c2811 100755 --- a/openwrt-openwisp-monitoring/files/monitoring.agent +++ b/openwrt-openwisp-monitoring/files/monitoring.agent @@ -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') @@ -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 @@ -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 @@ -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") @@ -114,8 +127,7 @@ 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 } @@ -123,8 +135,7 @@ 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" & @@ -153,16 +164,13 @@ 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 @@ -170,12 +178,9 @@ 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 @@ -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 diff --git a/openwrt-openwisp-monitoring/files/monitoring.init b/openwrt-openwisp-monitoring/files/monitoring.init index 9e7202a..f583a39 100755 --- a/openwrt-openwisp-monitoring/files/monitoring.init +++ b/openwrt-openwisp-monitoring/files/monitoring.init @@ -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" \