1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/consistency-check.pl
|
---|
3 | # Does full check to see if the data in the db is consistent and complete.
|
---|
4 | ###
|
---|
5 | # SVN revision info
|
---|
6 | # $Date: 2004-10-26 17:03:16 +0000 (Tue, 26 Oct 2004) $
|
---|
7 | # SVN revision $Rev: 26 $
|
---|
8 | # Last update by $Author: kdeugau $
|
---|
9 | ###
|
---|
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 | $prev = $master;
|
---|
85 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
86 | " from allocations where cidr <<= '$master') union ".
|
---|
87 | "(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
88 | "from freeblocks where cidr <<= '$master') order by net");
|
---|
89 | $sth->execute;
|
---|
90 |
|
---|
91 | while (@data = $sth->fetchrow_array) {
|
---|
92 | $cur = new NetAddr::IP $data[0];
|
---|
93 |
|
---|
94 | if ($master->numeric == $prev->numeric) {
|
---|
95 | # check if cur starts with master
|
---|
96 | if ($cur->numeric > $prev->numeric) {
|
---|
97 | print " Gap from start of master $master to first block $cur\n";
|
---|
98 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
99 | print " BIG problem! Current block $cur begins before master $master!\n";
|
---|
100 | }
|
---|
101 | } else {
|
---|
102 | if ($cur->numeric < ($prev->numeric + 1)) {
|
---|
103 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
104 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
105 | print " Gap between end of block ".$prev->network." and block $cur\n";
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | $prev = $cur;
|
---|
110 | $prev--;
|
---|
111 |
|
---|
112 | } # while (@data...)
|
---|
113 |
|
---|
114 | $cur--;
|
---|
115 | $master--;
|
---|
116 | if ($cur->numeric ne $master->numeric) {
|
---|
117 | print " Gap from $cur to end of master at $master\n";
|
---|
118 | }
|
---|
119 | print "done $master.\n";
|
---|
120 | }
|
---|