Skip to content

Commit f0bba4d

Browse files
committed
add filters
1 parent e90104f commit f0bba4d

File tree

8 files changed

+231
-1
lines changed

8 files changed

+231
-1
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3'
2+
require 'json' if Puppet.features.json?
3+
4+
require 'puppet_x/sensu/to_type'
5+
6+
Puppet::Type.type(:sensu_filter).provide(:json) do
7+
confine :feature => :json
8+
include Puppet_X::Sensu::Totype
9+
10+
def initialize(*args)
11+
super
12+
@conf = nil
13+
end
14+
15+
def conf
16+
begin
17+
@conf ||= JSON.parse(File.read("/etc/sensu/conf.d/filters/#{resource[:name]}.json"))
18+
rescue
19+
@conf ||= {}
20+
end
21+
end
22+
23+
def flush
24+
File.open("/etc/sensu/conf.d/filters/#{resource[:name]}.json", 'w') do |f|
25+
f.puts JSON.pretty_generate(conf)
26+
end
27+
end
28+
29+
def create
30+
conf['filter'] = {}
31+
conf['filter'][resource[:name]] = {}
32+
self.negate = resource[:negate]
33+
end
34+
35+
def destroy
36+
conf = nil
37+
end
38+
39+
def exists?
40+
conf.has_key?('filter') and conf['filter'].has_key?(resource[:name])
41+
end
42+
43+
def negate
44+
case conf['filter'][resource[:name]]['negate']
45+
when true
46+
:true
47+
else
48+
:false
49+
end
50+
end
51+
52+
def negate=(value)
53+
case value
54+
when true, 'true', 'True', :true, 1
55+
conf['filter'][resource[:name]]['negate'] = true
56+
else
57+
conf['filter'][resource[:name]]['negate'] = false
58+
end
59+
end
60+
61+
def attributes
62+
conf['filter'][resource[:name]]['attributes']
63+
end
64+
65+
def attributes=(value)
66+
conf['filter'][resource[:name]]['attributes'].merge!(to_type value)
67+
end
68+
69+
end

lib/puppet/provider/sensu_handler/json.rb

+12
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ def mutator=(value)
9393
@conf['handlers'][resource[:name]]['mutator'] = value
9494
end
9595

96+
def filters
97+
@conf['filters'][resource[:name]]['filters']
98+
end
99+
100+
def filters=(value)
101+
if value.is_a?(Array)
102+
@conf['filters'][resource[:name]]['filters'] = value
103+
else
104+
@conf['filters'][resource[:name]]['filters'] = [ value ]
105+
end
106+
end
107+
96108
def severities
97109
@conf['handlers'][resource[:name]]['severities']
98110
end

lib/puppet/type/sensu_filter.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'puppet_x/sensu/to_type'
2+
Puppet::Type.newtype(:sensu_filter) do
3+
@doc = ""
4+
5+
def initialize(*args)
6+
super
7+
end
8+
9+
ensurable do
10+
newvalue(:present) do
11+
provider.create
12+
end
13+
14+
newvalue(:absent) do
15+
provider.destroy
16+
end
17+
18+
defaultto :present
19+
end
20+
21+
newparam(:name) do
22+
desc "The name of the filter."
23+
end
24+
25+
26+
newparam(:attributes) do
27+
desc ""
28+
29+
include Puppet_X::Sensu::Totype
30+
31+
def is_to_s(hash = @is)
32+
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
33+
end
34+
35+
def should_to_s(hash = @should)
36+
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
37+
end
38+
39+
def insync?(is)
40+
if defined? @should[0]
41+
if is == @should[0].each { |k, v| value[k] = to_type(v) }
42+
true
43+
else
44+
false
45+
end
46+
else
47+
true
48+
end
49+
end
50+
51+
defaultto {}
52+
end
53+
54+
newproperty(:negate) do
55+
desc ""
56+
end
57+
58+
autorequire(:package) do
59+
['sensu']
60+
end
61+
end

lib/puppet/type/sensu_handler.rb

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def insync?(is)
6767
desc "Handler specific data massager"
6868
end
6969

70+
newproperty(:filters, :array_matching => :all) do
71+
desc "Handler filters"
72+
end
73+
7074
newproperty(:severities, :array_matching => :all) do
7175
desc "Severities applicable to this handler"
7276
end

manifests/filter.pp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# = Define: sensu::filter
2+
#
3+
# Defines Sensu filters
4+
#
5+
# == Parameters
6+
#
7+
# [*ensure*]
8+
# String. Whether the check should be present or not
9+
# Default: present
10+
# Valid values: present, absent
11+
#
12+
# [*negate*]
13+
# Boolean. Negate the filter
14+
# Default: undef
15+
# Valid values: true, false
16+
#
17+
# [*attributes*]
18+
# Hash. Hash of attributes for the filter
19+
# Default: undef
20+
#
21+
define sensu::filter (
22+
$ensure = 'present',
23+
$negate = undef,
24+
$attributes = undef,
25+
) {
26+
27+
validate_re($ensure, ['^present$', '^absent$'] )
28+
if $negate {
29+
validate_bool($negate)
30+
}
31+
32+
if $attributes and !is_hash($attributes) {
33+
fail('attributes must be a hash')
34+
}
35+
36+
file { "/etc/sensu/conf.d/filters/${name}.json":
37+
ensure => $ensure,
38+
owner => 'sensu',
39+
group => 'sensu',
40+
mode => '0444',
41+
before => Sensu_client_subscription[$name],
42+
}
43+
44+
sensu_filter { $name:
45+
ensure => $ensure,
46+
negate => $negate,
47+
attributes => $attributes,
48+
}
49+
50+
}

manifests/handler.pp

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
# Keys: host, port
3838
# Default: undef
3939
#
40+
# [*filters*]
41+
# Hash. Filter command to apply
42+
# Default: undef
43+
#
4044
# [*source*]
4145
# String. Source of the puppet handler
4246
# Default: undef
@@ -59,6 +63,7 @@
5963
$exchange = undef,
6064
$mutator = undef,
6165
$socket = undef,
66+
$filters = undef,
6267
# Used to install the handler
6368
$source = undef,
6469
$install_path = '/etc/sensu/handlers',
@@ -133,6 +138,7 @@
133138
exchange => $exchange,
134139
socket => $socket,
135140
mutator => $mutator,
141+
filters => $filters,
136142
config => $config,
137143
notify => $notify_services,
138144
require => File['/etc/sensu/conf.d/handlers'],

manifests/package.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
require => Package['sensu'],
3636
}
3737

38-
file { [ '/etc/sensu/conf.d', '/etc/sensu/conf.d/handlers', '/etc/sensu/conf.d/checks' ]:
38+
file { [ '/etc/sensu/conf.d', '/etc/sensu/conf.d/handlers', '/etc/sensu/conf.d/checks', '/etc/sensu/conf.d/filters' ]:
3939
ensure => directory,
4040
owner => 'sensu',
4141
group => 'sensu',

spec/defines/sensu_filter_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe 'sensu::filter', :type => :define do
4+
let(:title) { 'myfilter' }
5+
6+
context 'negate' do
7+
let(:params) { {:negate => false } }
8+
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') }
9+
it { should contain_sensu_filter('myfilter').with( :negate => false ) }
10+
end
11+
12+
context 'attributes' do
13+
let(:params) { {
14+
:attributes => { 'a' => 'b', 'c' => 'd' }
15+
} }
16+
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') }
17+
it { should contain_sensu_filter('myfilter').with(:attributes => { 'a' => 'b', 'c' => 'd' } ) }
18+
end
19+
20+
context 'absent' do
21+
let(:params) { {
22+
:ensure => 'absent'
23+
} }
24+
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'absent') }
25+
it { should contain_sensu_filter('myfilter').with(:ensure => 'absent') }
26+
end
27+
28+
end

0 commit comments

Comments
 (0)