jolokia 0.1.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/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +12 -0
- data/LICENSE +22 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/jolokia.gemspec +20 -0
- data/lib/jolokia.rb +11 -0
- data/lib/jolokia/client.rb +67 -0
- data/lib/jolokia/remote_error.rb +9 -0
- data/lib/jolokia/version.rb +3 -0
- data/spec/lib/jolokia/client_spec.rb +101 -0
- data/spec/lib/jolokia_spec.rb +7 -0
- data/spec/spec_helper.rb +13 -0
- metadata +94 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tower He
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Jolokia
|
2
|
+
|
3
|
+
The Jolokia Ruby library provides a ruby API to the to the [Jolokia](http://www.jolokia.org) Agent.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'jolokia'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install jolokia
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
First of all, you need to create an instance of Jolokia::Client to
|
22
|
+
comunicate with the remote [Jolokia](http://www.jolokia.org) Agent. You just need
|
23
|
+
to pass the url of the [Jolokia](http://www.jolokia.org) Agent to the constructor for creating the
|
24
|
+
client.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
jolokia = Jolokia.new(url: 'http://localhost:8080/jolokia')
|
28
|
+
```
|
29
|
+
|
30
|
+
And then, you can use the created client to read or write the attributes
|
31
|
+
of the MBeans, or execute the operations of the MBeans.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
response = jolokia.request(:post, type: 'read',
|
35
|
+
mbean: 'java.lang:type=Memory',
|
36
|
+
attribute: 'HeapMemoryUsage')
|
37
|
+
pp response
|
38
|
+
|
39
|
+
# =>
|
40
|
+
{"timestamp"=>1340783789,
|
41
|
+
"status"=>200,
|
42
|
+
"request"=>
|
43
|
+
{"mbean"=>"java.lang:type=Memory",
|
44
|
+
"attribute"=>"HeapMemoryUsage",
|
45
|
+
"type"=>"read"},
|
46
|
+
"value"=>
|
47
|
+
{"max"=>477233152,
|
48
|
+
"committed"=>190382080,
|
49
|
+
"init"=>134217728,
|
50
|
+
"used"=>116851464}}
|
51
|
+
```
|
52
|
+
|
53
|
+
### API
|
54
|
+
|
55
|
+
* `get_attribute(mbean, attribute, path = nil)`
|
56
|
+
* `set_attribute(mbean, attribute, value, path = nil)`
|
57
|
+
* `execute(mbean, operations, args)`
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/jolokia.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/jolokia/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Tower He']
|
6
|
+
gem.email = ['towerhe@gmail.com']
|
7
|
+
gem.description = %q{The Jolokia ruby library }
|
8
|
+
gem.summary = %q{The Jolokia ruby library provides a ruby API to the to the Jolokia agent. }
|
9
|
+
gem.homepage = 'https://github.com/towerhe/jolokia'
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = 'jolokia'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = Jolokia::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'faraday', '~> 0.8.1'
|
19
|
+
gem.add_dependency 'faraday_middleware', '~> 0.8.8'
|
20
|
+
end
|
data/lib/jolokia.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
module Jolokia
|
5
|
+
class Client
|
6
|
+
attr_accessor :url
|
7
|
+
|
8
|
+
def initialize(opts = {})
|
9
|
+
@url = opts[:url]
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_attribute(mbean, attribute, path = nil)
|
13
|
+
options = { 'type' => 'read', 'mbean' => mbean, 'attribute' => attribute }
|
14
|
+
options['path'] = path if path
|
15
|
+
|
16
|
+
request(:post, options)['value']
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_attribute(mbean, attribute, value, path = nil)
|
20
|
+
options = {
|
21
|
+
'type' => 'write',
|
22
|
+
'mbean' => mbean,
|
23
|
+
'attribute' => attribute,
|
24
|
+
'value' => value
|
25
|
+
}
|
26
|
+
options['path'] = path if path
|
27
|
+
|
28
|
+
request(:post, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def execute(mbean, operation, args = nil)
|
32
|
+
options = {
|
33
|
+
'type' => 'exec',
|
34
|
+
'mbean' => mbean,
|
35
|
+
'operation' => operation
|
36
|
+
}
|
37
|
+
|
38
|
+
if args
|
39
|
+
options['arguments'] = args.is_a?(Array) ? args : [args]
|
40
|
+
end
|
41
|
+
|
42
|
+
request(:post, options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def request(method, opts)
|
46
|
+
resp = connection.send(method, '', opts)
|
47
|
+
|
48
|
+
if resp.body['status'] != 200
|
49
|
+
raise RemoteError.new(resp.body['status'],
|
50
|
+
resp.body['error'],
|
51
|
+
resp.body['stacktrace'])
|
52
|
+
end
|
53
|
+
|
54
|
+
resp.body
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def connection
|
60
|
+
@conn ||= ::Faraday.new(url) do |f|
|
61
|
+
f.request :json
|
62
|
+
f.response :json
|
63
|
+
f.adapter :net_http
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jolokia::Client do
|
4
|
+
let(:url) { 'http://localhost:8080/jolokia' }
|
5
|
+
let(:client) { Jolokia::Client.new(url: url) }
|
6
|
+
|
7
|
+
before do
|
8
|
+
client.stub(:connection) do
|
9
|
+
@conn = Faraday.new do |b|
|
10
|
+
b.request :json
|
11
|
+
b.response :json
|
12
|
+
b.adapter :test do |stub|
|
13
|
+
stub.post('/') do |env|
|
14
|
+
posted_as = env[:request_headers]['Content-Type']
|
15
|
+
body = Oj.load(env[:body]).merge('status' => 200)
|
16
|
+
|
17
|
+
[200, {'Content-Type' => posted_as }, Oj.dump(body)]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#request' do
|
25
|
+
shared_examples 'Request executed successfully' do
|
26
|
+
it 'passes valid params' do
|
27
|
+
response.should match_json_expression(options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:response) do
|
32
|
+
client.request :post, options
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when reading attributes' do
|
36
|
+
let(:options) do
|
37
|
+
{
|
38
|
+
'type' => 'read',
|
39
|
+
'mbean' => 'java.lang:type=Memory',
|
40
|
+
'attribute' => attribute,
|
41
|
+
'status' => 200
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'only a single attribute' do
|
46
|
+
let(:attribute) { 'HeapMemoryUsage' }
|
47
|
+
|
48
|
+
it_should_behave_like 'Request executed successfully'
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'multi-attributes' do
|
52
|
+
let(:attribute) { ['HeapMemoryUsage', 'NonHeapMemoryUsage'] }
|
53
|
+
|
54
|
+
it_should_behave_like 'Request executed successfully'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when writing an attribute' do
|
59
|
+
let(:options) do
|
60
|
+
{
|
61
|
+
'type' => 'write',
|
62
|
+
'mbean' => 'java.lang:type=ClassLoading',
|
63
|
+
'attribute' => 'Verbose',
|
64
|
+
'value' => true,
|
65
|
+
'status' => 200
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
it_should_behave_like 'Request executed successfully'
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when executing JMX operations (exec)' do
|
73
|
+
context 'without any params' do
|
74
|
+
let(:options) do
|
75
|
+
{
|
76
|
+
'type' => 'exec',
|
77
|
+
'mbean' => 'java.lang:type=Memory',
|
78
|
+
'operation' => 'gc',
|
79
|
+
'status' => 200
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
it_should_behave_like 'Request executed successfully'
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with params' do
|
87
|
+
let(:options) do
|
88
|
+
{
|
89
|
+
'type' => 'exec',
|
90
|
+
'mbean' => 'java.lang:type=Threading',
|
91
|
+
'operation' => 'dumpAllThreads',
|
92
|
+
'arguments' => [true, true],
|
93
|
+
'status' => 200
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
it_should_behave_like 'Request executed successfully'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jolokia
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tower He
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.8.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: faraday_middleware
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.8.8
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.8.8
|
46
|
+
description: ! 'The Jolokia ruby library '
|
47
|
+
email:
|
48
|
+
- towerhe@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- jolokia.gemspec
|
60
|
+
- lib/jolokia.rb
|
61
|
+
- lib/jolokia/client.rb
|
62
|
+
- lib/jolokia/remote_error.rb
|
63
|
+
- lib/jolokia/version.rb
|
64
|
+
- spec/lib/jolokia/client_spec.rb
|
65
|
+
- spec/lib/jolokia_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
homepage: https://github.com/towerhe/jolokia
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.24
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: The Jolokia ruby library provides a ruby API to the to the Jolokia agent.
|
91
|
+
test_files:
|
92
|
+
- spec/lib/jolokia/client_spec.rb
|
93
|
+
- spec/lib/jolokia_spec.rb
|
94
|
+
- spec/spec_helper.rb
|