All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ldirectord: Allow sending email via external SMTP server
@ 2009-04-27 19:54 Caleb Anthony
  2009-04-28  6:26 ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Caleb Anthony @ 2009-04-27 19:54 UTC (permalink / raw)
  To: lvs-devel; +Cc: horms

[-- Attachment #1: Type: text/plain, Size: 808 bytes --]

Hello all,

Attached is a patch to ldirectord (version 1.186-ha-2.1.4) that adds a
new configuration directive "smtp". This directive allows ldirectord
to send notification emails via an external SMTP server instead of
using the built-in Mail::Mailer methods. I wrote this patch because
none of the methods in Mail::Mailer were convenient for use in our
environment. However, we do run an enterprise wide SMTP relay for
internal notifications, such as those generated by ldirectord. I would
imagine many other organizations are in the same situation we are, and
this would be a good addition to ldirectord.

Feel free to add/change/remove anything that doesn't seem right with the code.

Also, let me know if should post this to the Linux-HA development
mailing list instead of here.

Thanks.

Caleb Anthony

[-- Attachment #2: ldirectord.patch --]
[-- Type: application/octet-stream, Size: 2131 bytes --]

--- /tmp/ldirecord.orig/ldirectord	2009-01-09 14:55:49.000000000 -0700
+++ /tmp/ldirecord.new/ldirectord	2009-04-27 13:33:58.000000000 -0600
@@ -229,6 +229,13 @@
 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
@@ -611,6 +618,7 @@
 	    $EMAILALERT
 	    $EMAILALERTFREQ
 	    $EMAILALERTSTATUS
+	    $SMTP
 
 	    $CALLBACK
 	    $CFGNAME
@@ -1321,6 +1329,9 @@
 						&config_error($line, "unable to open monitorfile $monitorfile: $!");
 					}
 					$vsrv{monitorfile} = $monitorfile;
+				} 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\"");
 				}
@@ -1432,6 +1443,10 @@
 			$EMAILALERTFREQ = $1;
 		} elsif  ($linedata  =~ /^emailalertstatus\s*=\s*(.*)/) {
 			$EMAILALERTSTATUS = &parse_emailalertstatus($line, $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, 
@@ -3928,6 +3943,34 @@
 		$status = 1;
 	}
 
+	use Net::SMTP;
+	use Sys::Hostname;
+
+	my $smtp_server;
+	my $hostname = hostname;
+	
+	$smtp_server = defined $v->{smtp} ? $v->{smtp} :
+				$SMTP;
+
+	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);
 }
 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-04-27 19:54 [PATCH] ldirectord: Allow sending email via external SMTP server Caleb Anthony
@ 2009-04-28  6:26 ` Simon Horman
  2009-04-28 13:47   ` Caleb Anthony
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Horman @ 2009-04-28  6:26 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

On Mon, Apr 27, 2009 at 01:54:07PM -0600, Caleb Anthony wrote:
> Hello all,
> 
> Attached is a patch to ldirectord (version 1.186-ha-2.1.4) that adds a
> new configuration directive "smtp". This directive allows ldirectord
> to send notification emails via an external SMTP server instead of
> using the built-in Mail::Mailer methods. I wrote this patch because
> none of the methods in Mail::Mailer were convenient for use in our
> environment. However, we do run an enterprise wide SMTP relay for
> internal notifications, such as those generated by ldirectord. I would
> imagine many other organizations are in the same situation we are, and
> this would be a good addition to ldirectord.
> 
> Feel free to add/change/remove anything that doesn't seem right with the code.
> 
> Also, let me know if should post this to the Linux-HA development
> mailing list instead of here.
> 
> Thanks.

Hi Caleb,

I'm a little confused about how this is different to
Using the "smtp" type for Mail::Mailer? Could you explain
a little about why that wasn't useful for your set-up?

Thanks

-- 
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-04-28  6:26 ` Simon Horman
@ 2009-04-28 13:47   ` Caleb Anthony
  2009-04-28 15:11     ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Caleb Anthony @ 2009-04-28 13:47 UTC (permalink / raw)
  To: Simon Horman; +Cc: lvs-devel

From what I understand about Mail::Mailer (maybe incorrectly!), in
order to use the SMTP functionality, you have to be running an SMTP
server on the same server as ldirectord. That isn't an option in our
organization, so I had to add the ability to use an external SMTP
server.

Am I wrong about Mail::Mailer? Can it use an SMTP server other than
one on the server that ldirectord is also running on?

Thanks.

On Tue, Apr 28, 2009 at 12:26 AM, Simon Horman <horms@verge.net.au> wrote:
> On Mon, Apr 27, 2009 at 01:54:07PM -0600, Caleb Anthony wrote:
>> Hello all,
>>
>> Attached is a patch to ldirectord (version 1.186-ha-2.1.4) that adds a
>> new configuration directive "smtp". This directive allows ldirectord
>> to send notification emails via an external SMTP server instead of
>> using the built-in Mail::Mailer methods. I wrote this patch because
>> none of the methods in Mail::Mailer were convenient for use in our
>> environment. However, we do run an enterprise wide SMTP relay for
>> internal notifications, such as those generated by ldirectord. I would
>> imagine many other organizations are in the same situation we are, and
>> this would be a good addition to ldirectord.
>>
>> Feel free to add/change/remove anything that doesn't seem right with the code.
>>
>> Also, let me know if should post this to the Linux-HA development
>> mailing list instead of here.
>>
>> Thanks.
>
> Hi Caleb,
>
> I'm a little confused about how this is different to
> Using the "smtp" type for Mail::Mailer? Could you explain
> a little about why that wasn't useful for your set-up?
>
> Thanks
>
> --
> Simon Horman
>  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
>  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
>
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-04-28 13:47   ` Caleb Anthony
@ 2009-04-28 15:11     ` Simon Horman
  2009-05-01 19:42       ` Caleb Anthony
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Horman @ 2009-04-28 15:11 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

On Tue, Apr 28, 2009 at 07:47:07AM -0600, Caleb Anthony wrote:
> >From what I understand about Mail::Mailer (maybe incorrectly!), in
> order to use the SMTP functionality, you have to be running an SMTP
> server on the same server as ldirectord. That isn't an option in our
> organization, so I had to add the ability to use an external SMTP
> server.
> 
> Am I wrong about Mail::Mailer? Can it use an SMTP server other than
> one on the server that ldirectord is also running on?

Sorry, I was a bit confused.

I think that Mail::Mailer can probably do what you are after,
but the current ldirectord code uses Mail::Send, which does
not apper to be able to access an SMTP server.

With regards to the patch, I think that it might be nice
to arrance things such that there are two functions,
one whcih contains the existing Mail::Send code, including
the call to "use Mail::Send;". One that includes your new code,
including any calls to use or require. And have ld_emailalert_send()
call one of these functions depending on if $SMTP is set or not.

-- 
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-04-28 15:11     ` Simon Horman
@ 2009-05-01 19:42       ` Caleb Anthony
  2009-05-05 12:47         ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Caleb Anthony @ 2009-05-01 19:42 UTC (permalink / raw)
  To: Simon Horman; +Cc: lvs-devel

I think that's a good idea, I'll see what I can do to get a patch that
adds that functionality.

On Tue, Apr 28, 2009 at 9:11 AM, Simon Horman <horms@verge.net.au> wrote:
> On Tue, Apr 28, 2009 at 07:47:07AM -0600, Caleb Anthony wrote:
>> >From what I understand about Mail::Mailer (maybe incorrectly!), in
>> order to use the SMTP functionality, you have to be running an SMTP
>> server on the same server as ldirectord. That isn't an option in our
>> organization, so I had to add the ability to use an external SMTP
>> server.
>>
>> Am I wrong about Mail::Mailer? Can it use an SMTP server other than
>> one on the server that ldirectord is also running on?
>
> Sorry, I was a bit confused.
>
> I think that Mail::Mailer can probably do what you are after,
> but the current ldirectord code uses Mail::Send, which does
> not apper to be able to access an SMTP server.
>
> With regards to the patch, I think that it might be nice
> to arrance things such that there are two functions,
> one whcih contains the existing Mail::Send code, including
> the call to "use Mail::Send;". One that includes your new code,
> including any calls to use or require. And have ld_emailalert_send()
> call one of these functions depending on if $SMTP is set or not.
>
> --
> Simon Horman
>  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
>  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
>
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-01 19:42       ` Caleb Anthony
@ 2009-05-05 12:47         ` Simon Horman
  2009-05-07 20:09           ` Caleb Anthony
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Horman @ 2009-05-05 12:47 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

On Fri, May 01, 2009 at 01:42:06PM -0600, Caleb Anthony wrote:
> I think that's a good idea, I'll see what I can do to get a patch that
> adds that functionality.

Great, thanks.

-- 
Simon Horman
  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-05 12:47         ` Simon Horman
@ 2009-05-07 20:09           ` Caleb Anthony
  2009-05-08  0:28             ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Caleb Anthony @ 2009-05-07 20:09 UTC (permalink / raw)
  To: Simon Horman; +Cc: lvs-devel

[-- Attachment #1: Type: text/plain, Size: 851 bytes --]

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.

Caleb

On Tue, May 5, 2009 at 6:47 AM, Simon Horman <horms@verge.net.au> wrote:
> On Fri, May 01, 2009 at 01:42:06PM -0600, Caleb Anthony wrote:
>> I think that's a good idea, I'll see what I can do to get a patch that
>> adds that functionality.
>
> Great, thanks.
>
> --
> Simon Horman
>  VA Linux Systems Japan K.K. Satellite Lab in Sydney, Australia
>  H: www.vergenet.net/~horms/            W: www.valinux.co.jp/en
>
>

[-- Attachment #2: ldirectord.patch --]
[-- Type: application/octet-stream, Size: 3462 bytes --]

--- /tmp/ldirectord.orig/ldirectord	2009-01-09 14:55:49.000000000 -0700
+++ /tmp/ldirectord.smtp/ldirectord	2009-05-07 14:06:46.000000000 -0600
@@ -229,6 +229,13 @@
 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
@@ -611,6 +618,7 @@
 	    $EMAILALERT
 	    $EMAILALERTFREQ
 	    $EMAILALERTSTATUS
+	    $SMTP
 
 	    $CALLBACK
 	    $CFGNAME
@@ -1321,6 +1329,9 @@
 						&config_error($line, "unable to open monitorfile $monitorfile: $!");
 					}
 					$vsrv{monitorfile} = $monitorfile;
+				} 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\"");
 				}
@@ -1432,6 +1443,10 @@
 			$EMAILALERTFREQ = $1;
 		} elsif  ($linedata  =~ /^emailalertstatus\s*=\s*(.*)/) {
 			$EMAILALERTSTATUS = &parse_emailalertstatus($line, $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, 
@@ -3880,14 +3895,13 @@
 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;
@@ -3914,6 +3928,72 @@
 		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");
@@ -3931,7 +4011,6 @@
 	return($status);
 }
 
-
 # ld_emailalert_resend
 # Resend email alerts as neccessary
 # pre: none

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-07 20:09           ` Caleb Anthony
@ 2009-05-08  0:28             ` Simon Horman
  2009-05-08 13:38               ` Caleb Anthony
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Horman @ 2009-05-08  0:28 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

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);
 }
 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-08  0:28             ` Simon Horman
@ 2009-05-08 13:38               ` Caleb Anthony
  2009-05-08 15:26                 ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Caleb Anthony @ 2009-05-08 13:38 UTC (permalink / raw)
  To: Simon Horman; +Cc: lvs-devel

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@verge.net.au> 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
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-08 13:38               ` Caleb Anthony
@ 2009-05-08 15:26                 ` Simon Horman
  2009-05-09  2:05                   ` Simon Horman
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Horman @ 2009-05-08 15:26 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

On Fri, May 08, 2009 at 07:38:34AM -0600, Caleb Anthony wrote:
> 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?

Sure, will do. Though I'll hold off on the commit until after I get some sleep.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ldirectord: Allow sending email via external SMTP server
  2009-05-08 15:26                 ` Simon Horman
@ 2009-05-09  2:05                   ` Simon Horman
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Horman @ 2009-05-09  2:05 UTC (permalink / raw)
  To: Caleb Anthony; +Cc: lvs-devel

On Sat, May 09, 2009 at 01:26:57AM +1000, Simon Horman wrote:
> On Fri, May 08, 2009 at 07:38:34AM -0600, Caleb Anthony wrote:
> > 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?

I have moved the &ld_log() call to the parent function applied the change.

http://hg.linux-ha.org/dev/rev/fdd0e04fb520

If any further changes/fixes are needed please make them
as an additional patch.

Thanks


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-05-09  2:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-27 19:54 [PATCH] ldirectord: Allow sending email via external SMTP server Caleb Anthony
2009-04-28  6:26 ` Simon Horman
2009-04-28 13:47   ` Caleb Anthony
2009-04-28 15:11     ` Simon Horman
2009-05-01 19:42       ` Caleb Anthony
2009-05-05 12:47         ` Simon Horman
2009-05-07 20:09           ` Caleb Anthony
2009-05-08  0:28             ` Simon Horman
2009-05-08 13:38               ` Caleb Anthony
2009-05-08 15:26                 ` Simon Horman
2009-05-09  2:05                   ` Simon Horman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.