forget about device fd, we don't need it
[arla.git] / conf / check-cellservdb.pl
blobe01232a89a0cb5d3fbeda602b67cd41f8e9b53ac
1 #!/usr/pkg/bin/perl -w
2 # $Id$
4 # Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
5 # (Royal Institute of Technology, Stockholm, Sweden).
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of the Institute nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # SUCH DAMAGE.
36 # You can install Net::DNS with ``perl -MCPAN -e install Net::DNS;''
39 use strict;
40 use Net::DNS;
41 use Getopt::Long;
43 my $cell;
44 my $found;
45 my %db;
46 my %comments;
48 sub print_cell
50 my $cell = shift;
51 my $comment = shift || "";
53 print ">$cell \#$comment\n";
56 sub query_local_cell
58 my $cell = shift;
60 print "local cellservdb\n";
61 print_cell($cell,$comments{$cell});
62 my $hostlist = $db{$cell};
63 print foreach (@$hostlist);
66 sub query_remote_cell
68 my $cell = shift;
69 my $hostlist = $db{$cell};
71 print "query remote host\n";
72 my $host;
73 foreach (@$hostlist) {
74 if (/^([^ \t\n]+)/) {
75 system "bos listhost -server $1 -db -comment \"$comments{$cell}\" -noauth";
76 last if ($? == 0);
81 sub query_afsdb
83 my $cell = shift;
84 my $comment = $comments{$cell};
85 my $res;
86 my $query;
87 my $rr;
88 my @hosts = ();
89 my $host;
91 $res = new Net::DNS::Resolver;
92 $query = $res->search($cell, "AFSDB");
94 if ($query) {
95 foreach $rr ($query->answer) {
96 next unless $rr->type eq "AFSDB" and $rr->subtype == 1;
97 push @hosts, $rr->hostname;
100 if ($#hosts > 0) {
101 printf ("query dns\n");
102 print_cell($cell, $comment);
104 foreach $host (@hosts) {
105 $query = $res->search($host, "A");
106 if ($query) {
107 foreach $rr ($query->answer) {
108 next unless $rr->type eq "A";
109 print $rr->address, " \#$host\n";
115 sub parse_cellservdb
117 my $cellservdb = shift || "/usr/arla/etc/CellServDB";
118 my @hosts;
119 my $FILE;
121 open FILE, "<$cellservdb";
123 while (<FILE>) {
124 $found = 0;
125 if (/^>([^ \t]*)[\t ]*#(.*)/) {
126 $cell = $1;
127 $comments{$cell} = $2;
128 $found = 1;
129 } elsif (/^>([^ \t]*)/) {
130 $cell = $1;
131 $comments{$cell} = "";
132 $found = 1;
134 if (!$found) {
135 push @hosts, $_;
136 my @hostcopy = @hosts;
137 $db{$cell} = \@hostcopy;
138 } else {
139 while (defined(pop @hosts)) {}
142 close FILE;
145 sub usage
147 print "check-cellservdb.pl";
148 print "\t--help\n";
149 print "\t--cell <cellname>\n";
150 print "\t--CellServDB <file>\n";
151 print "\t--nodns\n";
152 print "\t--nolocal\n";
153 print "\t--noremote\n";
154 print "\t--all\n";
155 exit 1;
158 my $cell = 0;
159 my $all = 0;
160 my $nodns = 0;
161 my $noremote = 0;
162 my $nolocal = 0;
163 my $cellservdb;
165 GetOptions("help" => \&usag,
166 "cell=s" => \$cell,
167 "CellServDB=s" => \$cellservdb,
168 "nodns" => \$nodns,
169 "noremote" => \$noremote,
170 "nolocal" => \$nolocal,
171 "all" => \$all);
174 parse_cellservdb($cellservdb);
176 if ($all) {
177 my $query_cell;
178 foreach $query_cell (sort { reverse($a) cmp reverse($b) } keys %db) {
180 query_local_cell($query_cell) if(!$nolocal);
181 query_remote_cell($query_cell) if(!$noremote);
182 query_afsdb($query_cell) if (!$nodns);
184 } elsif ($cell) {
185 query_local_cell($cell) if(!$nolocal);
186 query_remote_cell($cell) if(!$noremote);
187 query_afsdb($cell) if (!$nodns);
188 } else {
189 usage();