Skip to content

Commit 036caf6

Browse files
Yuri Zubovmajormoses
Yuri Zubov
authored andcommitted
Added VertX health check and metrics
1 parent 95d52b3 commit 036caf6

16 files changed

+1333
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ mkmf.log
1616
.DS_Store
1717
.idea/*
1818
*.gem
19+
.ruby-version

.rubocop.yml

+8
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ RegexpLiteral:
2828

2929
Style/Documentation:
3030
Enabled: false
31+
32+
Style/FrozenStringLiteralComment:
33+
Enabled: false
34+
35+
Metrics/BlockLength:
36+
Exclude:
37+
- 'test/**/*.rb'
38+
- 'sensu-plugins-java.gemspec'

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ cache:
44
install:
55
- bundle install
66
rvm:
7-
- 2.0
87
- 2.1
98
- 2.2
109
- 2.3.0
@@ -27,7 +26,6 @@ deploy:
2726
on:
2827
tags: true
2928
all_branches: true
30-
rvm: 2.0
3129
rvm: 2.1
3230
rvm: 2.2
3331
rvm: 2.3.0

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
55

66
## [Unreleased]
7+
### Breaking Changes
8+
- dropped ruby 2.1 support (@yuri-zubov sponsored by Actility, https://www.actility.com)
9+
- added `rest-client` as a dependency which requires you to have a local c compiler present to install this plugin (@yuri-zubov sponsored by Actility, https://www.actility.com)
10+
11+
### Security
12+
- updated yard dependency to `~> 0.9.11` per: https://nvd.nist.gov/vuln/detail/CVE-2017-17042 (@yuri-zubov sponsored by Actility, https://www.actility.com)
13+
- updated rubocop dependency to `~> 0.51.0` per: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8418. (@yuri-zubov sponsored by Actility, https://www.actility.com)
14+
15+
### Added
16+
- Added ability to get metrics from VertX (@yuri-zubov sponsored by Actility, https://www.actility.com)
17+
- Added health-check VertX (@yuri-zubov sponsored by Actility, https://www.actility.com)
718

819
## [1.3.0] - 2017-09-05
920
### Added

Rakefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ require 'yard'
77
require 'yard/rake/yardoc_task'
88

99
YARD::Rake::YardocTask.new do |t|
10-
OTHER_PATHS = %w().freeze
10+
OTHER_PATHS = %w[].freeze
1111
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
12-
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md)
12+
t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md]
1313
end
1414

1515
RuboCop::RakeTask.new
@@ -35,4 +35,4 @@ task :check_binstubs do
3535
end
3636
end
3737

38-
task default: [:spec, :make_bin_executable, :yard, :rubocop, :check_binstubs]
38+
task default: %i[spec make_bin_executable yard rubocop check_binstubs]

bin/check-java-heap-pcnt.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env ruby
22

3-
bin_dir = File.expand_path(File.dirname(__FILE__))
4-
shell_script_path = File.join(bin_dir, File.basename($PROGRAM_NAME, '.rb') + '.sh')
3+
shell_script_path = File.join(__dir__, File.basename($PROGRAM_NAME, '.rb') + '.sh')
54

65
exec shell_script_path, *ARGV

bin/check-vertx-health.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# check-vertx-health
4+
#
5+
# DESCRIPTION:
6+
# check-vertx-health simple way to expose health checks for VertX
7+
#
8+
# OUTPUT:
9+
# plain text
10+
#
11+
# PLATFORMS:
12+
# Linux
13+
#
14+
# DEPENDENCIES:
15+
# gem: rest-client
16+
#
17+
#
18+
# USAGE:
19+
#
20+
#
21+
# NOTES:
22+
#
23+
# LICENSE:
24+
# Zubov Yuri <[email protected]> sponsored by Actility, https://www.actility.com
25+
# Released under the same terms as Sensu (the MIT license); see LICENSE
26+
# for details.
27+
#
28+
require 'sensu-plugin/check/cli'
29+
require 'rest-client'
30+
require 'json'
31+
32+
class CheckVertXHealth < Sensu::Plugin::Check::CLI
33+
include CommonVertX
34+
35+
option :endpoint,
36+
short: '-p ENDPOINT',
37+
long: '--endpointn ENDPOINT',
38+
description: 'VertX Endpoint',
39+
default: 'http://localhost:8080/rest/health'
40+
41+
def check_health
42+
response = request
43+
44+
result = JSON.parse(response)
45+
result['outcome']
46+
end
47+
48+
def run
49+
result = check_health
50+
51+
if result == 'UP'
52+
ok 'VertX is UP'
53+
elsif result == 'DOWN'
54+
warning 'VertX is DOWN'
55+
end
56+
rescue StandardError => e
57+
critical e.message
58+
end
59+
end

bin/metrics-java-heap-graphite.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env ruby
22

3-
bin_dir = File.expand_path(File.dirname(__FILE__))
4-
shell_script_path = File.join(bin_dir, File.basename($PROGRAM_NAME, '.rb') + '.sh')
3+
shell_script_path = File.join(__dir__, File.basename($PROGRAM_NAME, '.rb') + '.sh')
54

65
exec shell_script_path, *ARGV

bin/metrics-jstat.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env ruby
22

3-
bin_dir = File.expand_path(File.dirname(__FILE__))
4-
shell_script_path = File.join(bin_dir, File.basename($PROGRAM_NAME, '.rb') + '.py')
3+
shell_script_path = File.join(__dir__, File.basename($PROGRAM_NAME, '.rb') + '.py')
54

65
exec shell_script_path, *ARGV

bin/metrics-vertx.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#! /usr/bin/env ruby
2+
#
3+
# metrics-vertx
4+
#
5+
# DESCRIPTION:
6+
# metrics-vertx get metrics from VertX
7+
#
8+
# OUTPUT:
9+
# metric-data
10+
#
11+
# PLATFORMS:
12+
# Linux
13+
#
14+
# DEPENDENCIES:
15+
# gem: rest-clien
16+
#
17+
# USAGE:
18+
#
19+
#
20+
# NOTES:
21+
#
22+
# LICENSE:
23+
# Zubov Yuri <[email protected]> sponsored by Actility, https://www.actility.com
24+
# Released under the same terms as Sensu (the MIT license); see LICENSE
25+
# for details.
26+
#
27+
28+
require 'sensu-plugin/metric/cli'
29+
require 'rest-client'
30+
require 'json'
31+
32+
class MetricsVertX < Sensu::Plugin::Metric::CLI::Graphite
33+
include CommonVertX
34+
35+
option :endpoint,
36+
short: '-p ENDPOINT',
37+
long: '--endpointn ENDPOINT',
38+
description: 'VertX Endpoint',
39+
default: 'http://localhost:8080/rest/metrics'
40+
41+
option :scheme,
42+
description: 'Metric naming scheme, text to prepend to metric',
43+
short: '-S SCHEME',
44+
long: '--scheme SCHEME',
45+
default: "#{Socket.gethostname}.vertx"
46+
47+
def metrics
48+
response = request
49+
JSON.parse(response)
50+
end
51+
52+
def run
53+
metrics.each do |key, metrics|
54+
type_of_metric = metrics.delete('type')
55+
metrics.each do |metric_name, value|
56+
output("#{config[:scheme]}.#{key}.#{type_of_metric}.#{metric_name}", value)
57+
end
58+
end
59+
ok
60+
end
61+
end

lib/sensu-plugins-java.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
require 'sensu-plugins-java/common_vertx'
12
require 'sensu-plugins-java/version'
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module CommonVertX
2+
def request
3+
RestClient::Request.execute(
4+
method: :get,
5+
url: config[:endpoint]
6+
)
7+
end
8+
end

sensu-plugins-java.gemspec

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
1010
s.description = 'Sensu plugins for Java'
1111
s.email = '<[email protected]>'
1212
s.executables = Dir.glob('bin/**/*.rb').map { |file| File.basename(file) }
13-
s.files = Dir.glob('{bin,lib}/**/*') + %w(LICENSE README.md CHANGELOG.md)
13+
s.files = Dir.glob('{bin,lib}/**/*') + %w[LICENSE README.md CHANGELOG.md]
1414
s.homepage = 'https://github.com/sensu-plugins/sensu-plugins-java'
1515
s.license = 'MIT'
1616
s.metadata = { 'maintainer' => 'sensu-plugin',
@@ -22,21 +22,23 @@ Gem::Specification.new do |s|
2222
s.platform = Gem::Platform::RUBY
2323
s.post_install_message = 'You can use the embedded Ruby by setting EMBEDDED_RUBY=true in /etc/default/sensu'
2424
s.require_paths = ['lib']
25-
s.required_ruby_version = '>= 2.0.0'
25+
s.required_ruby_version = '>= 2.1.0'
2626
s.summary = 'Sensu plugins for Java'
2727
s.test_files = s.files.grep(%r{^(test|spec|features)/})
2828
s.version = SensuPluginsJava::Version::VER_STRING
2929

30-
s.add_runtime_dependency 'sensu-plugin', '~> 1.2'
3130
s.add_runtime_dependency 'english', '0.6.3'
31+
s.add_runtime_dependency 'rest-client', '~> 2.0'
32+
s.add_runtime_dependency 'sensu-plugin', '~> 1.2'
3233

3334
s.add_development_dependency 'bundler', '~> 1.7'
3435
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
3536
s.add_development_dependency 'github-markup', '~> 1.3'
3637
s.add_development_dependency 'pry', '~> 0.10'
3738
s.add_development_dependency 'rake', '~> 10.5'
3839
s.add_development_dependency 'redcarpet', '~> 3.2'
39-
s.add_development_dependency 'rubocop', '~> 0.40.0'
40-
s.add_development_dependency 'rspec', '~> 3.4'
41-
s.add_development_dependency 'yard', '~> 0.8'
40+
s.add_development_dependency 'rspec', '~> 3.7'
41+
s.add_development_dependency 'rspec-mocks', '~> 3.7'
42+
s.add_development_dependency 'rubocop', '~> 0.51.0'
43+
s.add_development_dependency 'yard', '~> 0.9.11'
4244
end

test/check-vertx-health_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require_relative './spec_helper.rb'
2+
require_relative '../bin/check-vertx-health'
3+
require_relative './fixtures.rb'
4+
5+
describe 'CheckVertXHealth' do
6+
before do
7+
CheckVertXHealth.class_variable_set(:@@autorun, false)
8+
end
9+
10+
describe 'with positive answer' do
11+
before do
12+
@check = CheckVertXHealth.new
13+
allow(@check).to receive(:request).and_return(vertx_health_good_response)
14+
end
15+
16+
describe '#run' do
17+
it 'tests that a check are ok' do
18+
expect(@check).to receive(:ok).with('VertX is UP')
19+
20+
@check.run
21+
end
22+
end
23+
end
24+
25+
describe 'with negative answer' do
26+
before do
27+
@check = CheckVertXHealth.new
28+
allow(@check).to receive(:request).and_return(vertx_health_bad_response)
29+
end
30+
31+
describe '#run' do
32+
it 'tests that a check are ok' do
33+
expect(@check).to receive(:warning).with('VertX is DOWN')
34+
35+
@check.run
36+
end
37+
end
38+
end
39+
40+
describe 'can not receive answer' do
41+
before do
42+
@check = CheckVertXHealth.new
43+
allow(@check).to receive(:request).and_raise(StandardError, 'error')
44+
end
45+
46+
describe '#run' do
47+
it 'tests that a check are ok' do
48+
expect(@check).to receive(:critical).with('error')
49+
50+
@check.run
51+
end
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)