set up email alerts using mailx

An open discussion forum for obtaining help with Nagios Core. Nagios Core users of all experience levels are welcome here. Subforum have been created for the discussion of Nagios Core and Nagios Plugin development.

NOTE: The SourceForge.net mailing lists have been deprecated in favor of this forum in order to expedite support and provide additional features not available on the old mailing list.

Re: set up email alerts using mailx

Postby nib01 » Thu Dec 20, 2018 2:12 pm

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 108 times
nib01
 
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Postby tgriep » Thu Dec 20, 2018 2:57 pm

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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
tgriep
Madmin
 
Posts: 8375
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Postby nib01 » Thu Dec 20, 2018 4:58 pm

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$
}
nib01
 
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Postby tgriep » Thu Dec 20, 2018 5:53 pm

Yes, that is a typo. I fixed the example.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
tgriep
Madmin
 
Posts: 8375
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Postby nib01 » Thu Jan 10, 2019 5:48 pm

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 66 times
nib01
 
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Postby tgriep » Thu Jan 10, 2019 5:57 pm

In your example, you are specifying a smtp server and a from address, did you add them to the Nagios commands?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
tgriep
Madmin
 
Posts: 8375
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Postby nib01 » Thu Jan 10, 2019 6:36 pm

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 75 times
nib01
 
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Postby tgriep » Fri Jan 11, 2019 9:14 am

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$
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
tgriep
Madmin
 
Posts: 8375
Joined: Thu Oct 30, 2014 9:02 am

Re: set up email alerts using mailx

Postby nib01 » Mon Jan 14, 2019 1:32 pm

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 84 times
commands.cfg
(7.15 KiB) Downloaded 80 times
nib01
 
Posts: 31
Joined: Tue Oct 09, 2018 9:31 am

Re: set up email alerts using mailx

Postby tgriep » Mon Jan 14, 2019 5:09 pm

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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
tgriep
Madmin
 
Posts: 8375
Joined: Thu Oct 30, 2014 9:02 am

PreviousNext

Return to Nagios Core

Who is online

Users browsing this forum: kmartin2007, MSN [Bot], sandeepatil and 27 guests