7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
12 my $originfile = shift @ARGV;
13 my $clusterfile = shift @ARGV;
14 my $zone = shift @ARGV;
15 my $targetoriginfile = shift @ARGV;
16 my $origins = YAML::LoadFile($originfile);
17 my $clusters = YAML::LoadFile($clusterfile);
20 # Initialise cluster details
21 while (my($name,$cluster) = each %$clusters)
23 if ($cluster->{servers})
25 $cluster->{bandwidth} = 0;
27 foreach my $server (@{$cluster->{servers}})
29 $server->{cluster} = $cluster;
30 $cluster->{bandwidth} = $cluster->{bandwidth} + $server->{bandwidth};
32 push @servers, $server;
39 pingdom => $cluster->{pingdom},
40 bandwidth => $cluster->{bandwidth},
41 ipv4 => $cluster->{ipv4},
42 ipv6 => $cluster->{ipv6}
45 $cluster->{servers} = [ $server ];
47 push @servers, $server;
50 $cluster->{name} = $name;
52 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
54 $cluster->{status} = "down";
58 $cluster->{status} = "up";
62 # Initialise server details
63 foreach my $server (@servers)
65 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
67 $server->{status} = "down";
71 $server->{status} = "up";
75 # If pingdom support is enabled then check which servers are up
76 if ($ENV{PINGDOM_USERNAME} && $ENV{PINGDOM_PASSWORD})
78 my $ua = LWP::UserAgent->new;
80 $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
81 $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
83 foreach my $server (@servers)
85 if (my $checkid = $server->{pingdom})
87 my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
89 if ($response->is_success)
91 my $check = decode_json($response->content);
93 $server->{status} = $check->{check}->{status};
95 if ($server->{status} eq "up")
97 $server->{cluster}->{status} = "up";
101 $server->{cluster}->{bandwidth} = $server->{cluster}->{bandwidth} - $server->{bandwidth};
108 # Initialise cluster details
109 while (my($name,$cluster) = each %$clusters)
111 $cluster->{bandwidth_limit} = $cluster->{bandwidth} * 1024 * 1024;
112 $cluster->{bandwidth_used} = 0;
117 # Scan origins and work out which clusters each can use
118 foreach my $origin (values %$origins)
120 foreach my $cluster (values %$clusters)
122 my $match = match_origin($cluster, $origin);
124 if ($cluster->{status} eq "up" && $match ne "denied")
126 my $priority = $match eq "preferred" ? 20 : 10;
127 my $distance = distance($origin->{lat}, $origin->{lon}, $cluster->{lat}, $cluster->{lon});
130 origin => $origin, cluster => $cluster,
131 priority => $priority, distance => $distance
137 # Allocate each country to a cluster
138 allocate_clusters(@mappings);
140 # If we failed to allocate every origin then loop, increasing
141 # the bandwidth for each cluster by a little and retrying until
142 # we manage to allocate everything
143 while (grep { !exists($_->{cluster}) } values %$origins)
145 # Clear any existing mappings of countries to clusters
146 foreach my $origin (values %$origins)
148 delete $origin->{cluster};
151 # Reset bandwidth usage for clusters and increase limits by 10%
152 foreach my $cluster (values %$clusters)
154 $cluster->{bandwidth_used} = 0;
155 $cluster->{bandwidth_limit} = $cluster->{bandwidth_limit} * 1.1;
158 # Try the allocate again
159 allocate_clusters(@mappings);
162 # Create JSON collection object
165 # Create target origins object
166 my $targetorigins = {};
169 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
170 my $jsonfile = IO::File->new("> json/${zone}.json") || die "$!";
172 # Output details for each country
173 foreach my $origin (values %$origins)
175 my $cluster = $origin->{cluster};
176 my $clon = $origin->{lon};
177 my $clat = $origin->{lat};
178 my $slon = $cluster->{lon};
179 my $slat = $cluster->{lat};
181 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
185 elsif ($slon > 0 && $clon < 0 && 360 + $clon - $slon < $slon - $clon)
190 $zonefile->print("# $origin->{name}\n");
191 $zonefile->print("C\L$origin->{code}\E.${zone}:$cluster->{name}.${zone}:600\n");
196 type => "LineString",
197 coordinates => [ [ $clon, $clat ], [ $slon, $slat ] ]
200 origin => $origin->{name},
201 server => $cluster->{name},
202 colour => $cluster->{colour}
206 unless (exists($targetorigins->{$cluster->{name}}))
208 $targetorigins->{$cluster->{name}} = {
209 code => $cluster->{name},
210 name => $cluster->{name},
211 lat => $cluster->{lat},
212 lon => $cluster->{lon},
217 $targetorigins->{$cluster->{name}}->{bandwidth} += $origin->{bandwidth};
220 # Header for default records
221 $zonefile->print("# Unknown origins\n");
223 # Output default records for IPs that can't be mapped to a country
224 while (my($name,$cluster) = each %$clusters)
226 if (my $default = $cluster->{default})
228 $zonefile->print("C${default}.${zone}:${name}.${zone}:600\n");
230 elsif (exists($cluster->{default}))
232 $zonefile->print("C${zone}:${name}.${zone}:600\n");
236 # Header for underlying servers
237 $zonefile->print("# Servers\n");
239 # Output A records for each cluster
240 while (my($name,$cluster) = each %$clusters)
242 foreach my $server (@{$cluster->{servers}})
244 if ($server->{status} eq "up")
246 $zonefile->print("+${name}.${zone}:$server->{ipv4}:600\n");
250 # $zonefile->print("3${name}.${zone}:$server->{ipv6}:600\n");
256 # Output the GeoJSON text
257 $jsonfile->print(encode_json(\@json));
259 # Close the output files
263 # Output the target details in origin format if required
264 YAML::DumpFile($targetoriginfile, $targetorigins) if $targetoriginfile;
269 # Match an origin against a cluster
277 if ($cluster->{preferred} &&
278 $cluster->{preferred}->{countries} &&
279 grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
281 $match = "preferred";
283 elsif ($cluster->{preferred} &&
284 $cluster->{preferred}->{continents} &&
285 grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
287 $match = "preferred";
289 elsif ($cluster->{allowed} &&
290 $cluster->{allowed}->{countries} &&
291 grep { $_ eq $origin->{country} } @{$cluster->{allowed}->{countries}})
295 elsif ($cluster->{allowed} &&
296 $cluster->{allowed}->{continents} &&
297 grep { $_ eq $origin->{continent} } @{$cluster->{allowed}->{continents}})
301 elsif ($cluster->{denied} &&
302 $cluster->{denied}->{countries} &&
303 grep { $_ eq $origin->{country} } @{$cluster->{preferred}->{countries}})
307 elsif ($cluster->{denied} &&
308 $cluster->{denied}->{continents} &&
309 grep { $_ eq $origin->{continent} } @{$cluster->{preferred}->{continents}})
313 elsif ($cluster->{allowed})
326 # Compute the great circle distance between two points
330 my $lat1 = deg2rad(shift);
331 my $lon1 = deg2rad(shift);
332 my $lat2 = deg2rad(shift);
333 my $lon2 = deg2rad(shift);
335 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);
339 # Allocate each origin to a cluster
341 sub allocate_clusters
343 my @mappings = sort { compare_mappings($a, $b) } @_;
345 # Loop over the mappings, trying to assign each origin to the
346 # nearest cluster, but subject to the bandwidth limits
347 while (my $mapping = shift @mappings)
351 push @group, $mapping;
353 while (@mappings && compare_mappings($mapping, $mappings[0]) == 0)
355 push @group, shift @mappings;
358 for my $mapping (sort compare_bandwidth @group)
360 my $origin = $mapping->{origin};
361 my $cluster = $mapping->{cluster};
363 if (!exists($origin->{cluster}) &&
364 $cluster->{bandwidth_used} + $origin->{bandwidth} <= $cluster->{bandwidth_limit})
366 $origin->{cluster} = $cluster;
367 $cluster->{bandwidth_used} = $cluster->{bandwidth_used} + $origin->{bandwidth};
376 # Compare two mappings to decide which to use
383 return $b->{priority} <=> $a->{priority} ||
384 $a->{distance} <=> $b->{distance};
388 # Compare two mappings to decide which to try first
390 sub compare_bandwidth
392 my $a_used = ( $a->{cluster}->{bandwidth_used} * 100.0 ) / ( $a->{cluster}->{bandwidth_limit} * 1.0 );
393 my $b_used = ( $b->{cluster}->{bandwidth_used} * 100.0 ) / ( $b->{cluster}->{bandwidth_limit} * 1.0 );
395 return $a_used <=> $b_used;