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

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

Fix missing svn:keywords revision info in scripts

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 3.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: 2004-10-26 17:03:16 +0000 (Tue, 26 Oct 2004) $
7# SVN revision $Rev: 26 $
8# Last update by $Author: kdeugau $
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 $prev = $master;
85 $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
86 " from allocations where cidr <<= '$master') union ".
87 "(select network(cidr) as net, broadcast(cidr) as bcast ".
88 "from freeblocks where cidr <<= '$master') order by net");
89 $sth->execute;
90
91 while (@data = $sth->fetchrow_array) {
92 $cur = new NetAddr::IP $data[0];
93
94 if ($master->numeric == $prev->numeric) {
95 # check if cur starts with master
96 if ($cur->numeric > $prev->numeric) {
97 print " Gap from start of master $master to first block $cur\n";
98 } elsif ($cur->numeric < $prev->numeric) {
99 print " BIG problem! Current block $cur begins before master $master!\n";
100 }
101 } else {
102 if ($cur->numeric < ($prev->numeric + 1)) {
103 print " Block ".$prev->network." overlaps block $cur\n";
104 } elsif ($cur->numeric > ($prev->numeric + 1)) {
105 print " Gap between end of block ".$prev->network." and block $cur\n";
106 }
107 }
108
109 $prev = $cur;
110 $prev--;
111
112 } # while (@data...)
113
114 $cur--;
115 $master--;
116 if ($cur->numeric ne $master->numeric) {
117 print " Gap from $cur to end of master at $master\n";
118 }
119 print "done $master.\n";
120}
Note: See TracBrowser for help on using the repository browser.