diff --git a/README.md b/README.md index bd8b9ab..9895162 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,48 @@ -mmonit-ruby -=========== +# mmonit-ruby Ruby interface for M/Monit -All the commands listed here are currently available: +A subset of the [M/Monit HTTP API](http://mmonit.com/documentation/http-api/) commands are currently available. Requests are currently read-only. -http://mmonit.com/wiki/MMonit/HTTP-API +## Available Commands -Requests are read-only until I find a way to do more. +* `connect` - Connect to M/Monit and establish a session +* `status` - Status overview +* `status_detailed(id_or_fqdn)` - Detailed status for a specified host + * `id_or_fqdn` - Either the numeric id or the fully-qualified domain name for a host +* `events` - Events overview +* `event(id)` - Detailed information about an event + * `id` - The numeric id for an event +* `hosts` - A list of hosts +* `host(id_or_fqdn)` - Detailed information about a specified host + * `id_or_fqdn` - Either the numeric id or the fully-qualified domain name for a host +* `users` - A list of users +* `user(id)` - Detailed information about a user + * `id` - The numeric id for a user +* `groups` - A list of groups +## Usage + require 'mmonit-ruby' + mmonit = MMonit::Connection.new({ + :ssl => true, + :username => 'USERNAME', + :password => 'PASSWORD', + :address => 'example.com', + :port => '443' + }) -mmonit = MMonit::Connection.new({ - :ssl => true, - :username => 'USERNAME', - :password => 'PASSWORD', - :address => 'example.com', - :port => '443' -}) - -mmonit.connect - -hosts = mmonit.hosts - -p hosts + mmonit.connect + hosts = mmonit.hosts +## Custom Requests Custom requests can be made like: -mmonit.request(path, [body]) + require 'mmonit-ruby' + + mmonit.request(path, [body]) -body is optional \ No newline at end of file +`body` is optional \ No newline at end of file diff --git a/lib/mmonit/connection.rb b/lib/mmonit/connection.rb index bb7dc09..901c959 100644 --- a/lib/mmonit/connection.rb +++ b/lib/mmonit/connection.rb @@ -21,7 +21,7 @@ def initialize(options = {}) 'Referer' => "#{@url}/index.csp", 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => @useragent, - 'Connection' => 'keepalive' + 'Connection' => 'Keep-Alive' } end @@ -38,59 +38,91 @@ def connect end def login - self.request('/z_security_check', "z_username=#{@username}&z_password=#{@password}").code.to_i == 302 + self.request('/z_security_check', "z_username=#{@username}&z_password=#{@password}", true).code.to_i == 302 end + # Status API: http://mmonit.com/documentation/http-api/Methods/Status def status - JSON.parse(self.request('/json/status/list').body)['records'] + JSON.parse(self.request('/status/hosts/list').body)['records'] end + def status_detailed(id_or_fqdn) + id = find_status_id(id_or_fqdn) + id.nil? ? nil : JSON.parse(self.request("/status/hosts/get?id=#{id}").body)['records']['host'] rescue nil + end + + # Events API: http://mmonit.com/documentation/http-api/Methods/Events + def events + JSON.parse(self.request('/reports/events/list').body)['records'] + end + + def event(id) + JSON.parse(self.request("/reports/events/get?id=#{id}").body) rescue nil + end + + # Admin Hosts API: http://mmonit.com/documentation/http-api/Methods/Admin_Hosts def hosts - JSON.parse(self.request('/json/admin/hosts/list').body)['records'] + JSON.parse(self.request('/admin/hosts/list').body)['records'] + end + + def host(id_or_fqdn) + id = find_host_id(id_or_fqdn) + id.nil? ? nil : JSON.parse(self.request("/admin/hosts/get?id=#{id}").body) rescue nil end + # Admin Users API: http://mmonit.com/documentation/http-api/Methods/Admin_Users def users - JSON.parse(self.request('/json/admin/users/list').body) + JSON.parse(self.request('/admin/users/list').body) end - def alerts - JSON.parse(self.request('/json/admin/alerts/list').body) + def user(username) + JSON.parse(self.request("/admin/users/get?uname=#{username}").body) rescue nil end - def events - JSON.parse(self.request('/json/events/list').body)['records'] + # Admin Groups API: http://mmonit.com/documentation/http-api/Methods/Admin_Groups + def groups + JSON.parse(self.request('/admin/groups/list').body) end - #### topography and reports are disabled until I figure out their new equivalent in M/Monit - # def topography - # JSON.parse(self.request('/json/status/topography').body) - # end + # Helpers + def find_host(id_or_fqdn) + hosts = self.hosts rescue [] + host = hosts.select{ |h| h['id'] == id_or_fqdn || h['hostname'] == id_or_fqdn } + host.empty? ? nil : host.first + end - # def reports(hostid=nil) - # body = String.new - # body = "hostid=#{hostid.to_s}" if hostid - # JSON.parse(self.request('/json/reports/overview', body).body) - # end + def find_host_id(id_or_fqdn) + return id_or_fqdn if id_or_fqdn.is_a?(Integer) - def find_host(fqdn) - host = self.hosts.select{ |h| h['host'] == fqdn } - host.empty? ? nil : host.first + data = find_status(id_or_fqdn) + data.nil? ? nil : data['id'] end - # another option: /admin/hosts/json/get?id=#### - def get_host_details(id) - JSON.parse(self.request("/json/status/detail?hostid=#{id}").body)['records']['host'] rescue nil + def find_status(id_or_fqdn) + statuses = self.status rescue [] + status = statuses.select{ |s| s['id'] == id_or_fqdn || s['hostname'] == id_or_fqdn } + status.empty? ? nil : status.first end - def delete_host(host) - host = self.find_host(host['host']) if host.key?('host') && ! host.key?('id') - return false unless host['id'] - self.request("/admin/hosts/delete?id=#{host['id']}") + def find_status_id(id_or_fqdn) + return id_or_fqdn if id_or_fqdn.is_a?(Integer) + + data = find_status(id_or_fqdn) + data.nil? ? nil : data['id'] end - def request(path, body="", headers = {}) + def request(path, body="", is_post = false, headers = {}) self.connect unless @http.is_a?(Net::HTTP) - @http.post(path, body, @headers.merge(headers)) + if is_post + @http.post(path, body, @headers.merge(headers)) + else + @http.get(path, @headers.merge(headers)) + end + end + + # Backwards compatability + def get_host_details(id) # Left for backwards compatability + status_detailed(id) end end end \ No newline at end of file