source: trunk/cgi-bin/consistency-check.pl@ 893

Last change on this file since 893 was 635, checked in by Kris Deugau, 10 years ago

/trunk

Fix up consistency-check.pl; it was so stale it broke on the stable branch

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