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-03-10 19:15:19 +0000 (Thu, 10 Mar 2005) $
|
---|
7 | # SVN revision $Rev: 192 $
|
---|
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 "Checking master containment...\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 | $sth->execute;
|
---|
26 | $flag = '';
|
---|
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 "\n $cidr not mastered";
|
---|
33 | }
|
---|
34 | print "$flag 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 | $flag = '';
|
---|
41 | ALLOCATED: while (@data = $sth->fetchrow_array) {
|
---|
42 | $cidr = new NetAddr::IP $data[0];
|
---|
43 | foreach $master (@masterblocks) {
|
---|
44 | if ($master->contains($cidr)) { next ALLOCATED; }
|
---|
45 | }
|
---|
46 | print "\n $cidr not mastered";
|
---|
47 | $flag = "\n ";
|
---|
48 | }
|
---|
49 | print "$flag done.\n";
|
---|
50 |
|
---|
51 | # Next: free blocks
|
---|
52 | print " Checking freeblocks:";
|
---|
53 | $sth = $dbh->prepare("select cidr from freeblocks order by cidr");
|
---|
54 | $sth->execute;
|
---|
55 | $flag = '';
|
---|
56 | FREEBLOCK: while (@data = $sth->fetchrow_array) {
|
---|
57 | $cidr = new NetAddr::IP $data[0];
|
---|
58 | foreach $master (@masterblocks) {
|
---|
59 | if ($master->contains($cidr)) { next FREEBLOCK; }
|
---|
60 | }
|
---|
61 | print "\n $cidr not mastered";
|
---|
62 | $flag = "\n ";
|
---|
63 | }
|
---|
64 | print "$flag done.\n";
|
---|
65 |
|
---|
66 | print "Done checking master containment.\n\n";
|
---|
67 |
|
---|
68 | print "Checking pool containment...\n";
|
---|
69 |
|
---|
70 | $sth = $dbh->prepare("select pool,ip from poolips order by ip");
|
---|
71 | $sth->execute;
|
---|
72 | while (@data = $sth->fetchrow_array) {
|
---|
73 | $pool = new NetAddr::IP $data[0];
|
---|
74 | $ip = new NetAddr::IP $data[1];
|
---|
75 | print " IP $ip listed with incorrect pool $pool\n"
|
---|
76 | if !$pool->contains($ip);
|
---|
77 | }
|
---|
78 | $sth = $dbh->prepare("select distinct pool from poolips order by pool");
|
---|
79 | $sth->execute;
|
---|
80 | while (@data = $sth->fetchrow_array) {
|
---|
81 | $sth2 = $dbh->prepare("select cidr from allocations where cidr='$data[0]'");
|
---|
82 | $sth2->execute;
|
---|
83 | print " Pool $data[0] does not exist in allocations table\n"
|
---|
84 | if (($sth2->fetchrow_array)[0] eq '');
|
---|
85 | }
|
---|
86 |
|
---|
87 | print "Done checking pool containment.\n\n";
|
---|
88 |
|
---|
89 | print "Checking block-alignment consistency...\n";
|
---|
90 |
|
---|
91 | # Block alignment consistency: All allocated+free blocks within a master
|
---|
92 | # must NOT overlap, and they must show no gaps.
|
---|
93 | # eg, if we have blocks:
|
---|
94 | # master is 192.168.2.0/24
|
---|
95 | # allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
|
---|
96 | # free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
|
---|
97 | # then we're OK, but if any one of the allocated or free blocks is missing,
|
---|
98 | # something b0rked.
|
---|
99 |
|
---|
100 | # (select cidr from allocations where cidr <<= '$master') union
|
---|
101 | # (select cidr from freeblocks where cidr <<= '$master')
|
---|
102 | # order by cidr
|
---|
103 |
|
---|
104 | foreach $master (@masterblocks) {
|
---|
105 | print " Master $master:\n";
|
---|
106 | $prev = $master;
|
---|
107 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
108 | "from allocations where cidr <<= '$master' and type not like '_c') ".
|
---|
109 | "union (select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
110 | "from freeblocks where cidr <<= '$master' and not (routed='c')) order by net");
|
---|
111 | $sth->execute;
|
---|
112 |
|
---|
113 | while (@data = $sth->fetchrow_array) {
|
---|
114 | $cur = new NetAddr::IP $data[0];
|
---|
115 |
|
---|
116 | if ($master->numeric == $prev->numeric) {
|
---|
117 | # check if cur starts with master
|
---|
118 | if ($cur->numeric > $prev->numeric) {
|
---|
119 | print " Gap from start of master $master to first block $cur\n";
|
---|
120 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
121 | print " BIG problem! Current block $cur begins before master $master!\n";
|
---|
122 | }
|
---|
123 | } else {
|
---|
124 | if ($cur->numeric < ($prev->numeric + 1)) {
|
---|
125 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
126 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
127 | print " Gap between end of block ".$prev->network." and block $cur\n";
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | $prev = $cur;
|
---|
132 | $prev--;
|
---|
133 |
|
---|
134 | } # while (@data...)
|
---|
135 |
|
---|
136 | $cur--;
|
---|
137 | $master--;
|
---|
138 | if ($cur->numeric ne $master->numeric) {
|
---|
139 | print " Gap from $cur to end of master at $master\n";
|
---|
140 | }
|
---|
141 | print " done $master.\n";
|
---|
142 | }
|
---|
143 |
|
---|
144 | print "Done checking block alignment.\n\n";
|
---|
145 |
|
---|
146 | print "Checking containment on container blocks...\n";
|
---|
147 | # First, we need a list of containers.
|
---|
148 | # Then, we check all of the contained blocks to see if they're within
|
---|
149 | # the proper container.
|
---|
150 | $sth = $dbh->prepare("select cidr from allocations where type like '_c' order by cidr");
|
---|
151 | $sth->execute;
|
---|
152 | $i=0;
|
---|
153 | while (@data = $sth->fetchrow_array) {
|
---|
154 | $containers[$i++] = new NetAddr::IP $data[0];
|
---|
155 | }
|
---|
156 | print " Checking general containment:";
|
---|
157 | $sth = $dbh->prepare("select cidr from allocations where type like '_r' order by cidr");
|
---|
158 | $sth->execute;
|
---|
159 | $flag = '';
|
---|
160 | CONTAINED: while (@data = $sth->fetchrow_array) {
|
---|
161 | $cidr = new NetAddr::IP $data[0];
|
---|
162 | foreach $container (@containers) {
|
---|
163 | next CONTAINED if $container->contains($cidr);
|
---|
164 | }
|
---|
165 | print "\n $cidr not contained";
|
---|
166 | $flag = "\n ";
|
---|
167 | }
|
---|
168 | print "$flag done.\n";
|
---|
169 | print " Checking alignment:\n";
|
---|
170 | foreach $container (@containers) {
|
---|
171 | print " Container $container:\n";
|
---|
172 | $prev = $container;
|
---|
173 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
174 | "from allocations where cidr <<= '$container' and type like '_r') ".
|
---|
175 | "union (select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
176 | "from freeblocks where cidr <<= '$container' and routed='w') order by net");
|
---|
177 | $sth->execute;
|
---|
178 |
|
---|
179 | while (@data = $sth->fetchrow_array) {
|
---|
180 | $cur = new NetAddr::IP $data[0];
|
---|
181 |
|
---|
182 | if ($container->numeric == $prev->numeric) {
|
---|
183 | # check if cur starts with master
|
---|
184 | if ($cur->numeric > $prev->numeric) {
|
---|
185 | print " Gap from start of container $container to first block $cur\n";
|
---|
186 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
187 | print " BIG problem! Current block $cur begins before container $container!\n";
|
---|
188 | }
|
---|
189 | } else {
|
---|
190 | if ($cur->numeric < ($prev->numeric + 1)) {
|
---|
191 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
192 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
193 | print " Gap between end of block ".$prev->network." and block $cur\n";
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | $prev = $cur;
|
---|
198 | $prev--;
|
---|
199 |
|
---|
200 | } # while (@data...)
|
---|
201 |
|
---|
202 | $cur--;
|
---|
203 | $container--;
|
---|
204 | if ($cur->numeric ne $container->numeric) {
|
---|
205 | print " Gap from $cur to end of container at $container\n";
|
---|
206 | }
|
---|
207 | print " done $container.\n";
|
---|
208 |
|
---|
209 | }
|
---|
210 | print " done container alignment.\n";
|
---|
211 | print "Done checking container containment.\n\n";
|
---|
212 |
|
---|
213 | print "Checking for correctness on 'defined' CustIDs:\n";
|
---|
214 | # New check: Make sure "defined" CustIDs are correct.
|
---|
215 | $sth = $dbh->prepare("select cidr,type,custid from allocations where not type='cn' order by cidr");
|
---|
216 | $sth->execute;
|
---|
217 | while (@data = $sth->fetchrow_array) {
|
---|
218 | print "$data[0] ($disp_alloctypes{$data[1]}) has incorrect CustID $data[2]\n"
|
---|
219 | if $data[2] ne $def_custids{$data[1]};
|
---|
220 | }
|
---|
221 |
|
---|
222 | print "Done CustID correctness check.\n";
|
---|