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