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(host) ⇒ Object
- #events ⇒ Object
- #get_host(fqdn) ⇒ Object
- #hosts ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #login ⇒ Object
- #reports(hostid = nil) ⇒ Object
- #request(path, body = "", headers = {}) ⇒ Object
- #rules ⇒ Object
- #status ⇒ Object
- #topography ⇒ Object
- #users ⇒ 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(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 |
#events ⇒ Object
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 |
#hosts ⇒ Object
48 49 50 |
# File 'lib/mmonit/connection.rb', line 48 def hosts JSON.parse(self.request('/json/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 |
#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 |
#rules ⇒ Object
56 57 58 |
# File 'lib/mmonit/connection.rb', line 56 def rules JSON.parse(self.request('/json/admin/rules/list').body)['records'] end |
#status ⇒ Object
44 45 46 |
# File 'lib/mmonit/connection.rb', line 44 def status JSON.parse(self.request('/json/status/list').body)['records'] end |
#topography ⇒ Object
64 65 66 |
# File 'lib/mmonit/connection.rb', line 64 def topography JSON.parse(self.request('/json/status/topography').body)['records']['Data'] end |
#users ⇒ Object
52 53 54 |
# File 'lib/mmonit/connection.rb', line 52 def users JSON.parse(self.request('/json/admin/users/list').body)['records'] end |