Skip to content

Commit 9eb15c2

Browse files
[skip ci][refactor] Refactor init script
1 parent d9c83d8 commit 9eb15c2

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

openwrt-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')
@@ -260,6 +274,8 @@ main() {
260274
done
261275

262276
INTERVAL=${INTERVAL:-300}
277+
INTERVAL="$(time_to_seconds "$INTERVAL")"
278+
[ -z "$INTERVAL" ] && echoerr "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')"
263279
VERBOSE_MODE=${VERBOSE_MODE:-0}
264280
TMP_DIR="/tmp/openwisp/monitoring"
265281

openwrt-openwisp-monitoring/files/monitoring.init

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,62 @@
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=SC2039
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-
# 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" "--interval" interval "300"
41+
add_option "http" "--verify_ssl" verify_ssl "1"
42+
4343
config_load openwisp-monitoring
44-
config_get monitored_interfaces monitoring monitored_interfaces "*"
45-
config_get interval monitoring interval "300"
46-
config_get_bool verbose_mode monitoring verbose_mode "0"
47-
config_get required_memory monitoring required_memory "0.05"
48-
config_get max_retries monitoring max_retries "5"
49-
50-
interval="$(time_to_seconds "$interval")"
51-
[ -z "$interval" ] && { echo "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')" 1>&2 && exit 1; }
52-
53-
interval="--interval $interval"
54-
verbose="--verbose_mode ${verbose_mode:-0}"
55-
set -- --monitored_interfaces \""$monitored_interfaces"\"
56-
monitored_interfaces="$*"
57-
required_memory="--required_memory $required_memory"
58-
max_retries="--max_retries $max_retries"
5944

60-
procd_open_instance "openwisp-monitoring_collect_data"
61-
# shellcheck disable=SC2086
62-
procd_set_param command $PROG $interval $monitored_interfaces $verbose $required_memory --mode collect
45+
add_option "monitoring" "--verbose_mode" verbose_mode "0"
46+
add_option "monitoring" "--max_retries" max_retries "5"
47+
procd_append_param command "--mode" "send"
48+
6349
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
6450
procd_close_instance
6551

66-
procd_open_instance "openwisp-monitoring_send_data"
67-
# shellcheck disable=SC2086
68-
procd_set_param command $PROG $base_url $uuid $key $verify_ssl $interval $verbose $max_retries --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+
59+
config_load openwisp
60+
add_option "http" "--interval" interval "300"
61+
procd_append_param command "--mode" "collect"
62+
6963
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
7064
procd_close_instance
7165

0 commit comments

Comments
 (0)