check, rcheck: Propagate exit status better; capture lines more precisely.
[rcheck] / rcheck
1 #! /bin/sh
2 ###
3 ### Run everyday health checks on remote systems.
4
5 set -e
6 prog=$0
7 while
8 case "$prog" in */*) ;; *) prog=./$prog ;; esac
9 cd "${prog%/*}"
10 [ -L "${prog##*/}" ]
11 do
12 prog=$(readlink "${prog##*/}")
13 done
14
15 exec 4>&1
16 rc=0
17 for host in "$@"; do
18
19 nrc=$(
20 exec 5>&1
21 { { { set +e
22 tar czf - check check.d |
23 ssh "$host" '
24 set -e
25 cd /tmp
26
27 i=0
28 while :; do
29 tmpdir=check.$$.$i
30 if mkdir -m700 $tmpdir >/dev/null 2>&1; then break; fi
31 case $i in
32 500) echo >&2 "failed to create temporary directory"; exit 125 ;;
33 esac
34 i=$((i + 1))
35 done
36 trap "cd /; rm -rf /tmp/$tmpdir" INT TERM QUIT HUP EXIT
37 cd $tmpdir
38
39 tar xzf -
40 ./check'
41 rc=$?; set -e; echo $rc >&5
42 case $rc in
43 0) ;;
44 *) echo >&2 "remote check process failed (rc = $rc)" ;;
45 esac
46 } 1>&3; } 2>&1 |
47 sed 's/^/!!! /'
48 } 3>&1 |
49 if IFS= read -r line; then
50 cat <<EOF
51 ###--------------------------------------------------------------------------
52 ### $host
53
54 $line
55 EOF
56 cat
57 fi >&4
58 )
59 if [ $nrc -gt $rc ]; then rc=$nrc; fi
60 done
61
62 exit $rc