diff --git a/.gitignore b/.gitignore index c00df13..39897ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.deb +*.gem diff --git a/README.md b/README.md index bd8b9ab..305bc05 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -mmonit-ruby -=========== +# mmonit-ruby +---- Ruby interface for M/Monit @@ -7,11 +7,9 @@ All the commands listed here are currently available: http://mmonit.com/wiki/MMonit/HTTP-API -Requests are read-only until I find a way to do more. - - - +## Examples +``` mmonit = MMonit::Connection.new({ :ssl => true, :username => 'USERNAME', @@ -26,10 +24,12 @@ hosts = mmonit.hosts p hosts - +``` Custom requests can be made like: -mmonit.request(path, [body]) + mmonit.request(path, [body]) + +## TODO -body is optional \ No newline at end of file +- Review events methods diff --git a/lib/mmonit/connection.rb b/lib/mmonit/connection.rb index bb7dc09..456abc0 100644 --- a/lib/mmonit/connection.rb +++ b/lib/mmonit/connection.rb @@ -18,7 +18,6 @@ def initialize(options = {}) @useragent = options[:useragent] @headers = { 'Host' => @address, - 'Referer' => "#{@url}/index.csp", 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => @useragent, 'Connection' => 'keepalive' @@ -33,32 +32,40 @@ def connect @http.verify_mode = OpenSSL::SSL::VERIFY_NONE end - @headers['Cookie'] = @http.get('/index.csp').response['set-cookie'].split(';').first + @headers['Cookie'] = @http.get('/index.csp', initheader = @headers).response['set-cookie'].split(';').first self.login 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}&z_csrf_protection=off").code.to_i == 302 + end + + def logout + self.request('/login/logout.csp') end def status - JSON.parse(self.request('/json/status/list').body)['records'] + JSON.parse(self.request('/status/list').body)['records'] end def hosts - JSON.parse(self.request('/json/admin/hosts/list').body)['records'] + JSON.parse(self.request('/admin/hosts/list').body)['records'] + end + + def groups + JSON.parse(self.request('/admin/groups/list').body)['groups'] end 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) + JSON.parse(self.request('/admin/alerts/list').body) end def events - JSON.parse(self.request('/json/events/list').body)['records'] + JSON.parse(self.request('/events/list').body)['records'] end #### topography and reports are disabled until I figure out their new equivalent in M/Monit @@ -73,13 +80,17 @@ def events # end def find_host(fqdn) - host = self.hosts.select{ |h| h['host'] == fqdn } + host = self.hosts.select{ |h| h['hostname'] == fqdn } host.empty? ? nil : host.first end - # another option: /admin/hosts/json/get?id=#### + def find_group(group) + groups = self.groups.select{ |g| g['name'] == group } + groups.empty? ? nil : groups.first + end + def get_host_details(id) - JSON.parse(self.request("/json/status/detail?hostid=#{id}").body)['records']['host'] rescue nil + JSON.parse(self.request("/status/hosts/get?id=#{id}").body)['records']['host'] rescue nil end def delete_host(host) @@ -88,9 +99,36 @@ def delete_host(host) self.request("/admin/hosts/delete?id=#{host['id']}") end + def delete_group(group) + group = self.find_group(group) unless group.key?('id') + return false unless group['id'] + self.request("/admin/groups/delete", "id=#{group['id']}") + end + + def create_group(group) + self.request("/admin/groups/create", "name=#{group}") + end + + def add_group(group, hostids) + group = self.find_group(group) if group.is_a?(String) + return false unless group['id'] + if hostids.is_a?(Array) + hostids_str = hostids.join('&hostid=') + elsif hostids.is_a?(String) + hostids_str = hostids + elsif hostid.key('id') + hostids_str = hostids['id'] + end + self.request("/admin/groups/add", "id=#{group['id']}&hostid=#{hostids_str}") + end + def request(path, body="", headers = {}) self.connect unless @http.is_a?(Net::HTTP) - @http.post(path, body, @headers.merge(headers)) + if body == "" + @http.get(path, initheader = @headers.merge(headers)) + else + @http.post(path, body, @headers.merge(headers)) + end end end -end \ No newline at end of file +end diff --git a/lib/mmonit/version.rb b/lib/mmonit/version.rb index 0a3f29c..f600647 100644 --- a/lib/mmonit/version.rb +++ b/lib/mmonit/version.rb @@ -1,3 +1,3 @@ module MMonit - VERSION = "0.0.4" + VERSION = "0.0.6" end diff --git a/mmonit.gemspec b/mmonit.gemspec index 7626487..cff0675 100644 --- a/mmonit.gemspec +++ b/mmonit.gemspec @@ -1,15 +1,16 @@ # -*- encoding: utf-8 -*- -$:.unshift File.expand_path("../lib", __FILE__) -require "mmonit/version" + +require "./lib/mmonit/version" Gem::Specification.new do |gem| gem.authors = ['Josh Blancett'] gem.email = ['joshblancett@gmail.com'] gem.homepage = 'http://github.com/jblancett/mmonit-ruby' gem.summary = 'Ruby interface to M/Monit' - gem.description = gem.summary + gem.description = "Ruby interface for M/Monit\nAll the commands listed here are currently available:\nhttp://mmonit.com/wiki/MMonit/HTTP-API\n" gem.name = 'mmonit' - gem.files = `git ls-files`.split("\n") + gem.files = Dir["README.md", "lib/**/*"] gem.require_paths = ['lib'] gem.version = MMonit::VERSION + gem.license = 'MIT' end