Skip to content

Commit

Permalink
Merge pull request #721 from jeffmccune/658_support_when_attribute_in…
Browse files Browse the repository at this point in the history
…_filters

(#658) Manage the when attribute of sensu filters
  • Loading branch information
ghoneycutt authored Jul 8, 2017
2 parents 15808b8 + 6721c55 commit e88cc97
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 28 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ sensu::filters:
occurrences: "eval: value == 1 || value % 30 == 0"
sensu::filter_defaults:
negate: true
when:
days:
all:
- begin: 5:00 PM
end: 8:00 AM
sensu::check_defaults:
handlers: 'mail'
sensu::mutators:
Expand Down
8 changes: 8 additions & 0 deletions lib/puppet/provider/sensu_filter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ def attributes=(value)
conf['filters'][resource[:name]]['attributes'].merge!(to_type(value))
end

def when
conf['filters'][resource[:name]]['when']
end

def when=(value)
conf['filters'][resource[:name]]['when'] ||= {}
conf['filters'][resource[:name]]['when'].merge!(to_type(value))
end
end
27 changes: 27 additions & 0 deletions lib/puppet/type/sensu_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ def insync?(is)
defaultto {}
end

newproperty(:when) do
desc 'Used to determine when a filter is applied.'
include PuppetX::Sensu::ToType

def is_to_s(hash = @is)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

def should_to_s(hash = @should)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

def insync?(is)
if defined? @should[0]
if is == @should[0].each { |k, v| value[k] = to_type(v) }
true
else
false
end
else
true
end
end

defaultto {}
end

newproperty(:negate, :parent => PuppetX::Sensu::BooleanProperty) do
desc ""

Expand Down
6 changes: 6 additions & 0 deletions manifests/filter.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
$ensure = 'present',
$negate = undef,
$attributes = undef,
$when = undef,
) {

validate_re($ensure, ['^present$', '^absent$'] )
Expand All @@ -33,6 +34,10 @@
fail('attributes must be a hash')
}

if $when and !is_hash($when) {
fail('when must be a hash')
}

file { "/etc/sensu/conf.d/filters/${name}.json":
ensure => $ensure,
owner => 'sensu',
Expand All @@ -44,6 +49,7 @@
ensure => $ensure,
negate => $negate,
attributes => $attributes,
when => $when,
require => File['/etc/sensu/conf.d/filters'],
}

Expand Down
28 changes: 14 additions & 14 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
# Boolean. Use SSL transport to connect to RabbitMQ. If rabbitmq_ssl_private_key and/or
# rabbitmq_ssl_cert_chain are set, then this is enabled automatically. Set rabbitmq_ssl => true
# without specifying a private key or cert chain to use SSL transport, but not cert auth.
# Defaul: false
# Default: false
# Valid values: true, false
#
# [*rabbitmq_ssl_private_key*]
Expand Down Expand Up @@ -184,7 +184,7 @@
# Default: undef
#
# [*redis_reconnect_on_error*]
# Boolean. In the event the connection or channel is closed by Reddis, attempt to automatically
# Boolean. In the event the connection or channel is closed by redis, attempt to automatically
# reconnect when possible.
# Default: true
# Valid values: true, false
Expand Down Expand Up @@ -245,7 +245,7 @@
# Default: undef
#
# [*subscriptions*]
# Array of strings. Default suscriptions used by the client
# Array of strings. Default subscriptions used by the client
# Default: []
#
# [*client_address*]
Expand Down Expand Up @@ -341,7 +341,7 @@
# Default: true
#
# [*path*]
# String. used to set PATH in /etc/default/sensu
# String. Used to set PATH in /etc/default/sensu
#
# [*redact*]
# Array of strings. Use to redact passwords from checks on the client side
Expand All @@ -356,37 +356,37 @@
# Default: undef
#
# [*handlers*]
# Hash of handlers for use with create_sources(sensu::handler).
# Hash of handlers for use with create_resources(sensu::handler).
# Example value: { 'email' => { 'type' => 'pipe', 'command' => 'mail' } }
# Default: {}
#
# [*handler_defaults*]
# Handler defaults when not provided explicitely in $handlers.
# Handler defaults when not provided explicitly in $handlers.
# Example value: { 'filters' => ['production'] }
# Default: {}
#
# [*checks*]
# Hash of checks for use with create_sources(sensu::check).
# Hash of checks for use with create_resources(sensu::check).
# Example value: { 'check-cpu' => { 'command' => 'check-cpu.rb' } }
# Default: {}
#
# [*check_defaults*]
# Check defaults when not provided explicitely in $checks.
# Example value: { 'occurences' => 3 }
# Check defaults when not provided explicitly in $checks.
# Example value: { 'occurrences' => 3 }
# Default: {}
#
# [*filters*]
# Hash of filters for use with create_sources(sensu::filter).
# Hash of filters for use with create_resources(sensu::filter).
# Example value: { 'occurrence' => { 'attributes' => { 'occurrences' => '1' } } }
# Default: {}
#
# [*filter_defaults*]
# Filter defaults when not provided explicitely in $filters.
# Filter defaults when not provided explicitly in $filters.
# Example value: { 'negate' => true }
# Default: {}
#
# [*package_checksum*]
# String. used to set package_checksum for windows installs
# String. Used to set package_checksum for windows installs
# Default: undef
#
# [*windows_pkg_url*]
Expand Down Expand Up @@ -541,11 +541,11 @@
if $purge_plugins_dir { fail('purge_plugins_dir is deprecated, set the purge parameter to a hash containing `plugins => true` instead') }
if !is_integer($redis_db) { fail('redis_db must be an integer') }

# sensu-enterprise supercedes sensu-server and sensu-api
# sensu-enterprise supersedes sensu-server and sensu-api
if ( $enterprise and $api ) or ( $enterprise and $server ) {
fail('Sensu Enterprise: sensu-enterprise replaces sensu-server and sensu-api')
}
# validate enterprise repo creds
# validate enterprise repo credentials
if $manage_repo {
if ( $enterprise or $enterprise_dashboard ) and $install_repo {
validate_string($enterprise_user, $enterprise_pass)
Expand Down
11 changes: 11 additions & 0 deletions spec/defines/sensu_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
it { should contain_sensu_filter('myfilter').with(:attributes => { 'a' => 'b', 'c' => 'd' } ) }
end

describe 'when' do
let(:when_spec) do
{ 'days' => { 'all' => [ { 'begin' => '5:00 PM', 'end' => '8:00 AM' } ] } }
end
let(:params) do
{ :when => when_spec }
end
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') }
it { should contain_sensu_filter('myfilter').with(:when => when_spec) }
end

context 'absent' do
let(:params) { {
:ensure => 'absent'
Expand Down
66 changes: 52 additions & 14 deletions tests/sensu-client.pp
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
# Use the internal 192.168.56.* address
if $facts['networking']['interfaces']['eth1'] != undef {
$ip = $facts['networking']['interfaces']['eth1']['ip']
} elsif $facts['networking']['interfaces']['enp0s8'] != undef {
$ip = $facts['networking']['interfaces']['enp0s8']['ip']
} else {
$ip = $facts['networking']['ip']
}
node default {

$filters = {
'offhours' => {
'attributes' => {
'client' => {
'environment' => 'production',
},
},
'when' => {
'days' => {
'all' => [
{
'begin' => '2:00 AM',
'end' => '1:00 AM',
},
],
},
},
},
}

$filter_defaults = {
'when' => {
'days' => {
'all' => [
{
'begin' => '2:00 AM',
'end' => '1:00 AM',
},
],
},
},
}

# Use the internal 192.168.56.* address
if $facts['networking']['interfaces']['eth1'] != undef {
$ip = $facts['networking']['interfaces']['eth1']['ip']
} elsif $facts['networking']['interfaces']['enp0s8'] != undef {
$ip = $facts['networking']['interfaces']['enp0s8']['ip']
} else {
$ip = $facts['networking']['ip']
}

class { '::sensu':
rabbitmq_password => 'correct-horse-battery-staple',
rabbitmq_host => '192.168.56.10',
rabbitmq_vhost => '/sensu',
subscriptions => 'all',
client_address => $ip,
class { '::sensu':
rabbitmq_password => 'correct-horse-battery-staple',
rabbitmq_host => '192.168.56.10',
rabbitmq_vhost => '/sensu',
subscriptions => 'all',
client_address => $ip,
filters => $filters,
filter_defaults => $filter_defaults,
}
}

0 comments on commit e88cc97

Please sign in to comment.