Lets spice it up with coffeescript!
Nagios
define contact{
contact_name mike
alias Mike Kunze
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-with-nodejs
host_notification_commands notify-host-by-email
email
}
define command {
command_name notify-service-with-nodejs
command_line /opt/bin/notify-service.coffee "$HOSTNAME$" "$SERVICEDESC$" "$HOSTADDRESS$" "$NOTIFICATIONTYPE$" "$SERVICESTATE$" "$LONGDATETIME$" "$SERVICEOUTPUT$"
}
CoffeeScript
This script will be executed by nagios and will contain the macros provided by the nagios command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' | |
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] | |
nodemailer = require 'nodemailer' | |
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 | |
" | |
html = "Nagios requires your attention.\n | |
\n | |
Hostname: " + argvObject.hostname + "<br> | |
service: " + argvObject.service + "<br> | |
ip: " + argvObject.ip + "<br> | |
notify: " + argvObject.notify_type + "<br> | |
status: " + argvObject.status + "<br> | |
date: " + argvObject.date + "<br> | |
output: " + argvObject.output + "<br> | |
" | |
mailOptions = | |
from: "Nagios <you@yourdomain.com>" | |
to: "you@yourdomain.com" | |
subject: "Hello, notification from nagios" | |
text: text | |
html: html | |
smtpTransport.sendMail mailOptions, (err, res) -> | |
if err | |
console.log err | |
process.exit() |
No comments:
Post a Comment