Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple updates #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ Style/GuardClause:
Enabled: false

Style/Next:
Enabled: false
Enabled: false

AllCops:
TargetRubyVersion: 2.3
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ cache:
install:
- bundle install
rvm:
- 2.0
- 2.1
- 2.2
- 2.3.0
- 2.4.1
notifications:
Expand All @@ -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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in sensu-plugins-sensu.gemspec
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'github/markup'
require 'redcarpet'
Expand All @@ -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
Expand All @@ -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]
10 changes: 6 additions & 4 deletions bin/check-aggregate.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# Check Aggregate
# ===
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand Down
12 changes: 9 additions & 3 deletions bin/check-stale-results.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# check-stale-results.rb
#
Expand Down Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -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

Expand Down
33 changes: 18 additions & 15 deletions bin/handler-purge-stale-results.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# handler-purge-stale-results.rb
#
Expand Down Expand Up @@ -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

Expand All @@ -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 = <<EOF
From: Sensu <#{config[:mail_sender]}>
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])
Expand Down
15 changes: 8 additions & 7 deletions bin/handler-sensu-deregister.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

require 'rubygems'
require 'sensu-handler'
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions bin/handler-sensu.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
##################
# Sensu Remediator
Expand Down
29 changes: 8 additions & 21 deletions bin/metrics-aggregate.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# Aggregate Metrics
#
Expand Down Expand Up @@ -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'],
Expand Down
13 changes: 7 additions & 6 deletions bin/metrics-delete-expired-stashes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# Delete stashes when their 'expires' timestamp is exceeded
# ===
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bin/metrics-events.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: false

#
# Event Metrics
#
Expand Down
2 changes: 2 additions & 0 deletions lib/sensu-plugins-sensu.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'sensu-plugins-sensu/version'
2 changes: 2 additions & 0 deletions lib/sensu-plugins-sensu/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module SensuPluginsSensu
# This defines the version of the gem
module Version
Expand Down
Loading