Skip to content

Commit

Permalink
(sensu#637) Add check proxy_requests functionality
Browse files Browse the repository at this point in the history
Without this patch, the [Proxy Check
Requests](https://sensuapp.org/docs/latest/reference/checks.html#proxy-requests-attributes)
functionality must be defined as a custom attribute.  This patch adds
`proxy_requests` as a first class attribute of the `sensu::check` defined type.

The behavior can be exercised with `vagrant up sensu-server`, then applying the
proxy request specific configuration with `vagrant ssh puppet-server sudo
puppet apply -v /vagrant/tests/vagrant/tests/sensu-server-proxy-checks.pp`.

Resolves sensu#637
  • Loading branch information
jeffmccune committed Jul 12, 2017
1 parent 9e5b3d9 commit ffba6b1
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,4 @@ def set_property(property, value)
conf['checks'][resource[:name]][property.to_s] = value
end
end

end
5 changes: 5 additions & 0 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ def insync?(is)
newvalues(/.*/, :absent)
end

newproperty(:proxy_requests) do
desc "Proxy Requests"
newvalues(/.*/, :absent)
end

newproperty(:ttl) do
desc "Check ttl in seconds"
newvalues(/.*/, :absent)
Expand Down
18 changes: 18 additions & 0 deletions manifests/check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
# Set this to 'absent' to remove it completely.
# Default: undef
#
# [*proxy_requests*]
# Hash. [Proxy Check
# Requests](https://sensuapp.org/docs/latest/reference/checks.html#proxy-requests-attributes)
# Since Sensu 0.28.0. Publishes a check request to every Sensu client which
# matches the defined client attributes. See the documentation for the format
# of the Hash value.
# Default: undef
define sensu::check(
$command,
$ensure = 'present',
Expand All @@ -133,6 +140,7 @@
$custom = undef,
$ttl = undef,
$subdue = undef,
$proxy_requests = undef,
) {

validate_re($ensure, ['^present$', '^absent$'] )
Expand Down Expand Up @@ -187,6 +195,15 @@
if $aggregates and !is_array($aggregates) and !is_string($aggregates) {
fail("sensu::check{${name}}: aggregates must be an array or a string (got: ${aggregates})")
}
if $proxy_requests {
if is_hash($proxy_requests) {
if !( has_key($proxy_requests, 'client_attributes') ) {
fail("sensu::check{${name}}: proxy_requests hash should have a proper format. (got: ${proxy_requests}) See https://sensuapp.org/docs/latest/reference/checks.html#proxy-requests-attributes")
}
} elsif !($proxy_requests == 'absent') {
fail("sensu::check{${name}}: proxy_requests must be a hash or 'absent' (got: ${proxy_requests})")
}
}

$check_name = regsubst(regsubst($name, ' ', '_', 'G'), '[\(\)]', '', 'G')

Expand Down Expand Up @@ -238,6 +255,7 @@
dependencies => $dependencies,
custom => $custom,
subdue => $subdue,
proxy_requests => $proxy_requests,
require => File["${conf_dir}/checks"],
notify => $::sensu::check_notify,
ttl => $ttl,
Expand Down
41 changes: 31 additions & 10 deletions spec/defines/sensu_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
end
let(:facts) { { :osfamily => 'RedHat' } }

let(:params_base) {{ command: '/etc/sensu/somecommand.rb' }}
let(:params_override) {{}}
let(:params) { params_base.merge(params_override) }

context 'without whitespace in name' do
let(:title) { 'mycheck' }

context 'defaults' do
let(:params) { { :command => '/etc/sensu/somecommand.rb' } }

it { should contain_sensu_check('mycheck').with(
:command => '/etc/sensu/somecommand.rb',
:interval => 60
) }

end

context 'setting params' do
Expand Down Expand Up @@ -114,29 +115,23 @@
:type => :absent
) }
end

end

context 'with whitespace in name' do
let(:title) { 'mycheck foobar' }
context 'defaults' do
let(:params) { { :command => '/etc/sensu/somecommand.rb' } }

it { should contain_sensu_check('mycheck_foobar').with(
:command => '/etc/sensu/somecommand.rb',
:interval => '60'
) }

it { should contain_file('/etc/sensu/conf.d/checks/mycheck_foobar.json') }

end
end

context 'with brackets in name' do
let(:title) { 'mycheck (foo) bar' }
context 'defaults' do
let(:params) { { :command => '/etc/sensu/somecommand.rb' } }

it { should contain_sensu_check('mycheck_foo_bar').with(
'command' => '/etc/sensu/somecommand.rb',
'interval' => '60'
Expand All @@ -148,7 +143,6 @@

context 'notifications' do
let(:title) { 'mycheck' }
let(:params) { { :command => '/etc/sensu/somecommand.rb' } }

context 'no client, sever, or api' do
let(:pre_condition) { 'class {"sensu": client => false, api => false, server => false}' }
Expand Down Expand Up @@ -253,4 +247,31 @@
it { should contain_sensu_check('mycheck').without_subdue }
end
end

describe 'param proxy_requests' do
let(:title) { 'mycheck' }

context 'valid proxy_requests hash' do
let(:params_override) do
{ proxy_requests: { 'client_attributes' => { 'subscriptions' => 'eval: value.include?("http")' } } }
end

it { should contain_sensu_check('mycheck').with_proxy_requests(params_override[:proxy_requests]) }
end

context 'invalid proxy_requests hash' do
let(:params_override) { {proxy_requests: {}} }
it { should raise_error(Puppet::Error, /proxy_requests hash should have a proper format/) }
end

context '=> \'absent\'' do
let(:params_override) { { proxy_requests: 'absent' } }

it { should contain_sensu_check('mycheck').with_proxy_requests(:absent) }
end

context '= undef' do
it { should contain_sensu_check('mycheck').without_proxy_requests }
end
end
end
94 changes: 94 additions & 0 deletions tests/sensu-server-proxy-checks.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This test manifest is intended to layer on top of the sensu-server Vagrant VM.
# It defines additional checks executed using proxy clients. To add a remote
# proxy client, use the Uchiwa UI at http://localhost:3000/#/clients and add a
# client like so. Once added, request a check for `remote_http` and it will
# automatically run against the Google proxy client.
#
# {
# "address": "www.google.com",
# "keepalives": false,
# "name": "google",
# "subscriptions": [
# "client:google",
# "http"
# ],
# "type": "proxy"
# }
node 'sensu-server' {
Package {
ensure => installed,
}
Sensu::Plugin {
type => 'package',
pkg_provider => 'sensu_gem',
pkg_version => 'installed',
require => [Package['gcc-c++']],
}

class { '::sensu':
install_repo => true,
server => true,
manage_services => true,
manage_user => true,
rabbitmq_password => 'correct-horse-battery-staple',
rabbitmq_vhost => '/sensu',
api => true,
api_user => 'admin',
api_password => 'secret',
client_address => $::ipaddress_eth1,
subscriptions => ['all', 'poller', 'proxytarget', 'roundrobin:poller'],
}

sensu::handler { 'default':
command => 'mail -s \'sensu alert\' [email protected]',
}

sensu::check { 'check_ntp':
command => 'PATH=$PATH:/usr/lib64/nagios/plugins check_ntp_time -H pool.ntp.org -w 30 -c 60',
handlers => 'default',
subscribers => 'sensu-test',
}

# sensu-plugins-http requires a c++ compiler during install
package { 'gcc-c++': }

# (#637) Create a [Proxy
# Check](https://sensuapp.org/docs/latest/reference/checks.html#proxy-requests-attributes)
# This needs to have a corresponding sensu-client matching the client
# attributes.
sensu::plugin { 'sensu-plugins-http': }

# A client defined in the Dashboard with a subscription of "http" will
# automatically have this check associated with it.
sensu::check { 'remote_http':
command => '/opt/sensu/embedded/bin/check-http.rb -u http://:::address:::',
occurrences => 2,
interval => 300,
refresh => 600,
low_flap_threshold => 20,
high_flap_threshold => 60,
standalone => false,
subscribers => 'roundrobin:poller',
proxy_requests => {
'client_attributes' => {
'subscriptions' => 'eval: value.include?("http")',
},
},
}
# Similar to above, but not using round robin checks.
sensu::check { 'remote_http-dashboard':
command => '/opt/sensu/embedded/bin/check-http.rb -u http://:::address::::3000',
occurrences => 2,
interval => 300,
refresh => 600,
low_flap_threshold => 20,
high_flap_threshold => 60,
standalone => false,
subscribers => 'poller',
proxy_requests => {
'client_attributes' => {
'subscriptions' => 'eval: value.include?("proxytarget")',
},
},
}
}

0 comments on commit ffba6b1

Please sign in to comment.