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

Last change on this file since 8 was 8, checked in by Kris Deugau, 20 years ago

Added SVN revision info section to cgi-bin/CommonWeb.pm,
cgi-bin/main.cgi, cgi-bin/consistency-check.pl, and
cgi-bin/IPDB.pm

  • Property svn:executable set to *
File size: 3.3 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$
7# SVN revision $Rev$
8# Last update by $Author$
9###
10
11use DBI;
12use IPDB qw(:ALL);
13use NetAddr::IP;
14
15$dbh = connectDB;
16
17# Schlep up the masters
18$sth = $dbh->prepare("select * from masterblocks order by cidr");
19$sth->execute;
20for ($i=0; @data = $sth->fetchrow_array; $i++) {
21 $masterblocks[$i] = new NetAddr::IP $data[0];
22}
23
24print "First check: All blocks must be within one of the master blocks\n";
25
26# First check - make sure ALL routes and allocated blocks are part
27# of one of the master blocks.
28print "Checking routed blocks: ";
29$sth = $dbh->prepare("select cidr from routed");
30# union select cidr from allocations union select cidr from freeblocks");
31$sth->execute;
32ROUTED: while (@data = $sth->fetchrow_array) {
33 $cidr = new NetAddr::IP $data[0];
34 foreach $master (@masterblocks) {
35 if ($master->contains($cidr)) { next ROUTED; }
36 }
37 print "$cidr not mastered\n";
38}
39print " done.\n";
40
41# Next test: All allocations must be part of a master.
42print "Checking allocations: ";
43$sth = $dbh->prepare("select cidr from allocations");
44$sth->execute;
45ALLOCATED: 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 "$cidr not mastered\n";
51}
52print " done.\n";
53
54# Next: free blocks
55print "Checking freeblocks: ";
56$sth = $dbh->prepare("select cidr from freeblocks");
57$sth->execute;
58FREEBLOCK: while (@data = $sth->fetchrow_array) {
59 $cidr = new NetAddr::IP $data[0];
60 foreach $master (@masterblocks) {
61 if ($master->contains($cidr)) { next FREEBLOCK; }
62 }
63 print "$cidr not mastered\n";
64}
65print " done.\n";
66
67print "Done checking master containment.\n\nChecking block-alignment consistency:\n";
68
69# Block alignment consistency: All allocated+free blocks within a master
70# must NOT overlap, and they must show no gaps.
71# eg, if we have blocks:
72# master is 192.168.2.0/24
73# allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
74# free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
75# then we're OK, but if any one of the allocated or free blocks is missing,
76# something b0rked.
77
78# (select cidr from allocations where cidr <<= '$master') union
79# (select cidr from freeblocks where cidr <<= '$master')
80# order by cidr
81
82foreach $master (@masterblocks) {
83 print "Master $master:\n";
84 ($next,undef) = split /\//, $master;
85 ($last,undef) = split /\//, ($master->broadcast);
86 $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
87 " from allocations where cidr <<= '$master') union ".
88 "(select network(cidr) as net, broadcast(cidr) as bcast ".
89 "from freeblocks where cidr <<= '$master') order by net");
90 $sth->execute;
91 while (@data = $sth->fetchrow_array) {
92 ($cur,undef) = split /\//, $data[0];
93 if ($master !~ /$next/) {
94 ($tmp,undef) = split /\//, $next;
95 @tmp1 = split /\./, $tmp;
96 $tmp1[3]++;
97 if ($tmp1[3] == 256) { $tmp1[3] = 0; $tmp1[2]++; }
98 $cmp = join '.', @tmp1;
99 } else {
100 $cmp = $next;
101 }
102 if ($cur ne $cmp) {
103 print " Gap from $next to $cur\n";
104 }
105 ($next,undef) = split /\//, $data[1];
106 }
107 if ($next ne $last) {
108 print " Gap from $next to end of master at $last\n";
109 }
110 print "done $master.\n";
111}
Note: See TracBrowser for help on using the repository browser.