On the CentOS 7, the notify by email command is defined in /usr/local/nagios/etc/objects/commands.cfg

################################################################################
#
# SAMPLE NOTIFICATION COMMANDS
#
# These are some example notification commands.  They may or may not work on
# your system without modification.  As an example, some systems will require
# you to use "/usr/bin/mailx" instead of "/usr/bin/mail" in the commands below.
#
################################################################################

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\
nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is
$HOSTSTATE$ **" $CONTACTEMAIL$
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOST
ALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -s
"** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

The $CONTACTEMAIL$ is defined /usr/local/nagios/etc/objects/contacts.cfg

# Just one contact defined by default - the Nagios admin (that's you)
# This contact definition inherits a lot of default values from the 'generic-contact'
# template which is defined elsewhere.

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios-Admin_Frank      ; Full name of user
        service_notification_commands   notify-by-email
        host_notification_commands      host-notify-by-email
        host_notifications_enabled      1
        service_notifications_enabled   1
        email                           [email protected]         ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

###############################################################################
#
# CONTACT GROUPS
#
###############################################################################

# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.

define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin
        }

 

Mailx

First off all, make sure you installed heirloom-mailx.

If you want to user external smtp server, mailx will be a good option, it’s something like this:

$ echo "This is the message body and contains the message" | mailx -v -r "[email protected]" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="abc123" -S ssl-verify=ignore [email protected]

If you use Gmail, you need to enable less secure apps settings before you can send mail.

Note, on RHEL or CentOS, you need to add below option:

-S nss-config-dir="/etc/pki/nssdb/"

Eg. If your server does support encryption, so we set -S smtp-auth=plain to adopt that, in nagios the setting will be like:

define command{
 command_name notify-host-by-email
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -v -r "[email protected]" -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -S smtp="smtp.server.com" -S smtp-auth=plain -S smtp-auth-user="nagios" -S smtp-auth-password="password" $CONTACTEMAIL$

}
Notification interval

By default, the Nagios will send you warning or critical alarm every 60 minutes. You can change this by setup the notification_interval parameters in the host definition or Service definition.

Or you can change this globally by change the interval_lengh in the main configuration file nagios.cfg

References

How To Send Alerts From Nagios Core Using Gmail And Yahoo

 

9 mail/mailx command examples to send emails from command line on Linux