Sha256: 033171a8b6d5bb7457bbf356439b527caef015b0722fdf49827b408737d08182

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module NagiosPlugin
  class Plugin
    NAGIOS_PLUGIN_EXIT_CODES = {
      :unknown  => 3,
      :critical => 2,
      :warning  => 1,
      :ok       => 0
    }

    class << self
      def check
        plugin = new
        puts plugin.nagios_plugin_output
        exit plugin.nagios_plugin_exit_code
      rescue => e
        pretty_error = ([e.to_s, nil] + e.backtrace).join("\n")
        puts "PLUGIN UNKNOWN: #{pretty_error}"
        exit NAGIOS_PLUGIN_EXIT_CODES[:unknown]
      end
    end

    def nagios_plugin_exit_code
      NAGIOS_PLUGIN_EXIT_CODES[nagios_plugin_status]
    end

    def nagios_plugin_output
      output = [nagios_plugin_service] << ' '
      output << nagios_plugin_status.to_s.upcase
      output << ': ' + message if ( respond_to?(:message) && !message.empty? )
      output.join
    end

  private

    def nagios_plugin_status
      @nagios_plugin_status ||=
        case
        when critical? then :critical
        when warning?  then :warning
        when ok?       then :ok
        else                :unknown
        end
    end

    def nagios_plugin_service
      self.class.name.split('::').last.gsub(/plugin$/i, '').upcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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