[117] | 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-07-26 21:00:00 +0000 (Mon, 26 Jul 2010) $
|
---|
| 7 | # SVN revision $Rev: 445 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[445] | 10 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
[117] | 11 |
|
---|
| 12 | use DBI;
|
---|
| 13 | use NetAddr::IP;
|
---|
| 14 |
|
---|
[445] | 15 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 16 | ##uselib##
|
---|
| 17 |
|
---|
| 18 | use MyIPDB;
|
---|
| 19 |
|
---|
[158] | 20 | ($dbh,$errstr) = connectDB_My;
|
---|
[117] | 21 |
|
---|
| 22 | print "Content-type: text/plain\n\n";
|
---|
| 23 |
|
---|
| 24 | $tnumfree = $bigrfree = $bigufree = 0;
|
---|
| 25 |
|
---|
[317] | 26 | $sql = 'select * from freeblocks where ';
|
---|
[117] | 27 | # General counts first
|
---|
| 28 | if ($ARGV[0]) {
|
---|
[317] | 29 | $sql .= 'maskbits >= $ARGV[0] and ';
|
---|
[117] | 30 | }
|
---|
[317] | 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);
|
---|
[117] | 37 | $sth->execute;
|
---|
| 38 | while (@data = $sth->fetchrow_array) {
|
---|
| 39 | # cidr,maskbits,city,routed
|
---|
| 40 | $tnumfree++;
|
---|
| 41 | $numfree{"$data[1]"}++;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[319] | 44 | print "Free block counts:\n";
|
---|
[117] | 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 |
|
---|
[319] | 56 | print "Aggregate free space:\n";
|
---|
[117] | 57 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
| 58 | print "/$size: $numfree{$size}\n";
|
---|
| 59 | }
|
---|