Sha256: bba9f19ea305c84a99eb63a834015892a5162cfe7d0faf25dee24a16e2646fc2

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 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
    
    alias_method :to_s, :message
    alias_method :to_i, :code
  
  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

1 entries across 1 versions & 1 rubygems

Version Path
nagiosplugin-0.0.7 lib/nagiosplugin/plugin.rb