Skip to content

Commit

Permalink
(#658) Manage the when attribute of sensu filters
Browse files Browse the repository at this point in the history
Without this patch sensu filters are unable to manage the [when
attribute](https://sensuapp.org/docs/0.26/reference/filters.html#when-attributes)

This patch implements the `when` property on the sensu_filter type and json
provider.  The `sensu::filter` defined type is also updated to support the
`when` parameter.

The behavior may be exercised with `vagrant up el7-client`.  This will produce
`/etc/sensu/conf.d/filters/offhours.json` with the content of:

    {
      "filters": {
        "offhours": {
          "attributes": {
            "client": {
              "environment": "production"
            }
          },
          "when": {
            "days": {
              "all": [
                {
                  "begin": "2:00 AM",
                  "end": "1:00 AM"
                }
              ]
            }
          },
          "negate": false
        }
      }
    }
  • Loading branch information
jeffmccune committed Jul 7, 2017
1 parent 37e5fae commit e43ab62
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 14 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
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 e43ab62

Please sign in to comment.