#!/bin/sh /etc/rc.common # # OpenWISP Monitoring Daemon # shellcheck disable=SC2034 START=99 STOP=15 USE_PROCD=1 PROG="/usr/sbin/openwisp-monitoring" PROG_NAME="OpenWISP monitoring daemon" 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 } start_service() { # for openwisp-config config_load openwisp config_get base_url http url config_get uuid http uuid config_get key http key config_get_bool verify_ssl http verify_ssl "1" config_get respawn_threshold http respawn_threshold config_get respawn_timeout http respawn_timeout config_get respawn_retry http respawn_retry [ -n "$base_url" ] && base_url="--url $base_url" [ -n "$uuid" ] && uuid="--uuid $uuid" [ -n "$key" ] && key="--key $key" [ -n "$verify_ssl" ] && verify_ssl="--verify_ssl $verify_ssl" if [ -z "$base_url" ]; then logger -s "url is not set, please add it to /etc/config/openwisp" \ -t openwisp-monitoring \ -p daemon.err exit 1 fi # for openwisp-monitoring config_load openwisp-monitoring config_get monitored_interfaces monitoring monitored_interfaces "*" config_get interval monitoring interval "300" config_get_bool verbose_mode monitoring verbose_mode "0" config_get required_memory monitoring required_memory "0.05" config_get max_retries monitoring max_retries "5" interval="$(time_to_seconds "$interval")" if [ "$interval" -lt 1 ]; then logger -s "Interval is invalid. Use time value(eg: '10', '2m', '3h', '1d')" \ -t openwisp-monitoring \ -p daemon.err exit 1 fi interval="--interval $interval" verbose="--verbose_mode ${verbose_mode:-0}" required_memory="--required_memory $required_memory" max_retries="--max_retries $max_retries" procd_open_instance "openwisp-monitoring_collect_data" # shellcheck disable=SC2086,SC2154 procd_set_param command $PROG $interval $verbose $required_memory --mode collect --monitored_interfaces "$monitored_interfaces" procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" [ "$verbose_mode" -eq "1" ] && procd_set_param stdout 1 && procd_set_param stderr 1 procd_close_instance procd_open_instance "openwisp-monitoring_send_data" # shellcheck disable=SC2086 procd_set_param command $PROG $base_url $uuid $key $verify_ssl $interval $verbose $max_retries --mode send procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" [ "$verbose_mode" -eq "1" ] && procd_set_param stdout 1 && procd_set_param stderr 1 procd_close_instance logger -s "$PROG_NAME started" \ -t openwisp-monitoring \ -p daemon.info } stop_service() { logger -s "$PROG_NAME stopping" \ -t openwisp-monitoring \ -p daemon.info } service_triggers() { procd_add_reload_trigger "openwisp" "openwisp-monitoring" }