use IO::File;
use Math::Trig qw(deg2rad pip2 great_circle_distance);
use JSON::XS;
-use LWP::UserAgent;
+use LWP::UserAgent::Determined;
use YAML;
my $originfile = shift @ARGV;
# If pingdom support is enabled then check which servers are up
if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
{
- my $ua = LWP::UserAgent->new;
+ my $ua = LWP::UserAgent::Determined->new;
+ $ua->timeout(15);
$ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
$ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
}
}
+# Create target origins object
+my $targetorigins = {};
+
# Initialise cluster details
while (my($name,$cluster) = each %$clusters)
{
$cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
$cluster->{bandwidth_used} = 0;
+
+ $targetorigins->{$cluster->{name}} = {
+ code => $cluster->{name},
+ name => $cluster->{name},
+ lat => $cluster->{lat},
+ lon => $cluster->{lon},
+ bandwidth => 0
+ };
}
my @mappings = ();
# Create JSON collection object
my @json;
-# Create target origins object
-my $targetorigins = {};
-
# Open output files
my $zonefile = IO::File->new("> data/${zone}") || die "$!";
my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
}
};
- unless (exists($targetorigins->{$cluster->{name}}))
- {
- $targetorigins->{$cluster->{name}} = {
- code => $cluster->{name},
- name => $cluster->{name},
- lat => $cluster->{lat},
- lon => $cluster->{lon},
- bandwidth => 0
- };
- }
-
$targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
}
+# Header for default records
+$zonefile->print("# Unknown origins\n");
+
# Output default records for IPs that can't be mapped to a country
-foreach my $cluster (grep { $clusters->{$_}->{default} } keys %$clusters)
+while (my($name,$cluster) = each %$clusters)
{
- $zonefile->print("# Unknown origins\n");
- $zonefile->print("Cxx.${zone}:${cluster}.${zone}:600\n");
+ if (my $default = $cluster->{default})
+ {
+ output_server($zonefile, "${default}.${zone}", $cluster);
+ }
+ elsif (exists($cluster->{default}))
+ {
+ output_server($zonefile, "${zone}", $cluster);
+ }
}
+# Header for underlying servers
$zonefile->print("# Servers\n");
# Output A records for each cluster
while (my($name,$cluster) = each %$clusters)
{
- foreach my $server (@{$cluster->{servers}})
- {
- if ($server->{status} eq "up")
- {
- $zonefile->print("+${name}.${zone}:$server->{ipv4}:600\n");
-
- if ($server->{ipv6})
- {
-# $zonefile->print("3${name}.${zone}:$server->{ipv6}:600\n");
- }
- }
- }
+ output_server($zonefile, "${name}.${zone}", $cluster);
}
# Output the GeoJSON text
return $a_used <=> $b_used;
}
+
+#
+# Output DNS records for a server
+#
+sub output_server
+{
+ my $zonefile = shift;
+ my $name = shift;
+ my $cluster = shift;
+
+ foreach my $server (@{$cluster->{servers}})
+ {
+ if ($server->{status} eq "up")
+ {
+ $zonefile->print("+${name}:$server->{ipv4}:3600\n");
+
+ if ($server->{ipv6})
+ {
+# $zonefile->print("3${name}:$server->{ipv6}:3600\n");
+ }
+ }
+ }
+
+ return;
+}