1#!/bin/sh
2
3#
4# Analyze a rancid configuration file, and extracts equipment names
5# and types. Then, from each equipment configuration file, extract
6# then exact model.
7#
8# Syntax :
9#	list-rancid <dbfile> <confdir>	> <output-file>
10#
11# History :
12#   2004/06/08 : pda/jean : design
13#   2008/07/07 : pda      : add hp
14#   2008/12/15 : jean     : add pattern of types to ignore
15#   2010/09/27 : pda      : case where a file does not exist yet
16#
17
18if [ $# != 2 ]
19then
20    echo "usage: $0 <dbfile> <confdir>" >&2
21    exit 1
22fi
23
24DB="$1"
25DIR="$2"
26
27IGNORE="^[a-zA-Z0-9][-a-zA-Z0-9.]*:wrapper\."
28
29fetch_model_cisco ()
30{
31    sed -n "/^!Chassis type:/s/!Chassis type: \([^ ]*\).*/\1/p" "$1"|head -n1
32}
33
34fetch_model_hp ()
35{
36    sed -n "/^;Chassis type:/s/;Chassis type: \([^ ]*\).*/\1/p" "$1"|head -n1
37}
38
39fetch_model_juniper ()
40{
41    sed -n "/^# Chassis/s/.* \([^ ]*\)$/\1/p" "$1"|head -n1
42}
43
44fetch_model_server ()
45{
46    sed -n "/^! uname: \([^ ]*\)$/s//\1/p" "$1"|head -n1
47}
48
49IFS=":;"
50grep "[:;]up$" $DB | egrep -v "$IGNORE" |
51    while read eq type up
52    do
53	f="$DIR/$eq"
54	if [ ! -s "$f" ]
55	then
56	    echo "Missing or empty configuration file for '$eq'" >&2
57	else
58	    case "$type" in
59		cisco)
60		    model=`fetch_model_cisco "$DIR/$eq"`
61		    ;;
62		hp)
63		    model=`fetch_model_hp "$DIR/$eq"`
64		    ;;
65		juniper)
66		    model=`fetch_model_juniper "$DIR/$eq"`
67		    ;;
68		server)
69		    model=`fetch_model_server "$DIR/$eq"`
70		    ;;
71		*)
72		    echo "Unsupported type '$type' for '$eq' in $DB" >&2
73		    exit 1
74		    ;;
75	    esac
76	    echo "$eq $type $model"
77	fi
78    done
79