7 use Math::Trig qw(deg2rad pip2 great_circle_distance);
14 my $source = shift @ARGV;
15 my $zone = shift @ARGV;
16 my $servers = YAML::LoadFile("src/${source}");
18 my $ua = LWP::UserAgent->new;
20 $ua->default_header("App-Key", "2cohi62u5haxvqmypk3ljqqrze1jufrh");
21 $ua->credentials("api.pingdom.com:443", "Pingdom API", $ENV{PINGDOM_USERNAME}, $ENV{PINGDOM_PASSWORD});
23 foreach my $server (values %$servers)
25 $server->{status} = "down";
27 if (my $checkid = $server->{pingdom})
29 my $response = $ua->get("https://api.pingdom.com/api/2.0/checks/${checkid}");
31 if ($response->is_success)
33 my $check = decode_json($response->content);
35 $server->{status} = $check->{check}->{status};
42 my $countries = XML::TreeBuilder->new;
44 $countries->parsefile("lib/countries.xml");
46 foreach my $country ($countries->look_down("_tag" => "country"))
48 my $code = $country->look_down("_tag" => "countryCode")->as_text;
49 my $name = $country->look_down("_tag" => "countryName")->as_text;
50 my $continent = $country->look_down("_tag" => "continent")->as_text;
51 my $west = $country->look_down("_tag" => "bBoxWest")->as_text;
52 my $north = $country->look_down("_tag" => "bBoxNorth")->as_text;
53 my $east = $country->look_down("_tag" => "bBoxEast")->as_text;
54 my $south = $country->look_down("_tag" => "bBoxSouth")->as_text;
55 my $lat = centre_lat( $south, $north );
56 my $lon = centre_lon( $west, $east );
59 foreach my $servername (keys %$servers)
61 my $server = $servers->{$servername};
62 my $match = match_country($server, $code, $continent);
64 if ($match eq "preferred" || $match eq "allowed")
66 my $priority = $match eq "preferred" ? 20 : 10;
67 my $distance = distance($lat, $lon, $server->{lat}, $server->{lon});
69 $priority = $priority * 10 if $server->{status} eq "up";
71 # print STDERR "$servername is $match for $name with distance $distance\n";
73 push @servers, { name => $servername, priority => $priority, distance => $distance };
78 code => $code, name => $name, continent => $continent,
79 lat => $lat, lon => $lon, servers => \@servers
85 my $zonefile = IO::File->new("> data/${zone}") || die "$!";
86 my $kmlfile = IO::File->new("> kml/${zone}.kml") || die "$!";
87 my $kmlwriter = XML::Writer->new(OUTPUT => $kmlfile, ENCODING => 'utf-8');
89 $kmlwriter->xmlDecl();
90 $kmlwriter->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
91 $kmlwriter->startTag("Document");
93 foreach my $country (values %countries)
95 my @servers = sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @{$country->{servers}};
96 my $server = $servers->{$servers[0]->{name}};
97 my $clon = $country->{lon};
98 my $clat = $country->{lat};
99 my $slon = $server->{lon};
100 my $slat = $server->{lat};
102 if ($clon > 0 && $slon < 0 && 360 + $slon - $clon < $clon - $slon)
107 $zonefile->print("C\L$country->{code}\E.${zone}:$servers[0]->{name}.${zone}:600\n");
109 $kmlwriter->startTag("Placemark");
110 $kmlwriter->dataElement("name", $country->{name});
111 $kmlwriter->startTag("LineString");
112 $kmlwriter->dataElement("coordinates", "$clon,$clat $slon,$slat");
113 $kmlwriter->endTag("LineString");
114 $kmlwriter->endTag("Placemark");
117 foreach my $server (grep { $servers->{$_}->{default} } keys %$servers)
119 $zonefile->print("Cxx.${zone}:${server}.${zone}:600\n");
122 $kmlwriter->endTag("Document");
123 $kmlwriter->endTag("kml");
136 return ( $south + $north ) / 2;
147 $lon = ( $west + $east ) / 2;
151 $lon = ( $west + $east + 360 ) / 2;
154 $lon = $lon - 360 if $lon > 180;
163 my $continent = shift;
166 if ($server->{preferred} &&
167 $server->{preferred}->{countries} &&
168 grep { $_ eq $country } @{$server->{preferred}->{countries}})
170 $match = "preferred";
172 elsif ($server->{preferred} &&
173 $server->{preferred}->{continents} &&
174 grep { $_ eq $continent } @{$server->{preferred}->{continents}})
176 $match = "preferred";
178 elsif ($server->{allowed} &&
179 $server->{allowed}->{countries} &&
180 grep { $_ eq $country } @{$server->{allowed}->{countries}})
184 elsif ($server->{allowed} &&
185 $server->{allowed}->{continents} &&
186 grep { $_ eq $continent } @{$server->{allowed}->{continents}})
190 elsif ($server->{allowed})
204 my $lat1 = deg2rad(shift);
205 my $lon1 = deg2rad(shift);
206 my $lat2 = deg2rad(shift);
207 my $lon2 = deg2rad(shift);
209 return great_circle_distance($lon1, pip2 - $lat1, $lon2, pip2 - $lat2);