[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$
|
---|
| 7 | # SVN revision $Rev$
|
---|
| 8 | # Last update by $Author$
|
---|
| 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 |
|
---|
| 67 | print "Done checking master containment.\n\nChecking block-alignment consistency:\n";
|
---|
| 68 |
|
---|
| 69 | # Block alignment consistency: All allocated+free blocks within a master
|
---|
| 70 | # must NOT overlap, and they must show no gaps.
|
---|
| 71 | # eg, if we have blocks:
|
---|
| 72 | # master is 192.168.2.0/24
|
---|
| 73 | # allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
|
---|
| 74 | # free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
|
---|
| 75 | # then we're OK, but if any one of the allocated or free blocks is missing,
|
---|
| 76 | # something b0rked.
|
---|
| 77 |
|
---|
| 78 | # (select cidr from allocations where cidr <<= '$master') union
|
---|
| 79 | # (select cidr from freeblocks where cidr <<= '$master')
|
---|
| 80 | # order by cidr
|
---|
| 81 |
|
---|
| 82 | foreach $master (@masterblocks) {
|
---|
| 83 | print "Master $master:\n";
|
---|
| 84 | ($next,undef) = split /\//, $master;
|
---|
| 85 | ($last,undef) = split /\//, ($master->broadcast);
|
---|
| 86 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
| 87 | " from allocations where cidr <<= '$master') union ".
|
---|
| 88 | "(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
| 89 | "from freeblocks where cidr <<= '$master') order by net");
|
---|
| 90 | $sth->execute;
|
---|
| 91 | while (@data = $sth->fetchrow_array) {
|
---|
| 92 | ($cur,undef) = split /\//, $data[0];
|
---|
| 93 | if ($master !~ /$next/) {
|
---|
| 94 | ($tmp,undef) = split /\//, $next;
|
---|
| 95 | @tmp1 = split /\./, $tmp;
|
---|
| 96 | $tmp1[3]++;
|
---|
| 97 | if ($tmp1[3] == 256) { $tmp1[3] = 0; $tmp1[2]++; }
|
---|
| 98 | $cmp = join '.', @tmp1;
|
---|
| 99 | } else {
|
---|
| 100 | $cmp = $next;
|
---|
| 101 | }
|
---|
| 102 | if ($cur ne $cmp) {
|
---|
| 103 | print " Gap from $next to $cur\n";
|
---|
| 104 | }
|
---|
| 105 | ($next,undef) = split /\//, $data[1];
|
---|
| 106 | }
|
---|
| 107 | if ($next ne $last) {
|
---|
| 108 | print " Gap from $next to end of master at $last\n";
|
---|
| 109 | }
|
---|
| 110 | print "done $master.\n";
|
---|
| 111 | }
|
---|