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



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

def initialize(options = {})
  @host     = options[:host]    || "localhost"
  @port     = (options[:port]   || 2812).to_i
  @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.



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

def auth
  @auth
end

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



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

def password=(value)
  @password = value
end

#platformObject (readonly)

Returns the value of attribute platform.



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

def platform
  @platform
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#servicesObject (readonly)

Returns the value of attribute services.



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

def services
  @services
end

#sslObject

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

#urlObject (readonly)

Construct the URL



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

def url
  @url
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#xmlObject (readonly)

Returns the value of attribute xml.



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

def xml
  @xml
end

Instance Method Details

#getObject

Get the status from Monit.



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
71
# File 'lib/monit/status.rb', line 46

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 =~ /\A2\d\d\z/)
    @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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/monit/status.rb', line 74

def parse(xml)
  @hash     = Hash.from_xml(xml)
  @server   = Server.new(@hash["monit"]["server"])
  @platform = 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|
      Service.new(service, options)
    end
  else
    @services = [Service.new(@hash["monit"]["service"], options)]
  end
  true
rescue
  false
end