]> git.openstreetmap.org Git - dns.git/blob - bin/sumlogs
mkgeo: add country name comment to tinydns zone to improve readability
[dns.git] / bin / sumlogs
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Net::Patricia;
7 use YAML;
8
9 my $pt = new Net::Patricia;
10
11 open(COUNTRIES, "< /etc/powerdns/countries.conf") || die "Can't open /etc/powerdns/countries.conf";
12
13 while (my $line = <COUNTRIES>)
14 {
15     if ($line =~ /^(\d+\.\d+\.\d+\.\d+\/\d+)\s+:127\.\d+\.\d+\.\d+:([a-z]{2})/)
16     {
17         my $address = $1;
18         my $country = uc($2);
19
20         $pt->add_string($address, $country);
21     }
22 }
23
24 close(COUNTRIES);
25
26 my $total_bytes = 0;
27 my %country_bytes;
28
29 while (my $record = <>)
30 {
31     if ($record =~ /^\d+\.\d+\s+\d+\s+(\d+\.\d+\.\d+\.\d+)\s+TCP_[A-Z_]+\/\d+\s+(\d+) /)
32     {
33         my $ip = $1;
34         my $bytes = $2;
35         my $country = $pt->match_string($ip);
36
37         $country_bytes{$country} += $bytes if defined($country);
38
39         $total_bytes += $bytes;
40     }
41     else
42     {
43         warn $record;
44     }
45 }
46
47 my %country_bandwidth;
48
49 while (my($country,$bytes) = each %country_bytes)
50 {
51     $country_bandwidth{$country} = $bytes * 250 * 1024 * 1024 / $total_bytes;
52 }
53
54 print Dump(\%country_bandwidth);
55
56 exit 0;