[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-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $
|
---|
| 7 | # SVN revision $Rev: 417 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[417] | 10 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
[6] | 11 |
|
---|
| 12 | use DBI;
|
---|
| 13 | use NetAddr::IP;
|
---|
| 14 |
|
---|
[417] | 15 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 16 | ##uselib##
|
---|
| 17 |
|
---|
| 18 | use MyIPDB;
|
---|
| 19 |
|
---|
[214] | 20 | print "Content-type: text/plain\n\n";
|
---|
| 21 |
|
---|
[142] | 22 | ($dbh,$errstr) = connectDB_My;
|
---|
[402] | 23 | die $errstr if !$dbh;
|
---|
[167] | 24 | # May as well. We need a number of globals.
|
---|
| 25 | initIPDBGlobals($dbh);
|
---|
[6] | 26 |
|
---|
[190] | 27 | print "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.
|
---|
[190] | 31 | print " Checking routed blocks:";
|
---|
[6] | 32 | $sth = $dbh->prepare("select cidr from routed");
|
---|
| 33 | $sth->execute;
|
---|
[190] | 34 | $flag = '';
|
---|
[6] | 35 | ROUTED: while (@data = $sth->fetchrow_array) {
|
---|
| 36 | $cidr = new NetAddr::IP $data[0];
|
---|
| 37 | foreach $master (@masterblocks) {
|
---|
| 38 | if ($master->contains($cidr)) { next ROUTED; }
|
---|
| 39 | }
|
---|
[190] | 40 | print "\n $cidr not mastered";
|
---|
[6] | 41 | }
|
---|
[190] | 42 | print "$flag done.\n";
|
---|
[6] | 43 |
|
---|
| 44 | # Next test: All allocations must be part of a master.
|
---|
[190] | 45 | print " Checking allocations:";
|
---|
[6] | 46 | $sth = $dbh->prepare("select cidr from allocations");
|
---|
| 47 | $sth->execute;
|
---|
[190] | 48 | $flag = '';
|
---|
[6] | 49 | ALLOCATED: while (@data = $sth->fetchrow_array) {
|
---|
| 50 | $cidr = new NetAddr::IP $data[0];
|
---|
| 51 | foreach $master (@masterblocks) {
|
---|
| 52 | if ($master->contains($cidr)) { next ALLOCATED; }
|
---|
| 53 | }
|
---|
[190] | 54 | print "\n $cidr not mastered";
|
---|
| 55 | $flag = "\n ";
|
---|
[6] | 56 | }
|
---|
[190] | 57 | print "$flag done.\n";
|
---|
[6] | 58 |
|
---|
| 59 | # Next: free blocks
|
---|
[190] | 60 | print " Checking freeblocks:";
|
---|
[167] | 61 | $sth = $dbh->prepare("select cidr from freeblocks order by cidr");
|
---|
[6] | 62 | $sth->execute;
|
---|
[190] | 63 | $flag = '';
|
---|
[6] | 64 | FREEBLOCK: while (@data = $sth->fetchrow_array) {
|
---|
| 65 | $cidr = new NetAddr::IP $data[0];
|
---|
| 66 | foreach $master (@masterblocks) {
|
---|
| 67 | if ($master->contains($cidr)) { next FREEBLOCK; }
|
---|
| 68 | }
|
---|
[190] | 69 | print "\n $cidr not mastered";
|
---|
| 70 | $flag = "\n ";
|
---|
[6] | 71 | }
|
---|
[190] | 72 | print "$flag done.\n";
|
---|
[6] | 73 |
|
---|
[190] | 74 | print "Done checking master containment.\n\n";
|
---|
[6] | 75 |
|
---|
[190] | 76 | print "Checking pool containment...\n";
|
---|
| 77 |
|
---|
[62] | 78 | $sth = $dbh->prepare("select pool,ip from poolips order by ip");
|
---|
| 79 | $sth->execute;
|
---|
| 80 | while (@data = $sth->fetchrow_array) {
|
---|
| 81 | $pool = new NetAddr::IP $data[0];
|
---|
| 82 | $ip = new NetAddr::IP $data[1];
|
---|
[190] | 83 | print " IP $ip listed with incorrect pool $pool\n"
|
---|
[62] | 84 | if !$pool->contains($ip);
|
---|
| 85 | }
|
---|
| 86 | $sth = $dbh->prepare("select distinct pool from poolips order by pool");
|
---|
| 87 | $sth->execute;
|
---|
| 88 | while (@data = $sth->fetchrow_array) {
|
---|
| 89 | $sth2 = $dbh->prepare("select cidr from allocations where cidr='$data[0]'");
|
---|
| 90 | $sth2->execute;
|
---|
[190] | 91 | print " Pool $data[0] does not exist in allocations table\n"
|
---|
[62] | 92 | if (($sth2->fetchrow_array)[0] eq '');
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[190] | 95 | print "Done checking pool containment.\n\n";
|
---|
[62] | 96 |
|
---|
[190] | 97 | print "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 |
|
---|
| 112 | foreach $master (@masterblocks) {
|
---|
[190] | 113 | print " Master $master:\n";
|
---|
[11] | 114 | $prev = $master;
|
---|
[6] | 115 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
[190] | 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) {
|
---|
[190] | 127 | print " Gap from start of master $master to first block $cur\n";
|
---|
[11] | 128 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
[190] | 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)) {
|
---|
[190] | 133 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
[11] | 134 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
[190] | 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) {
|
---|
[190] | 147 | print " Gap from $cur to end of master at $master\n";
|
---|
[6] | 148 | }
|
---|
[214] | 149 | $master++;
|
---|
[190] | 150 | print " done $master.\n";
|
---|
[6] | 151 | }
|
---|
[167] | 152 |
|
---|
[190] | 153 | print "Done checking block alignment.\n\n";
|
---|
| 154 |
|
---|
| 155 | print "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;
|
---|
| 162 | while (@data = $sth->fetchrow_array) {
|
---|
| 163 | $containers[$i++] = new NetAddr::IP $data[0];
|
---|
| 164 | }
|
---|
| 165 | print " Checking general containment:";
|
---|
| 166 | $sth = $dbh->prepare("select cidr from allocations where type like '_r' order by cidr");
|
---|
| 167 | $sth->execute;
|
---|
| 168 | $flag = '';
|
---|
| 169 | CONTAINED: 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 | }
|
---|
| 177 | print "$flag done.\n";
|
---|
| 178 | print " Checking alignment:\n";
|
---|
| 179 | foreach $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 ".
|
---|
[214] | 185 | "from freeblocks where cidr <<= '$container' and not (routed='y' or routed='n')) ".
|
---|
| 186 | "order by net");
|
---|
[190] | 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 | }
|
---|
[214] | 217 | $container++;
|
---|
[190] | 218 | print " done $container.\n";
|
---|
| 219 |
|
---|
| 220 | }
|
---|
| 221 | print " done container alignment.\n";
|
---|
| 222 | print "Done checking container containment.\n\n";
|
---|
| 223 |
|
---|
| 224 | print "Checking for correctness on 'defined' CustIDs:\n";
|
---|
[167] | 225 | # New check: Make sure "defined" CustIDs are correct.
|
---|
[214] | 226 | $sth = $dbh->prepare("select cidr,type,custid from allocations where not (type='cn' or type like '_r') order by cidr");
|
---|
[167] | 227 | $sth->execute;
|
---|
| 228 | while (@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 | }
|
---|
[352] | 232 | print "Done predefined CustID correctness check.\n\n";
|
---|
[167] | 233 |
|
---|
[352] | 234 | print "Checking for customer blocks with 'bad' CustIDs:\n";
|
---|
| 235 | # Make sure cn-type ("customer netblock") blocks have "real" CustIDs.
|
---|
[417] | 236 | $sth = $dbh->prepare("select cidr,type,custid from allocations where type='cn' and (custid='$IPDB::defcustid' or custid='STAFF') order by cidr");
|
---|
[352] | 237 | $sth->execute;
|
---|
| 238 | while (@data = $sth->fetchrow_array) {
|
---|
| 239 | print "cn block $data[0] has incorrect CustID $data[2]\n";
|
---|
| 240 | }
|
---|
| 241 | print "Done checking customer blocks\n";
|
---|