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

rewrite chef checks to use Ridley instead of deprecated chef rest api. #26

Merged
merged 7 commits into from
May 9, 2017
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ install:
- gem install bundler -v 1.12
- bundle install
rvm:
- 2.1
- 2.2
- 2.3.0
- 2.4.1
notifications:
email:
recipients:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
### Breaking Change
- re-wrote all checks to use latest ridley. As ridley 5.x requires buff-extension 2.x which requires >= ruby 2.2 and sensu has never shipped with ruby 2.1 the decision was made to drop this support. If you are not using an embedded ruby and are using 2.1 this will break for you. To avoid this you can either switch to embedded ruby or upgrade your ruby to at least 2.2

## [2.0.1] - 2017-04-28
### Fixed
Expand Down
17 changes: 13 additions & 4 deletions bin/check-chef-node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#

require 'sensu-plugin/check/cli'
require 'chef/rest'
require 'ridley'

class ChefNodeChecker < Sensu::Plugin::Check::CLI
option :node_name,
Expand All @@ -50,13 +50,18 @@ class ChefNodeChecker < Sensu::Plugin::Check::CLI
short: '-K CLIENT-KEY',
long: '--keys CLIENT-KEY'

option :ignore_ssl_verification,
description: 'Ignore SSL certificate verification',
short: '-i',
long: '--ignore-ssl'

def connection
@connection ||= chef_api_connection
end

def run
node = connection.get_rest("/nodes/#{config[:node_name]}")
if node['ohai_time']
node = connection.node.find(config[:node_name])
if node['automatic']['ohai_time']
ok "Node #{config[:node_name]} found"
else
warning "Node #{config[:node_name]} does not contain 'ohai_time' attribute"
Expand All @@ -71,6 +76,10 @@ def chef_api_connection
chef_server_url = config[:chef_server_url]
client_name = config[:client_name]
signing_key_filename = config[:key]
Chef::REST.new(chef_server_url, client_name, signing_key_filename)
ignore_ssl = config[:ignore_ssl_verification]
verify_ssl = ignore_ssl.nil?

Celluloid.boot
Ridley.new(server_url: chef_server_url, client_name: client_name, client_key: signing_key_filename, ssl: { verify: verify_ssl })
end
end
27 changes: 18 additions & 9 deletions bin/check-chef-nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#

require 'sensu-plugin/check/cli'
require 'chef/rest'
require 'ridley'

#
# Chef Nodes Status Checker
Expand Down Expand Up @@ -68,19 +68,24 @@ class ChefNodesStatusChecker < Sensu::Plugin::Check::CLI
long: '--exclude-nodes EXCLUDE-NODES',
default: '^$'

option :ignore_ssl_verification,
description: 'Ignore SSL certificate verification',
short: '-i',
long: '--ignore-ssl'

def connection
@connection ||= chef_api_connection
end

def nodes_last_seen
nodes = connection.get_rest('/nodes')
nodes.delete_if { |node_name| node_name =~ /#{config[:exclude_nodes]}/ }
nodes.keys.map do |node_name|
node = connection.get_rest("/nodes/#{node_name}")
if node['ohai_time']
{ node_name => (Time.now - Time.at(node['ohai_time'])) > config[:critical_timespan].to_i }
nodes = connection.node.all
nodes.delete_if { |node| node.name =~ /#{config[:exclude_nodes]}/ }
nodes.map do |node|
node.reload
if node['automatic']['ohai_time']
{ node.name => (Time.now - Time.at(node['automatic']['ohai_time'])) > config[:critical_timespan].to_i }
else
{ node_name => true }
{ node.name => true }
end
end
end
Expand All @@ -99,7 +104,11 @@ def chef_api_connection
chef_server_url = config[:chef_server_url]
client_name = config[:client_name]
signing_key_filename = config[:key]
Chef::REST.new(chef_server_url, client_name, signing_key_filename)
ignore_ssl = config[:ignore_ssl_verification]
verify_ssl = ignore_ssl.nil?

Celluloid.boot
Ridley.new(server_url: chef_server_url, client_name: client_name, client_key: signing_key_filename, ssl: { verify: verify_ssl })
end

def any_node_stuck?
Expand Down
4 changes: 2 additions & 2 deletions sensu-plugins-chef.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'rack', '~> 1.6.5'
end

s.add_runtime_dependency 'ridley', '4.1.2'
s.add_runtime_dependency 'ridley', '5.1.0'
s.add_runtime_dependency 'sensu-plugin', '~> 1.2'
s.add_runtime_dependency 'varia_model', '0.4.1'
s.add_runtime_dependency 'varia_model', '0.6'

s.add_development_dependency 'bundler', '~> 1.12'

Expand Down