summaryrefslogtreecommitdiffstats
path: root/generate-mirror-mail.pl
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2016-02-14 15:58:22 +0100
committerFlorian Pritz <bluewind@xinu.at>2016-02-14 15:58:22 +0100
commit8da969254e17a0e21ddc7e6f77ba3f1d6c8b66d3 (patch)
treeecf2fa48e42e5f1396071ee8ffe3c38bb9050594 /generate-mirror-mail.pl
parentee44011540bdc048b45905ea547fc4f135d96b27 (diff)
downloadbin-8da969254e17a0e21ddc7e6f77ba3f1d6c8b66d3.tar.gz
bin-8da969254e17a0e21ddc7e6f77ba3f1d6c8b66d3.tar.xz
Add generate-mirror-mail.pl
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'generate-mirror-mail.pl')
-rwxr-xr-xgenerate-mirror-mail.pl143
1 files changed, 143 insertions, 0 deletions
diff --git a/generate-mirror-mail.pl b/generate-mirror-mail.pl
new file mode 100755
index 0000000..765ae63
--- /dev/null
+++ b/generate-mirror-mail.pl
@@ -0,0 +1,143 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use JSON;
+use WWW::Mechanize;
+use Date::Parse;
+use Date::Format;
+use Text::Template;
+use Try::Tiny;
+
+use Data::Dumper;
+
+#$ENV{HTTPS_CA_FILE} = '/etc/ssl/certs/ca-certificates.crt';
+
+my %templates = (
+ 'out-of-sync' => {
+ 'subject' => '[{$mirror_name}] Arch Linux mirror out of sync',
+ 'template' => 'Hi,
+
+Your mirror seems to be out of sync since {$last_sync}, could you please
+investigate?
+
+{$mirror_urls}
+
+Thanks,
+Florian
+',
+ },
+ 'connection-failed' => {
+ 'subject' => '[{$mirror_name}] Arch Linux mirror not accessible',
+ 'template' => 'Hi,
+
+We\'re having trouble connecting to your mirror, could you
+please check what\'s going on?
+
+{$mirror_urls}
+
+Thanks,
+Florian
+',
+ },
+);
+
+my $mech = WWW::Mechanize->new(cookie_jar => {});
+
+sub send_mail {
+ my $to = shift;
+ my $subject = shift;
+ my $body = shift;
+
+ open my $fh, "|compose-mail-from-stdin" or die "Failed to run mailer: $!";
+ print $fh "To: $to\n";
+ print $fh "From: bluewind\@archlinux.org\n";
+ print $fh "Subject: $subject\n";
+ print $fh "\n";
+ print $fh "$body";
+ close $fh;
+}
+
+sub send_template_mail {
+ my $to = shift;
+ my $subject = shift;
+ my $body = shift;
+ my $values = shift;
+
+ send_mail($to, fill_template($subject, $values), fill_template($body, $values));
+}
+
+sub fill_template {
+ my $template = shift;
+ my $values = shift;
+ my $result = Text::Template::fill_in_string($template, HASH => $values)
+ or die "Failed to fill in template: $Text::Template::ERROR";
+
+ return $result;
+}
+
+while (<>) {
+ try {
+ my $url = $_;
+ chomp($url);
+ die "Skipping non-mirror detail URL" if $url =~ m/\/[0-9]+(\/|$)/;
+
+ $mech->get($url."/json/");
+ my ($mirror_name) = ($url =~ m#/([^/]+)/?$#);
+ my $json = JSON::decode_json($mech->content());
+
+ my @out_of_sync;
+ my @connection_failed;
+
+ for my $mirror (@{$json->{urls}}) {
+ if ($mirror->{last_sync}) {
+ my $time = str2time($mirror->{last_sync});
+ if ($time < time() - 60*60*24*3) {
+ push @out_of_sync, {
+ time => $time,
+ url => $mirror->{url},
+ details_link => "", # TODO
+ };
+ }
+ } else {
+ push @connection_failed, {
+ url => $mirror->{url},
+ details_link => "", # TODO
+ };
+ }
+ }
+
+ # extract and deduplicate sync times
+ my @last_sync = keys { map { ${$_}{time} => 1 } @out_of_sync };
+ my $sent_mail = 0;
+
+ # TODO: set $to
+ my $to = '';
+
+ if (@out_of_sync) {
+ my %values = (
+ last_sync => join(", ", map {time2str("%Y-%m-%d", $_)} @last_sync),
+ mirror_urls => join("\n", $url, map {${$_}{details_link}} @out_of_sync),
+ mirror_name => $mirror_name,
+ );
+ send_template_mail($to, $templates{"out-of-sync"}{"subject"}, $templates{"out-of-sync"}{"template"}, \%values);
+ $sent_mail = 1;
+ }
+
+ if (@connection_failed) {
+
+ my %values = (
+ mirror_urls => join("\n", $url, map {${$_}{details_link}} @connection_failed),
+ mirror_name => $mirror_name,
+ );
+ send_template_mail($to, $templates{"connection-failed"}{"subject"}, $templates{"connection-failed"}{"template"}, \%values);
+ $sent_mail = 1;
+ }
+
+ if (!$sent_mail) {
+ say STDERR "No issue detected for mirror $mirror_name";
+ }
+
+ } catch {
+ warn "ignoring error: $_";
+ }
+}