Using mailx to send emails on Nagios core

Hello ,

I used Mailx to send emails using Nagios core 4.4.6 is the SSH was down for example

The problem that I receive only a message like this one

** CUSTOM Service Alert: Box/Check SSH is CRITICAL

But no information about the host or the IP address of the Linux machine that the SSH was down on it ?

Is there any help to fix this issue ?

Best Regards

Hello,

The contents of the body of a Nagios e-mail are determined by the Nagios command used to generate that e-mail. For example, for most services, ultimately an e-mail notification is generated by running the notify-service-by-email command. If you look at the file in which your Nagios server defines its internal commands (normally called commands.cfg, but check your master nagios.cfg to be sure), you'll see a command definition for notify-service-by-email that most probably looks something like this:

define command {
    command_name    notify-service-by-email
    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" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

The basic idea here is that the content and formatting of the e-mail's contents is taken care of by the printf command, which is then piped into /usr/bin/mail (in this case) with the subject being set using the -s flag. The contents for all of these are populated directly using internal Nagios variables, such as $NOTIFICATIONTYPE$, $HOSTALIAS$, and so on and so forth.

So essentially, the e-mails your Nagios server sends will contain whatever content the command you've defined to send them says they should. Likewise, their formatting is also controlled by that command. If this is a custom check, as you say, then it appears either you're also using a custom notification command, or you're not including all the necessary Nagios variables (such as $HOSTALIAS$, $SERVICEDESC$ and $SERVICESTATE$, at a minimum) in your e-mail generation command.

Hope this helps !

Thanks a lot the problem is solved but I have another question now I use only the contacts.cfg to receive emails if I have problems but how to configure a contact details only for a specific host for example

I have a host with 192.168.100.50 and I need a contact only for this host ?

Hi,

Contacts on their own do not directly correspond to any checks that Nagios carries out. For each service or host check you define, you must specify the contact or contact group that should be used for notifications for that configuration entry.

So to use a specific contact for this specific host, you would first define the contact you wanted to use for your 192.168.100.50 host, and then create host and service config entries for that host. And in each of those entries, you could then specify the desired contact which would then be used for alert notifications for that host or service.

Essentially, think of setting up a Nagios check as a multi-part process, that will typically go like this:

  1. Create your contacts and/or contact groups
  2. Create your hosts and/or host groups
  3. Create the service checks for those hosts and/or host groups, using the appropriate contact for notifications

Hope this helps. If not, then if you can let me know what isn't clear, we'll see what we can do to assist you further.

Thanks a lot for the quick response so you mean

When I add define host and the information of the host then I have to add the contact information of this host? And for each service on that host also I need to add a contact information if something wrong happend?

Can you please provide me at least with one example I mean like a template?

Best regards

Hi,

OK. You can either define a contact as part of a host definition, like this:

define host {
    use                     linux-server
    contacts                nagiosadmin
    host_name               localhost
    alias                   localhost
    address                 127.0.0.1
}

Or you can specify it in an individual service definition, like so:

define service {
    use                     local-service
    host_name               localhost
    contacts                nagiosadmin
    service_description     HTTP
    check_command           check_http
    notifications_enabled   1
}

So if you specify a contact for a host, that contact will be used by default for all services defined for that host, as well as for the host itself, naturally. But you could then also specify a different contact for specific services (e.g. a 'webmasters' contact group for an HTTP service check, or something similar), which would then over-ride the contact specified for the host. You can also include multiple contacts by separating them with commas.

Lastly, if you want to specify whole contact groups rather than a contact, you can use the contact_groups directive, rather than contacts.

Hope this helps.

1 Like

Hi,

As an aside to this particular issue, I would like to offer a bit of general advice. As someone who is relatively new to Nagios, I'd encourage you to take some time to familiarise yourself with how it all works, at a conceptual level as well as a technical level. Once you actually get a feel for how Nagios "thinks", so to speak, all of this will make a lot more sense and come more easily to you.

Fortunately Nagios has some excellent on-line documentation. For Nagios Core 4.x (which is what I believe you're running), this makes an excellent starting point:

Nagios Core - Table of Contents

More specifically, you may find these sections in particular to be useful and of interest:

Nagios Core - Advice for Beginners

Nagios Core - Configuration Overview

Nagios Core - Object Configuration Overview

Hope this helps !

2 Likes

This is also what I advised in the other Nagios topic by @Zurattos .

It seems to me @Zurattos is trying to configure software when he does not take the time to first understand the basics of how the software works.

Sometimes (often, actually) it’s actually quicker and easier to read the documentation first, not last :slight_smile:

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.