Class: Monit::Service

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/monit/service.rb

Overview

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

Constant Summary collapse

TYPES =
{ 0 => "Filesystem", 1 => "Directory", 2 => "File", 3 => "Daemon", 4 => "Connection", 5 => "System" }

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, options = {}) ⇒ Service

Returns a new instance of Service.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/monit/service.rb', line 7

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

Instance Method Details

#do(action) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/monit/service.rb', line 30

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) ⇒ Object



19
20
21
22
# File 'lib/monit/service.rb', line 19

def url(path)
  url_params = { :host => @host, :port => @port, :path => "/#{path}" }
  @ssl ? URI::HTTPS.build(url_params) : URI::HTTP.build(url_params)
end