|
4 | 4 |
|
5 | 5 | # shellcheck disable=SC2034 |
6 | 6 | START=99 |
7 | | -STOP=15 |
| 7 | + |
8 | 8 | USE_PROCD=1 |
9 | 9 | PROG="/usr/sbin/openwisp-monitoring" |
10 | 10 | PROG_NAME="OpenWISP monitoring daemon" |
11 | 11 |
|
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 | + } |
20 | 21 |
|
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" |
24 | 24 | } |
25 | 25 |
|
26 | 26 | start_service() { |
27 | 27 | # for openwisp-config |
28 | 28 | 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 | + |
50 | 42 | 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" |
66 | 43 |
|
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 | + |
70 | 49 | procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" |
71 | 50 | procd_close_instance |
72 | 51 |
|
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 | + |
76 | 61 | procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" |
77 | 62 | procd_close_instance |
78 | 63 |
|
|
0 commit comments