[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: 2017-08-15 17:53:23 +0000 (Tue, 15 Aug 2017) $
|
---|
| 7 | # SVN revision $Rev: 906 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[417] | 10 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
[117] | 11 |
|
---|
| 12 | use DBI;
|
---|
| 13 | use NetAddr::IP;
|
---|
| 14 |
|
---|
[417] | 15 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 16 | ##uselib##
|
---|
| 17 |
|
---|
[906] | 18 | # push "the directory the script is in" into @INC
|
---|
| 19 | use FindBin;
|
---|
| 20 | use lib "$FindBin::RealBin/";
|
---|
| 21 |
|
---|
[417] | 22 | use MyIPDB;
|
---|
| 23 |
|
---|
[142] | 24 | ($dbh,$errstr) = connectDB_My;
|
---|
[117] | 25 |
|
---|
| 26 | print "Content-type: text/plain\n\n";
|
---|
| 27 |
|
---|
| 28 | $tnumfree = $bigrfree = $bigufree = 0;
|
---|
| 29 |
|
---|
[320] | 30 | $sql = 'select * from freeblocks where ';
|
---|
[117] | 31 | # General counts first
|
---|
| 32 | if ($ARGV[0]) {
|
---|
[320] | 33 | $sql .= 'maskbits >= $ARGV[0] and ';
|
---|
[117] | 34 | }
|
---|
[320] | 35 | $sql .= "not (cidr <<= '192.168.0.0/16') ".
|
---|
| 36 | "and not (cidr <<= '172.16.0.0/12') ".
|
---|
| 37 | "and not (cidr <<= '10.0.0.0/8') ";
|
---|
| 38 | $sql .= "order by maskbits desc";
|
---|
| 39 |
|
---|
| 40 | $sth = $dbh->prepare($sql);
|
---|
[117] | 41 | $sth->execute;
|
---|
| 42 | while (@data = $sth->fetchrow_array) {
|
---|
| 43 | # cidr,maskbits,city,routed
|
---|
| 44 | $tnumfree++;
|
---|
| 45 | $numfree{"$data[1]"}++;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[320] | 48 | print "Free block counts:\n";
|
---|
[117] | 49 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
| 50 | print "/$size: $numfree{$size}\n";
|
---|
| 51 | }
|
---|
| 52 | print "\n";
|
---|
| 53 |
|
---|
| 54 | for ($i=30; $i>16; $i--) {
|
---|
| 55 | $j = $i-1;
|
---|
| 56 | $numfree{"$j"} += int $numfree{"$i"}/2;
|
---|
| 57 | $numfree{"$i"} -= (int $numfree{"$i"}/2)*2;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[320] | 60 | print "Aggregate free space:\n";
|
---|
[117] | 61 | foreach $size (sort {$a cmp $b} keys %numfree) {
|
---|
| 62 | print "/$size: $numfree{$size}\n";
|
---|
| 63 | }
|
---|