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