Make "shutdown time" configurable: SHUTDOWN_TIME
[trigger-rcmd.git] / scripts / trigger-rcmd
1 #!/bin/sh
2 #
3 # trigger-rcmp: Trigger remote commands
4 # Copyright (c)2014 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 <rcmd> [<rcmd> [...]]" >&2
20         exit 2
21 }
22
23 do_rcmd() {
24         SYSTEM="localhost"
25         IFACE="eth0"
26         CMD="uptime"
27         TIMEOUT="180"
28         SSH_CHECKS="6"
29         WAKE_DELAY="10s"
30         SHUTDOWN_TIME="+10"
31
32         . "$cnf"
33
34         if [ $? -ne 0 ]; then
35                 echo "$NAME: failed to read \"$cnf\"!"
36                 return 2
37         fi
38
39         NAME="$NAME($1)"
40
41         echo "$NAME: checking system \"$SYSTEM\" ..."
42         fping -c1 -q "$SYSTEM" 2>/dev/null
43         if [ $? -ne 0 ]; then
44                 if [ -n "$NO_WAKE" ]; then
45                         echo "$NAME: \"$SYSTEM\" seems to be down, skipping job."
46                         return 9
47                 fi
48
49                 echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
50                 etherwake -b -i "$IFACE" `echo "$SYSTEM" | cut -d'.' -f1`
51                 if [ $? -ne 0 ]; then
52                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
53                         return 3
54                 fi
55                 echo "$NAME: waiting for \"$SYSTEM\" to respond ..."
56                 for i in `seq 1 $TIMEOUT`; do
57                         fping -c1 -q "$SYSTEM" 2>/dev/null
58                         [ $? -ne 0 ] || break
59                         sleep 1s
60                 done
61                 fping -c1 -q "$SYSTEM" 2>/dev/null
62                 if [ $? -ne 0 ]; then
63                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
64                         return 4
65                 fi
66                 sleep "$WAKE_DELAY"
67                 rsys_was_running=0
68         else
69                 echo "$NAME: \"$SYSTEM\" is already alive, ok."
70                 rsys_was_running=1
71         fi
72
73         for i in `seq 1 $SSH_CHECKS`; do
74                 echo "$NAME: checking SSH connection ($i/$SSH_CHECKS) ..."
75                 ssh -q "$SYSTEM" true >/dev/null 2>&1 && break
76                 echo "$NAME: SSH on \"$SYSTEM\" not ready. Waiting ..."
77                 sleep 10s
78         done
79
80         echo "$NAME: calling \"$CMD\" on \"$SYSTEM\" ..."
81         echo
82
83         ssh -q "$SYSTEM" "$CMD" </dev/null ; r=$?
84
85         echo
86         echo "$NAME: remote command ended with return code $r."
87         sleep 2
88
89         if [ "$rsys_was_running" -eq 0 ]; then
90                 rsys_users=`ssh -q "$SYSTEM" w | tail -n +3 | wc -l`
91                 if [ "$rsys_users" = "0" ]; then
92                         echo "$NAME: power off \"$SYSTEM\" again ..."
93                         ssh -q "$SYSTEM" "sync; shutdown -hP $SHUTDOWN_TIME" >/dev/null
94                         if [ $? -ne 0 ]; then
95                                 echo "$NAME: failed to power off \"$SYSTEM\"!" >&2
96                                 return 5
97                         fi
98                 else
99                         echo "$NAME: not shutting down \"$SYSTEM\", \c"
100                         [ $rsys_users -eq 1 ] \
101                                 && echo "there is 1 user logged in!" \
102                                 || echo "there are $rsys_users users logged in!"
103                 fi
104         else
105                 echo "$NAME: \"$SYSTEM\" was already alive, not shutting down."
106         fi
107         [ $r -eq 0 ] && return 0 || return 1
108 }
109
110 # Defaults
111 NO_WAKE=
112
113 while [ $# -gt 0 ]; do
114         case "$1" in
115           --no-wake|-n)
116                 NO_WAKE=1; ;;
117           --help|-*)
118                 do_help; ;;
119           *)
120                 break
121         esac
122         shift
123 done
124
125 [ $# -gt 0 ] || do_help
126
127 result=0
128 while [ $# -gt 0 ]; do
129         cnf="$RCMD_D/$1"
130         if [ ! -r "$cnf" ]; then
131                 echo "$NAME: can't read \"$cnf\"!"
132                 r=1
133         else
134                 echo "$NAME: working on \"$1\" - `date "+%Y-%m-%d %H:%M:%S"`"
135                 do_rcmd "$1" ; r=$?
136                 echo "$NAME: done ($r) - `date "+%Y-%m-%d %H:%M:%S"`"
137         fi
138         [ $r -ne 0 ] && result=1
139         shift
140         [ $# -gt 0 ] && echo
141 done
142 exit $result