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