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
29 30 31 32 33 34 35 36 37 |
# File 'lib/monit/status.rb', line 29 def initialize( = {}) @host = [:host] || "localhost" @port = ([:port] || 2812).to_i @ssl = [:ssl] || false @auth = [:auth] || false @username = [:username] @password = [:password] @services = [] end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
17 18 19 |
# File 'lib/monit/status.rb', line 17 def auth @auth end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def hash @hash end |
#host ⇒ Object
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
18 19 20 |
# File 'lib/monit/status.rb', line 18 def password=(value) @password = value end |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def platform @platform end |
#port ⇒ Object
Returns the value of attribute port.
17 18 19 |
# File 'lib/monit/status.rb', line 17 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def server @server end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def services @services end |
#ssl ⇒ Object
Returns the value of attribute ssl.
17 18 19 |
# File 'lib/monit/status.rb', line 17 def ssl @ssl end |
#url ⇒ Object (readonly)
Construct the URL
40 41 42 |
# File 'lib/monit/status.rb', line 40 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
17 18 19 |
# File 'lib/monit/status.rb', line 17 def username @username end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
16 17 18 |
# File 'lib/monit/status.rb', line 16 def xml @xml end |
Instance Method Details
#get ⇒ Object
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 72 73 74 75 |
# 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}" begin response = http.request(request) rescue Errno::ECONNREFUSED return false end 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/monit/status.rb', line 78 def parse(xml) @hash = Hash.from_xml(xml) @server = Server.new(@hash["monit"]["server"]) @platform = Platform.new(@hash["monit"]["platform"]) = { :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, ) end else @services = [Service.new(@hash["monit"]["service"], )] end true rescue false end |