set up email alerts using mailx

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
nib01
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Post by nib01 »

Ive attached command.cfg file.

Here's the output.
[root@nagioscore ~]# ls -l /usr/bin/printf
-rwxr-xr-x. 1 root root 49776 Apr 11 2018 /usr/bin/printf
[root@nagioscore ~]# ls -l /usr/bin/mailx
-rwxr-xr-x. 1 root root 392880 Apr 11 2018 /usr/bin/mailx
[root@nagioscore ~]#
Attachments
commands.cfg
(7.23 KiB) Downloaded 333 times
User avatar
tgriep
Madmin
Posts: 9177
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Post by tgriep »

First, lest put back the New Lines back in to the commands so replace both of the command definitions with the following.

Code: Select all

define command {

    command_name     notify-service-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios Monitor Core Alert*****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nHost: $HOSTNAME$\n\nState: $HOSTSTATE$\n\nAddress: $HOSTADDRESS$\n\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

Code: Select all

define command {

    command_name    notify-host-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios Monitor Core Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\n\nHost: $HOSTALIAS$\n\nAddress: $HOSTADDRESS$\n\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

FYI, I tested out those commands and they both worked and did not generate any errors so the missing " must be somewhere else.

What I did find is that is looks like you tried to comment out thecheck_nt command but a few of the lines were missed so change the following section from

Code: Select all

#define command {
#
	command_name    check_nt
	command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p $ARG1$ $ARG2$
#}
to

Code: Select all

#define command {
#
#	command_name    check_nt
#	command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p $ARG1$ $ARG2$
#}
Save the change and restart nagios by running

Code: Select all

systemctl restart nagios
Try testing the email commands by sending a custom notification and see if they work and do not generate the previous errors in the nagios.log file.
Be sure to check out our Knowledgebase for helpful articles and solutions!
nib01
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Post by nib01 »

I noticed there are two "n" on the "Address" and "Additional" field below. Is that a typo?

define command {

command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios Monitor Core Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\n\nHost: $HOSTALIAS$\n\nnAddress: $HOSTADDRESS$\n\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nnAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
User avatar
tgriep
Madmin
Posts: 9177
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Post by tgriep »

Yes, that is a typo. I fixed the example.
Be sure to check out our Knowledgebase for helpful articles and solutions!
nib01
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Post by nib01 »

Ive generated a test email by sending a custom notification as shown below:

[nagios@nagioscore objects]$ mailx -v -s "Test2" -S smtp=mailrelay.int.company.com -S from=alerts.gainspeed@company.com ricson.liberato@company.com
.
EOT
Null message body; hope that's ok
Resolving host mailrelay.int.company.com . . . done.
Connecting to 10.130.128.30:smtp . . . connected.
220 fihe3nok0735.emea.nsn-net.net ESMTP Thu, 10 Jan 2019 21:44:48 GMT
>>> HELO nagioscore
250 fihe3nok0735.emea.nsn-net.net Hello [10.3.17.130], pleased to meet you
>>> MAIL FROM:<alerts.gainspeed@company.com>
250 2.1.0 <alerts.gainspeed@company.com>... Sender ok
>>> RCPT TO:<ricson.liberato@company.com>
250 2.1.5 <ricson.liberato@company.com>... Recipient ok
>>> DATA
354 Enter mail, end with "." on a line by itself
>>> .
250 2.0.0 x0ALimaO029780 Message accepted for delivery
>>> QUIT
221 2.0.0 fihe3nok0735.emea.nsn-net.net closing connection

Please see attached nagios log and mail log.
Attachments
nagios.log
(737.29 KiB) Downloaded 219 times
User avatar
tgriep
Madmin
Posts: 9177
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Post by tgriep »

In your example, you are specifying a smtp server and a from address, did you add them to the Nagios commands?
Be sure to check out our Knowledgebase for helpful articles and solutions!
nib01
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Post by nib01 »

Could you please help me here? Where should I put the information for smtp server and from address in the command.cfg file? Please see attached command.cfg config file. Thanks!
Attachments
commands.cfg
(7.02 KiB) Downloaded 235 times
User avatar
tgriep
Madmin
Posts: 9177
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Post by tgriep »

For the notify-service-by-email command, you would define the command line this. Adjust the values to the actual SMTP server and from address if needed.

Code: Select all

/usr/bin/printf "%b" "***** Nagios Monitor Core Alert*****\n\Notification Type: $NOTIFICATIONTYPE$\n\nHost: $HOSTNAME$\n\nState: $HOSTSTATE$\n\nAddress: $HOSTADDRESS$\n\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -S smtp=mailrelay.int.company.com -S from=alerts.gainspeed@company.com $CONTACTEMAIL$
For the notify-host-by-email command, do this

Code: Select all

/usr/bin/printf "%b" "***** Nagios Monitor Core Alert *****\n\Notification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\n\nHost: $HOSTALIAS$\n\nAddress: $HOSTADDRESS$\n\State: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nnAdditional Info:\n\n$SERVICEOUTPUT$\" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -S smtp=mailrelay.int.company.com -S from=alerts.gainspeed@company.com $CONTACTEMAIL$
Be sure to check out our Knowledgebase for helpful articles and solutions!
nib01
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Post by nib01 »

Ive made all the changes on the command.cfg file as instructed, and finally email notification it works but only on certain object file or hostgroup.

Ive created another object file called "linuxhosts.cfg" within object directory and added some hosts in this file. Ive configured nagios.cfg script and added linuxhosts.cfg dir path within the nagios.cfg script.

I ran some test by shutting down a host (rancid) completely, but nothing happen when I turn off the host, email notification didn't work.

Please see attached linuxhosts.cfg and command.cfg files.
Attachments
linuxhosts.cfg
(10.36 KiB) Downloaded 254 times
commands.cfg
(7.15 KiB) Downloaded 256 times
User avatar
tgriep
Madmin
Posts: 9177
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Post by tgriep »

Your host called rancid has a Contact Group assigned to it called admin.
Make sure you have added a Contact that you want to receive emails to the Contact Group called admin and that will tie the contact to that host for receiving emails.
Also, make sure the contact is setup to receive emails. Just copy the settings from the working contact.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked