Skip to content

Commit

Permalink
add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlambert121 committed Jan 7, 2014
1 parent e90104f commit f0bba4d
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 1 deletion.
69 changes: 69 additions & 0 deletions lib/puppet/provider/sensu_filter/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3'
require 'json' if Puppet.features.json?

require 'puppet_x/sensu/to_type'

Puppet::Type.type(:sensu_filter).provide(:json) do
confine :feature => :json
include Puppet_X::Sensu::Totype

def initialize(*args)
super
@conf = nil
end

def conf
begin
@conf ||= JSON.parse(File.read("/etc/sensu/conf.d/filters/#{resource[:name]}.json"))
rescue
@conf ||= {}
end
end

def flush
File.open("/etc/sensu/conf.d/filters/#{resource[:name]}.json", 'w') do |f|
f.puts JSON.pretty_generate(conf)
end
end

def create
conf['filter'] = {}
conf['filter'][resource[:name]] = {}
self.negate = resource[:negate]
end

def destroy
conf = nil
end

def exists?
conf.has_key?('filter') and conf['filter'].has_key?(resource[:name])
end

def negate
case conf['filter'][resource[:name]]['negate']
when true
:true
else
:false
end
end

def negate=(value)
case value
when true, 'true', 'True', :true, 1
conf['filter'][resource[:name]]['negate'] = true
else
conf['filter'][resource[:name]]['negate'] = false
end
end

def attributes
conf['filter'][resource[:name]]['attributes']
end

def attributes=(value)
conf['filter'][resource[:name]]['attributes'].merge!(to_type value)
end

end
12 changes: 12 additions & 0 deletions lib/puppet/provider/sensu_handler/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ def mutator=(value)
@conf['handlers'][resource[:name]]['mutator'] = value
end

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

def filters=(value)
if value.is_a?(Array)
@conf['filters'][resource[:name]]['filters'] = value
else
@conf['filters'][resource[:name]]['filters'] = [ value ]
end
end

def severities
@conf['handlers'][resource[:name]]['severities']
end
Expand Down
61 changes: 61 additions & 0 deletions lib/puppet/type/sensu_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'puppet_x/sensu/to_type'
Puppet::Type.newtype(:sensu_filter) do
@doc = ""

def initialize(*args)
super
end

ensurable do
newvalue(:present) do
provider.create
end

newvalue(:absent) do
provider.destroy
end

defaultto :present
end

newparam(:name) do
desc "The name of the filter."
end


newparam(:attributes) do
desc ""

include Puppet_X::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) do
desc ""
end

autorequire(:package) do
['sensu']
end
end
4 changes: 4 additions & 0 deletions lib/puppet/type/sensu_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def insync?(is)
desc "Handler specific data massager"
end

newproperty(:filters, :array_matching => :all) do
desc "Handler filters"
end

newproperty(:severities, :array_matching => :all) do
desc "Severities applicable to this handler"
end
Expand Down
50 changes: 50 additions & 0 deletions manifests/filter.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# = Define: sensu::filter
#
# Defines Sensu filters
#
# == Parameters
#
# [*ensure*]
# String. Whether the check should be present or not
# Default: present
# Valid values: present, absent
#
# [*negate*]
# Boolean. Negate the filter
# Default: undef
# Valid values: true, false
#
# [*attributes*]
# Hash. Hash of attributes for the filter
# Default: undef
#
define sensu::filter (
$ensure = 'present',
$negate = undef,
$attributes = undef,
) {

validate_re($ensure, ['^present$', '^absent$'] )
if $negate {
validate_bool($negate)
}

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

file { "/etc/sensu/conf.d/filters/${name}.json":
ensure => $ensure,
owner => 'sensu',
group => 'sensu',
mode => '0444',
before => Sensu_client_subscription[$name],
}

sensu_filter { $name:
ensure => $ensure,
negate => $negate,
attributes => $attributes,
}

}
6 changes: 6 additions & 0 deletions manifests/handler.pp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
# Keys: host, port
# Default: undef
#
# [*filters*]
# Hash. Filter command to apply
# Default: undef
#
# [*source*]
# String. Source of the puppet handler
# Default: undef
Expand All @@ -59,6 +63,7 @@
$exchange = undef,
$mutator = undef,
$socket = undef,
$filters = undef,
# Used to install the handler
$source = undef,
$install_path = '/etc/sensu/handlers',
Expand Down Expand Up @@ -133,6 +138,7 @@
exchange => $exchange,
socket => $socket,
mutator => $mutator,
filters => $filters,
config => $config,
notify => $notify_services,
require => File['/etc/sensu/conf.d/handlers'],
Expand Down
2 changes: 1 addition & 1 deletion manifests/package.pp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require => Package['sensu'],
}

file { [ '/etc/sensu/conf.d', '/etc/sensu/conf.d/handlers', '/etc/sensu/conf.d/checks' ]:
file { [ '/etc/sensu/conf.d', '/etc/sensu/conf.d/handlers', '/etc/sensu/conf.d/checks', '/etc/sensu/conf.d/filters' ]:
ensure => directory,
owner => 'sensu',
group => 'sensu',
Expand Down
28 changes: 28 additions & 0 deletions spec/defines/sensu_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe 'sensu::filter', :type => :define do
let(:title) { 'myfilter' }

context 'negate' do
let(:params) { {:negate => false } }
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') }
it { should contain_sensu_filter('myfilter').with( :negate => false ) }
end

context 'attributes' do
let(:params) { {
:attributes => { 'a' => 'b', 'c' => 'd' }
} }
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') }
it { should contain_sensu_filter('myfilter').with(:attributes => { 'a' => 'b', 'c' => 'd' } ) }
end

context 'absent' do
let(:params) { {
:ensure => 'absent'
} }
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'absent') }
it { should contain_sensu_filter('myfilter').with(:ensure => 'absent') }
end

end

0 comments on commit f0bba4d

Please sign in to comment.