class Kumolus::Monit::Status

Attributes

auth[RW]
hash[R]
host[RW]
password[W]
platform[R]
port[RW]
server[R]
services[R]
ssl[RW]
url[R]
username[RW]
xml[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/kumolus/monit/status.rb, line 18
def initialize(options = {})
  @host     = options[:host]    || "localhost"
  @port     = (options[:port]   || 2812).to_i
  @ssl      = options[:ssl]     || false
  @auth     = options[:auth]    || false
  @path     = options[:path]
  @username = options[:username]
  @password = options[:password]
  @services = []
end

Public Instance Methods

get() click to toggle source
# File lib/kumolus/monit/status.rb, line 34
def get
  uri = self.url
  http = Net::HTTP.new(uri.host, uri.port)

  if @ssl
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Get.new(uri.request_uri)

  if @auth
    request.basic_auth(@username, @password)
  end

  request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"

  begin
    response = http.request(request)
  rescue Errno::ECONNREFUSED
    return false
  end

  if (response.code =~ /\A2\d\d\z/)
    @xml = response.body
    return self.parse(@xml)
  else
    return false
  end
end
parse(xml) click to toggle source
# File lib/kumolus/monit/status.rb, line 65
def parse(xml)
  @hash     = Hash.from_xml(xml)
  @server   = Kumolus::Monit::Server.new(@hash["monit"]["server"])
  @platform = Kumolus::Monit::Platform.new(@hash["monit"]["platform"])

  options = {
    :host     => @host,
    :port     => @port,
    :ssl      => @ssl,
    :auth     => @auth,
    :username => @username,
    :password => @password
  }

  if @hash["monit"]["service"].is_a? Array
    @services = @hash["monit"]["service"].map do |service|
      Kumolus::Monit::Service.new(service, options)
    end
  else
    @services = [Kumolus::Monit::Service.new(@hash["monit"]["service"], options)]
  end
  true
rescue
  false
end