class Kumolus::Monit::Service

The service section from the Monit XML. Inherits from OpenStruct.

Constants

TYPES

Public Class Methods

new(hash = nil, options = {}) click to toggle source
Calls superclass method
# File lib/kumolus/monit/service.rb, line 8
def initialize(hash = nil, options = {})
  @host     ||= options[:host] || "localhost"
  @port     ||= options[:port] || 2812
  @ssl      ||= options[:ssl]  || false
  @auth     ||= options[:auth] || false
  @username = options[:username]
  @password = options[:password]

  hash = rename_service_type(hash) if hash
  super(hash)
end

Public Instance Methods

do(action) click to toggle source
# File lib/kumolus/monit/service.rb, line 31
def do(action)
    uri = self.url self.name
    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::Post.new(uri.request_uri)
    request.body = "action=#{action}"

    request.basic_auth(@username, @password) if @auth
    request["User-Agent"] = "Monit Ruby client #{Monit::VERSION}"

    begin
      response = http.request(request)
      !!(response.code =~ /\A2\d\d\z/)
    rescue Errno::ECONNREFUSED => e
      false
    end
  end
url(path) click to toggle source
# File lib/kumolus/monit/service.rb, line 20
def url(path)
  url_params = { :host => @host, :port => @port, :path => "/#{path}" }
  @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
end

Private Instance Methods

rename_service_type(hash) click to toggle source

Renames the Service type from “type” to “service_type” to avoid conflicts

# File lib/kumolus/monit/service.rb, line 56
def rename_service_type(hash)
  hash["service_type"] = hash["type"]
  hash.delete("type")
  hash
end