[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: 2006-04-03 20:31:11 +0000 (Mon, 03 Apr 2006) $
|
---|
| 7 | # SVN revision $Rev: 319 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[319] | 10 | # Copyright (C) 2004-2006 - Kris Deugau
|
---|
[117] | 11 |
|
---|
| 12 | use DBI;
|
---|
[158] | 13 | use MyIPDB;
|
---|
[117] | 14 | use NetAddr::IP;
|
---|
| 15 |
|
---|
[158] | 16 | ($dbh,$errstr) = connectDB_My;
|
---|
[117] | 17 |
|
---|
| 18 | print "Content-type: text/plain\n\n";
|
---|
| 19 |
|
---|
| 20 | $tnumfree = $bigrfree = $bigufree = 0;
|
---|
| 21 |
|
---|
[317] | 22 | $sql = 'select * from freeblocks where ';
|
---|
[117] | 23 | # General counts first
|
---|
| 24 | if ($ARGV[0]) {
|
---|
[317] | 25 | $sql .= 'maskbits >= $ARGV[0] and ';
|
---|
[117] | 26 | }
|
---|
[317] | 27 | $sql .= "not (cidr <<= '192.168.0.0/16') ".
|
---|
| 28 | "and not (cidr <<= '172.16.0.0/12') ".
|
---|
| 29 | "and not (cidr <<= '10.0.0.0/8') ";
|
---|
| 30 | $sql .= "order by maskbits desc";
|
---|
| 31 |
|
---|
| 32 | $sth = $dbh->prepare($sql);
|
---|
[117] | 33 | $sth->execute;
|
---|
| 34 | while (@data = $sth->fetchrow_array) {
|
---|
| 35 | # cidr,maskbits,city,routed
|
---|
| 36 | $tnumfree++;
|
---|
| 37 | $numfree{"$data[1]"}++;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[319] | 40 | print "Free block counts:\n";
|
---|
[117] | 41 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
| 42 | print "/$size: $numfree{$size}\n";
|
---|
| 43 | }
|
---|
| 44 | print "\n";
|
---|
| 45 |
|
---|
| 46 | for ($i=30; $i>16; $i--) {
|
---|
| 47 | $j = $i-1;
|
---|
| 48 | $numfree{"$j"} += int $numfree{"$i"}/2;
|
---|
| 49 | $numfree{"$i"} -= (int $numfree{"$i"}/2)*2;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[319] | 52 | print "Aggregate free space:\n";
|
---|
[117] | 53 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
| 54 | print "/$size: $numfree{$size}\n";
|
---|
| 55 | }
|
---|