Fix SMTP without authentication

Closes-bug: #1558140
Change-Id: I1272414be40d79f2f32c937a0b6e7c852b649af5
This commit is contained in:
Swann Croiset 2016-07-12 17:29:30 +02:00 committed by Simon Pasquier
parent 9195f763bd
commit 3ce87b3aac
4 changed files with 46 additions and 26 deletions

View File

@ -23,7 +23,7 @@ define nagios::contact (
$properties = {},
$send_from = 'nagios@localhost.localdomain',
$smtp_auth = 'none',
$smtp_host = '127.0.0.1:25',
$smtp_host = undef,
$smtp_user = undef,
$smtp_password = undef,
$ensure = present,
@ -41,8 +41,8 @@ define nagios::contact (
$_smtp_auth = $smtp_auth
}
if $_smtp_auth and (! $smtp_user or ! $smtp_password) {
fail("smtp_user and smtp_password must be provided with smtp_auth = ${smtp_auth}")
if $_smtp_auth and ! ($smtp_user and $smtp_password and $smtp_host) {
fail("smtp_host, smtp_user and smtp_password must be provided with smtp_auth = ${smtp_auth}")
}
$opts = {}
@ -65,7 +65,14 @@ define nagios::contact (
$opts['contactgroups'] = $properties['contactgroups']
}
if ! defined(Package[$nagios::params::package_mailx_smtp]){
package { $nagios::params::package_mailx_smtp:
ensure => present,
}
}
if $_smtp_auth {
# SMTP authentication
if $properties['service_notification_commands'] == undef {
$opts['service_notification_commands'] = $nagios::params::service_notification_command_by_smtp
}
@ -86,26 +93,32 @@ define nagios::contact (
require => Package[$nagios::params::package_mailx_smtp],
}
}
if ! defined(Package[$nagios::params::package_mailx_smtp]){
package { $nagios::params::package_mailx_smtp:
ensure => present,
}
}
} else {
if $properties['service_notification_commands'] == undef {
$opts['service_notification_commands'] = $nagios::params::service_notification_command
}
if $properties['host_notification_commands'] == undef {
$opts['host_notification_commands'] = $nagios::params::service_notification_command
}
file {"${path}/cmd_notify-service-by-email-with-long-service-output.cfg":
ensure => present,
source => 'puppet:///modules/nagios/cmd_notify_service_by_email.cfg',
owner => 'root',
group => 'root',
mode => '0644',
}
if $properties['service_notification_commands'] == undef {
$opts['service_notification_commands'] = $nagios::params::service_notification_command
}
if $properties['host_notification_commands'] == undef {
$opts['host_notification_commands'] = $nagios::params::service_notification_command
}
if ! $smtp_host {
# No SMTP server is provided then the local MTA is used (local socket).
file {"${path}/cmd_notify-service-by-email-with-long-service-output.cfg":
ensure => present,
content => template('nagios/cmd_notify_service_by_email.cfg.erb'),
owner => 'root',
group => 'root',
mode => '0644',
}
} else {
# Use SMTP without authentication
file {"${path}/cmd_notify-service-by-email-with-long-service-output.cfg":
ensure => present,
content => template('nagios/notify-by-smtp-noauth.cfg.erb'),
owner => 'root',
group => 'root',
mode => '0644',
}
}
}
if $onefile {

View File

@ -0,0 +1,5 @@
define command{
command_name notify-service-by-email-with-long-service-output
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n$LONGSERVICEOUTPUT$\n" | /usr/bin/bsd-mailx -a "From: <%= @send_from %>" -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

View File

@ -1,4 +1,6 @@
define command{
command_name notify-service-by-email-with-long-service-output
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n$LONGSERVICEOUTPUT$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n$LONGSERVICEOUTPUT$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -r '<%= @send_from %>' \
-S smtp="smtp://<%= @smtp_host %>" \
$CONTACTEMAIL$
}

View File

@ -58,12 +58,12 @@ attributes:
smtp_host:
value: ''
label: 'SMTP server IP and port'
description: 'ie: 10.2.2.3:25'
label: 'External SMTP server and port'
description: 'IP address (or fully qualified domain name) and port of the external SMTP server. Leave empty to use the local MTA service.'
weight: 49
type: "text"
regex:
source: '^[a-zA-Z\d][a-zA-Z\d_\-.]+(:[0-9]+)?$'
source: '^$|[a-zA-Z\d][a-zA-Z\d_\-.]+(:[0-9]+)?$'
error: 'Invalid address or name'
restrictions: *disable_notifications