Skip to content

Commit b47e2a5

Browse files
[refactor] Refactor init script
1 parent e91c4f0 commit b47e2a5

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

openwisp-monitoring/files/monitoring.agent

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ log() {
5555
logger -s "$@" -p "$level" -t openwisp-monitoring
5656
}
5757

58+
time_to_seconds() {
59+
time=$1
60+
61+
{ [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } \
62+
|| { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } \
63+
|| { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } \
64+
|| { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } \
65+
|| { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
66+
67+
echo $seconds
68+
unset seconds
69+
unset time
70+
}
71+
5872
check_available_memory() {
5973
while true; do
6074
total=$(ubus call system info | jsonfilter -e '@.memory.total')
@@ -302,6 +316,8 @@ main() {
302316

303317
INTERVAL=${INTERVAL:-300}
304318
REGISTRATION_INTERVAL=$((INTERVAL / 10))
319+
INTERVAL="$(time_to_seconds "$INTERVAL")"
320+
[ -z "$INTERVAL" ] && echoerr "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')"
305321
VERBOSE_MODE=${VERBOSE_MODE:-0}
306322
BOOTUP_DELAY=${BOOTUP_DELAY:-10}
307323
TMP_DIR="/tmp/openwisp/monitoring"

openwisp-monitoring/files/monitoring.init

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,60 @@
44

55
# shellcheck disable=SC2034
66
START=99
7-
STOP=15
7+
88
USE_PROCD=1
99
PROG="/usr/sbin/openwisp-monitoring"
1010
PROG_NAME="OpenWISP monitoring daemon"
1111

12-
time_to_seconds() {
13-
time=$1
14-
15-
{ [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } \
16-
|| { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } \
17-
|| { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } \
18-
|| { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } \
19-
|| { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
12+
add_option() {
13+
# shellcheck disable=SC3043
14+
{
15+
local cfg="$1"
16+
local flag="$2"
17+
local option="$3"
18+
local default="$4"
19+
local value
20+
}
2021

21-
echo $seconds
22-
unset seconds
23-
unset time
22+
config_get value "$cfg" "$option" "$default"
23+
[ -n "$value" ] && procd_append_param command "$flag" "$value"
2424
}
2525

2626
start_service() {
2727
# for openwisp-config
2828
config_load openwisp
29-
config_get base_url http url
30-
config_get uuid http uuid
31-
config_get key http key
32-
config_get_bool verify_ssl http verify_ssl "1"
33-
config_get respawn_threshold http respawn_threshold
34-
config_get respawn_timeout http respawn_timeout
35-
config_get respawn_retry http respawn_retry
36-
37-
[ -n "$base_url" ] && base_url="--url $base_url"
38-
[ -n "$uuid" ] && uuid="--uuid $uuid"
39-
[ -n "$key" ] && key="--key $key"
40-
[ -n "$verify_ssl" ] && verify_ssl="--verify_ssl $verify_ssl"
41-
42-
if [ -z "$base_url" ]; then
43-
logger -s "url is not set, please add it to /etc/config/openwisp" \
44-
-t openwisp-monitoring \
45-
-p daemon.err
46-
exit 1
47-
fi
48-
49-
# for openwisp-monitoring
29+
30+
respawn_threshold=$(config_get http respawn_threshold)
31+
respawn_timeout=$(config_get http respawn_timeout)
32+
respawn_retry=$(config_get http respawn_retry)
33+
34+
procd_open_instance "openwisp-monitoring_send_data"
35+
procd_set_param command $PROG
36+
37+
add_option "http" "--url" url
38+
add_option "http" "--uuid" uuid
39+
add_option "http" "--key" key
40+
add_option "http" "--verify_ssl" verify_ssl "1"
41+
5042
config_load openwisp-monitoring
51-
config_get monitored_interfaces monitoring monitored_interfaces "*"
52-
config_get interval monitoring interval "300"
53-
config_get_bool verbose_mode monitoring verbose_mode "0"
54-
config_get required_memory monitoring required_memory "0.05"
55-
config_get max_retries monitoring max_retries "5"
56-
config_get bootup_delay monitoring bootup_delay "10"
57-
58-
interval="$(time_to_seconds "$interval")"
59-
[ -z "$interval" ] && { echo "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')" 1>&2 && exit 1; }
60-
61-
interval="--interval $interval"
62-
verbose="--verbose_mode ${verbose_mode:-0}"
63-
required_memory="--required_memory $required_memory"
64-
max_retries="--max_retries $max_retries"
65-
bootup_delay="--bootup_delay $bootup_delay"
6643

67-
procd_open_instance "openwisp-monitoring_collect_data"
68-
# shellcheck disable=SC2086,SC2154
69-
procd_set_param command $PROG $interval $verbose $required_memory --mode collect --monitored_interfaces "$monitored_interfaces"
44+
add_option "monitoring" "--verbose_mode" verbose_mode "0"
45+
add_option "monitoring" "--max_retries" max_retries "5"
46+
add_option "monitoring" "--interval" interval "300"
47+
procd_append_param command "--mode" "send"
48+
7049
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
7150
procd_close_instance
7251

73-
procd_open_instance "openwisp-monitoring_send_data"
74-
# shellcheck disable=SC2086
75-
procd_set_param command $PROG $base_url $uuid $key $verify_ssl $interval $verbose $max_retries $bootup_delay --mode send
52+
procd_open_instance "openwisp-monitoring_collect_data"
53+
procd_set_param command $PROG
54+
55+
add_option "monitoring" "--monitored_interfaces" monitored_interfaces "*"
56+
add_option "monitoring" "--required_memory" required_memory "0.05"
57+
add_option "monitoring" "--verbose_mode" verbose_mode "0"
58+
add_option "monitoring" "--interval" interval "300"
59+
procd_append_param command "--mode" "collect"
60+
7661
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
7762
procd_close_instance
7863

0 commit comments

Comments
 (0)