Class: Monit::Status
- Inherits:
-
Object
- Object
- Monit::Status
- 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
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
writeonly
Sets the attribute password.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#services ⇒ Object
readonly
Returns the value of attribute services.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#url ⇒ Object
readonly
Construct the URL.
-
#username ⇒ Object
Returns the value of attribute username.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#get ⇒ Object
Get the status from Monit.
-
#initialize(options = {}) ⇒ Status
constructor
Create a new instance of the status class with the given options.
-
#parse(xml) ⇒ Object
Parse the XML from Monit into a hash and into a Ruby representation.
Constructor Details
#initialize(options = {}) ⇒ Status
Create a new instance of the status class with the given options
Options:
-
host
- the host for monit, defaults tolocalhost
-
port
- the Monit port, defaults to2812
-
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( = {}) @host ||= [:host] ||= "localhost" @port ||= [:port] ||= 2812 @ssl ||= [:ssl] ||= false @auth ||= [:auth] ||= false @username = [:username] @password = [:password] @services = [] end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def auth @auth end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
15 16 17 |
# File 'lib/monit/status.rb', line 15 def hash @hash end |
#host ⇒ Object
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
17 18 19 |
# File 'lib/monit/status.rb', line 17 def password=(value) @password = value end |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
15 16 17 |
# File 'lib/monit/status.rb', line 15 def platform @platform end |
#port ⇒ Object
Returns the value of attribute port.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
15 16 17 |
# File 'lib/monit/status.rb', line 15 def server @server end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
15 16 17 |
# File 'lib/monit/status.rb', line 15 def services @services end |
#ssl ⇒ Object
Returns the value of attribute ssl.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def ssl @ssl end |
#url ⇒ Object (readonly)
Construct the URL
39 40 41 |
# File 'lib/monit/status.rb', line 39 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def username @username end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
15 16 17 |
# File 'lib/monit/status.rb', line 15 def xml @xml end |
Instance Method Details
#get ⇒ Object
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 |