Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.deb
*.gem
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
mmonit-ruby
===========
# mmonit-ruby
----

Ruby interface for M/Monit

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',
Expand All @@ -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
- Review events methods
64 changes: 51 additions & 13 deletions lib/mmonit/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
end
2 changes: 1 addition & 1 deletion lib/mmonit/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MMonit
VERSION = "0.0.4"
VERSION = "0.0.6"
end
9 changes: 5 additions & 4 deletions mmonit.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']
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