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

Last change on this file since 613 was 445, checked in by Kris Deugau, 14 years ago

/branches/stable

Bring /branches/stable up to date with /trunk. See #13.

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