lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

initd_watchcat (3202B)


      1 #!/bin/sh /etc/rc.common
      2 
      3 START=97
      4 
      5 PIDFILE="/tmp/run/watchcat"
      6 
      7 append_string() {
      8 	local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
      9 	eval "actual=\$$varname"
     10 
     11 	new="${actual:+$actual$separator}$add"
     12 	eval "$varname=\$new"
     13 }
     14 
     15 timetoseconds() {
     16 	local time=$1
     17 	unset seconds
     18 
     19 	{ [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } || \
     20 	{ [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } || \
     21 	{ [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m}*60)); } || \
     22 	{ [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h}*3600)); } || \
     23 	{ [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d}*86400)); }
     24 }
     25 
     26 load_watchcat() {
     27 	config_get period	$1 period
     28 	config_get mode		$1 mode		"always"
     29 	config_get pinghosts	$1 pinghosts	"8.8.8.8"
     30 	config_get pingperiod	$1 pingperiod
     31 	config_get forcedelay	$1 forcedelay	"0"
     32 
     33 	# Fix potential typo in mode (backward compatibility).
     34 	[ "$mode" = "allways" ] && mode="always"
     35 
     36 	error=""
     37 
     38 	timetoseconds "$period"
     39 	period="$seconds"
     40 	[ "$period" -ge 1 ] \
     41 		|| append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
     42 	[ "$mode" = "always" -o "$mode" = "ping" ] \
     43 		|| append_string "error" "mode must be 'always' or 'ping'" "; "
     44 	[ -n "$pinghosts" -o "$mode" = "always" ] \
     45 		|| append_string "error" "pinghosts must be set when in 'ping' mode" "; "
     46 	[ "$mode" = "ping" ] && {
     47 		if [ -n "$pingperiod" ]
     48 		then
     49 			timetoseconds "$pingperiod"
     50 			pingperiod="$seconds"
     51 			if [ "$pingperiod" -ge 0 ]
     52 			then
     53 				[ "$pingperiod" -lt "$period" ] \
     54 					|| append_string "error" "pingperiod must be less than period" "; "
     55 			else
     56 				append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
     57 			fi
     58 		else
     59 			pingperiod="$((period/20))"
     60 		fi
     61 	}
     62 	[ "$forcedelay" -ge 0 ] \
     63 		|| append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
     64 
     65 	[ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
     66 
     67 	if [ "$mode" = "always" ]
     68 	then
     69 		/usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
     70 		logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
     71 	else
     72 		/usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
     73 		logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
     74 	fi
     75 
     76 	echo $! >> "${PIDFILE}.pids"
     77 }
     78 
     79 stop() {
     80 	if [ -f "${PIDFILE}.pids" ]
     81 	then
     82 		logger -p user.info -t "watchcat" "stopping all tasks"
     83 
     84 		while read pid
     85 		do
     86 			kill "$pid"
     87 		done < "${PIDFILE}.pids"
     88 
     89 		rm "${PIDFILE}.pids"
     90 
     91 		logger -p user.info -t "watchcat" "all tasks stopped"
     92 	else
     93 		logger -p user.info -t "watchcat" "no tasks running"
     94 	fi
     95 }
     96 
     97 start() {
     98 	[ -f "${PIDFILE}.pids" ] && stop
     99 
    100 	config_load system
    101 	if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
    102 	then
    103 		logger -p user.info -t "watchcat" "starting all tasks"
    104 		config_foreach load_watchcat watchcat
    105 		logger -p user.info -t "watchcat" "all tasks started"
    106 	else
    107 		logger -p user.info -t "watchcat" "no tasks defined"
    108 	fi
    109 }