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