Page 2 of 4

Re: set up email alerts using mailx

Posted: Thu Dec 20, 2018 2:12 pm
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 ~]#

Re: set up email alerts using mailx

Posted: Thu Dec 20, 2018 2:57 pm
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.

Re: set up email alerts using mailx

Posted: Thu Dec 20, 2018 4:58 pm
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$
}

Re: set up email alerts using mailx

Posted: Thu Dec 20, 2018 5:53 pm
by tgriep
Yes, that is a typo. I fixed the example.

Re: set up email alerts using mailx

Posted: Thu Jan 10, 2019 5:48 pm
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.

Re: set up email alerts using mailx

Posted: Thu Jan 10, 2019 5:57 pm
by tgriep
In your example, you are specifying a smtp server and a from address, did you add them to the Nagios commands?

Re: set up email alerts using mailx

Posted: Thu Jan 10, 2019 6:36 pm
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!

Re: set up email alerts using mailx

Posted: Fri Jan 11, 2019 9:14 am
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$

Re: set up email alerts using mailx

Posted: Mon Jan 14, 2019 1:32 pm
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.

Re: set up email alerts using mailx

Posted: Mon Jan 14, 2019 5:09 pm
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.