jolokia 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format Fivemat
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ group :test do
4
+ gem 'json_expressions'
5
+ gem 'rspec'
6
+ gem 'fivemat'
7
+ gem 'pry'
8
+ gem 'oj'
9
+ end
10
+
11
+ # Specify your gem's dependencies in jolokia.gemspec
12
+ gemspec
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.
@@ -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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -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
@@ -0,0 +1,11 @@
1
+ require 'jolokia/version'
2
+ require 'jolokia/client'
3
+ require 'jolokia/remote_error'
4
+
5
+ module Jolokia
6
+ class << self
7
+ def new(opts = {})
8
+ Jolokia::Client.new(opts)
9
+ end
10
+ end
11
+ end
@@ -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,9 @@
1
+ module Jolokia
2
+ class RemoteError < RuntimeError
3
+ attr_reader :status, :message, :stacktrace
4
+
5
+ def initialize(status, message, stacktrace)
6
+ @status, @message, @stacktrace = status, message, stacktrace
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Jolokia
2
+ VERSION = "0.1.0"
3
+ 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
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jolokia do
4
+ it 'creates an instance of Jolokia::Client' do
5
+ Jolokia.new.should be_a Jolokia::Client
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:default, :test)
4
+
5
+ require 'jolokia'
6
+
7
+ require 'json_expressions/rspec'
8
+ require 'pry'
9
+ require 'oj'
10
+
11
+ RSpec.configure do |c|
12
+ c.mock_with :rspec
13
+ end
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