hobodave.com


Subscribe to RSS Feed

Simple Nagios probes in ruby

10 Jan 2010

I needed to write a custom nagios probe a few weeks ago. I googled for existing solutions in Ruby, but surprisingly found none. A nagios probe can really be written in any language, it just has to return a single line of output and an exit code of 0 (OK), 1 (WARNING), 2 (CRITICAL), or 3 (UNKNOWN). I chose Ruby because of the syntactical simplicity, as well as the ease of bundling it as a gem using Gemcutter.

You can view the source here.

Installation

# gem install nagios-probe

Usage

Simply create a subclass of Nagios::Probe and define the following methods:

Example

class MyProbe < Nagios::Probe
  def check_crit
    true
  end

  def check_warn
    false
  end

  def crit_message
    "Things are bad"
  end

  def warn_message
    "Things aren't going well"
  end

  def ok_message
    "Nothing to see here"
  end
end

To use your probe you must wrap it in a begin/rescue block to catch any exceptions and accurately report the status to Nagios.

begin
  options = {} # constructor accepts a single optional param that is assigned to @opts
  probe = MyProbe.new(options)
  probe.run
rescue Exception => e
  puts "Unknown: " + e
  exit Nagios::UNKNOWN
end

puts probe.message
exit probe.retval


Copyright © 2010 David Abdemoulaie. All rights reserved. Hosted by GitHub and powered by Jekyll.