Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 770161c24aa1c47885db94ad266e61a76256ed62 @jhstatewide committed Jun 29, 2011
Showing with 230 additions and 0 deletions.
  1. +4 −0 History.txt
  2. +11 −0 Manifest.txt
  3. +1 −0 PostInstall.txt
  4. +32 −0 README.rdoc
  5. +26 −0 Rakefile
  6. +87 −0 lib/nagios-plugin.rb
  7. +10 −0 script/console
  8. +14 −0 script/destroy
  9. +14 −0 script/generate
  10. +3 −0 test/test_helper.rb
  11. +28 −0 test/test_nagios-plugin.rb
@@ -0,0 +1,4 @@
+=== 0.0.1 2011-06-29
+
+* 1 major enhancement:
+ * Initial release
@@ -0,0 +1,11 @@
+History.txt
+Manifest.txt
+PostInstall.txt
+README.rdoc
+Rakefile
+lib/nagios-plugin.rb
+script/console
+script/destroy
+script/generate
+test/test_helper.rb
+test/test_nagios-plugin.rb
@@ -0,0 +1 @@
+Thanks for using this plugin.
@@ -0,0 +1,32 @@
+= nagios-plugin
+
+* http://github.com/jhstatewide/ruby-nagios-plugin
+
+== DESCRIPTION:
+
+This allows you to easily write Nagios plugins in Ruby.
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2011 Joshua Harding
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
+require 'rubygems'
+gem 'hoe', '>= 2.1.0'
+require 'hoe'
+require 'fileutils'
+require './lib/nagios-plugin'
+
+Hoe.plugin :newgem
+# Hoe.plugin :website
+# Hoe.plugin :cucumberfeatures
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.spec 'nagios-plugin' do
+ self.developer 'Joshua Harding', 'josh@statewidesoftware.com'
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
+ self.rubyforge_name = self.name # TODO this is default value
+ self.extra_deps = [['newgem','>= 1']]
+
+end
+
+require 'newgem/tasks'
+Dir['tasks/**/*.rake'].each { |t| load t }
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# remove_task :default
+# task :default => [:spec, :features]
@@ -0,0 +1,87 @@
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+module Nagios
+ class Plugin
+ VERSION = '0.0.1'
+ OK_STATE = 0
+ WARNING_STATE = 1
+ CRITICAL_STATE = 2
+ UNKNOWN_STATE = 3
+
+ def initialize(args = {})
+ @shortname = args[:shortname] || "Nagios::Plugin"
+ @perfdata = Hash.new
+ @exit = true
+ end
+
+ def exit=(enabled)
+ @exit = enabled
+ end
+
+ # actually does the check
+ def run
+ raise "You must implement the run method!"
+ end
+
+ def state
+ @state
+ end
+
+ def output
+ @output
+ end
+
+ def add_perfdata(args)
+ field = args[:field] or raise "must supply field!"
+ data = args[:data] or raise "must supply data!"
+ @perfdata[field] = {:data => data}
+ @perfdata[field][:uom] = args[:uom] if args[:uom]
+ @perfdata[field][:warning] = args[:warning] if args[:warning]
+ @perfdata[field][:critical] = args[:critical] if args[:critical]
+ end
+
+ private
+
+ def critical(message)
+ @state = :CRITICAL
+ do_exit(CRITICAL_STATE, message)
+ end
+
+ def ok(message)
+ @state = :OK
+ do_exit(OK_STATE, message)
+ end
+
+ def warning(message)
+ @state = :WARNING
+ do_exit(WARNING_STATE, message)
+ end
+
+ def do_exit(status, message)
+ @output = @shortname + " "
+ case status
+ when OK_STATE
+ @output += "OK - "
+ when WARNING_STATE
+ @output += "WARNING - "
+ when CRITICAL_STATE
+ @output += "CRITICAL - "
+ when UNKNOWN_STATE
+ @output += "UNKNOWN -"
+ end
+ @output += message
+ # see if we have keys...
+ if @perfdata.keys.length > 0
+ @output += " | "
+ @perfdata.each_pair do |key, value|
+ @output += "#{key}=#{value[:data]}#{value[:uom]};#{value[:warning]};#{value[:critical]}"
+ end
+ end
+ exit(status) if @exit
+ @output.strip!
+ return @output
+ end
+
+ end
+end
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/nagios-plugin.rb'}"
+puts "Loading nagios-plugin gem"
+exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
+require 'stringio'
+require 'test/unit'
+require File.dirname(__FILE__) + '/../lib/nagios-plugin'
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+
+class TestPlugin < Nagios::Plugin
+ def run
+ critical("waffles are getting really low...")
+ end
+end
+
+class TestNagiosPlugin < Test::Unit::TestCase
+
+ def setup
+ @tp = TestPlugin.new(:shortname => "Waffles")
+ @tp.exit = false
+ end
+
+ def test_critical
+ output = @tp.run
+ assert_equal("Waffles CRITICAL - waffles are getting really low...", output)
+ end
+
+ def test_perf
+ @tp.add_perfdata(:field => "waffles_per_second", :data => 10, :warning => 2, :critical => 5, :uom => "wps")
+ @tp.run
+ output = @tp.output
+ assert_equal("Waffles CRITICAL - waffles are getting really low... | waffles_per_second=10wps;2;5", output)
+ end
+end

0 comments on commit 770161c

Please sign in to comment.