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(host) ⇒ Object



79
80
81
82
83
# File 'lib/mmonit/connection.rb', line 79

def delete_host(host)
  host = get_host(host['host']) if host.key?('host') && ! host.key?('id')
  return false unless host['id']
  self.request('/admin/hosts/', "id=#{host['id']}&Delete=1")
end

#eventsObject



60
61
62
# File 'lib/mmonit/connection.rb', line 60

def events
  JSON.parse(self.request('/json/events/list').body)['records']
end

#get_host(fqdn) ⇒ Object



74
75
76
77
# File 'lib/mmonit/connection.rb', line 74

def get_host(fqdn)
  host = self.hosts.select{ |h| h['host'] == fqdn }
  host.empty? ? nil : host.first
end

#hostsObject



48
49
50
# File 'lib/mmonit/connection.rb', line 48

def hosts
  JSON.parse(self.request('/json/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

#reports(hostid = nil) ⇒ Object



68
69
70
71
72
# File 'lib/mmonit/connection.rb', line 68

def reports(hostid=nil)
  body = String.new
  body = "hostid=#{hostid.to_s}" if hostid
  JSON.parse(self.request('/json/reports/overview', body).body)
end

#request(path, body = "", headers = {}) ⇒ Object



85
86
87
88
# File 'lib/mmonit/connection.rb', line 85

def request(path, body="", headers = {})
  self.connect unless @http.is_a?(Net::HTTP)
  @http.post(path, body, @headers.merge(headers))
end

#rulesObject



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

def rules
  JSON.parse(self.request('/json/admin/rules/list').body)['records']
end

#statusObject



44
45
46
# File 'lib/mmonit/connection.rb', line 44

def status
  JSON.parse(self.request('/json/status/list').body)['records']
end

#topographyObject



64
65
66
# File 'lib/mmonit/connection.rb', line 64

def topography
  JSON.parse(self.request('/json/status/topography').body)['records']['Data']
end

#usersObject



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

def users
  JSON.parse(self.request('/json/admin/users/list').body)['records']
end