#!/usr/bin/perl
# script to be used for sending munin alert emails. This lets us add
# useful information and links for certain alerts.
# configuration in munin.conf:
# contact.o3admins.command /path/to/munin-mail "${var:group} ${var:host} ${var:plugin} ${var:graph_category} '${var:graph_title}'" email-address

use strict;
use warnings;

my ($subject, $email) = @ARGV;
my $content = '';
while (<STDIN>) {
    $content .= $_;
}
if ($subject =~ m/systemd_(units|status)/) {
    $content .= "\n" . qx{systemctl --failed};
}

open my $pipe, '|-', 'mail', '-s', $subject, '-r', $email, $email or die "Could not open pipe: $?";
print $pipe $content or die "Could not print: $!";;
close $pipe or die "Could not close pipe: $?";

