summaryrefslogtreecommitdiffstats
path: root/bin/control_rancid
blob: 89ec0df5d85457849497cd505cf1eb74679f18f0 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/sh
##
##
## Copyright (C) 1996 by Henry Kilmer.
## All rights reserved.
##
## This software may be freely copied, modified and redistributed without
## fee for non-commerical purposes provided that this copyright notice is
## preserved intact on all copies and modified copies.
##
## There is no warranty or other guarantee of fitness of this software.
## It is provided solely "as is". The author(s) disclaim(s) all
## responsibility and liability with respect to this software's usage
## or its effect upon hardware, computer systems, other software, or
## anything else.
##
##
#
# control_rancid $GROUP
#

# Number of things par should run in parallel.
PAR_COUNT=5

# Must specify a group to run rancid
if [ $# -lt 1 ]; then
    echo 'must specify group'; exit 1
else
    GROUP=$1
fi
TMP=/var/tmp/rancid.$GROUP.$$
DIR=$BASEDIR/$GROUP

# Bail if we do not have the necessary info to run
if [ ! -d $DIR ]
then
    (
	echo "$DIR does not exist."
    ) | Mail -s "no $GROUP directory" rancid-admin-$GROUP
    exit 1
fi
if [ ! -f $DIR/router.db ]
then
    (
	echo "$DIR/router.db does not exist."
    ) | Mail -s "no $GROUP/router.db file" rancid-admin-$GROUP
    exit 1;
elif [ ! -s $DIR/router.db ]
then
    exit
fi

# generate the list of routers we should try to fetch
cd $DIR
rm -f $DIR/allrouters.new
perl -F: -ane '{($F[0] =~ tr@A-Z@a-z@,print "$F[0]:$F[1]\n")
	if ($F[2] =~ /^up$/i);}' $DIR/router.db | sort -u > $DIR/allrouters.new

if diff $DIR/allrouters $DIR/allrouters.new > $DIR/allrouters.diffs
then
    rm -f $DIR/allrouters.new
else
    (
	echo New routers:
	comm -13 $DIR/allrouters $DIR/allrouters.new | sed -e 's/^/        /' -e 's/:.*$//'
	echo
	echo Deleted routers:
	comm -23 $DIR/allrouters $DIR/allrouters.new | sed -e 's/^/        /' -e 's/:.*$//'
    ) | Mail -s "changes in $GROUP routers" rancid-admin-$GROUP

    cd $DIR/configs

    # Add new routers to the CVS structure.
    for router in `comm -13 $DIR/allrouters $DIR/allrouters.new`
    do
	OFS=$IFS
	IFS=:
	set $router
	IFS=$OFS
	router=$1

	touch $router
	cvs add $router
	cvs commit -m 'new router' $router
	echo "Added $router"
    done
    echo
    cd $DIR

    mv $DIR/allrouters.new $DIR/allrouters
fi
rm -f $DIR/allrouters.diffs $DIR/allrouters.new

# no routers, empty list or all 'down'
if [ ! -s $DIR/allrouters ]
then
	exit;
fi

# Now we can actually try to get the configs
cd $DIR/configs

# The number of processes running at any given time can be
# tailored to the specific installation.
echo "Trying to get all of the configs."
par -q -n $PAR_COUNT -c "rancid-fe \{}" $DIR/allrouters

# This section will generate a list of missed routers
# and try to grab them again.  It will run through
# $pass times.
pass=4
round=1
if [ -f $DIR/allrouters.missed ]; then
    rm -f $DIR/allrouters.missed
fi
while [ $round -le $pass ]
do
    echo "====================================="
    echo "Getting missed routers: round $round."

    for router in `cat $DIR/allrouters`
    do
	OFS=$IFS
	IFS=':'
	set $router
	IFS=$OFS
	router=$1; mfg=$2

	if [ ! -f $DIR/configs/$router.new ]
	then
	    echo "$router:$mfg" >> $DIR/allrouters.missed
	fi
    done

    if [ -f $DIR/allrouters.missed ]; then
	par -q -n $PAR_COUNT -c "rancid-fe \{}" $DIR/allrouters.missed
	rm -f $DIR/allrouters.missed
	round=`expr $round + 1`
    else
	echo "All routers sucessfully completed."
	round=`expr $pass + 1`
    fi
done
echo

# Now that we have the new configs, rename them to their proper
# name.
rename 's/.new$//' *.new

# This has been different for different machines...
# Diff the directory and then checkin.
cd $DIR
#cvs diff -c3 >$TMP.diff
#cvs diff -C 3 >$TMP.diff
cat > $TMP.sedf << EOF
/^RCS file: /d
#/^retrieving revision /d
/^--- /d
/^+++ /d
s/^\([-+ ]\)/\1 /
EOF
cvs -f diff -u -4 | sed -f $TMP.sedf >$TMP.diff
rm -f $TMP.sedf

cvs commit -m updates

# Mail out the diffs (if there are any).
if [ -s $TMP.diff ]; then
    sendmail -t <<EMAIL
To: rancid-$GROUP
Subject: $GROUP router config diffs
Precedence: bulk

`cat $TMP.diff`
EMAIL
fi

# If any machines have not been reached within 24 hours, mail
# out a list of them.
cd $DIR/configs
rm -f $DIR/routers.failed
perl -F: -ane '{$t = (stat($F[0]))[9]; print `ls -ld $F[0]`
	if (time() - $t >= 86400);}' $DIR/allrouters | sort -u > $DIR/routers.failed
if [ -s $DIR/routers.failed ]
then
	(
		cat <<END
The following routers have not been successfully contacted within the
last 24 hours.

END
		cat $DIR/routers.failed
	) | Mail -s "config fetcher problems - $GROUP" rancid-admin-$GROUP
fi

# Cleanup
rm -f $TMP.diff $TMP.lst