Sha256: 754c89b794e90ca99068742e66e99425dc1273a44be8e5bd18e5d55e9967a371

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module NagiosPlugin
  EXIT_CODES = {
    :ok       => 0,
    :warning  => 1,
    :critical => 2,
    :unknown  => 3,
  }
  
  PluginError = Class.new(StandardError)
  
  class Plugin
    def self.check!
      plugin = self.new
      plugin.check
    rescue Exception => e
      puts [plugin.prefix, e.to_s].join(' ')
    else
      puts plugin.message
    ensure
      exit plugin.code
    end
    
    def check
      measure if respond_to?(:measure)
      set_status
    rescue Exception
      @status = :unknown
      raise
    end
    
    def message
      [prefix, (output if respond_to?(:output))].compact.join(' ')
    end
    
    def prefix
      "#{self.class.name.upcase} #{status.to_s.upcase}:"
    end
    
    def code
      EXIT_CODES[status]
    end
    
    def status
      @status || :unknown
    end
  
  protected
  
    def set_status
      @status = [:critical, :warning, :ok].select { |s| send("#{s}?") }.first
      raise PluginError, "All status checks returned false!" if @status.nil?
    end
    
    def ok?
      true
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nagiosplugin-0.0.6 lib/nagiosplugin/plugin.rb
nagiosplugin-0.0.5 lib/nagiosplugin/plugin.rb
nagiosplugin-0.0.4 lib/nagiosplugin/plugin.rb