Views: 14781
Last Modified: 06.02.2015

Attention! In order to send notices by Nagios to email, Monitoring (Monitoring in pool) should be activated in the BitrixVM menu.


Thus, the contacts and notice template must be set up in order to receive Nagios notices about various events on the server:

  1. /etc/nagios/objects/contacts.cfg specify the email parameter which is an email of a user who will receive the notification:
    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		email@myaddress.com	; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
    	}
    

    Note: A new contact can be created, but do not forget to indicate it in the section CONTACT GROUPS in the same file. Please refer to the Nagios documentation for detailed instructions on how to do it.

  2. Afterwards, in /etc/nagios/objects/commands.cfg change the lines in the section SAMPLE NOTIFICATION COMMANDS of the MTA launch to:
    # 'notify-host-by-email' command definition
    define command{
       command_name   notify-host-by-email
       command_line   /usr/bin/printf "%b" "Subject: ** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **\n\n ***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/msmtp --host=hostname --port=number --user=username --passwordeval=eval --from=mailfrom@email.com $CONTACTEMAIL$   
       }
    
    # 'notify-service-by-email' command definition
    define command{
       command_name   notify-service-by-email
       command_line   /usr/bin/printf "%b" "Subject: ** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **\n\n ***** 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/msmtp --host=hostname --port=number --user=username --passwordeval=eval --from=mailfrom@email.com $CONTACTEMAIL$   
       } 
    
    where:
    • --host=hostname – smtp server address;
    • --port=number – smtp server port;
    • --user=username – login for authorization on smtp server;
    • --passwordeval=eval – password for authorization on smtp server;
    • --from=mailfrom@email.com – from whom the letter will be sent.

    Note: Also, additional msmtp keys can be set up (TLS options etc.); the list of all msmtp keys can be looked up using the console command: msmtp --help.

  3. Restart Nagios to apply settings:
    service nagios restart

In other words, in the Nagios configuration file we basically change MTA from mail to msmtp with keys and slightly modify the message text. Since msmtp has no Subject: key, we include it in the body of the letter, and it will be correctly processed when received by a mail client.


We can check the work of notifications by, for example, stopping MySQL:

service mysqld stop

By default Nagios will write 3 messages to log with the status CRITICAL\SOFT every minute, and every fourth message will receive the status of CRITICAL\HARD. After that, the command notify-service-by-email will be initiated. It will send the text of the message through msmtp with keys set up above. As a result, within 4-5 minutes, a message similar to this one must be sent to mail:

Subject: ** PROBLEM Service Alert: test1/MySQL: connection to 3306 is CRITICAL ** 

 ***** Nagios *****

Notification Type: PROBLEM

Service: MySQL: connection to 3306
Host: server1
Address: 192.168.2.130
State: CRITICAL

Date/Time: Tue Jan 27 20:15:15 MSK 2015

Additional Info:

Connection refused

After launching the MySQL service by using the command # service mysqld start the message must be delivered to mail:

Subject: ** RECOVERY Service Alert: server1/MySQL: connection to 3306 is OK **

 ***** Nagios *****

Notification Type: RECOVERY

Service: MySQL: connection to 3306
Host: server1
Address: 192.168.2.130
State: OK

Date/Time: Wed Jan 28 12:30:50 MSK 2015

Additional Info:

TCP OK - 0.001 second response time on port 3306

Note: Please refer to the Nagios documentation for more details about email notices.





Courses developed by Bitrix24