source: branches/dns/cgi-bin/consistency-check.pl@ 823

Last change on this file since 823 was 214, checked in by Kris Deugau, 20 years ago

/trunk

Port bugfixes from /branches/stable r203-206 and r209-213 forward.
Clean merge.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 7.1 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/consistency-check.pl
3# Does full check to see if the data in the db is consistent and complete.
4###
5# SVN revision info
6# $Date: 2005-04-05 18:24:52 +0000 (Tue, 05 Apr 2005) $
7# SVN revision $Rev: 214 $
8# Last update by $Author: kdeugau $
9###
10# Copyright (C) 2004 - Kris Deugau
11
12use DBI;
13use MyIPDB;
14use NetAddr::IP;
15
16print "Content-type: text/plain\n\n";
17
18($dbh,$errstr) = connectDB_My;
19
20# May as well. We need a number of globals.
21initIPDBGlobals($dbh);
22
23print "Checking master containment...\n";
24
25# First check - make sure ALL routes and allocated blocks are part
26# of one of the master blocks.
27print " Checking routed blocks:";
28$sth = $dbh->prepare("select cidr from routed");
29$sth->execute;
30$flag = '';
31ROUTED: while (@data = $sth->fetchrow_array) {
32 $cidr = new NetAddr::IP $data[0];
33 foreach $master (@masterblocks) {
34 if ($master->contains($cidr)) { next ROUTED; }
35 }
36 print "\n $cidr not mastered";
37}
38print "$flag done.\n";
39
40# Next test: All allocations must be part of a master.
41print " Checking allocations:";
42$sth = $dbh->prepare("select cidr from allocations");
43$sth->execute;
44$flag = '';
45ALLOCATED: while (@data = $sth->fetchrow_array) {
46 $cidr = new NetAddr::IP $data[0];
47 foreach $master (@masterblocks) {
48 if ($master->contains($cidr)) { next ALLOCATED; }
49 }
50 print "\n $cidr not mastered";
51 $flag = "\n ";
52}
53print "$flag done.\n";
54
55# Next: free blocks
56print " Checking freeblocks:";
57$sth = $dbh->prepare("select cidr from freeblocks order by cidr");
58$sth->execute;
59$flag = '';
60FREEBLOCK: while (@data = $sth->fetchrow_array) {
61 $cidr = new NetAddr::IP $data[0];
62 foreach $master (@masterblocks) {
63 if ($master->contains($cidr)) { next FREEBLOCK; }
64 }
65 print "\n $cidr not mastered";
66 $flag = "\n ";
67}
68print "$flag done.\n";
69
70print "Done checking master containment.\n\n";
71
72print "Checking pool containment...\n";
73
74$sth = $dbh->prepare("select pool,ip from poolips order by ip");
75$sth->execute;
76while (@data = $sth->fetchrow_array) {
77 $pool = new NetAddr::IP $data[0];
78 $ip = new NetAddr::IP $data[1];
79 print " IP $ip listed with incorrect pool $pool\n"
80 if !$pool->contains($ip);
81}
82$sth = $dbh->prepare("select distinct pool from poolips order by pool");
83$sth->execute;
84while (@data = $sth->fetchrow_array) {
85 $sth2 = $dbh->prepare("select cidr from allocations where cidr='$data[0]'");
86 $sth2->execute;
87 print " Pool $data[0] does not exist in allocations table\n"
88 if (($sth2->fetchrow_array)[0] eq '');
89}
90
91print "Done checking pool containment.\n\n";
92
93print "Checking block-alignment consistency...\n";
94
95# Block alignment consistency: All allocated+free blocks within a master
96# must NOT overlap, and they must show no gaps.
97# eg, if we have blocks:
98# master is 192.168.2.0/24
99# allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
100# free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
101# then we're OK, but if any one of the allocated or free blocks is missing,
102# something b0rked.
103
104# (select cidr from allocations where cidr <<= '$master') union
105# (select cidr from freeblocks where cidr <<= '$master')
106# order by cidr
107
108foreach $master (@masterblocks) {
109 print " Master $master:\n";
110 $prev = $master;
111 $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
112 "from allocations where cidr <<= '$master' and type not like '_c') ".
113 "union (select network(cidr) as net, broadcast(cidr) as bcast ".
114 "from freeblocks where cidr <<= '$master' and not (routed='c')) order by net");
115 $sth->execute;
116
117 while (@data = $sth->fetchrow_array) {
118 $cur = new NetAddr::IP $data[0];
119
120 if ($master->numeric == $prev->numeric) {
121 # check if cur starts with master
122 if ($cur->numeric > $prev->numeric) {
123 print " Gap from start of master $master to first block $cur\n";
124 } elsif ($cur->numeric < $prev->numeric) {
125 print " BIG problem! Current block $cur begins before master $master!\n";
126 }
127 } else {
128 if ($cur->numeric < ($prev->numeric + 1)) {
129 print " Block ".$prev->network." overlaps block $cur\n";
130 } elsif ($cur->numeric > ($prev->numeric + 1)) {
131 print " Gap between end of block ".$prev->network." and block $cur\n";
132 }
133 }
134
135 $prev = $cur;
136 $prev--;
137
138 } # while (@data...)
139
140 $cur--;
141 $master--;
142 if ($cur->numeric ne $master->numeric) {
143 print " Gap from $cur to end of master at $master\n";
144 }
145 $master++;
146 print " done $master.\n";
147}
148
149print "Done checking block alignment.\n\n";
150
151print "Checking containment on container blocks...\n";
152# First, we need a list of containers.
153# Then, we check all of the contained blocks to see if they're within
154# the proper container.
155$sth = $dbh->prepare("select cidr from allocations where type like '_c' order by cidr");
156$sth->execute;
157$i=0;
158while (@data = $sth->fetchrow_array) {
159 $containers[$i++] = new NetAddr::IP $data[0];
160}
161print " Checking general containment:";
162$sth = $dbh->prepare("select cidr from allocations where type like '_r' order by cidr");
163$sth->execute;
164$flag = '';
165CONTAINED: while (@data = $sth->fetchrow_array) {
166 $cidr = new NetAddr::IP $data[0];
167 foreach $container (@containers) {
168 next CONTAINED if $container->contains($cidr);
169 }
170 print "\n $cidr not contained";
171 $flag = "\n ";
172}
173print "$flag done.\n";
174print " Checking alignment:\n";
175foreach $container (@containers) {
176 print " Container $container:\n";
177 $prev = $container;
178 $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
179 "from allocations where cidr <<= '$container' and type like '_r') ".
180 "union (select network(cidr) as net, broadcast(cidr) as bcast ".
181 "from freeblocks where cidr <<= '$container' and not (routed='y' or routed='n')) ".
182 "order by net");
183 $sth->execute;
184
185 while (@data = $sth->fetchrow_array) {
186 $cur = new NetAddr::IP $data[0];
187
188 if ($container->numeric == $prev->numeric) {
189 # check if cur starts with master
190 if ($cur->numeric > $prev->numeric) {
191 print " Gap from start of container $container to first block $cur\n";
192 } elsif ($cur->numeric < $prev->numeric) {
193 print " BIG problem! Current block $cur begins before container $container!\n";
194 }
195 } else {
196 if ($cur->numeric < ($prev->numeric + 1)) {
197 print " Block ".$prev->network." overlaps block $cur\n";
198 } elsif ($cur->numeric > ($prev->numeric + 1)) {
199 print " Gap between end of block ".$prev->network." and block $cur\n";
200 }
201 }
202
203 $prev = $cur;
204 $prev--;
205
206 } # while (@data...)
207
208 $cur--;
209 $container--;
210 if ($cur->numeric ne $container->numeric) {
211 print " Gap from $cur to end of container at $container\n";
212 }
213 $container++;
214 print " done $container.\n";
215
216}
217print " done container alignment.\n";
218print "Done checking container containment.\n\n";
219
220print "Checking for correctness on 'defined' CustIDs:\n";
221# New check: Make sure "defined" CustIDs are correct.
222$sth = $dbh->prepare("select cidr,type,custid from allocations where not (type='cn' or type like '_r') order by cidr");
223$sth->execute;
224while (@data = $sth->fetchrow_array) {
225 print "$data[0] ($disp_alloctypes{$data[1]}) has incorrect CustID $data[2]\n"
226 if $data[2] ne $def_custids{$data[1]};
227}
228
229print "Done CustID correctness check.\n";
Note: See TracBrowser for help on using the repository browser.