LVS
lvs-devel
Google
 
Web LinuxVirtualServer.org

Re: [PATCH] ldirectord: Allow sending email via external SMTP server

To: Simon Horman <horms@xxxxxxxxxxxx>
Subject: Re: [PATCH] ldirectord: Allow sending email via external SMTP server
Cc: lvs-devel@xxxxxxxxxxxxxxx
From: Caleb Anthony <caleb.anthony@xxxxxxxxx>
Date: Fri, 8 May 2009 07:38:34 -0600
It's fairly well tested. I tried to break it as much as I could -
setting an unreachable SMTP server, setting an invalid SMTP server
address, sending a ton of e-mails with the code by starting, stopping,
adding and removing real servers, etc. I'm going to put it into
production on a few IPVS directors I have here. I can report back in
about a week if you would like.

Also, there needs to be one addition to the ld_emailalert_net_smtp
sub. The ld_emailalert_mail_send sub logs a message in ldirectord.log
once an email is sent: &ld_log("emailalert: $subject");. I meant to
include that same line in ld_emailalert_net_smtp so that each sub
would be as similar as possible.

Could you add that line to ld_emailalert_net_smtp before you commit?

Thanks.

On Thu, May 7, 2009 at 6:28 PM, Simon Horman <horms@xxxxxxxxxxxx> wrote:
> On Thu, May 07, 2009 at 02:09:34PM -0600, Caleb Anthony wrote:
>> Attached is a new patch. This one adds two new subs:
>> ld_emailalert_net_smtp and ld_emailalert_mail_send. If an SMTP server
>> is set in ldirectord.cf, then ld_emailalert_net_smtp will be called
>> and the email will be sent via SMTP. If an SMTP server is not set in
>> ldirectord.cf then ld_emailalert_mail_send will be called and the
>> email will be sent via the Mail::Send methods.
>>
>> Let me know what you think.
>
> Hi Caleb,
>
> That looks pretty good to me. How well tested is it?
>
> I have rediffed it against the current http://hg.linux-ha.org/dev tree.
> The result is below:
>
> Index: dev/ldirectord/ldirectord.in
> ===================================================================
> --- dev.orig/ldirectord/ldirectord.in   2009-05-08 10:21:45.000000000 +1000
> +++ dev/ldirectord/ldirectord.in        2009-05-08 10:27:02.000000000 +1000
> @@ -248,6 +248,13 @@ If defined in a virtual server section t
>  Default: all
>
>
> +B<smtp = >I<ip_address|hostname>B<">
> +
> +A valid SMTP server address to use for sending email via SMTP.
> +
> +If defined in a virtual server section then the global value is overridden.
> +
> +
>  B<execute = ">I<configuration>B<">
>
>  Use this directive to start an instance of ldirectord for
> @@ -662,6 +669,7 @@ use vars qw(
>            $EMAILALERT
>            $EMAILALERTFREQ
>            $EMAILALERTSTATUS
> +           $SMTP
>            $CLEANSTOP
>
>            $CALLBACK
> @@ -1422,6 +1430,9 @@ sub read_config
>                                        ($1 eq "yes" || $1 eq "no")
>                                                or &config_error($line, 
> "cleanstop must be 'yes' or 'no'");
>                                        $vsrv{cleanstop} = $1;
> +                               } elsif  ($rcmd =~ /^smtp\s*=\s*(.*)/) {
> +                                       $1 =~ /(^([0-9A-Za-z._+-]+))/ or 
> &config_error($line, "invalid SMTP server address");
> +                                       $vsrv{smtp} = $1;
>                                } else {
>                                        &config_error($line, "Unknown command 
> \"$linedata\"");
>                                }
> @@ -1540,6 +1551,10 @@ sub read_config
>                        ($1 eq "yes" || $1 eq "no")
>                            or &config_error($line, "cleanstop must be 'yes' 
> or 'no'");
>                        $CLEANSTOP = $1;
> +               } elsif  ($linedata  =~ /^smtp\s*=\s*(.*)/) {
> +                       $1 =~ /(^([0-9A-Za-z._+-]+))/ or &config_error($line,
> +                                       "invalid SMTP server address");
> +                       $SMTP = $1;
>                } else {
>                        if ($linedata  =~ /^timeout\s*=\s*(.*)/) {
>                                &config_error($line,
> @@ -4109,14 +4124,13 @@ sub daemon_status_str
>  sub ld_emailalert_send
>  {
>        my ($subject, $v, $rserver, $currenttime) = (@_);
> -       my $emailmsg;
> -       my $emailfh;
>        my $status = 0;
>        my $to_addr;
>        my $frequency;
>        my $virtual_str;
>        my $id;
>        my $statusfilter;
> +       my $smtp_server;
>
>        $frequency = defined $v->{emailalertfreq} ? $v->{emailalert} :
>                                $EMAILALERTFREQ;
> @@ -4143,6 +4157,72 @@ sub ld_emailalert_send
>                return 0;
>        }
>
> +       $smtp_server = defined $v->{smtp} ? $v->{smtp} :
> +                               $SMTP;
> +
> +       if (defined $smtp_server) {
> +               $status = &ld_emailalert_net_smtp($smtp_server, $to_addr, 
> $subject);
> +       }
> +       else {
> +               $status = &ld_emailalert_mail_send($to_addr, $subject);
> +       }
> +
> +       return($status);
> +}
> +
> +# ld_emailalert_net_smtp
> +# Send email alerts via SMTP server
> +# pre: smtp: SMTP server defined
> +# post: message is emailed if SMTP server is valid and working
> +# return: 0 on success
> +#        1 on error
> +
> +sub ld_emailalert_net_smtp
> +{
> +       my ($smtp_server, $to_addr, $subject) = (@_);
> +       my $status = 0;
> +
> +       use Net::SMTP;
> +       use Sys::Hostname;
> +
> +       my $hostname = hostname;
> +
> +       my $smtp = Net::SMTP->new($smtp_server);
> +
> +       if ($smtp) {
> +               $smtp->mail("$ENV{USER}\@$hostname");
> +               $smtp->to($to_addr);
> +               $smtp->data();
> +               $smtp->datasend("From: $ENV{USER}\@$hostname\n");
> +               $smtp->datasend("To: $to_addr\n");
> +               $smtp->datasend("Subject: $subject\n\n");
> +               $smtp->datasend("Log-Message: $subject\n" .
> +                               "Daemon-Status: " .
> +                               &daemon_status_str() . "\n");
> +               $smtp->dataend();
> +               $smtp->quit;
> +       } else {
> +               &ld_log("failed to send SMTP email message\n");
> +               $status = 1;
> +       }
> +
> +       return($status);
> +}
> +
> +# ld_emailalert_mail_send
> +# Send email alerts via Mail::Send
> +# pre: smtp: SMTP server not defined
> +# post: message is emailed if one of the Mail::Send methods works
> +# return: 0 on success
> +#        1 on error
> +
> +sub ld_emailalert_mail_send
> +{
> +       my ($to_addr, $subject) = (@_);
> +       my $emailmsg;
> +       my $emailfh;
> +       my $status = 0;
> +
>        use Mail::Send;
>
>        &ld_log("emailalert: $subject");
> @@ -4160,7 +4240,6 @@ sub ld_emailalert_send
>        return($status);
>  }
>
> -
>  # ld_emailalert_resend
>  # Resend email alerts as neccessary
>  # pre: none
>
--
To unsubscribe from this list: send the line "unsubscribe lvs-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

<Prev in Thread] Current Thread [Next in Thread>