Custom Nagios Notifications, now with Jade

Recently, I posted about using coffee-script and node-mailer to send custom Nagios notification emails.  While it is a great way of extending the capabilities of Nagios Alerts by allowing any arbitrary hook into other coffee / javascript apis (such as node-mailer), it is also capable of creating pretty HTML e-mails with the help of the Jade template engine.

Since I've already created the Nagios Command and bound it to my contact information, all I need to do is install jade into the project folder's node_modules:

$ npm install jade

We then need to create our templates.  Since I wanted to use a layout, I created a jade sub-folder in my projects folder.

Here is the layout (./jade/layout.jade):
doctype 5
html
head
link(rel='stylesheet', href='http://domain.com/components/bootstrap/docs/assets/css/bootstrap.css')
link(rel='stylesheet', href='http://domain.com/components/bootstrap/docs/assets/css/bootstrap-responsive.css')
link(rel='stylesheet', href='http://domain.com/components/font-awesome/css/font-awesome.min.css')
script(type="text/javascript", src="http://domain.com/components/jquery/jquery.min.js")
script(type="text/javascript", src="http://domain.com/components/bootstrap/docs/assets/js/bootstrap.js")
body
block content
view raw layout.jade hosted with ❤ by GitHub

Here is a neat header bar that I'm using(./jade/nav-bar-email.jade):
.container-fluid
.navbar
.navbar-inner
a.brand(href="domain.com/") Company
ul.nav
li
a(href="domain.com/") Home
li
a(href="domain.com/projects") Projects
li
a(href="domain.com/about") About
ul.nav.pull-right
li
a(href="domain.com/docs") Docs

Here is the alert e-mail(./jade/nagiosAlert.jade):
extends layout.jade
block content
include nav-bar-email
.container-fluid
.row-fluid
.span12
.well
i.icon-warning-sign.icon-4x.pull-right
b
p.lead Nagios Alert for #{hostname}
hr
.container-fluid
.row-fluid
.span3
label.label Hostname:
.span9
span #{hostname}
if service
.row-fluid
.span3
label.label service:
.span9
span #{service}
.row-fluid
.span3
label.label ip:
.span9
span #{ip}
.row-fluid
.span3
label.label notify:
.span9
span #{notify_type}
.row-fluid
.span3
label.label status:
.span9
span #{status}
.row-fluid
.span3
label.label date:
.span9
span #{date}
.row-fluid
.span3
label.label output:
.span9
span #{output}

Here is what my notify-service.coffee script looks like(/opt/bin/notify-service.coffee):
#!/usr/bin/env coffee
#
# incoming argv is order sensitive
#
# [0] - coffee
# [1] - /opt/bin/notify-service.coffee
# [2] - hostname
# [3] - service
# [4] - IP address
# [5] - notification type [ PROBLEM, RECOVERY, OK ]
# [6] - status [ OK, WARNING, CRITICAL ]
# [7] - long date time
# [8] - service output
require 'coffee-script'
nodemailer = require 'nodemailer'
jade = require 'jade'
argvObject =
hostname: process.argv[2]
service: process.argv[3]
ip: process.argv[4]
notify_type: process.argv[5]
status: process.argv[6]
date: process.argv[7]
output: process.argv[8]
smtpTransport = nodemailer.createTransport "SMTP"
text = "Nagios requires your attention.\n
\n
Hostname:\t\t" + argvObject.hostname + "\n
service:\t\t" + argvObject.service + "\n
ip:\t\t" + argvObject.ip + "\n
notify:\t\t" + argvObject.notify_type + "\n
status:\t\t" + argvObject.status + "\n
date:\t\t" + argvObject.date + "\n
output:\t\t" + argvObject.output + "\n
"
jade.renderFile '/opt/bin/jade/nagiosAlert.jade', argvObject, (err, html)->
if err
console.log err
mailOptions =
from: "Nagios <admin@domain.com>"
to: "admin@domain.com"
subject: argvObject.hostname + "'s " + argvObject.service +
" IS " + argvObject.status
text: text
html: html
smtpTransport.sendMail mailOptions, (err, res) ->
if err
console.log err
process.exit()


The final result is a pretty looking E-Mail. I'm viewing this with Mail.app in OS X.  Ive blanked out a few bits.  Just imagine there is Company Text to the left of home and a link between home and projects in the nav-bar