Test for required external commands
[trigger-rcmd.git] / scripts / trigger-rcmd
1 #!/bin/sh
2 #
3 # trigger-rcmp: Trigger remote commands
4 # Copyright (c)2014-2015 Alexander Barton (alex@barton.de)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
11 #
12
13 NAME="`basename "$0"`"
14 RCMD_D="/usr/local/etc/rcmd.d"
15
16 PATH="$PATH:/usr/local/sbin:/usr/sbin"
17
18 do_help() {
19         echo "Usage: $0 [--no-wake|-n] <rcmd> [<rcmd> [...]]" >&2
20         exit 2
21 }
22
23 do_rcmd() {
24         SYSTEM="localhost"
25         IFACE="eth0"
26         CMD="uptime"
27         TIMEOUT="300"
28         SSH_CHECKS="6"
29         WAKE_DELAY="10s"
30         BOOT_DELAY="1m"
31         SHUTDOWN_TIME="+10"
32
33         . "$cnf"
34
35         if [ $? -ne 0 ]; then
36                 echo "$NAME: failed to read \"$cnf\"!"
37                 return 2
38         fi
39
40         NAME="$NAME($1)"
41
42         echo "$NAME: checking system \"$SYSTEM\" ..."
43         fping -c1 -q "$SYSTEM" 2>/dev/null
44         if [ $? -ne 0 ]; then
45                 if [ -n "$NO_WAKE" ]; then
46                         echo "$NAME: \"$SYSTEM\" seems to be down, skipping job."
47                         return 9
48                 fi
49
50                 echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
51                 etherwake -b -i "$IFACE" `echo "$SYSTEM" | cut -d'.' -f1`
52                 if [ $? -ne 0 ]; then
53                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
54                         return 3
55                 fi
56                 echo "$NAME: waiting for \"$SYSTEM\" to respond ..."
57                 for i in `seq 1 $TIMEOUT`; do
58                         fping -c1 -q "$SYSTEM" 2>/dev/null
59                         [ $? -ne 0 ] || break
60                         sleep 1s
61                 done
62                 fping -c1 -q "$SYSTEM" 2>/dev/null
63                 if [ $? -ne 0 ]; then
64                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
65                         return 4
66                 fi
67                 sleep "$WAKE_DELAY"
68                 rsys_was_running=0
69         else
70                 echo "$NAME: \"$SYSTEM\" is already alive, ok."
71                 rsys_was_running=1
72         fi
73
74         for i in `seq 1 $SSH_CHECKS`; do
75                 echo "$NAME: checking SSH connection ($i/$SSH_CHECKS) ..."
76                 ssh -q -o "ServerAliveInterval 120" "$SYSTEM" true >/dev/null 2>&1 && break
77                 echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
78                 sleep 10s
79         done
80         echo "$NAME: ok, SSH seems to be available on \"$SYSTEM\"."
81
82         if [ "$rsys_was_running" -eq 0 ]; then
83                 echo "$NAME: \"$SYSTEM\" just started up. Delaying actions for $BOOT_DELAY ..."
84                 sleep "$BOOT_DELAY"
85         fi
86
87         echo "$NAME: calling \"$CMD\" on \"$SYSTEM\" ..."
88         echo
89
90         ssh -q  -o "ServerAliveInterval 120" "$SYSTEM" "$CMD" </dev/null ; r=$?
91
92         echo
93         echo "$NAME: remote command ended with return code $r."
94         sleep 2
95
96         if [ "$rsys_was_running" -eq 0 ]; then
97                 rsys_users=`ssh -q "$SYSTEM" w | tail -n +3 | wc -l`
98                 if [ "$rsys_users" = "0" ]; then
99                         echo "$NAME: power off \"$SYSTEM\" again ..."
100                         ssh -q  -o "ServerAliveInterval 120" "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
101                         if [ $? -ne 0 ]; then
102                                 echo "$NAME: failed to power off \"$SYSTEM\"!" >&2
103                                 return 5
104                         fi
105                 else
106                         echo "$NAME: not shutting down \"$SYSTEM\", \c"
107                         [ $rsys_users -eq 1 ] \
108                                 && echo "there is 1 user logged in!" \
109                                 || echo "there are $rsys_users users logged in!"
110                 fi
111         else
112                 echo "$NAME: \"$SYSTEM\" was already alive, not shutting down."
113         fi
114         [ $r -eq 0 ] && return 0 || return 1
115 }
116
117 # Defaults
118 NO_WAKE=
119
120 # Check required tools
121 for t in fping ssh etherwake; do
122         type "$t" >/dev/null 2>&1
123         if [ $? -ne 0 ]; then
124                 echo "$NAME: Required tool \"$t\" missing, aborting!"
125                 exit 3
126         fi
127 done
128
129 while [ $# -gt 0 ]; do
130         case "$1" in
131           --no-wake|-n)
132                 NO_WAKE=1; ;;
133           --help|-*)
134                 do_help; ;;
135           *)
136                 break
137         esac
138         shift
139 done
140
141 [ $# -gt 0 ] || do_help
142
143 result=0
144 while [ $# -gt 0 ]; do
145         cnf="$RCMD_D/$1"
146         if [ ! -r "$cnf" ]; then
147                 echo "$NAME: can't read \"$cnf\"!"
148                 r=1
149         else
150                 echo "$NAME: working on \"$1\" - `date "+%Y-%m-%d %H:%M:%S"`"
151                 do_rcmd "$1" ; r=$?
152                 echo "$NAME: done ($r) - `date "+%Y-%m-%d %H:%M:%S"`"
153         fi
154         [ $r -ne 0 ] && result=1
155         shift
156         [ $# -gt 0 ] && echo
157 done
158 exit $result