Initial commit, importing script used on Arthur
[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_rcmd() {
19         SYSTEM="localhost"
20         IFACE="eth0"
21         CMD="uptime"
22         TIMEOUT="180"
23
24         . "$cnf"
25
26         if [ $? -ne 0 ]; then
27                 echo "$NAME: failed to read \"$cnf\"!"
28                 return 2
29         fi
30
31         NAME="$NAME($1)"
32
33         echo "$NAME: checking system \"$SYSTEM\" ..."
34         fping -c1 -q "$SYSTEM" 2>/dev/null
35         if [ $? -ne 0 ]; then
36                 echo "$NAME: \"$SYSTEM\" seems to be down, wake it up ..."
37                 etherwake -b -i "$IFACE" `echo "$SYSTEM" | cut -d'.' -f1`
38                 if [ $? -ne 0 ]; then
39                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
40                         return 3
41                 fi
42                 echo "$NAME: waiting for \"$SYSTEM\" to respond ..."
43                 for i in `seq 1 $TIMEOUT`; do
44                         fping -c1 -q "$SYSTEM" 2>/dev/null
45                         [ $? -ne 0 ] || break
46                         sleep 1s
47                 done
48                 fping -c1 -q "$SYSTEM" 2>/dev/null
49                 if [ $? -ne 0 ]; then
50                         echo "$NAME: failed to wake \"$SYSTEM\"!" >&2
51                         return 4
52                 fi
53                 sleep 3
54                 rsys_was_running=0
55         else
56                 echo "$NAME: \"$SYSTEM\" is already alive, ok."
57                 rsys_was_running=1
58         fi
59
60         echo "$NAME: calling \"$CMD\" on \"$SYSTEM\" ..."
61         echo
62
63         ssh -q "$SYSTEM" "$CMD" </dev/null ; r=$?
64
65         echo
66         echo "$NAME: remote command ended with return code $r."
67         sleep 2
68
69         if [ "$rsys_was_running" -eq 0 ]; then
70                 rsys_users=`ssh -q "$SYSTEM" w | tail -n +3 | wc -l`
71                 if [ "$rsys_users" = "0" ]; then
72                         echo "$NAME: power off \"$SYSTEM\" again ..."
73                         ssh -q "$SYSTEM" "sync; shutdown -hP +1"
74                         if [ $? -ne 0 ]; then
75                                 echo "$NAME: failed to power off \"$SYSTEM\"!" >&2
76                                 return 5
77                         fi
78                 else
79                         echo "$NAME: not shutting down \"$SYSTEM\", \c"
80                         [ $rsys_users -eq 1 ] \
81                                 && echo "there is 1 user logged in!" \
82                                 || echo "there are $rsys_users users logged in!"
83                 fi
84         else
85                 echo "$NAME: \"$SYSTEM\" was already alive, not shutting down."
86         fi
87         [ $r -eq 0 ] && return 0 || return 1
88 }
89
90 if [ $# -lt 1 ]; then
91         echo "Usage: $0 <rcmd> [<rcms> [...]]" >&2
92         exit 2
93 fi
94
95 result=0
96 while [ $# -gt 0 ]; do
97         cnf="$RCMD_D/$1"
98         if [ ! -r "$cnf" ]; then
99                 echo "$NAME: can't read \"$cnf\"!"
100                 r=1
101         else
102                 echo "$NAME: working on \"$1\" - `date "+%Y-%m-%d %H:%M:%S"`"
103                 do_rcmd "$1" ; r=$?
104                 echo "$NAME: done ($r) - `date "+%Y-%m-%d %H:%M:%S"`"
105         fi
106         [ $r -ne 0 ] && result=1
107         shift
108         [ $# -gt 0 ] && echo
109 done
110 exit $result