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