[smokeping-users] Mail script for SMTP Auth, alternate destination ports, etc.
Gregory Sloop
gregs at sloop.net
Mon Sep 5 21:25:16 CEST 2011
Well, since there wasn't apparently any ready-made scripts for using
an off-site mail server, using SMTP auth or alternate ports, I coded
one in perl. [My first perl code - aren't you lucky! :) ]
I used Mail::Sender - so if you use my script you'll need to install
that from CPAN or somewhere else.
Line wrap is probably going to mangle things a bit, but I think you'll
manage.
I placed this in the same directory as the smokeping config file and set
+execute permissions on the user.
Then, in my smokeping config file, I have a line like this:
to = |/etc/smokeping/smokeping-smtp-auth-alert-0.0.2
This will kick off the perl script. Unfortunately there's no way to
specify the destination etc in the config file, so you'll have to edit
the perl script below.
I've tried to keep the messages minimal, so you can send to SMS
devices. [I need to work on a pattern that will alert me quickly, but
not generate a huge number of alerts - so I'll have a question about
that soon.]
I don't do a lot of error checking [virtually none] in my script, but
even if I did - there's not much one could do about any failures.
Either things work or they don't - an unattended script won't be able
to do much to help.
Sorry, no TLS or secured channel communication - it was just as quick
and dirty as I could manage.
I've left some debug code commented out in the code - I found it
useful, and perhaps others would too.
Would be more than happy if someone wants to mod or integrate the
code.
Code follows.
---
#!/usr/bin/perl -w
use Mail::Sender;
$vAlert=$ARGV[0];
$vTarget=$ARGV[1];
$vPktLoss=$ARGV[2];
$vRTT=$ARGV[3];
$vHostName=$ARGV[4];
$vEdgeTrigger=$ARGV[5];
#For debug assist
#$vAlert="SomeAlert";
#$vTarget="SomeTarget";
#$vPktLoss="20%";
#$vRTT="3000";
#$vHostName="BogusHost";
#$vEdgeTrigger="1";
# If you leave any fields blank [most are required] leave them as empty strings [i.e. ""].
# Optional fields are CC/BCC/MessageBodyPrefix and Postfix.
# I've tailored this to attempt to fit in a 160 char SMS.
# Leave MessageBodyPrefix/MessageBodyProstfix blank if you try to fit
in SMS's 160 chars
# Keeping everything short is a must! Short email addy's etc.
# I've stripped the message body as much as possible too.
# You're welcome to tinker if you like, just don't blame me if you break something! :)
# You can modify most all of the following variables - leave the code
be, unless you know what you're doing. [I don't, and look what
happened to me!]
$vSMTPHost="smtp.someserver.local";
$vHelloHost="smokeping.somenet.local";
$vDestPort="26";
$vFrom='smokeping at somenet.local';
$vSMTPAuthUser='smokeping at somenet.local';
$vSMTPAuthPass="opensesame";
$vTo='alerts at somenet.local';
#Multi-Address is like this: $vTo='alerts at somenet.local, some.other.addy at somewhere.local';
$vCC='';
$vBCC='';
$vReplyTo='reply.to.smokeping.alerts.here at somenet.local';
$vPriority="1"; #Priority in Mail::Sender: 1-5, 1=high, 5=low
$vHeaderSubject="Subject: Smokeping alert: $vTarget";
$vMessageBodyPrefix="";
$vMessageBodyPostfix="";
eval {
$smtp = new Mail::Sender {
smtp => "$vSMTPHost",
from => "$vFrom",
port => "$vDestPort",
client => "$vHelloHost",
authid => "$vSMTPAuthUser",
authpwd => "$vSMTPAuthPass",
on_errors => 'die',
};
$smtp->Open({
to => "$vTo",
cc => "$vCC",
bcc => "$vBCC",
replyto => "$vReplyTo",
subject => "$vHeaderSubject",
priority => "$vPriority"
});
if ($vMessageBodyPrefix ne "")
{$smtp->datasend("$vMessageBodyPrefix");}
$smtp->SendLineEnc("Alert:$vAlert");
$smtp->SendLineEnc("$vTarget");
$smtp->SendLineEnc("$vPktLoss");
$smtp->SendLineEnc("$vRTT");
$smtp->SendLineEnc("$vHostName");
$smtp->SendLineEnc("ETSt: $vEdgeTrigger");
if ($vMessageBodyPostfix ne "")
{$smtp->SendLineEnc("$vMessageBodyPostfix");}
$smtp->Close();
};
if ($@)
{
die "Failed to send the message: $@\n";
}
---
TAGS: SMTP Auth, Alternate smtp port, SASL, External smtp server, mail
customization, SMS
--
Gregory Sloop, Principal: Sloop Network & Computer Consulting
503.251.0452 x82 Voice | 503.251.0452 Fax
www.sloop.net
mailto:gregs at sloop.net
More information about the smokeping-users
mailing list