-
-
Notifications
You must be signed in to change notification settings - Fork 27
[enhancement] Move logger calls in subroutine #57 #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e91c4f0
b47e2a5
88a27f6
e04b2b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,41 @@ show_version() { | |
|
|
||
| echoerr() { echo "$@" 1>&2 && exit 1; } | ||
|
|
||
| log() { | ||
| level="$1" | ||
| type="$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.err ;; | ||
| -*) | ||
| echoerr "Invalid message level : $level" | ||
| ;; | ||
| esac | ||
|
|
||
| logger -s "$@" -p "$level" -t openwisp-monitoring | ||
| } | ||
|
|
||
| time_to_seconds() { | ||
| time=$1 | ||
|
|
||
| { [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } \ | ||
| || { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } \ | ||
| || { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } \ | ||
| || { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } \ | ||
| || { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); } | ||
|
|
||
| echo $seconds | ||
| unset seconds | ||
| unset time | ||
| } | ||
|
|
||
| check_available_memory() { | ||
| while true; do | ||
| total=$(ubus call system info | jsonfilter -e '@.memory.total') | ||
|
|
@@ -50,9 +85,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 -w -n "Not enough memory available, skipping collect data." | ||
| return 1 | ||
| fi | ||
| fi | ||
|
|
@@ -61,14 +94,11 @@ check_available_memory() { | |
|
|
||
| collect_data() { | ||
| n=0 | ||
| [ "$VERBOSE_MODE" -eq "1" ] && logger -s "Collecting NetJSON Monitoring data." \ | ||
| -p daemon.info | ||
| log -i -v "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 -e -v "Collecting data failed!" | ||
| n=$((n + 1)) | ||
| sleep 5 | ||
| done | ||
|
|
@@ -101,8 +131,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 -i -v "Data saved temporarily." | ||
| fi | ||
| # get process id of the process sending data | ||
| pid=$(pgrep -f "openwisp-monitoring.*--mode send") | ||
|
|
@@ -112,17 +141,15 @@ save_data() { | |
| } | ||
|
|
||
| handle_sigusr1() { | ||
| [ "$VERBOSE_MODE" -eq "1" ] && logger -s "SIGUSR1 received! Sending data." \ | ||
| -p daemon.info | ||
| log -i -v "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 -i -v "No data file found to send. Checking again in $INTERVAL seconds." | ||
| trap handle_sigusr1 USR1 | ||
| # SIGUSR1 signal received, interrupt sleep and continue sending data | ||
| sleep "$INTERVAL" & | ||
|
|
@@ -149,46 +176,36 @@ send_data() { | |
| while true; do | ||
| 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 | ||
| elif [ "$FAILING" -eq "0" ]; then | ||
| log -e -v "Data not sent successfully. Response code is \"$response_code\"." \ | ||
| "Error message is $error_message" | ||
| # check if agent was already passing or not to avoid repeating log messages | ||
| if [ "$FAILING" -eq "0" ]; then | ||
| FAILING=1 | ||
| 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 | ||
| [ "$VERBOSE_MODE" -ne "1" ] && log -e -n "Data not sent successfully. Response code is \"$response_code\"." \ | ||
| "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 | ||
| elif [ "$FAILING" -eq "1" ]; then | ||
| logger -s "Data sent successfully." \ | ||
| -t openwisp-monitoring \ | ||
| -p daemon.info | ||
| log -i -v "Data sent successfully." | ||
| # check if agent was already failing or not to avoid repeating log messages | ||
| if [ "$FAILING" -eq "1" ]; then | ||
| FAILING=0 | ||
| rm -f "$RESPONSE_FILE" | ||
| [ "$VERBOSE_MODE" -ne "1" ] && log -i -n "Data sent successfully." | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this so that we don't fill logs uncessarily when data is transferred normally. In non-verbose mode, it will add this log "Data sent successfully" only once and the line will appear again if the connection is interrupted. |
||
| fi | ||
| # remove saved data | ||
| rm -f "$filename" | ||
| break | ||
| elif [ "$response_code" = "400" ]; then | ||
| logger -s "Data not sent successfully (HTTP status $response_code), discarding data." \ | ||
| -t openwisp-monitoring \ | ||
| -p daemon.err | ||
| log -i -n "Data not sent successfully (HTTP status $response_code), discarding data." | ||
| rm -f "$filename" | ||
| break | ||
| else | ||
| timeout=$(/usr/sbin/openwisp-get-random-number 2 15) | ||
| [ "$VERBOSE_MODE" -eq "1" ] && logger -s "Data not sent successfully. Retrying in $timeout seconds." \ | ||
| -p daemon.warn | ||
| log -w -v "Data not sent successfully. Retrying in $timeout seconds." | ||
| failures=$((failures + 1)) | ||
| sleep "$timeout" | ||
| fi | ||
|
|
@@ -203,17 +220,13 @@ wait_until_registered() { | |
| if [ -n "$UUID" ] && [ -n "$KEY" ]; then | ||
| return 0 | ||
| fi | ||
| logger -s "Waiting for device to register." \ | ||
| -t openwisp-monitoring \ | ||
| -p daemon.info | ||
| log -i -n "Waiting for device to register." | ||
| UUID=$(uci get openwisp.http.uuid 2>/dev/null) | ||
| KEY=$(uci get openwisp.http.key 2>/dev/null) | ||
| if [ -z "$UUID" ] || [ -z "$KEY" ]; then | ||
| return 1 | ||
| fi | ||
| logger -s "Setting uuid and key." \ | ||
| -t openwisp-monitoring \ | ||
| -p daemon.info | ||
| log -i -n "Setting uuid and key." | ||
| export UUID KEY | ||
| } | ||
|
|
||
|
|
@@ -223,9 +236,7 @@ bootup_delay() { | |
| if [ "$BOOTUP_DELAY" -ne "0" ]; then | ||
| # get a random number between zero and $BOOTUP_DELAY | ||
| DELAY=$(/usr/sbin/openwisp-get-random-number 0 "$BOOTUP_DELAY") | ||
| logger "Delaying initialization of the monitoring agent for $DELAY seconds." \ | ||
| -t openwisp-monitoring \ | ||
| -p daemon.info | ||
| log -i -n "Delaying initialization of the monitoring agent for $DELAY seconds." | ||
| sleep "$DELAY" | ||
| fi | ||
| # send bootup hotplug event | ||
|
|
@@ -288,8 +299,7 @@ main() { | |
| shift | ||
| ;; | ||
| -*) | ||
| echo "Invalid option: $1" | ||
| exit 1 | ||
| echoerr "Invalid option: $1." | ||
| ;; | ||
| *) break ;; | ||
| esac | ||
|
|
@@ -298,6 +308,8 @@ main() { | |
|
|
||
| INTERVAL=${INTERVAL:-300} | ||
| REGISTRATION_INTERVAL=$((INTERVAL / 10)) | ||
| INTERVAL="$(time_to_seconds "$INTERVAL")" | ||
| [ -z "$INTERVAL" ] && echoerr "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')" | ||
| VERBOSE_MODE=${VERBOSE_MODE:-0} | ||
| BOOTUP_DELAY=${BOOTUP_DELAY:-10} | ||
| TMP_DIR="/tmp/openwisp/monitoring" | ||
|
|
@@ -325,7 +337,7 @@ main() { | |
| RESPONSE_FILE="$TMP_DIR"/response.txt | ||
| set_url_and_curl && send_data | ||
| else | ||
| echoerr "The supplied mode is invalid. Only send and collect are allowed" | ||
| echoerr "The supplied mode is invalid. Only send and collect are allowed." | ||
| fi | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's still repetition here which makes the code hard to read.
I think we need to edit the log function to avoid this.
Ideally we could do something like:
The non verbose output should be printed also in verbose mode, while the verbose output is appended to the non verbose output but only when in verbose mode, so in non verbose mode we would have:
While in verbose mode we would have:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition is here to ensure that the logs are not added unnecessarily when the connection is interrupted due to some issue for a long time.
Discussed here in "Non-verbose mode" #42 (review)