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