Class: MMonit::Connection
- Inherits:
-
Object
- Object
- MMonit::Connection
- Defined in:
- lib/mmonit/connection.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#password ⇒ Object
writeonly
Sets the attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#useragent ⇒ Object
readonly
Returns the value of attribute useragent.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #connect ⇒ Object
- #delete_host(id) ⇒ Object
-
#find_host_by_hostname(fqdn) ⇒ Object
def reports(hostid=nil) body = String.new body = “hostid=#hostidhostid.to_s” if hostid JSON.parse(self.request(‘/json/reports/overview’, body).body) end.
- #get(path, headers = {}) ⇒ Object
-
#get_host_details(id) ⇒ Object
another option: /admin/hosts/json/get?id=####.
- #hosts ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #login ⇒ Object
- #request(path, body = "", headers = {}) ⇒ Object
-
#status(id) ⇒ Object
TODO: get by id.
-
#status_overview ⇒ Object
TODO: filter arguments.
- #status_summary ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mmonit/connection.rb', line 10 def initialize( = {}) @ssl = [:ssl] || false @address = [:address] [:port] ||= @ssl ? '8443' : '8080' @port = [:port] @username = [:username] @password = [:password] [:useragent] ||= "MMonit-Ruby/#{MMonit::VERSION}" @useragent = [:useragent] @headers = { 'Host' => @address, 'Referer' => "#{@url}/index.csp", 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => @useragent, 'Connection' => 'keepalive' } end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def address @address end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def headers @headers end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def http @http end |
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
8 9 10 |
# File 'lib/mmonit/connection.rb', line 8 def password=(value) @password = value end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def port @port end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def ssl @ssl end |
#useragent ⇒ Object (readonly)
Returns the value of attribute useragent.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def useragent @useragent end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/mmonit/connection.rb', line 7 def username @username end |
Instance Method Details
#connect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mmonit/connection.rb', line 28 def connect @http = Net::HTTP.new(@address, @port) if @ssl @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end @headers['Cookie'] = @http.get('/index.csp').response['set-cookie'].split(';').first self.login end |
#delete_host(id) ⇒ Object
94 95 96 |
# File 'lib/mmonit/connection.rb', line 94 def delete_host(id) self.request("/admin/hosts/delete?id=#{id}") end |
#find_host_by_hostname(fqdn) ⇒ Object
def reports(hostid=nil) body = String.new body = “hostid=#MMonit::Connection.hostidhostid.to_s” if hostid JSON.parse(self.request(‘/json/reports/overview’, body).body) end
84 85 86 87 |
# File 'lib/mmonit/connection.rb', line 84 def find_host_by_hostname(fqdn) host = self.hosts.select{ |h| h['hostname'] == fqdn } host.empty? ? nil : host.first end |
#get(path, headers = {}) ⇒ Object
103 104 105 106 |
# File 'lib/mmonit/connection.rb', line 103 def get(path, headers = {}) self.connect unless @http.is_a?(Net::HTTP) @http.get(path, @headers.merge(headers)) end |
#get_host_details(id) ⇒ Object
another option: /admin/hosts/json/get?id=####
90 91 92 |
# File 'lib/mmonit/connection.rb', line 90 def get_host_details(id) JSON.parse(self.get("/admin/hosts/get?id=#{id}").body) rescue nil end |
#hosts ⇒ Object
57 58 59 |
# File 'lib/mmonit/connection.rb', line 57 def hosts JSON.parse(self.get('/admin/hosts/list').body)['records'] end |
#login ⇒ Object
40 41 42 |
# File 'lib/mmonit/connection.rb', line 40 def login self.request('/z_security_check', "z_username=#{@username}&z_password=#{@password}").code.to_i == 302 end |
#request(path, body = "", headers = {}) ⇒ Object
98 99 100 101 |
# File 'lib/mmonit/connection.rb', line 98 def request(path, body="", headers = {}) self.connect unless @http.is_a?(Net::HTTP) @http.post(path, body, @headers.merge(headers)) end |
#status(id) ⇒ Object
TODO: get by id
50 51 |
# File 'lib/mmonit/connection.rb', line 50 def status(id) end |
#status_overview ⇒ Object
TODO: filter arguments
45 46 47 |
# File 'lib/mmonit/connection.rb', line 45 def status_overview JSON.parse(self.get('/status/hosts/list').body)['records'] end |
#status_summary ⇒ Object
53 54 55 |
# File 'lib/mmonit/connection.rb', line 53 def status_summary JSON.parse(self.get('/status/hosts/summary').body) end |