1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/freespace.pl
|
---|
3 | # Quick hack to calculate aggregate free IP space, and statistic-ify the blocks.
|
---|
4 | ###
|
---|
5 | # SVN revision info
|
---|
6 | # $Date: 2010-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $
|
---|
7 | # SVN revision $Rev: 417 $
|
---|
8 | # Last update by $Author: kdeugau $
|
---|
9 | ###
|
---|
10 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
11 |
|
---|
12 | use DBI;
|
---|
13 | use NetAddr::IP;
|
---|
14 |
|
---|
15 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
16 | ##uselib##
|
---|
17 |
|
---|
18 | use MyIPDB;
|
---|
19 |
|
---|
20 | ($dbh,$errstr) = connectDB_My;
|
---|
21 |
|
---|
22 | print "Content-type: text/plain\n\n";
|
---|
23 |
|
---|
24 | $tnumfree = $bigrfree = $bigufree = 0;
|
---|
25 |
|
---|
26 | $sql = 'select * from freeblocks where ';
|
---|
27 | # General counts first
|
---|
28 | if ($ARGV[0]) {
|
---|
29 | $sql .= 'maskbits >= $ARGV[0] and ';
|
---|
30 | }
|
---|
31 | $sql .= "not (cidr <<= '192.168.0.0/16') ".
|
---|
32 | "and not (cidr <<= '172.16.0.0/12') ".
|
---|
33 | "and not (cidr <<= '10.0.0.0/8') ";
|
---|
34 | $sql .= "order by maskbits desc";
|
---|
35 |
|
---|
36 | $sth = $dbh->prepare($sql);
|
---|
37 | $sth->execute;
|
---|
38 | while (@data = $sth->fetchrow_array) {
|
---|
39 | # cidr,maskbits,city,routed
|
---|
40 | $tnumfree++;
|
---|
41 | $numfree{"$data[1]"}++;
|
---|
42 | }
|
---|
43 |
|
---|
44 | print "Free block counts:\n";
|
---|
45 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
46 | print "/$size: $numfree{$size}\n";
|
---|
47 | }
|
---|
48 | print "\n";
|
---|
49 |
|
---|
50 | for ($i=30; $i>16; $i--) {
|
---|
51 | $j = $i-1;
|
---|
52 | $numfree{"$j"} += int $numfree{"$i"}/2;
|
---|
53 | $numfree{"$i"} -= (int $numfree{"$i"}/2)*2;
|
---|
54 | }
|
---|
55 |
|
---|
56 | print "Aggregate free space:\n";
|
---|
57 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
58 | print "/$size: $numfree{$size}\n";
|
---|
59 | }
|
---|