Bobcares

Send Alerts From Nagios Core Using Gmail

by | Feb 1, 2023

Wondering how to send Alerts From Nagios Core Using Gmail? Our in-house experts are here to help you out with this article. Our server management support is here to offer a lending hand with your queries and issues.

How to send Alerts From Nagios Core Using Gmail?

Today, let us see the steps followed by support techs to send alerts.

STEP1: Install & Configure Postfix Mail Server

1. Install Postfix Package

[root@nagios ~]# systemctl restart postfix
Copy Code

2. Create SASL Password to able to authenticate with the Gmail server

Open or create the /etc/postfix/sasl/sasl_passwd file and add the SMTP Host, username, and password information: Store Password Inside /etc/postfix/sasl_passwd

[root@nagios ~]# vim /etc/postfix/sasl/sasl_passwd
Copy Code
[smtp.gmail.com]:587 username@gmail.com:exhhwavdchsihk3l
Copy Code

3. Create a HASH DB for the password

Create the hash db file for Postfix by running the postmap command:

postmap /etc/postfix/sasl/sasl_passwd
Copy Code

Compile password to db (we can safely remove previous clear-text file now)

4. Set required ownership/permissions for the following files:

sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl sasl_passwd.db
Copy Code

5. Modify relayhost directive in /etc/postfix/main.cf to match the following example:

Define the following directives at the end of /etc/postfix/main.cf file:

# This tells Postfix to hand off all messages to Gmail, and never do direct delivery.
relayhost = [smtp.gmail.com]:587

# This tells Postfix to provide the username/password when Gmail asks for one.
# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Enable STARTTLS encryption
smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_security_level = verify
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
Copy Code

6: Enable & Restart Postfix Service

[root@nagios ~]# systemctl enable postfix
[root@nagios ~]# systemctl restart postfix
Copy Code

17. Sending an Test Email:

echo  "Mail Body - Test Message from NAgiso Digital Avenue" | mailx -vvv -s "Subjct is Mail Sending from Digital Avenue" username@gmail.com
Copy Code

STEP2: Define Email Notification Commands

[root@nagios ~]# vim /usr/local/nagios/etc/objects/commands.cfg
Copy Code
# '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" | mailx -vvv -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: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | mailx -vvv -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
Copy Code

STEP3: Define Email Contacts and Groups

[root@nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
Copy Code

Once you have that done all you will need is the email addresses you want to use for your alerts.

Define contact section and define email address to need to be get notified.

Modify “contact_name” and “email” address

define contact {

    contact_name            nagiosadmin             ; Short name of user
    use                     generic-contact         ; Inherit default values from generic-contact template (defined above)
    alias                   Nagios Admin            ; Full name of user
    email                   username1@gmail.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}


############### NEW CONTACT ###################
define contact{
        contact_name                    webadmin
        use                             generic-contact
        alias                           Web Admin
        email                           username1@orelit.com
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        service_notification_period     24×7
        host_notification_period        24×7
        service_notification_options    w,u,c,r,f
        host_notification_options       d,u,r,f
}
Copy Code

w = notify on warning states c = critical states r = recovery f = start/stop of flapping d = notify on down states u = notify on unreachable states s = notify on stopped states

You are now ready to move on. The next section will to define a contact group.

Contact groups allow you to group people together so it is easier to alert specific people to certain events.

Each group would have a specific user (or users) associated with it who would be alerted if a problem arises.

Define email group to which the mail need to be sent.

[root@nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
Copy Code
############# NEW CONTACT GROUPS ##############
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 webadmin,mailadmin,nagiosadmin
}
###############################################
Copy Code

STEP4: Define Contact Details in the Host & Service Configuration Section

Once you have defined all of your groups, save that file and close it.

Now you have to attach groups to services so those groups will alert when something is wrong with their specific service.

To do this open up the file…

[root@nagios ~]# vim /usr/local/nagios/etc/hosts/cl2.cfg
Copy Code

Head over to host configuration file and define “contacts” and “contact_groups”

# Host configuration Section
define host {
        use                          linux-server
        host_name                    cl2
        alias                        CentOS 7 - Server
        address                      172.25.10.100
        register                     1
        contact_groups               admins
}
Copy Code

Also you can define “contacts” and “contact_groups” on the

## SNMP Service Definiton - Uptime
define service{
use                     generic-service
host_name               cl2
service_description     System uptime
check_command           SNMP-Uptime!-C digitalavenue
contacts                username@gmail.com
}
Copy Code

STEP5: VERIFY NAGIOS CONFIGS AND CHECK FOR ERROR

[root@nagios ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Copy Code

Now restart Nagios server to reflect the configuration changes.

[root@nagios ~]# systemctl restart nagios httpd
Copy Code

Once your changes have been made, restart Nagios.

You can test notifications via the Nagios web portal. Follows these steps:

Click “Services” in the left menu. Thenlick on any failed service (yellow) in the main area.

Click “Send custom service notifications” in the right menu. Enter a custom “comment” and click the “commit” button.

Check your inbox for your notification email. If you don’t get it, check your server logs to see if there is a system issue on the Nagios server.

[Looking for a solution to another query? We’re happy to help.]

Conclusion

In this article, we provide a quick and simple solution from our Support team to send Alerts From Nagios Core Using Gmail

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Speed issues driving customers away?
We’ve got your back!