Writing commands to send notifications

Another part of Nagios that can be extended to fit your needs are notifications. These are messages that Nagios sends out whenever a problem occurs, or is resolved.

One way in which the Nagios notification system can be expanded is to create template-based e-mail sending. This will send notifications as both plain text and HTML messages. The template of the e-mail will be kept in separate files.

We will use two npm libraries: handlebars (http://handlebarsjs.com) for template processing and emailjs (https://github.com/eleith/emailjs) for the e-mail sending functionality.

E-mails that contain content in multiple formats need to be wrapped in the multipart/alternative MIME type. This type will contain two sub-parts—first the plain text version, and following this is the HTML version. This order makes e-mail clients choose HTML over plain text if both the types are supported. For the sake of example, we will ignore plain text.

In the same way as how macro substitution works in Nagios commands, templates will replace certain strings such as {{hoststate}} within the template. For example, the following can be used in an HTML template:

<tr><td>Notification type</td> 
<td><b>{{hoststate}}</b></td></tr> 

Similar macros can be used in plain text templates and will be substituted as well.

The following is a script that allows users to be notified in HTML format, through the use of templates:

var handlebars = require('handlebars') 
var email = require('emailjs'); 
var fs = require('fs'); 
 
var map = {} 
var args = process.argv.slice(2); 
['template', 'email', 'type', 'hostname', 'hoststate', 'hostoutput'].forEach(function (key, index) { 
  map[key] = args[index]; 
}); 
 
 
var template = fs.readFileSync(map.template, 'utf8'); 
var html = handlebars.compile(template)(map); 
 
var server = email.server.connect({ 
    user:    process.env.SMTP_USER, 
  password:process.env.SMTP_PASSWORD, 
  host:    process.env.SMTP_HOST, 
  ssl:     true 
}); 
 
var message = { 
  from:    'Nagios <[email protected]>', 
  to:      map.email, 
  subject: 'Notification from Nagios', 
  attachment: 
  [ 
     { 
       data: html, 
       alternative: true 
     } 
  ] 
}; 
 
server.send(message, function(error, message) { 
  console.log(error, message);  
}); 

To test it, simply run the following:

root@ubuntu:# node index.js template1.html 
[email protected] RECOVERY myhost1 OK "OK: host is alive"

This should cause an e-mail to be sent to [email protected]. Note that, in our case the script reads credentials for SMTP e-mail server from environment variables, so they must be set correctly, or another way of passing them should be applied.

We can now define a command that will send a notification for the host, as shown here:

define command{ 
    command_name    notify-host-by-email-html 
    command_line    $USER5$/notify-email-html 
                    template1 '$CONTACTEMAIL$' 
                    '$NOTIFICATIONTYPE$' '$HOSTNAME$'  
                    '$HOSTSTATE$' '$HOSTOUTPUT$' 
    } 

It will pass the appropriate arguments for the user's e-mail address, notification type, host's name, state, and output from the host check. The command can then be used for one or more contacts by setting the host_notification_commands option:

define contact{ 
    name                            jdoe 
    host_notification_period        24x7 
    host_notification_options       d,u,r,f,s 
    host_notification_commands      notify-host-by-email-html 
    (...) 
    } 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.190.144