diff --git a/.rubocop.yml b/.rubocop.yml index 90f83f8..4a096ad 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -36,4 +36,7 @@ Style/GuardClause: Enabled: false Style/Next: - Enabled: false \ No newline at end of file + Enabled: false + +AllCops: + TargetRubyVersion: 2.3 diff --git a/.travis.yml b/.travis.yml index 814039b..57a7bac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,6 @@ cache: install: - bundle install rvm: -- 2.0 -- 2.1 -- 2.2 - 2.3.0 - 2.4.1 notifications: @@ -27,9 +24,6 @@ deploy: on: tags: true all_branches: true - rvm: 2.0 - rvm: 2.1 - rvm: 2.2 rvm: 2.3.0 rvm: 2.4.1 repo: sensu-plugins/sensu-plugins-sensu diff --git a/CHANGELOG.md b/CHANGELOG.md index 97fb086..12f3a60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,26 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +#### Security +- updated `yard` dependency to `~> 0.9.11` per: https://nvd.nist.gov/vuln/detail/CVE-2017-17042 which closes attacks against a yard server loading arbitrary files (@majormoses) +- updated rubocop dependency to `~> 0.51.0` per: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8418. (@majormoses) + +### Breaking Changes +- removing ruby support for `< 2.3` versions as they are EOL (@majormoses) +- metrics-aggregate.rb: removed support for `sensu` api versions lower than `0.24` you can read about it [here](https://github.com/sensu/sensu/issues/1218) (@majormoses) + +### Removed +- gemnasium badge as github offers native feature and they were bought by gitlab and no longer available as a standalone product (@majormoses) + +### Added +- slack badge (@majormoses) + +### Changed +- check-stale-results.rb: improve error message when there is no api key in sensu settings (@majormoses) +- bumped dependency of `sensu-plugin` to `~> 2.5` (@majormoses) +- appeasing the cops (@majormoses) + +## [3.0.0] - 2018-05-17 ### Breaking Change - bumped dependency of `sensu-plugin` to 2.x you can read about it [here](https://github.com/sensu-plugins/sensu-plugin/blob/master/CHANGELOG.md#v200---2017-03-29) diff --git a/Gemfile b/Gemfile index be18ba1..c9fd755 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in sensu-plugins-sensu.gemspec diff --git a/README.md b/README.md index 06daf00..21c6e06 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Gem Version](https://badge.fury.io/rb/sensu-plugins-sensu.svg)](http://badge.fury.io/rb/sensu-plugins-sensu) [![Code Climate](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu/badges/gpa.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu) [![Test Coverage](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu/badges/coverage.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu) -[![Dependency Status](https://gemnasium.com/sensu-plugins/sensu-plugins-sensu.svg)](https://gemnasium.com/sensu-plugins/sensu-plugins-sensu) +[![Community Slack](https://slack.sensu.io/badge.svg)](https://slack.sensu.io/badge) ## Functionality diff --git a/Rakefile b/Rakefile index 2c45ac9..991f7b1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bundler/gem_tasks' require 'github/markup' require 'redcarpet' @@ -8,9 +10,9 @@ require 'yard/rake/yardoc_task' require 'English' YARD::Rake::YardocTask.new do |t| - OTHER_PATHS = %w().freeze + OTHER_PATHS = %w[].freeze t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS] - t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md) + t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md] end RuboCop::RakeTask.new @@ -36,4 +38,4 @@ task :check_binstubs do end end -task default: [:spec, :make_bin_executable, :yard, :rubocop, :check_binstubs] +task default: %i[spec make_bin_executable yard rubocop check_binstubs] diff --git a/bin/check-aggregate.rb b/bin/check-aggregate.rb index c3e949a..829d3e1 100755 --- a/bin/check-aggregate.rb +++ b/bin/check-aggregate.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # # Check Aggregate # === @@ -171,7 +173,7 @@ def honor_stash(aggregate) aggregate[:results].delete_if do |entry| begin api_request("/stashes/silence/#{entry[:client]}/#{config[:check]}") - if entry[:status] == 0 + if entry[:status].zero? aggregate[:ok] = aggregate[:ok] - 1 elsif entry[:status] == 1 aggregate[:warning] = aggregate[:warning] - 1 @@ -192,7 +194,7 @@ def honor_stash(aggregate) def collect_output(aggregate) output = '' aggregate[:results].each do |entry| - output << entry[:output] + "\n" unless entry[:status] == 0 + output << entry[:output] + "\n" unless entry[:status].zero? end aggregate[:outputs] = [output] end @@ -208,7 +210,7 @@ def acquire_aggregate def named_aggregate_results results = api_request("/aggregates/#{config[:check]}?max_age=#{config[:age]}")[:results] - warning "No aggregates found in last #{config[:age]} seconds" if %w(ok warning critical unknown).all? { |x| results[x.to_sym] == 0 } + warning "No aggregates found in last #{config[:age]} seconds" if %w[ok warning critical unknown].all? { |x| results[x.to_sym].zero? } results end @@ -263,7 +265,7 @@ def compare_pattern(aggregate) if config[:debug] message += "\n" + aggregate.to_s end - aggregate[:outputs].each do |output, _count| + aggregate[:outputs].each_key do |output| matched = regex.match(output.to_s) unless matched.nil? key = matched[1] diff --git a/bin/check-stale-results.rb b/bin/check-stale-results.rb index 54db581..dd20711 100755 --- a/bin/check-stale-results.rb +++ b/bin/check-stale-results.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # # check-stale-results.rb # @@ -54,7 +56,7 @@ def initialize def humanize(secs) [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map do |count, name| - if secs > 0 + if secs.positive? secs, n = secs.divmod(count) "#{n.to_i} #{name}" end @@ -69,7 +71,11 @@ def get_uri(path) def api_request(method, path) unless settings.key?('api') - raise 'api.json settings not found.' + unknown <<~HEREDOC + sensu does not have an api config stanza set, please configure it in + either /etc/sensu/config.json or in any config that is loaded by sensu + such as /etc/sensu/conf.d/api.json + HEREDOC end uri = get_uri(path) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| @@ -85,7 +91,7 @@ def api_request(method, path) def results res = [] req = api_request(:GET, '/results') - res = JSON.parse(req.body) if req && req.code == '200' + res = JSON.parse(req.body) if req&.code == '200' res end diff --git a/bin/handler-purge-stale-results.rb b/bin/handler-purge-stale-results.rb index 676df82..aebc694 100755 --- a/bin/handler-purge-stale-results.rb +++ b/bin/handler-purge-stale-results.rb @@ -1,3 +1,6 @@ +#!/usr/bin/env ruby +# frozen_string_literal: false + # # handler-purge-stale-results.rb # @@ -36,7 +39,7 @@ class HandlerPurgeStaleResults < Sensu::Handler def results res = [] req = api_request(:GET, '/results') - res = JSON.parse(req.body) if req && req.code == '200' + res = JSON.parse(req.body) if req&.code == '200' res end @@ -54,33 +57,33 @@ def handle else deleted << "#{result['client']} - #{result['check']['name']}" end - rescue + rescue StandardError failed << "#{result['client']} - #{result['check']['name']} (Caught exception: #{$ERROR_INFO})" end end end if !deleted.empty? || !failed.empty? - msg = < -To: <#{config[:mail_recipient]}> -Subject: Purge stale check results + msg = <<~MESSAGE + From: Sensu <#{config[:mail_sender]}> + To: <#{config[:mail_recipient]}> + Subject: Purge stale check results -This is a notification concerning the #{self.class.name} sensu handler running at #{Socket.gethostname} + This is a notification concerning the #{self.class.name} sensu handler running at #{Socket.gethostname} -* Summary + * Summary - Deleted: #{deleted.size} - Failed to delete: #{failed.size} + Deleted: #{deleted.size} + Failed to delete: #{failed.size} -* Failed to delete check results: + * Failed to delete check results: -#{failed.map { |m| " #{m}" }.join("\n")} + #{failed.map { |m| " #{m}" }.join("\n")} -* Deleted check results: + * Deleted check results: -#{deleted.map { |m| " #{m}" }.join("\n")} -EOF + #{deleted.map { |m| " #{m}" }.join("\n")} + MESSAGE Net::SMTP.start(config[:mail_server]) do |smtp| smtp.send_message(msg, config[:mail_sender], config[:mail_recipient]) diff --git a/bin/handler-sensu-deregister.rb b/bin/handler-sensu-deregister.rb index e993e77..4277518 100755 --- a/bin/handler-sensu-deregister.rb +++ b/bin/handler-sensu-deregister.rb @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: false require 'rubygems' require 'sensu-handler' @@ -21,13 +22,13 @@ def handle end def delete_sensu_client! - if config[:invalidate] && config[:invalidate_expire] - response = api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}&#{config[:invalidate_expire]}").code - elsif config[:invalidate] - response = api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}").code - else - response = api_request(:DELETE, '/clients/' + @event['client']['name']).code - end + response = if config[:invalidate] && config[:invalidate_expire] + api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}&#{config[:invalidate_expire]}").code + elsif config[:invalidate] + api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}").code + else + api_request(:DELETE, '/clients/' + @event['client']['name']).code + end deletion_status(response) end diff --git a/bin/handler-sensu.rb b/bin/handler-sensu.rb index a033d04..f5242a5 100755 --- a/bin/handler-sensu.rb +++ b/bin/handler-sensu.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # ################## # Sensu Remediator diff --git a/bin/metrics-aggregate.rb b/bin/metrics-aggregate.rb index 5b731d7..7f7170e 100755 --- a/bin/metrics-aggregate.rb +++ b/bin/metrics-aggregate.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # # Aggregate Metrics # @@ -115,27 +117,12 @@ def run acquire_checks.each do |check| aggregate = get_aggregate(check['name']) puts "#{check['name']} aggregates: #{aggregate}" if config[:debug] - aggregate.each do |result, count| - # in 0.24 they changed the api results for aggregates this helps - # maintain backwards compatibility with 0.23 and newer versions. - if count.is_a?(Hash) - count.each do |x, y| - output metric_name: x, - value: y, - graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{x}", - statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{x}", - influxdb_measurement: config[:measurement], - tags: { - check: check['name'], - host: Socket.gethostname - }, - timestamp: timestamp - end - else - output metric_name: result, - value: count, - graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{result}", - statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{result}", + aggregate.each_value do |count| + count.each do |x, y| + output metric_name: x, + value: y, + graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{x}", + statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{x}", influxdb_measurement: config[:measurement], tags: { check: check['name'], diff --git a/bin/metrics-delete-expired-stashes.rb b/bin/metrics-delete-expired-stashes.rb index 330e1af..1514a7b 100755 --- a/bin/metrics-delete-expired-stashes.rb +++ b/bin/metrics-delete-expired-stashes.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # # Delete stashes when their 'expires' timestamp is exceeded # === @@ -14,12 +16,11 @@ require 'json' require 'socket' -include Sensu::Plugin::Utils - class CheckSilenced < Sensu::Plugin::Metric::CLI::Generic + include Sensu::Plugin::Utils default_host = begin settings['api']['host'] - rescue + rescue StandardError 'localhost' end @@ -81,7 +82,7 @@ def acquire_stashes all_stashes.each do |stash| filtered_stashes << stash if stash['path'] =~ /^#{@config[:filter]}\/.*/ end - return filtered_stashes + filtered_stashes rescue Errno::ECONNREFUSED warning 'Connection refused' rescue RestClient::RequestTimeout @@ -99,9 +100,9 @@ def run stashes = acquire_stashes now = Time.now.to_i @count = 0 - if stashes.count > 0 + if stashes.count.positive? stashes.each do |stash| - if stash['content'].key?('expires') && now - stash['content']['expires'] > 0 + if stash['content'].key?('expires') && now - stash['content']['expires'] .positive? delete_stash(stash) unless config[:noop] @count += 1 end diff --git a/bin/metrics-events.rb b/bin/metrics-events.rb index 87cfdb2..c511bad 100755 --- a/bin/metrics-events.rb +++ b/bin/metrics-events.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: false + # # Event Metrics # diff --git a/lib/sensu-plugins-sensu.rb b/lib/sensu-plugins-sensu.rb index 4d3a0d4..edcac19 100644 --- a/lib/sensu-plugins-sensu.rb +++ b/lib/sensu-plugins-sensu.rb @@ -1 +1,3 @@ +# frozen_string_literal: true + require 'sensu-plugins-sensu/version' diff --git a/lib/sensu-plugins-sensu/version.rb b/lib/sensu-plugins-sensu/version.rb index a7fc136..d3bc004 100644 --- a/lib/sensu-plugins-sensu/version.rb +++ b/lib/sensu-plugins-sensu/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SensuPluginsSensu # This defines the version of the gem module Version diff --git a/sensu-plugins-sensu.gemspec b/sensu-plugins-sensu.gemspec index e0b6479..c1d39ff 100644 --- a/sensu-plugins-sensu.gemspec +++ b/sensu-plugins-sensu.gemspec @@ -1,17 +1,19 @@ -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'date' require_relative 'lib/sensu-plugins-sensu' -Gem::Specification.new do |s| +Gem::Specification.new do |s| # rubocop:disable Metrics/BlockLength s.authors = ['Sensu-Plugins and contributors'] s.date = Date.today.to_s s.description = 'This plugin provides monitoring and metrics for Sensu.' s.email = '' s.executables = Dir.glob('bin/**/*.rb').map { |file| File.basename(file) } - s.files = Dir.glob('{bin,lib}/**/*') + %w(LICENSE README.md CHANGELOG.md) + s.files = Dir.glob('{bin,lib}/**/*') + %w[LICENSE README.md CHANGELOG.md] s.homepage = 'https://github.com/sensu-plugins/sensu-plugins-sensu' s.license = 'MIT' s.metadata = { 'maintainer' => 'sensu-plugin', @@ -23,22 +25,22 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.post_install_message = 'You can use the embedded Ruby by setting EMBEDDED_RUBY=true in /etc/default/sensu' s.require_paths = ['lib'] - s.required_ruby_version = '>= 2.0.0' + s.required_ruby_version = '>= 2.3.0' s.summary = 'Sensu plugins for sensu' s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.version = SensuPluginsSensu::Version::VER_STRING - s.add_runtime_dependency 'sensu-plugin', '~> 2.4' - s.add_runtime_dependency 'rest-client', '1.8.0' s.add_runtime_dependency 'chronic_duration', '0.10.6' + s.add_runtime_dependency 'rest-client', '1.8.0' + s.add_runtime_dependency 'sensu-plugin', '~> 2.5' s.add_development_dependency 'bundler', '~> 1.7' s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4' s.add_development_dependency 'github-markup', '~> 1.3' s.add_development_dependency 'pry', '~> 0.10' - s.add_development_dependency 'rubocop', '~> 0.40.0' - s.add_development_dependency 'rspec', '~> 3.1' s.add_development_dependency 'rake', '~> 10.0' s.add_development_dependency 'redcarpet', '~> 3.2' - s.add_development_dependency 'yard', '~> 0.8' + s.add_development_dependency 'rspec', '~> 3.1' + s.add_development_dependency 'rubocop', '~> 0.51.0' + s.add_development_dependency 'yard', '~> 0.9.11' end diff --git a/test/spec_helper.rb b/test/spec_helper.rb index 9797982..3bb6bfe 100644 --- a/test/spec_helper.rb +++ b/test/spec_helper.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + require 'codeclimate-test-reporter' CodeClimate::TestReporter.start