Skip to content

Commit

Permalink
Merge pull request #775 from mcnewton/namecomment
Browse files Browse the repository at this point in the history
ldirectord: Add new "servicename" and "comment" options to each virtual server
  • Loading branch information
krig committed May 5, 2016
2 parents c6d605e + 9884757 commit 5a7ec85
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ldirectord/ldirectord.cf
Expand Up @@ -23,6 +23,8 @@ quiescent=no

# Sample for an http virtual service
virtual=192.168.6.240:80
servicename=Web Site
comment=Test load balanced web site
real=192.168.6.2:80 gate
real=192.168.6.3:80 gate
real=192.168.6.6:80 gate
Expand Down
53 changes: 49 additions & 4 deletions ldirectord/ldirectord.in 100644 → 100755
Expand Up @@ -712,6 +712,17 @@ can be used only for UDP services. If this option is specified, all connections
are created only to schedule one packet. Option is useful to schedule
UDP packets from same client port to different real servers.
B<servicename = >I<short name>
A name for this service. This is for the sole purpose of making it easier
to know which service is affected when e-mail notifications are sent out.
It will be included in the e-mail subject and body.
B<comment = >I<comment>
Notes about this service to be included in e-mail notifications (for example,
purpose of the service or relevant administrator to contact).
=head1 IPv6
Directives for IPv6 are virtual6, real6, fallback6.
Expand Down Expand Up @@ -1617,6 +1628,10 @@ sub read_config
} elsif ($rcmd =~ /^smtp\s*=\s*(.*)/) {
$1 =~ /(^([0-9A-Za-z._+-]+))/ or &config_error($line, "invalid SMTP server address");
$vsrv{smtp} = $1;
} elsif ($rcmd =~ /^servicename\s*=\s*(.*)/) {
$vsrv{servicename} = $1;
} elsif ($rcmd =~ /^comment\s*=\s*(.*)/) {
$vsrv{comment} = $1;
} else {
&config_error($line, "Unknown command \"$linedata\"");
}
Expand Down Expand Up @@ -4430,6 +4445,7 @@ sub ld_emailalert_send
my $id;
my $statusfilter;
my $smtp_server;
my $virtual_info;

$frequency = defined $v->{emailalertfreq} ? $v->{emailalertfreq} :
$EMAILALERTFREQ;
Expand Down Expand Up @@ -4460,16 +4476,43 @@ sub ld_emailalert_send
$SMTP;

&ld_log("emailalert: $subject");

# get extra service details
$virtual_info = _ld_virtual_server_details($v);

# add service name into e-mail subject if it has been set
if ($v->{servicename}) {
$subject = "[" . $v->{servicename} ."] $subject";
}

if (defined $smtp_server) {
$status = &ld_emailalert_net_smtp($smtp_server, $to_addr, $subject);
$status = &ld_emailalert_net_smtp($smtp_server, $to_addr, $subject, $virtual_info);
}
else {
$status = &ld_emailalert_mail_send($to_addr, $subject);
$status = &ld_emailalert_mail_send($to_addr, $subject, $virtual_info);
}

return($status);
}

# generate virtual server information to go in to alert e-mails
sub _ld_virtual_server_details
{
my ($v) = @_;
my $details;

if ($v->{servicename}) {
$details .= "Service name: " . $v->{servicename} . "\n"
}

if ($v->{comment}) {
$details .= "Comment: " . $v->{comment} . "\n";
}

return $details;
}


# ld_emailalert_net_smtp
# Send email alerts via SMTP server
# pre: smtp: SMTP server defined
Expand All @@ -4478,7 +4521,7 @@ sub ld_emailalert_send
# 1 on error
sub ld_emailalert_net_smtp
{
my ($smtp_server, $to_addr, $subject) = (@_);
my ($smtp_server, $to_addr, $subject, $extrabody) = (@_);
my $status = 0;

use Net::SMTP;
Expand All @@ -4504,6 +4547,7 @@ sub ld_emailalert_net_smtp
"Log-Message: $subject\n" .
"Daemon-Status: " .
&daemon_status_str() . "\n");
$smtp->datasend("\n$extrabody\n") if ($extrabody);
$smtp->dataend();
$smtp->quit;
} else {
Expand All @@ -4522,7 +4566,7 @@ sub ld_emailalert_net_smtp
# 1 on error
sub ld_emailalert_mail_send
{
my ($to_addr, $subject) = (@_);
my ($to_addr, $subject, $extrabody) = (@_);
my $emailmsg;
my $emailfh;
my $status = 0;
Expand All @@ -4535,6 +4579,7 @@ sub ld_emailalert_mail_send
print $emailfh "ldirectord host: " . hostname() . "\n" .
"Log-Message: $subject\n" .
"Daemon-Status: " . &daemon_status_str() . "\n";
print $emailfh "\n$extrabody\n" if ($extrabody);
unless ($emailfh->close) {
&ld_log("failed to send email message\n");
$status = 1;
Expand Down

0 comments on commit 5a7ec85

Please sign in to comment.