Skip to content

Commit bac6d54

Browse files
[refactor] Added main, help functions to agent #33
Closes #33
1 parent 7be1724 commit bac6d54

2 files changed

Lines changed: 107 additions & 79 deletions

File tree

openwrt-openwisp-monitoring/files/monitoring.agent

Lines changed: 107 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,35 @@
22
#
33
# OpenWISP Monitoring Daemon
44

5-
VERSION=0 # default value of version
6-
# parse options
7-
while [ -n "$1" ]; do
8-
case "$1" in
9-
--version | -v)
10-
export VERSION=1
11-
break
12-
;;
13-
--url)
14-
export BASE_URL="$2"
15-
shift
16-
;;
17-
--uuid)
18-
export UUID="$2"
19-
shift
20-
;;
21-
--key)
22-
export KEY="$2"
23-
shift
24-
;;
25-
--verify_ssl)
26-
export VERIFY_SSL="$2"
27-
shift
28-
;;
29-
--interval)
30-
export INTERVAL="$2"
31-
shift
32-
;;
33-
--monitored_interfaces)
34-
export MONITORED_INTERFACES="$2"
35-
shift
36-
;;
37-
--verbose_mode)
38-
export VERBOSE_MODE="$2"
39-
shift
40-
;;
41-
--required_memory)
42-
export REQUIRED_PERCENT="$2"
43-
shift
44-
;;
45-
--mode)
46-
export MODE="$2"
47-
shift
48-
;;
49-
--max_retries)
50-
export MAX_RETRY="$2"
51-
shift
52-
;;
53-
-*)
54-
echo "Invalid option: $1"
55-
exit 1
56-
;;
57-
*) break ;;
58-
esac
59-
shift
60-
done
61-
62-
if [ "$VERSION" -eq "1" ]; then
63-
VERSION=$(cat /etc/openwisp/monitoring/VERSION)
64-
echo "openwisp-monitoring $VERSION"
5+
show_help() {
6+
printf "Usage:\n"
7+
printf " openwisp_monitoring [OPTIONS...]\n"
8+
printf "\n"
9+
printf "Runs OpenWISP Monitoring Agent for collecting and sending data.\n"
10+
printf "\n"
11+
printf "General options:\n"
12+
printf " --help\t\t\t\t: Show this help text\n"
13+
printf " -v, --version\t\t\t\t: Shows version of the agent.\n"
14+
printf " --mode <send, collect>\t\t: Mode for running Monitoring Agent.\n"
15+
printf "OpenWISP Config options:\n"
16+
printf " --url <server-url>\t\t\t: URL of the OpenWISP server.\n"
17+
printf " --uuid <uuid>\t\t\t\t: UUID of the device.\n"
18+
printf " --key <key>\t\t\t\t: Key for the device.\n"
19+
printf " --verify_ssl <0, 1>\t\t\t: Whether SSL Authentication should be enabled or not.\n"
20+
printf "OpenWISP Monitoring config options:\n"
21+
printf " --interval <time>\t\t\t: Time in seconds after which data should be sent/collected.\n"
22+
printf " --monitored-interfaces <interfaces>\t: Interfaces that needs to be monitored.\n"
23+
printf " --verbose-mode <0, 1>\t\t\t: Run agent in verbose mode.\n"
24+
printf " --required-memory <0-1>\t\t: Fraction of total memory that should be available to collect data.\n"
25+
printf " --max-retries <max-retries-count>\t: No. of time agent should retry to send data.\n"
6526
exit 0
66-
fi
27+
}
6728

68-
INTERVAL=${INTERVAL:-300}
69-
VERBOSE_MODE=${VERBOSE_MODE:-0}
70-
TMP_DIR="/tmp/openwisp/monitoring"
29+
show_version() {
30+
VERSION=$(cat /etc/openwisp/monitoring/VERSION)
31+
echo "$(basename "$0") $VERSION"
32+
exit 0
33+
}
7134

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

@@ -219,17 +182,83 @@ send_data() {
219182
done
220183
}
221184

222-
[ -z "$MODE" ] && echoerr "missing required --mode option"
223-
224-
if [ "$MODE" = "collect" ]; then
225-
MONITORED_INTERFACES=${MONITORED_INTERFACES:-*}
226-
# remove double quotes from interfaces
227-
MONITORED_INTERFACES=$(echo "$MONITORED_INTERFACES" | tr -d '"')
228-
save_data
229-
elif [ "$MODE" = "send" ]; then
230-
VERIFY_SSL=${VERIFY_SSL:-0}
231-
RESPONSE_FILE="$TMP_DIR"/response.txt
232-
set_url_and_curl && send_data
233-
else
234-
echoerr "The supplied mode is invalid. Only send and collect are allowed"
235-
fi
185+
main() {
186+
# parse options
187+
while [ -n "$1" ]; do
188+
case "$1" in
189+
--version | -v)
190+
show_version
191+
;;
192+
--help | -h)
193+
show_help
194+
;;
195+
--url)
196+
export BASE_URL="$2"
197+
shift
198+
;;
199+
--uuid)
200+
export UUID="$2"
201+
shift
202+
;;
203+
--key)
204+
export KEY="$2"
205+
shift
206+
;;
207+
--verify_ssl)
208+
export VERIFY_SSL="$2"
209+
shift
210+
;;
211+
--interval)
212+
export INTERVAL="$2"
213+
shift
214+
;;
215+
--monitored_interfaces)
216+
export MONITORED_INTERFACES="$2"
217+
shift
218+
;;
219+
--verbose_mode)
220+
export VERBOSE_MODE="$2"
221+
shift
222+
;;
223+
--required_memory)
224+
export REQUIRED_PERCENT="$2"
225+
shift
226+
;;
227+
--mode)
228+
export MODE="$2"
229+
shift
230+
;;
231+
--max_retries)
232+
export MAX_RETRY="$2"
233+
shift
234+
;;
235+
-*)
236+
echo "Invalid option: $1"
237+
exit 1
238+
;;
239+
*) break ;;
240+
esac
241+
shift
242+
done
243+
244+
INTERVAL=${INTERVAL:-300}
245+
VERBOSE_MODE=${VERBOSE_MODE:-0}
246+
TMP_DIR="/tmp/openwisp/monitoring"
247+
248+
[ -z "$MODE" ] && echoerr "missing required --mode option"
249+
250+
if [ "$MODE" = "collect" ]; then
251+
MONITORED_INTERFACES=${MONITORED_INTERFACES:-*}
252+
# remove double quotes from interfaces
253+
MONITORED_INTERFACES=$(echo "$MONITORED_INTERFACES" | tr -d '"')
254+
save_data
255+
elif [ "$MODE" = "send" ]; then
256+
VERIFY_SSL=${VERIFY_SSL:-0}
257+
RESPONSE_FILE="$TMP_DIR"/response.txt
258+
set_url_and_curl && send_data
259+
else
260+
echoerr "The supplied mode is invalid. Only send and collect are allowed"
261+
fi
262+
}
263+
264+
main "$@"

openwrt-openwisp-monitoring/files/monitoring.init

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ start_service() {
6969
exit 1
7070
fi
7171
interval="--interval $interval"
72-
monitored_interfaces="--monitored_interfaces \"$monitored_interfaces\""
7372
verbose="--verbose_mode ${verbose_mode:-0}"
7473
set -- --monitored_interfaces \""$monitored_interfaces"\"
7574
monitored_interfaces="$*"

0 commit comments

Comments
 (0)