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: 2005-02-22 19:25:58 +0000 (Tue, 22 Feb 2005) $
|
---|
7 | # SVN revision $Rev: 167 $
|
---|
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 | ($dbh,$errstr) = connectDB_My;
|
---|
17 |
|
---|
18 | # May as well. We need a number of globals.
|
---|
19 | initIPDBGlobals($dbh);
|
---|
20 |
|
---|
21 | print "First check: All blocks must be within one of the master blocks\n";
|
---|
22 |
|
---|
23 | # First check - make sure ALL routes and allocated blocks are part
|
---|
24 | # of one of the master blocks.
|
---|
25 | print "Checking routed blocks: ";
|
---|
26 | $sth = $dbh->prepare("select cidr from routed");
|
---|
27 | # union select cidr from allocations union select cidr from freeblocks");
|
---|
28 | $sth->execute;
|
---|
29 | ROUTED: while (@data = $sth->fetchrow_array) {
|
---|
30 | $cidr = new NetAddr::IP $data[0];
|
---|
31 | foreach $master (@masterblocks) {
|
---|
32 | if ($master->contains($cidr)) { next ROUTED; }
|
---|
33 | }
|
---|
34 | print "$cidr not mastered\n";
|
---|
35 | }
|
---|
36 | print " done.\n";
|
---|
37 |
|
---|
38 | # Next test: All allocations must be part of a master.
|
---|
39 | print "Checking allocations: ";
|
---|
40 | $sth = $dbh->prepare("select cidr from allocations");
|
---|
41 | $sth->execute;
|
---|
42 | ALLOCATED: while (@data = $sth->fetchrow_array) {
|
---|
43 | $cidr = new NetAddr::IP $data[0];
|
---|
44 | foreach $master (@masterblocks) {
|
---|
45 | if ($master->contains($cidr)) { next ALLOCATED; }
|
---|
46 | }
|
---|
47 | print "$cidr not mastered\n";
|
---|
48 | }
|
---|
49 | print " done.\n";
|
---|
50 |
|
---|
51 | # Next: free blocks
|
---|
52 | print "Checking freeblocks: ";
|
---|
53 | $sth = $dbh->prepare("select cidr from freeblocks order by cidr");
|
---|
54 | $sth->execute;
|
---|
55 | FREEBLOCK: while (@data = $sth->fetchrow_array) {
|
---|
56 | $cidr = new NetAddr::IP $data[0];
|
---|
57 | foreach $master (@masterblocks) {
|
---|
58 | if ($master->contains($cidr)) { next FREEBLOCK; }
|
---|
59 | }
|
---|
60 | print "$cidr not mastered\n";
|
---|
61 | }
|
---|
62 | print " done.\n";
|
---|
63 |
|
---|
64 | print "Done checking master containment.\n\nChecking pool containment...\n";
|
---|
65 |
|
---|
66 | $sth = $dbh->prepare("select pool,ip from poolips order by ip");
|
---|
67 | $sth->execute;
|
---|
68 | while (@data = $sth->fetchrow_array) {
|
---|
69 | $pool = new NetAddr::IP $data[0];
|
---|
70 | $ip = new NetAddr::IP $data[1];
|
---|
71 | print "IP $ip listed with incorrect pool $pool\n"
|
---|
72 | if !$pool->contains($ip);
|
---|
73 | }
|
---|
74 | $sth = $dbh->prepare("select distinct pool from poolips order by pool");
|
---|
75 | $sth->execute;
|
---|
76 | while (@data = $sth->fetchrow_array) {
|
---|
77 | $sth2 = $dbh->prepare("select cidr from allocations where cidr='$data[0]'");
|
---|
78 | $sth2->execute;
|
---|
79 | print "Pool $data[0] does not exist in allocations table\n"
|
---|
80 | if (($sth2->fetchrow_array)[0] eq '');
|
---|
81 | }
|
---|
82 |
|
---|
83 | print "Done checking pool containment.\n\nChecking block-alignment consistency:\n";
|
---|
84 |
|
---|
85 | # Block alignment consistency: All allocated+free blocks within a master
|
---|
86 | # must NOT overlap, and they must show no gaps.
|
---|
87 | # eg, if we have blocks:
|
---|
88 | # master is 192.168.2.0/24
|
---|
89 | # allocated 192.168.2.0/29, 192.168.2.8/29, 192.168.2.64/26
|
---|
90 | # free 192.168.2.16/28, 192.168.2.32/27, 192.168.2.128/25
|
---|
91 | # then we're OK, but if any one of the allocated or free blocks is missing,
|
---|
92 | # something b0rked.
|
---|
93 |
|
---|
94 | # (select cidr from allocations where cidr <<= '$master') union
|
---|
95 | # (select cidr from freeblocks where cidr <<= '$master')
|
---|
96 | # order by cidr
|
---|
97 |
|
---|
98 | foreach $master (@masterblocks) {
|
---|
99 | print "Master $master:\n";
|
---|
100 | $prev = $master;
|
---|
101 | $sth = $dbh->prepare("(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
102 | " from allocations where cidr <<= '$master') union ".
|
---|
103 | "(select network(cidr) as net, broadcast(cidr) as bcast ".
|
---|
104 | "from freeblocks where cidr <<= '$master') order by net");
|
---|
105 | $sth->execute;
|
---|
106 |
|
---|
107 | while (@data = $sth->fetchrow_array) {
|
---|
108 | $cur = new NetAddr::IP $data[0];
|
---|
109 |
|
---|
110 | if ($master->numeric == $prev->numeric) {
|
---|
111 | # check if cur starts with master
|
---|
112 | if ($cur->numeric > $prev->numeric) {
|
---|
113 | print " Gap from start of master $master to first block $cur\n";
|
---|
114 | } elsif ($cur->numeric < $prev->numeric) {
|
---|
115 | print " BIG problem! Current block $cur begins before master $master!\n";
|
---|
116 | }
|
---|
117 | } else {
|
---|
118 | if ($cur->numeric < ($prev->numeric + 1)) {
|
---|
119 | print " Block ".$prev->network." overlaps block $cur\n";
|
---|
120 | } elsif ($cur->numeric > ($prev->numeric + 1)) {
|
---|
121 | print " Gap between end of block ".$prev->network." and block $cur\n";
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | $prev = $cur;
|
---|
126 | $prev--;
|
---|
127 |
|
---|
128 | } # while (@data...)
|
---|
129 |
|
---|
130 | $cur--;
|
---|
131 | $master--;
|
---|
132 | if ($cur->numeric ne $master->numeric) {
|
---|
133 | print " Gap from $cur to end of master at $master\n";
|
---|
134 | }
|
---|
135 | print "done $master.\n";
|
---|
136 | }
|
---|
137 |
|
---|
138 | print "Done checking block alignment.\n\nChecking for correctness on 'defined' CustIDs:\n";
|
---|
139 | # New check: Make sure "defined" CustIDs are correct.
|
---|
140 | $sth = $dbh->prepare("select cidr,type,custid from allocations where not type='cn' order by cidr");
|
---|
141 | $sth->execute;
|
---|
142 | while (@data = $sth->fetchrow_array) {
|
---|
143 | print "$data[0] ($disp_alloctypes{$data[1]}) has incorrect CustID $data[2]\n"
|
---|
144 | if $data[2] ne $def_custids{$data[1]};
|
---|
145 | }
|
---|
146 |
|
---|
147 | print "Done CustID correctness check.\n";
|
---|