Browse Source

watchcat: add with myself as maintainer

Signed-off-by: Roger D rogerdammit@gmail.com
Roger 10 years ago
parent
commit
f749bd7237

+ 44
- 0
utils/watchcat/Makefile View File

@@ -0,0 +1,44 @@
1
+#
2
+# Copyright (C) 2010 segal.di.ubi.pt
3
+#
4
+# This is free software, licensed under the GNU General Public License v2.
5
+# See /LICENSE for more information.
6
+#
7
+
8
+include $(TOPDIR)/rules.mk
9
+
10
+PKG_NAME:=watchcat
11
+PKG_VERSION:=1
12
+PKG_RELEASE:=5
13
+
14
+PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
15
+
16
+include $(INCLUDE_DIR)/package.mk
17
+
18
+define Package/watchcat
19
+  SECTION:=utils
20
+  CATEGORY:=Utilities
21
+  TITLE:=Enable the configuration of programed reboots
22
+endef
23
+
24
+define Package/watchcat/description
25
+Allows to configure a periodically reboot, or after loosing internet connectivity. Configured trough UCI /etc/config/system.
26
+endef
27
+
28
+define Package/watchcat/conffiles
29
+/etc/config/system
30
+endef
31
+
32
+define Build/Compile
33
+endef
34
+
35
+define Package/watchcat/install
36
+	$(INSTALL_DIR) $(1)/etc/init.d
37
+	$(INSTALL_BIN) ./files/initd_watchcat $(1)/etc/init.d/watchcat
38
+	$(INSTALL_DIR) $(1)/usr/bin
39
+	$(INSTALL_BIN) ./files/watchcat.sh $(1)/usr/bin/watchcat.sh
40
+	$(INSTALL_DIR) $(1)/etc/uci-defaults
41
+	$(INSTALL_BIN) ./files/uci_defaults_watchcat $(1)/etc/uci-defaults/50-watchcat
42
+endef
43
+
44
+$(eval $(call BuildPackage,watchcat))

+ 108
- 0
utils/watchcat/files/initd_watchcat View File

@@ -0,0 +1,108 @@
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		"allways"
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
+	error=""
34
+
35
+	timetoseconds "$period"
36
+	period="$seconds"
37
+	[ "$period" -ge 1 ] \
38
+		|| append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
39
+	[ "$mode" = "allways" -o "$mode" = "ping" ] \
40
+		|| append_string "error" "mode must be 'allways' or 'ping'" "; "
41
+	[ -n "$pinghosts" -o "$mode" = "allways" ] \
42
+		|| append_string "error" "pinghosts must be set when in 'ping' mode" "; "
43
+	[ "$mode" = "ping" ] && {
44
+		if [ -n "$pingperiod" ]
45
+		then
46
+			timetoseconds "$pingperiod"
47
+			pingperiod="$seconds"
48
+			if [ "$pingperiod" -ge 0 ]
49
+			then
50
+				[ "$pingperiod" -le "$period" ] \
51
+					|| append_string "error" "pingperiod must be less than period" "; "
52
+			else
53
+				append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
54
+			fi
55
+		else
56
+			pingperiod="$((period/20))"
57
+		fi
58
+	}
59
+	[ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \
60
+		|| append_string "error" "pingperiod is not recognized" "; "
61
+	[ "$forcedelay" -ge 0 ] \
62
+		|| append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
63
+
64
+	[ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
65
+
66
+	if [ "$mode" = "allways" ]
67
+	then
68
+		/usr/bin/watchcat.sh "allways" "$period" "$forcedelay" &
69
+		logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)" 
70
+	else
71
+		/usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
72
+		logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)" 
73
+	fi
74
+
75
+	echo $! >> "${PIDFILE}.pids"
76
+}
77
+
78
+stop() {
79
+	if [ -f "${PIDFILE}.pids" ]
80
+	then
81
+		logger -p user.info -t "watchcat" "stopping all tasks"
82
+
83
+		while read pid
84
+		do
85
+			kill "$pid"
86
+		done < "${PIDFILE}.pids"
87
+
88
+		rm "${PIDFILE}.pids"
89
+
90
+		logger -p user.info -t "watchcat" "all tasks stopped"
91
+	else
92
+		logger -p user.info -t "watchcat" "no tasks running"
93
+	fi
94
+}
95
+
96
+start() {
97
+	[ -f "${PIDFILE}.pids" ] && stop
98
+
99
+	config_load system
100
+	if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
101
+	then
102
+		logger -p user.info -t "watchcat" "starting all tasks"
103
+		config_foreach load_watchcat watchcat
104
+		logger -p user.info -t "watchcat" "all tasks started"
105
+	else
106
+		logger -p user.info -t "watchcat" "no tasks defined"
107
+	fi
108
+}

+ 10
- 0
utils/watchcat/files/uci_defaults_watchcat View File

@@ -0,0 +1,10 @@
1
+#!/bin/sh
2
+
3
+uci -q show system.@watchcat[0] || {
4
+	uci add system watchcat
5
+	uci set system.@watchcat[0].period=6h
6
+	uci set system.@watchcat[0].mode=ping
7
+	uci set system.@watchcat[0].pinghosts=8.8.8.8
8
+	uci set system.@watchcat[0].forcedelay=30
9
+	uci commit
10
+}

+ 69
- 0
utils/watchcat/files/watchcat.sh View File

@@ -0,0 +1,69 @@
1
+#!/bin/sh 
2
+
3
+mode="$1"
4
+
5
+shutdown_now() {
6
+	local forcedelay="$1"
7
+
8
+	reboot &
9
+
10
+	[ "$forcedelay" -ge 1 ] && {
11
+		sleep "$forcedelay"
12
+
13
+		echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
14
+	}
15
+}
16
+
17
+watchcat_allways() {
18
+	local period="$1"; local forcedelay="$2"
19
+
20
+	sleep "$period" && shutdown_now "$forcedelay"
21
+}
22
+
23
+watchcat_ping() {
24
+	local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"
25
+
26
+	time_now="$(cat /proc/uptime)"
27
+	time_now="${time_now%%.*}"
28
+	time_lastcheck="$time_now"
29
+	time_lastcheck_withinternet="$time_now"
30
+
31
+	while true
32
+	do
33
+		# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
34
+		time_now="$(cat /proc/uptime)"
35
+		time_now="${time_now%%.*}"
36
+		time_diff="$((time_now-time_lastcheck))"
37
+
38
+		[ "$time_diff" -lt "$pingperiod" ] && {
39
+			sleep_time="$((pingperiod-time_diff))"
40
+			sleep "$sleep_time"
41
+		}
42
+
43
+		time_now="$(cat /proc/uptime)"
44
+		time_now="${time_now%%.*}"
45
+		time_lastcheck="$time_now"
46
+
47
+		for host in "$pinghosts"
48
+		do
49
+			if ping -c 1 "$host" &> /dev/null 
50
+			then 
51
+				time_lastcheck_withinternet="$time_now"
52
+			else
53
+				time_diff="$((time_now-time_lastcheck_withinternet))"
54
+				logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period"       
55
+			fi
56
+		done
57
+
58
+		time_diff="$((time_now-time_lastcheck_withinternet))"
59
+		[ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay"
60
+
61
+	done
62
+}
63
+
64
+	if [ "$mode" = "allways" ]
65
+	then
66
+		watchcat_allways "$2" "$3"
67
+	else
68
+		watchcat_ping "$2" "$3" "$4" "$5"
69
+	fi