mmonit 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +35 -0
- data/lib/mmonit.rb +2 -0
- data/lib/mmonit/connection.rb +90 -0
- data/lib/mmonit/version.rb +3 -0
- data/mmonit.gemspec +15 -0
- metadata +50 -0
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
mmonit-ruby
|
2
|
+
===========
|
3
|
+
|
4
|
+
Ruby interface for M/Monit
|
5
|
+
|
6
|
+
All the commands listed here are currently available:
|
7
|
+
|
8
|
+
http://mmonit.com/wiki/MMonit/HTTP-API
|
9
|
+
|
10
|
+
Requests are read-only until I find a way to do more.
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
mmonit = MMonit::Connection.new({
|
16
|
+
:ssl => true,
|
17
|
+
:username => 'USERNAME',
|
18
|
+
:password => 'PASSWORD',
|
19
|
+
:address => 'example.com',
|
20
|
+
:port => '443'
|
21
|
+
})
|
22
|
+
|
23
|
+
mmonit.connect
|
24
|
+
|
25
|
+
hosts = mmonit.hosts
|
26
|
+
|
27
|
+
p hosts
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
Custom requests can be made like:
|
32
|
+
|
33
|
+
mmonit.request(path, [body])
|
34
|
+
|
35
|
+
body is optional
|
data/lib/mmonit.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module MMonit
|
6
|
+
class Connection
|
7
|
+
attr_reader :http, :address, :port, :ssl, :username, :useragent, :headers
|
8
|
+
attr_writer :password
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@ssl = options[:ssl] || false
|
12
|
+
@address = options[:address]
|
13
|
+
options[:port] ||= @ssl ? '8443' : '8080'
|
14
|
+
@port = options[:port]
|
15
|
+
@username = options[:username]
|
16
|
+
@password = options[:password]
|
17
|
+
options[:useragent] ||= "MMonit-Ruby/#{MMonit::VERSION}"
|
18
|
+
@useragent = options[:useragent]
|
19
|
+
@headers = {
|
20
|
+
'Host' => @address,
|
21
|
+
'Referer' => "#{@url}/index.csp",
|
22
|
+
'Content-Type' => 'application/x-www-form-urlencoded',
|
23
|
+
'User-Agent' => @useragent,
|
24
|
+
'Connection' => 'keepalive'
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def connect
|
29
|
+
@http = Net::HTTP.new(@address, @port)
|
30
|
+
|
31
|
+
if @ssl
|
32
|
+
@http.use_ssl = true
|
33
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
34
|
+
end
|
35
|
+
|
36
|
+
@headers['Cookie'] = @http.get('/index.csp').response['set-cookie'].split(';').first
|
37
|
+
self.login
|
38
|
+
end
|
39
|
+
|
40
|
+
def login
|
41
|
+
self.request('/z_security_check', "z_username=#{@username}&z_password=#{@password}").code.to_i == 302
|
42
|
+
end
|
43
|
+
|
44
|
+
def status
|
45
|
+
JSON.parse(self.request('/json/status/list').body)['records']
|
46
|
+
end
|
47
|
+
|
48
|
+
def hosts
|
49
|
+
JSON.parse(self.request('/json/admin/hosts/list').body)['records']
|
50
|
+
end
|
51
|
+
|
52
|
+
def users
|
53
|
+
JSON.parse(self.request('/json/admin/users/list').body)['records']
|
54
|
+
end
|
55
|
+
|
56
|
+
def rules
|
57
|
+
JSON.parse(self.request('/json/admin/rules/list').body)['records']
|
58
|
+
end
|
59
|
+
|
60
|
+
def events
|
61
|
+
JSON.parse(self.request('/json/events/list').body)['records']
|
62
|
+
end
|
63
|
+
|
64
|
+
def topography
|
65
|
+
JSON.parse(self.request('/json/status/topography').body)['records']['Data']
|
66
|
+
end
|
67
|
+
|
68
|
+
def reports(hostid=nil)
|
69
|
+
body = String.new
|
70
|
+
body = "hostid=#{hostid.to_s}" if hostid
|
71
|
+
JSON.parse(self.request('/json/reports/overview', body).body)
|
72
|
+
end
|
73
|
+
|
74
|
+
def get_host(fqdn)
|
75
|
+
host = self.hosts.select{ |h| h['host'] == fqdn }
|
76
|
+
host.empty? ? nil : host.first
|
77
|
+
end
|
78
|
+
|
79
|
+
def delete_host(host)
|
80
|
+
host = get_host(host['host']) if host.key?('host') && ! host.key?('id')
|
81
|
+
return false unless host['id']
|
82
|
+
self.request('/admin/hosts/', "id=#{host['id']}&Delete=1")
|
83
|
+
end
|
84
|
+
|
85
|
+
def request(path, body="", headers = {})
|
86
|
+
self.connect unless @http.is_a?(Net::HTTP)
|
87
|
+
@http.post(path, body, @headers.merge(headers))
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/mmonit.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
require "mmonit/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ['Josh Blancett']
|
7
|
+
gem.email = ['joshblancett@gmail.com']
|
8
|
+
gem.homepage = 'http://github.com/jblancett/mmonit-ruby'
|
9
|
+
gem.summary = 'Ruby interface to M/Monit'
|
10
|
+
gem.description = gem.summary
|
11
|
+
gem.name = 'mmonit'
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.version = MMonit::VERSION
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mmonit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josh Blancett
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ruby interface to M/Monit
|
15
|
+
email:
|
16
|
+
- joshblancett@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- lib/mmonit.rb
|
23
|
+
- lib/mmonit/connection.rb
|
24
|
+
- lib/mmonit/version.rb
|
25
|
+
- mmonit.gemspec
|
26
|
+
homepage: http://github.com/jblancett/mmonit-ruby
|
27
|
+
licenses: []
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.23
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Ruby interface to M/Monit
|
50
|
+
test_files: []
|