Skip to content

Commit d87f2bc

Browse files
committed
Multiple Updates:
- improving error message in `check-stale-results` when api config is not present - updating `rubocop` dep and appeasing the cops - updating `yard` dep - updating `sensu-plugin` dep - added slack badge - removed gemnasium badge CVE's addressed: - https://nvd.nist.gov/vuln/detail/CVE-2017-17042 - https://nvd.nist.gov/vuln/detail/CVE-2017-8418 Breaking Changes: - removed ruby `< 2.3` support Signed-off-by: Ben Abrams <[email protected]>
1 parent 0bf8ab2 commit d87f2bc

18 files changed

+109
-76
lines changed

.rubocop.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ Style/GuardClause:
3636
Enabled: false
3737

3838
Style/Next:
39-
Enabled: false
39+
Enabled: false
40+
41+
AllCops:
42+
TargetRubyVersion: 2.3

.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ cache:
44
install:
55
- bundle install
66
rvm:
7-
- 2.0
8-
- 2.1
9-
- 2.2
107
- 2.3.0
118
- 2.4.1
129
notifications:
@@ -27,9 +24,6 @@ deploy:
2724
on:
2825
tags: true
2926
all_branches: true
30-
rvm: 2.0
31-
rvm: 2.1
32-
rvm: 2.2
3327
rvm: 2.3.0
3428
rvm: 2.4.1
3529
repo: sensu-plugins/sensu-plugins-sensu

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
66

77
## [Unreleased]
88

9+
#### Security
10+
- 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)
11+
- updated rubocop dependency to `~> 0.51.0` per: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-8418. (@majormoses)
12+
13+
### Breaking Changes
14+
- removing ruby support for `< 2.3` versions as they are EOL (@majormoses)
15+
- 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)
16+
17+
### Removed
18+
- gemnasium badge as github offers native feature and they were bought by gitlab and no longer available as a standalone product (@majormoses)
19+
20+
### Added
21+
- slack badge (@majormoses)
22+
23+
### Changed
24+
- check-stale-results.rb: improve error message when there is no api key in sensu settings (@majormoses)
25+
- bumped dependency of `sensu-plugin` to `~> 2.5` (@majormoses)
26+
- appeasing the cops (@majormoses)
27+
28+
## [3.0.0] - 2018-05-17
929
### Breaking Change
1030
- 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)
1131

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in sensu-plugins-sensu.gemspec

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Gem Version](https://badge.fury.io/rb/sensu-plugins-sensu.svg)](http://badge.fury.io/rb/sensu-plugins-sensu)
55
[![Code Climate](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu/badges/gpa.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu)
66
[![Test Coverage](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu/badges/coverage.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-sensu)
7-
[![Dependency Status](https://gemnasium.com/sensu-plugins/sensu-plugins-sensu.svg)](https://gemnasium.com/sensu-plugins/sensu-plugins-sensu)
7+
[![Community Slack](https://slack.sensu.io/badge.svg)](https://slack.sensu.io/badge)
88

99
## Functionality
1010

Rakefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/gem_tasks'
24
require 'github/markup'
35
require 'redcarpet'
@@ -8,9 +10,9 @@ require 'yard/rake/yardoc_task'
810
require 'English'
911

1012
YARD::Rake::YardocTask.new do |t|
11-
OTHER_PATHS = %w().freeze
13+
OTHER_PATHS = %w[].freeze
1214
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
13-
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md)
15+
t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md]
1416
end
1517

1618
RuboCop::RakeTask.new
@@ -36,4 +38,4 @@ task :check_binstubs do
3638
end
3739
end
3840

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

bin/check-aggregate.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
# Check Aggregate
46
# ===
@@ -171,7 +173,7 @@ def honor_stash(aggregate)
171173
aggregate[:results].delete_if do |entry|
172174
begin
173175
api_request("/stashes/silence/#{entry[:client]}/#{config[:check]}")
174-
if entry[:status] == 0
176+
if entry[:status].zero?
175177
aggregate[:ok] = aggregate[:ok] - 1
176178
elsif entry[:status] == 1
177179
aggregate[:warning] = aggregate[:warning] - 1
@@ -192,7 +194,7 @@ def honor_stash(aggregate)
192194
def collect_output(aggregate)
193195
output = ''
194196
aggregate[:results].each do |entry|
195-
output << entry[:output] + "\n" unless entry[:status] == 0
197+
output << entry[:output] + "\n" unless entry[:status].zero?
196198
end
197199
aggregate[:outputs] = [output]
198200
end
@@ -208,7 +210,7 @@ def acquire_aggregate
208210

209211
def named_aggregate_results
210212
results = api_request("/aggregates/#{config[:check]}?max_age=#{config[:age]}")[:results]
211-
warning "No aggregates found in last #{config[:age]} seconds" if %w(ok warning critical unknown).all? { |x| results[x.to_sym] == 0 }
213+
warning "No aggregates found in last #{config[:age]} seconds" if %w[ok warning critical unknown].all? { |x| results[x.to_sym].zero? }
212214
results
213215
end
214216

@@ -263,7 +265,7 @@ def compare_pattern(aggregate)
263265
if config[:debug]
264266
message += "\n" + aggregate.to_s
265267
end
266-
aggregate[:outputs].each do |output, _count|
268+
aggregate[:outputs].each_key do |output|
267269
matched = regex.match(output.to_s)
268270
unless matched.nil?
269271
key = matched[1]

bin/check-stale-results.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
# check-stale-results.rb
46
#
@@ -54,7 +56,7 @@ def initialize
5456

5557
def humanize(secs)
5658
[[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map do |count, name|
57-
if secs > 0
59+
if secs.positive?
5860
secs, n = secs.divmod(count)
5961
"#{n.to_i} #{name}"
6062
end
@@ -69,7 +71,11 @@ def get_uri(path)
6971

7072
def api_request(method, path)
7173
unless settings.key?('api')
72-
raise 'api.json settings not found.'
74+
unknown <<~HEREDOC
75+
sensu does not have an api config stanza set, please configure it in
76+
either /etc/sensu/config.json or in any config that is loaded by sensu
77+
such as /etc/sensu/conf.d/api.json
78+
HEREDOC
7379
end
7480
uri = get_uri(path)
7581
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
@@ -85,7 +91,7 @@ def api_request(method, path)
8591
def results
8692
res = []
8793
req = api_request(:GET, '/results')
88-
res = JSON.parse(req.body) if req && req.code == '200'
94+
res = JSON.parse(req.body) if req&.code == '200'
8995
res
9096
end
9197

bin/handler-purge-stale-results.rb

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
14
#
25
# handler-purge-stale-results.rb
36
#
@@ -36,7 +39,7 @@ class HandlerPurgeStaleResults < Sensu::Handler
3639
def results
3740
res = []
3841
req = api_request(:GET, '/results')
39-
res = JSON.parse(req.body) if req && req.code == '200'
42+
res = JSON.parse(req.body) if req&.code == '200'
4043
res
4144
end
4245

@@ -54,33 +57,33 @@ def handle
5457
else
5558
deleted << "#{result['client']} - #{result['check']['name']}"
5659
end
57-
rescue
60+
rescue StandardError
5861
failed << "#{result['client']} - #{result['check']['name']} (Caught exception: #{$ERROR_INFO})"
5962
end
6063
end
6164
end
6265

6366
if !deleted.empty? || !failed.empty?
64-
msg = <<EOF
65-
From: Sensu <#{config[:mail_sender]}>
66-
To: <#{config[:mail_recipient]}>
67-
Subject: Purge stale check results
67+
msg = <<~MESSAGE
68+
From: Sensu <#{config[:mail_sender]}>
69+
To: <#{config[:mail_recipient]}>
70+
Subject: Purge stale check results
6871
69-
This is a notification concerning the #{self.class.name} sensu handler running at #{Socket.gethostname}
72+
This is a notification concerning the #{self.class.name} sensu handler running at #{Socket.gethostname}
7073
71-
* Summary
74+
* Summary
7275
73-
Deleted: #{deleted.size}
74-
Failed to delete: #{failed.size}
76+
Deleted: #{deleted.size}
77+
Failed to delete: #{failed.size}
7578
76-
* Failed to delete check results:
79+
* Failed to delete check results:
7780
78-
#{failed.map { |m| " #{m}" }.join("\n")}
81+
#{failed.map { |m| " #{m}" }.join("\n")}
7982
80-
* Deleted check results:
83+
* Deleted check results:
8184
82-
#{deleted.map { |m| " #{m}" }.join("\n")}
83-
EOF
85+
#{deleted.map { |m| " #{m}" }.join("\n")}
86+
MESSAGE
8487

8588
Net::SMTP.start(config[:mail_server]) do |smtp|
8689
smtp.send_message(msg, config[:mail_sender], config[:mail_recipient])

bin/handler-sensu-deregister.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
23

34
require 'rubygems'
45
require 'sensu-handler'
@@ -21,13 +22,13 @@ def handle
2122
end
2223

2324
def delete_sensu_client!
24-
if config[:invalidate] && config[:invalidate_expire]
25-
response = api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}&#{config[:invalidate_expire]}").code
26-
elsif config[:invalidate]
27-
response = api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}").code
28-
else
29-
response = api_request(:DELETE, '/clients/' + @event['client']['name']).code
30-
end
25+
response = if config[:invalidate] && config[:invalidate_expire]
26+
api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}&#{config[:invalidate_expire]}").code
27+
elsif config[:invalidate]
28+
api_request(:DELETE, '/clients/' + @event['client']['name'] + "?invalidate=#{config[:invalidate]}").code
29+
else
30+
api_request(:DELETE, '/clients/' + @event['client']['name']).code
31+
end
3132
deletion_status(response)
3233
end
3334

bin/handler-sensu.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
##################
46
# Sensu Remediator

bin/metrics-aggregate.rb

+8-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
# Aggregate Metrics
46
#
@@ -115,27 +117,12 @@ def run
115117
acquire_checks.each do |check|
116118
aggregate = get_aggregate(check['name'])
117119
puts "#{check['name']} aggregates: #{aggregate}" if config[:debug]
118-
aggregate.each do |result, count|
119-
# in 0.24 they changed the api results for aggregates this helps
120-
# maintain backwards compatibility with 0.23 and newer versions.
121-
if count.is_a?(Hash)
122-
count.each do |x, y|
123-
output metric_name: x,
124-
value: y,
125-
graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{x}",
126-
statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{x}",
127-
influxdb_measurement: config[:measurement],
128-
tags: {
129-
check: check['name'],
130-
host: Socket.gethostname
131-
},
132-
timestamp: timestamp
133-
end
134-
else
135-
output metric_name: result,
136-
value: count,
137-
graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{result}",
138-
statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{result}",
120+
aggregate.each_value do |count|
121+
count.each do |x, y|
122+
output metric_name: x,
123+
value: y,
124+
graphite_metric_path: "#{config[:scheme]}.#{check['name']}.#{x}",
125+
statsd_metric_name: "#{config[:scheme]}.#{check['name']}.#{x}",
139126
influxdb_measurement: config[:measurement],
140127
tags: {
141128
check: check['name'],

bin/metrics-delete-expired-stashes.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
# Delete stashes when their 'expires' timestamp is exceeded
46
# ===
@@ -14,12 +16,11 @@
1416
require 'json'
1517
require 'socket'
1618

17-
include Sensu::Plugin::Utils
18-
1919
class CheckSilenced < Sensu::Plugin::Metric::CLI::Generic
20+
include Sensu::Plugin::Utils
2021
default_host = begin
2122
settings['api']['host']
22-
rescue
23+
rescue StandardError
2324
'localhost'
2425
end
2526

@@ -81,7 +82,7 @@ def acquire_stashes
8182
all_stashes.each do |stash|
8283
filtered_stashes << stash if stash['path'] =~ /^#{@config[:filter]}\/.*/
8384
end
84-
return filtered_stashes
85+
filtered_stashes
8586
rescue Errno::ECONNREFUSED
8687
warning 'Connection refused'
8788
rescue RestClient::RequestTimeout
@@ -99,9 +100,9 @@ def run
99100
stashes = acquire_stashes
100101
now = Time.now.to_i
101102
@count = 0
102-
if stashes.count > 0
103+
if stashes.count.positive?
103104
stashes.each do |stash|
104-
if stash['content'].key?('expires') && now - stash['content']['expires'] > 0
105+
if stash['content'].key?('expires') && now - stash['content']['expires'] .positive?
105106
delete_stash(stash) unless config[:noop]
106107
@count += 1
107108
end

bin/metrics-events.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: false
3+
24
#
35
# Event Metrics
46
#

lib/sensu-plugins-sensu.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# frozen_string_literal: true
2+
13
require 'sensu-plugins-sensu/version'

lib/sensu-plugins-sensu/version.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module SensuPluginsSensu
24
# This defines the version of the gem
35
module Version

0 commit comments

Comments
 (0)