Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created September 1, 2014 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/2edacef42d3c35cc75e2 to your computer and use it in GitHub Desktop.
Save ilyaevseev/2edacef42d3c35cc75e2 to your computer and use it in GitHub Desktop.
MTR handler for SmokePing
#!/usr/bin/perl
# http://www.mail-archive.com/smokeping-users@lists.oetiker.ch/msg04048.html
# this script would be run when smokeping detects some loss. The
# smokeping trigger could look like:
#
# +highLossTwice
# type = loss
# pattern = >70%,>70%
# to = |/usr/local/smokeping/contrib/mtr_email.pl
# comment = 10 minutes of high loss
# this runs mtr on master and traceroute on a slave and emails results
# to an address hardcoded below. It was a throw-away so I didn't spend
# any time making it user or developer friendly.
use strict;
use warnings;
use Net::SMTP;
use English;
my $smtp_server = 'mx.example.ru';
my $sender_email = 'smokeping-daemon@smokeping.example.ru';
my $dest_email = 'smokeping-admin@example.ru';
my $mtr_binary = '/usr/bin/mtr';
my $cycles_count = 10;
die "Cannot find MTR binary: $mtr_binary, aborted.\n"
unless -x $mtr_binary;
die "Must be called from SmokePing core, aborted.\n"
if @ARGV != 5;
my ($name_of_alert, $target, $loss_pattern, $rtt_pattern, $hostname) = @ARGV;
my @s;
push @s, "To: $dest_email\n";
push @s, "From: $sender_email\n";
push @s, "Content-Type: text/html\n";
push @s, "Subject: SmokePing detected loss to $hostname\n";
push @s, "\n";
push @s, "<p><h3>SmokePing detected loss to $target</h3></p>\n";
push @s, "<p><ul>";
push @s, "<li>Alert name = <b>$name_of_alert</b></li>\n";
push @s, "<li>Target = <b>$target</b> </li>\n";
push @s, "<li>Loss pattern = <b>$loss_pattern</b> </li>\n";
push @s, "<li>RTT pattern = <b>$rtt_pattern</b> </li>\n";
push @s, "</ul></p><p><pre><tt>";
open MTR, ("$mtr_binary -nrc$cycles_count $hostname |");
push @s, (<MTR>);
close MTR;
push @s, "</tt></pre></p>\n";
# send the email.
my $smtp = Net::SMTP->new($smtp_server, Timeout => 5);
$smtp->mail($sender_email);
$smtp->to($dest_email);
$smtp->data();
$smtp->datasend($_) foreach @s;
$smtp->dataend();
$smtp->quit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment