source: trunk/cgi-bin/consistency-check.pl@ 702

Last change on this file since 702 was 635, checked in by Kris Deugau, 10 years ago

/trunk

Fix up consistency-check.pl; it was so stale it broke on the stable branch

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