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