-
Notifications
You must be signed in to change notification settings - Fork 5
/
monit_instance.rb
47 lines (40 loc) · 1.12 KB
/
monit_instance.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'nokogiri'
require 'open-uri'
require 'net/http'
class MonitInstance
attr_accessor :url
attr_accessor :username
attr_accessor :password
def initialize(url, username=nil, password=nil)
self.url = url
self.username = username
self.password = password
end
def self.all
@monit_installations = []
YAML::load( File.open( 'monit_installations.yml' ) ).each do |key, installation|
@monit_installations << MonitInstance.new(installation['url'], installation['username'], installation['password'])
end
return @monit_installations
end
def xml_doc
uri = URI.parse(url)
res = Net::HTTP.start(uri.host, uri.port) {|http|
req = Net::HTTP::Get.new('/_status?format=xml')
req.basic_auth username, password if username && password
http.request(req).response
}
doc = Nokogiri::XML.parse(res.body)
return doc
end
def link_with_auth
if username.nil? || password.nil?
return url
else
return "http://#{username}:#{password}@#{url.gsub(/http:\/\//, '')}"
end
end
def xml_seems_ok?
!xml_doc.xpath('//service[@type=5]').blank?
end
end