Class: MMonit::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mmonit/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
	@ssl = options[:ssl] || false
	@address = options[:address]
	options[:port] ||= @ssl ? '8443' : '8080'
	@port = options[:port]
	@username = options[:username]
	@password = options[:password]
	options[:useragent] ||= "MMonit-Ruby/#{MMonit::VERSION}"
	@useragent = options[:useragent]
	@headers = {
		'Host' => @address,
		'Referer' => "#{@url}/index.csp",
		'Content-Type' => 'application/x-www-form-urlencoded',
		'User-Agent' => @useragent,
		'Connection' => 'keepalive'
	}
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def address
  @address
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def headers
  @headers
end

#httpObject (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

Parameters:

  • value

    the value to set the attribute password to.



8
9
10
# File 'lib/mmonit/connection.rb', line 8

def password=(value)
  @password = value
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def ssl
  @ssl
end

#useragentObject (readonly)

Returns the value of attribute useragent.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def useragent
  @useragent
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/mmonit/connection.rb', line 7

def username
  @username
end

Instance Method Details

#connectObject



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.
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

#hostsObject



57
58
59
# File 'lib/mmonit/connection.rb', line 57

def hosts
	JSON.parse(self.get('/admin/hosts/list').body)['records']
end

#loginObject



40
41
42
# File 'lib/mmonit/connection.rb', line 40

def 
	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_overviewObject

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_summaryObject



53
54
55
# File 'lib/mmonit/connection.rb', line 53

def status_summary
	JSON.parse(self.get('/status/hosts/summary').body)
end