source: branches/stable/cgi-bin/consistency-check.pl@ 463

Last change on this file since 463 was 445, checked in by Kris Deugau, 14 years ago

/branches/stable

Bring /branches/stable up to date with /trunk. See #13.

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