summaryrefslogtreecommitdiffstats
path: root/bin/rancid-run.in
blob: ae249125b9732e2f822399b591ae8b5e9c377927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! /bin/sh
##
## $Id$
##
## @PACKAGE@ @VERSION@
## Copyright (c) 1997-2007 by Terrapin Communications, Inc.
## All rights reserved.
##
## This code is derived from software contributed to and maintained by
## Terrapin Communications, Inc. by Henry Kilmer, John Heasley, Andrew Partan,
## Pete Whiting, Austin Schutz, and Andrew Fort.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
## 3. All advertising materials mentioning features or use of this software
##    must display the following acknowledgement:
##        This product includes software developed by Terrapin Communications,
##        Inc. and its contributors for RANCID.
## 4. Neither the name of Terrapin Communications, Inc. nor the names of its
##    contributors may be used to endorse or promote products derived from
##    this software without specific prior written permission.
## 5. It is requested that non-binding fixes and modifications be contributed
##    back to Terrapin Communications, Inc.
##
## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. AND CONTRIBUTORS
## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
# 
# Run rancid for each of the rancid groups defined by $LIST_OF_GROUPS in
# @sysconfdir@/rancid.conf or those specified on the command-line.
#

# Default ENVFILE, overrideable with -f flag.
ENVFILE="@sysconfdir@/rancid.conf"

TMPDIR=${TMPDIR:=/tmp}; export TMPDIR

# control_rancid argv
CR_ARGV=""; export CR_ARGV

# print a usage message to stderr
pr_usage() {
    echo "usage: $0 [-V] [-f config_file] [-r device_name] [-m mail rcpt] [group [group ...]]" >&2;
}

# command-line options
# -V
# -f <config file name>
# -m <mailto address>
# -r <device name>
if [ $# -ge 1 ] ; then

    while [ 1 ] ; do
	case $1 in
	-V)
	    echo "@PACKAGE@ @VERSION@"
	    exit 0
	    ;;
	-f)
	    shift
	    # next arg is the alternate config file name
	    ENVFILE="$1"
	    if [ -z $ENVFILE ]; then
		pr_usage
		exit 1
	    fi
	    shift
	    ;;
	-m)
	    shift
	    # next arg is the mailto name
	    CR_ARGV="$CR_ARGV -m $1"; export CR_ARGV
	    shift
	    ;;
	-r)
	    shift
	    # next arg is the device name
	    CR_ARGV="$CR_ARGV -r $1"; export CR_ARGV
	    shift
	    ;;
	--)
	    shift; break;
	    ;;
	-h)
	    pr_usage
	    exit
	    ;;
	-*)
	    echo "unknown option: $1" >&2
	    pr_usage
	    exit 1
	    ;;
	*)
	    break;
	    ;;
	esac
    done
fi

. $ENVFILE


if [ $# -ge 1 ] ; then
    LIST_OF_GROUPS="$*"; export LIST_OF_GROUPS
elif [ "$LIST_OF_GROUPS" = "" ] ; then
    echo "LIST_OF_GROUPS is empty in $ENVFILE"
    exit 1
fi

if [ ! -d $LOGDIR ] ; then
    mkdir $LOGDIR || (echo "Could not create log directory: $LOGDIR"; exit 1)
fi

for GROUP in $LIST_OF_GROUPS
do

    LOCKFILE=$TMPDIR/.$GROUP.run.lock

    (
	echo starting: `date`
	echo

	if [ -f $LOCKFILE ]
	then
	    echo hourly config diffs failed: $LOCKFILE exists
	    ls -l $LOCKFILE

	    # Send email if the lock file is old.
	    if [ "X$LOCKTIME" = "X" ] ; then
		LOCKTIME=4
	    fi
	    @PERLV@ -e "\$t = (stat(\"$LOCKFILE\"))[9]; print \"OLD\\n\" if (time() - \$t >= $LOCKTIME*60*60);" > $TMPDIR/.$GROUP.old
	    if [ -s $TMPDIR/.$GROUP.old ]
	    then
		(
		  echo "To: @ADMINMAILPLUS@${GROUP}${MAILDOMAIN}"
		  echo "Subject: rancid hung - $GROUP"
		  echo "Precedence: bulk"
		  echo ""

		  cat <<END
rancid $GROUP hung on `hostname`?  Old lockfile still exists:
`ls -l $LOCKFILE`
END
		) | sendmail -t
	    fi
	    rm -f $TMPDIR/.$GROUP.old
	else
	    trap 'rm -fr $LOCKFILE;exit 1' 1 2 3 6 10 15
	    touch $LOCKFILE
	    if [ $? -eq 0 ] ; then
		control_rancid $CR_ARGV $GROUP
		rm -f $LOCKFILE
	    fi
	    trap ''  1 2 3 6 10 15
	fi

	echo
	echo ending: `date`
    ) >$LOGDIR/$GROUP.`date +%Y%m%d.%H%M%S` 2>&1
done