Skip to content

Instantly share code, notes, and snippets.

@stonith
Forked from benwtr/nagios.coffee
Created April 12, 2014 02:37
Show Gist options
  • Select an option

  • Save stonith/10515951 to your computer and use it in GitHub Desktop.

Select an option

Save stonith/10515951 to your computer and use it in GitHub Desktop.

Revisions

  1. @benwtr benwtr revised this gist Jun 1, 2013. 1 changed file with 83 additions and 30 deletions.
    113 changes: 83 additions & 30 deletions nagios.coffee
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,89 @@
    # Description:
    # This script receives pages in the formats
    # /usr/bin/curl -d host="$HOSTALIAS$" -d output="$SERVICEOUTPUT$" -d description="$SERVICEDESC$" -d type=service -d state="$SERVICESTATE$" $CONTACTADDRESS1$
    # /usr/bin/curl -d host="$HOSTNAME$" -d output="$HOSTOUTPUT$" -d type=host -d state="$HOSTSTATE$" $CONTACTADDRESS1$
    # /usr/bin/curl -d host="$HOSTALIAS$" -d output="$SERVICEOUTPUT$" -d description="$SERVICEDESC$" -d type=service -d notificationtype="$NOTIFICATIONTYPE$ -d state="$SERVICESTATE$" $CONTACTADDRESS1$
    # /usr/bin/curl -d host="$HOSTNAME$" -d output="$HOSTOUTPUT$" -d type=host -d notificationtype="$NOTIFICATIONTYPE$" -d state="$HOSTSTATE$" $CONTACTADDRESS1$
    #
    # Based on a gist by oremj (https://gist.github.com/oremj/3702073)
    #
    # Configuration:
    # HUBOT_NAGIOS_URL - https://<user>:<password>@nagios.example.com/cgi-bin/nagios3
    #
    # Commands:
    # hubot nagios ack <host>:<service> <descr> - acknowledge alert
    # hubot nagios mute <host>:<service> <minutes> - delay the next service notification
    # hubot nagios recheck <host>:<service> - force a recheck of a service
    # hubot nagios all_alerts_off - useful in emergencies. warning: disables all alerts, not just bot alerts
    # hubot nagios all_alerts_on - turn alerts back on
    #
    # Author:
    # oremj

    irc = require('irc')
    nagios_url = process.env.HUBOT_NAGIOS_URL

    module.exports = (robot) ->
    robot.router.post '/hubot/nagios/:room', (req, res) ->
    room = req.params.room

    host = irc.colors.wrap('orange', req.body.host)
    output = irc.colors.wrap('white', req.body.output)

    state = req.body.state
    if state == 'OK'
    state_color = 'light_green'
    else if state == 'CRITICAL'
    state_color = 'light_red'
    else if state == 'WARNING'
    state_color = 'yellow'
    else
    state_color = 'orange'
    state = irc.colors.wrap(state_color, state)

    if req.body.type == 'host'
    robot.messageRoom "##{room}", "nagios: #{host} is #{output}"
    else
    service = req.body.description
    robot.messageRoom "##{room}", "nagios: #{host}:#{service} is #{state}: #{output}"

    res.writeHead 204, { 'Content-Length': 0 }
    res.end()

    robot.router.post '/hubot/nagios/:room', (req, res) ->
    room = req.params.room
    host = req.body.host
    output = req.body.output
    state = req.body.state
    notificationtype = req.body.notificationtype

    if req.body.type == 'host'
    robot.messageRoom "#{room}", "nagios #{notificationtype}: #{host} is #{output}"
    else
    service = req.body.description
    robot.messageRoom "#{room}", "nagios #{notificationtype}: #{host}:#{service} is #{state}: #{output}"

    res.writeHead 204, { 'Content-Length': 0 }
    res.end()

    robot.respond /nagios ack(nowledge)? (.*):(.*) (.*)/i, (msg) ->
    host = msg.match[1]
    service = msg.match[2]
    message = msg.match[3] || ""
    call = "cmd.cgi"
    data = "cmd_typ=34&host=#{host}&service=#{service}&cmd_mod=2&sticky_ack=on&com_author=#{msg.envelope.user}&send_notification=on&com_data=#{encodeURIComponent(message)}"
    nagios_post msg, call, data, (res) ->
    if res.match(/Your command request was successfully submitted to Nagios for processing/)
    msg.send "Your acknowledgement was received by nagios"

    robot.respond /nagios mute (.*):(.*) (\d+)/i, (msg) ->
    host = msg.match[1]
    service = msg.match[2]
    minutes = msg.match[3] || 30
    call = "cmd.cgi"
    data = "cmd_typ=9&cmd_mod=2&&host=#{host}&service=#{service}&not_dly=#{minutes}"
    nagios_post msg, call, data, (res) ->
    if res.match(/Your command request was successfully submitted to Nagios for processing/)
    msg.send "Muting #{host}:#{service} for #{minutes}m"

    robot.respond /nagios recheck (.*):(.*)/i, (msg) ->
    host = msg.match[1]
    service = msg.match[2]
    call = "cmd.cgi"
    d = Date()
    start_time = "#{d.getUTCFullYear()}-#{d.getUTCMonth()}-#{d.getUTCDate()}+#{d.getUTCHours()}%3A#{d.getUTCMinutes()}%3A#{d.getUTCSeconds()}"
    data = "cmd_typ=7&cmd_mod=2&host=#{host}&service=#{service}&force_check=on&start_time=\"#{start_time}\""
    nagios_post msg, call, data, (res) ->
    if res.match(/Your command request was successfully submitted to Nagios for processing/)
    msg.send "Scheduled to recheck #{host}:#{service} at #{start_time}"

    robot.respond /nagios (all_alerts_off|stfu|shut up)/i, (msg) ->
    call = "cmd.cgi"
    data = "cmd_typ=11&cmd_mod=2"
    nagios_post msg, call, data, (res) ->
    if res.match(/Your command request was successfully submitted to Nagios for processing/)
    msg.send "Ok, all alerts off. (this disables ALL alerts, not just mine.)"

    robot.respond /nagios all_alerts_on/i, (msg) ->
    call = "cmd.cgi"
    data = "cmd_typ=12&cmd_mod=2"
    nagios_post msg, call, data, (res) ->
    if res.match(/Your command request was successfully submitted to Nagios for processing/)
    msg.send "Ok, alerts back on"

    nagios_post = (msg, call, data, cb) ->
    msg.http("#{nagios_url}/#{call}")
    .header('accept', '*/*')
    .header('User-Agent', "Hubot/#{@version}")
    .post(data) (err, res, body) ->
    cb body
  2. @oremj oremj created this gist Sep 11, 2012.
    36 changes: 36 additions & 0 deletions nagios.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # Description:
    # This script receives pages in the formats
    # /usr/bin/curl -d host="$HOSTALIAS$" -d output="$SERVICEOUTPUT$" -d description="$SERVICEDESC$" -d type=service -d state="$SERVICESTATE$" $CONTACTADDRESS1$
    # /usr/bin/curl -d host="$HOSTNAME$" -d output="$HOSTOUTPUT$" -d type=host -d state="$HOSTSTATE$" $CONTACTADDRESS1$
    #
    # Author:
    # oremj

    irc = require('irc')

    module.exports = (robot) ->
    robot.router.post '/hubot/nagios/:room', (req, res) ->
    room = req.params.room

    host = irc.colors.wrap('orange', req.body.host)
    output = irc.colors.wrap('white', req.body.output)

    state = req.body.state
    if state == 'OK'
    state_color = 'light_green'
    else if state == 'CRITICAL'
    state_color = 'light_red'
    else if state == 'WARNING'
    state_color = 'yellow'
    else
    state_color = 'orange'
    state = irc.colors.wrap(state_color, state)

    if req.body.type == 'host'
    robot.messageRoom "##{room}", "nagios: #{host} is #{output}"
    else
    service = req.body.description
    robot.messageRoom "##{room}", "nagios: #{host}:#{service} is #{state}: #{output}"

    res.writeHead 204, { 'Content-Length': 0 }
    res.end()