Class: Monit::Status

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

Overview

The Status class is used to get data from the Monit instance. You should not need to manually instantiate any of the other classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Status

Create a new instance of the status class with the given options

Options:

  • host - the host for monit, defaults to localhost

  • port - the Monit port, defaults to 2812

  • ssl - should we use SSL for the connection to Monit (default: false)

  • auth - should authentication be used, defaults to false

  • username - username for authentication

  • password - password for authentication



28
29
30
31
32
33
34
35
36
# File 'lib/monit/status.rb', line 28

def initialize(options = {})
  @host ||= options[:host] ||= "localhost"
  @port ||= options[:port] ||= 2812
  @ssl  ||= options[:ssl]  ||= false
  @auth ||= options[:auth] ||= false
  @username = options[:username]
  @password = options[:password]
  @services = []
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



16
17
18
# File 'lib/monit/status.rb', line 16

def auth
  @auth
end

#hashObject (readonly)

Returns the value of attribute hash.



15
16
17
# File 'lib/monit/status.rb', line 15

def hash
  @hash
end

#hostObject

Returns the value of attribute host.



16
17
18
# File 'lib/monit/status.rb', line 16

def host
  @host
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



17
18
19
# File 'lib/monit/status.rb', line 17

def password=(value)
  @password = value
end

#platformObject (readonly)

Returns the value of attribute platform.



15
16
17
# File 'lib/monit/status.rb', line 15

def platform
  @platform
end

#portObject

Returns the value of attribute port.



16
17
18
# File 'lib/monit/status.rb', line 16

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



15
16
17
# File 'lib/monit/status.rb', line 15

def server
  @server
end

#servicesObject (readonly)

Returns the value of attribute services.



15
16
17
# File 'lib/monit/status.rb', line 15

def services
  @services
end

#sslObject

Returns the value of attribute ssl.



16
17
18
# File 'lib/monit/status.rb', line 16

def ssl
  @ssl
end

#urlObject (readonly)

Construct the URL



39
40
41
# File 'lib/monit/status.rb', line 39

def url
  @url
end

#usernameObject

Returns the value of attribute username.



16
17
18
# File 'lib/monit/status.rb', line 16

def username
  @username
end

#xmlObject (readonly)

Returns the value of attribute xml.



15
16
17
# File 'lib/monit/status.rb', line 15

def xml
  @xml
end

Instance Method Details

#getObject

Get the status from Monit.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/monit/status.rb', line 45

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}"

  response = http.request(request)

  if response.code == "200"
    @xml = response.body
    return self.parse(@xml)
  else
    return false
  end
end

#parse(xml) ⇒ Object

Parse the XML from Monit into a hash and into a Ruby representation.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/monit/status.rb', line 73

def parse(xml)
  @hash = Hash.from_xml(xml)
  @server = Server.new(@hash["monit"]["server"])
  @platform = Platform.new(@hash["monit"]["platform"])
  if @hash["monit"]["service"].is_a? Array
    @services = @hash["monit"]["service"].map do |service|
      Service.new(service)
    end
  else
    @services = [Service.new(@hash["monit"]["service"])]
  end
  true
rescue
  false
end