source: branches/stable/cgi-bin/consistency-check.pl@ 427

Last change on this file since 427 was 384, checked in by Kris Deugau, 16 years ago

/branches/stable

Die before trying to process anything if consistency-check.pl can't
connect to the database.

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