[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: 2004-11-12 17:37:17 +0000 (Fri, 12 Nov 2004) $
|
---|
| 7 | # SVN revision $Rev: 64 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[6] | 10 |
|
---|
| 11 | use DBI;
|
---|
| 12 | use IPDB qw(:ALL);
|
---|
| 13 | use NetAddr::IP;
|
---|
| 14 |
|
---|
| 15 | $dbh = connectDB;
|
---|
| 16 |
|
---|
| 17 | # Schlep up the masters
|
---|
| 18 | $sth = $dbh->prepare("select * from masterblocks order by cidr");
|
---|
| 19 | $sth->execute;
|
---|
| 20 | for ($i=0; @data = $sth->fetchrow_array; $i++) {
|
---|
| 21 | $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | print "First check: All blocks must be within one of the master blocks\n";
|
---|
| 25 |
|
---|
| 26 | # First check - make sure ALL routes and allocated blocks are part
|
---|
| 27 | # of one of the master blocks.
|
---|
| 28 | print "Checking routed blocks: ";
|
---|
| 29 | $sth = $dbh->prepare("select cidr from routed");
|
---|
| 30 | # union select cidr from allocations union select cidr from freeblocks");
|
---|
| 31 | $sth->execute;
|
---|
| 32 | ROUTED: while (@data = $sth->fetchrow_array) {
|
---|
| 33 | $cidr = new NetAddr::IP $data[0];
|
---|
| 34 | foreach $master (@masterblocks) {
|
---|
| 35 | if ($master->contains($cidr)) { next ROUTED; }
|
---|
| 36 | }
|
---|
| 37 | print "$cidr not mastered\n";
|
---|
| 38 | }
|
---|
| 39 | print " done.\n";
|
---|
| 40 |
|
---|
| 41 | # Next test: All allocations must be part of a master.
|
---|
| 42 | print "Checking allocations: ";
|
---|
| 43 | $sth = $dbh->prepare("select cidr from allocations");
|
---|
| 44 | $sth->execute;
|
---|
| 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 | }
|
---|
| 50 | print "$cidr not mastered\n";
|
---|
| 51 | }
|
---|
| 52 | print " done.\n";
|
---|
| 53 |
|
---|
| 54 | # Next: free blocks
|
---|
| 55 | print "Checking freeblocks: ";
|
---|
| 56 | $sth = $dbh->prepare("select cidr from freeblocks");
|
---|
| 57 | $sth->execute;
|
---|
| 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 | }
|
---|
| 63 | print "$cidr not mastered\n";
|
---|
| 64 | }
|
---|
| 65 | print " done.\n";
|
---|
| 66 |
|
---|
[64] | 67 | print "Done checking master containment.\n\nChecking pool containment:\n";
|
---|
[6] | 68 |
|
---|
[64] | 69 | $sth = $dbh->prepare("select pool,ip from poolips order by ip");
|
---|
| 70 | $sth->execute;
|
---|
| 71 | while (@data = $sth->fetchrow_array) {
|
---|
| 72 | $pool = new NetAddr::IP $data[0];
|
---|
| 73 | $ip = new NetAddr::IP $data[1];
|
---|
| 74 | print "IP $ip listed with incorrect pool $pool\n"
|
---|
| 75 | if !$pool->contains($ip);
|
---|
| 76 | }
|
---|
| 77 | $sth = $dbh->prepare("select distinct pool from poolips order by pool");
|
---|
| 78 | $sth->execute;
|
---|
| 79 | while (@data = $sth->fetchrow_array) {
|
---|
| 80 | $sth2 = $dbh->prepare("select cidr from allocations where cidr='$data[0]'");
|
---|
| 81 | $sth2->execute;
|
---|
| 82 | print "Pool $data[0] does not exist in allocations table\n"
|
---|
| 83 | if (($sth2->fetchrow_array)[0] eq '');
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | print "Done checking pool containment.\n\nChecking block-alignment consistency:\n";
|
---|
| 87 |
|
---|
[6] | 88 | # Block alignment consistency: All allocated+free blocks within a master
|
---|
| 89 | # must NOT overlap, and they must show no gaps.
|
---|
| 90 | # eg, if we have blocks:
|
---|
| 91 | # master is 192.168.2.0/24
|
---|
| 92 | # allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
|
---|
| 93 | # free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
|
---|
| 94 | # then we're OK, but if any one of the allocated or free blocks is missing,
|
---|
| 95 | # something b0rked.
|
---|
| 96 |
|
---|
| 97 | # (select cidr from allocations where cidr <<= '$master') union
|
---|
| 98 | # (select cidr from freeblocks where cidr <<= '$master')
|
---|
| 99 | # order by cidr
|
---|
| 100 |
|
---|
| 101 | foreach $master (@masterblocks) {
|
---|
| 102 | print "Master $master:\n";
|
---|
[11] | 103 | $prev = $master;
|
---|
[6] | 104 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
| 105 | " from allocations where cidr <<= '$master') union ".
|
---|
| 106 | "(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
| 107 | "from freeblocks where cidr <<= '$master') order by net");
|
---|
| 108 | $sth->execute;
|
---|
[11] | 109 |
|
---|
[6] | 110 | while (@data = $sth->fetchrow_array) {
|
---|
[11] | 111 | $cur = new NetAddr::IP $data[0];
|
---|
| 112 |
|
---|
| 113 | if ($master->numeric == $prev->numeric) {
|
---|
| 114 | # check if cur starts with master
|
---|
| 115 | if ($cur->numeric > $prev->numeric) {
|
---|
| 116 | print " Gap from start of master $master to first block $cur\n";
|
---|
| 117 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
| 118 | print " BIG problem! Current block $cur begins before master $master!\n";
|
---|
| 119 | }
|
---|
[6] | 120 | } else {
|
---|
[11] | 121 | if ($cur->numeric < ($prev->numeric + 1)) {
|
---|
| 122 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
| 123 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
| 124 | print " Gap between end of block ".$prev->network." and block $cur\n";
|
---|
| 125 | }
|
---|
[6] | 126 | }
|
---|
[11] | 127 |
|
---|
| 128 | $prev = $cur;
|
---|
| 129 | $prev--;
|
---|
| 130 |
|
---|
| 131 | } # while (@data...)
|
---|
| 132 |
|
---|
| 133 | $cur--;
|
---|
| 134 | $master--;
|
---|
| 135 | if ($cur->numeric ne $master->numeric) {
|
---|
| 136 | print " Gap from $cur to end of master at $master\n";
|
---|
[6] | 137 | }
|
---|
| 138 | print "done $master.\n";
|
---|
| 139 | }
|
---|