ksymoops: remove package, it has been obsolete and useless for a long time
[openwrt/svn-archive/packages.git] / utils / watchcat / files / watchcat.sh
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