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

Last change on this file since 385 was 384, checked in by Kris Deugau, 17 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
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: 2008-06-09 16:23:42 +0000 (Mon, 09 Jun 2008) $
7# SVN revision $Rev: 384 $
8# Last update by $Author: kdeugau $
9###
10
11use DBI;
12use MyIPDB;
13use NetAddr::IP;
14
15print "Content-type: text/plain\n\n";
16
17($dbh,$errstr) = connectDB_My;
18die $errstr if !$dbh;
19# May as well. We need a number of globals.
20initIPDBGlobals($dbh);
21
22print "Checking master containment...\n";
23
24# First check - make sure ALL routes and allocated blocks are part
25# of one of the master blocks.
26print " Checking routed blocks:";
27$sth = $dbh->prepare("select cidr from routed");
28$sth->execute;
29$flag = '';
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 }
35 print "\n $cidr not mastered";
36}
37print "$flag done.\n";
38
39# Next test: All allocations must be part of a master.
40print " Checking allocations:";
41$sth = $dbh->prepare("select cidr from allocations");
42$sth->execute;
43$flag = '';
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 }
49 print "\n $cidr not mastered";
50 $flag = "\n ";
51}
52print "$flag done.\n";
53
54# Next: free blocks
55print " Checking freeblocks:";
56$sth = $dbh->prepare("select cidr from freeblocks order by cidr");
57$sth->execute;
58$flag = '';
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 }
64 print "\n $cidr not mastered";
65 $flag = "\n ";
66}
67print "$flag done.\n";
68
69print "Done checking master containment.\n\n";
70
71print "Checking pool containment...\n";
72
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];
78 print " IP $ip listed with incorrect pool $pool\n"
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;
86 print " Pool $data[0] does not exist in allocations table\n"
87 if (($sth2->fetchrow_array)[0] eq '');
88}
89
90print "Done checking pool containment.\n\n";
91
92print "Checking block-alignment consistency...\n";
93
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) {
108 print " Master $master:\n";
109 $prev = $master;
110 $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
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");
114 $sth->execute;
115
116 while (@data = $sth->fetchrow_array) {
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) {
122 print " Gap from start of master $master to first block $cur\n";
123 } elsif ($cur->numeric < $prev->numeric) {
124 print " BIG problem! Current block $cur begins before master $master!\n";
125 }
126 } else {
127 if ($cur->numeric < ($prev->numeric + 1)) {
128 print " Block ".$prev->network." overlaps block $cur\n";
129 } elsif ($cur->numeric > ($prev->numeric + 1)) {
130 print " Gap between end of block ".$prev->network." and block $cur\n";
131 }
132 }
133
134 $prev = $cur;
135 $prev--;
136
137 } # while (@data...)
138
139 $cur--;
140 $master--;
141 if ($cur->numeric ne $master->numeric) {
142 print " Gap from $cur to end of master at $master\n";
143 }
144 $master++;
145 print " done $master.\n";
146}
147
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 ".
180 "from freeblocks where cidr <<= '$container' and not (routed='y' or routed='n')) ".
181 "order by net");
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 $container++;
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";
220# New check: Make sure "defined" CustIDs are correct.
221$sth = $dbh->prepare("select cidr,type,custid from allocations where not (type='cn' or type like '_r') order by cidr");
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}
227print "Done predefined CustID correctness check.\n\n";
228
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.