Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exchanges, mutators, handler cleanup #39

Merged
merged 1 commit into from
Mar 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/puppet/provider/sensu_handler/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def initialize(*args)
super

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

def flush
File.open("/etc/sensu/conf.d/handlers_#{resource[:name]}.json", 'w') do |f|
File.open("/etc/sensu/conf.d/handler_#{resource[:name]}.json", 'w') do |f|
f.puts JSON.pretty_generate(@conf)
end
end
Expand Down Expand Up @@ -43,21 +43,34 @@ def command=(value)
@conf['handlers'][resource[:name]]['command'] = value
end

def exchange
@conf['handlers'][resource[:name]]['exchange']
end

def exchange=(value)
@conf['handlers'][resource[:name]]['exchange'] = value
end

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

def handlers=(value)
@conf['handlers'][resource[:name]]['handlers'] = value
munge do |value|
Array(value)
end
end


def mutator
@conf['handlers'][resource[:name]]['mutator']
end

def mutator=(value)
@conf['handlers'][resource[:name]]['mutator'] = value
end

def severities
@conf['handlers'][resource[:name]]['severities']
end

def severities=(value)
@conf['handlers'][resource[:name]]['severities'] = value
end
Expand Down
10 changes: 9 additions & 1 deletion lib/puppet/type/sensu_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ def initialize(*args)
desc "Command the handler should run"
end

newproperty(:exchange) do
desc "Exchange information used by the amqp type"
end

newproperty(:mutator) do
desc "Handler specific data massager"
end

newproperty(:severities, :array_matching => :all) do
desc "Severities applicable to this handler"
end

newproperty(:handlers) do
newproperty(:handlers, :array_matching => :all) do
desc "Handlers this handler mutexes into"
end

Expand Down
55 changes: 27 additions & 28 deletions manifests/handler.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
#

define sensu::handler(
$source = '',
$type = 'pipe',
$handlers = [],
$command = undef,
$handlers = undef,
$ensure = 'present',
$severities = ['ok', 'warning', 'critical', 'unknown'],
$exchange = undef,
$mutator = undef,
# Used to install the handler
$source = '',
$install_path = '/etc/sensu/handlers',
# Handler specific config
$config = '',
$config_key = '',
$ensure = 'present',
$severities = ['ok', 'warning', 'critical', 'unknown']
$config_key = $name,
) {

if defined(Class['sensu::service::server']) {
Expand All @@ -22,42 +27,36 @@
$notify_services = []
}

$filename = inline_template("<%= scope.lookupvar('source').split('/').last %>")
if $source != '' {

$real_key = $config_key ? {
'' => inline_template("<%= File.basename(scope.lookupvar('filename')).split('.').first %>"),
default => $config_key
}
$filename = inline_template("<%= scope.lookupvar('source').split('/').last %>")
$command_real = "${install_path}/${filename}"

if $handlers != [] {
sensu_handler { $name:
ensure => $ensure,
type => $type,
handlers => $handlers,
severities => $severities,
notify => $notify_services,
}
} else {
$file_ensure = $ensure ? {
'absent' => 'absent',
default => 'file'
}

file { "${install_path}/${filename}":
file { $command_real:
ensure => $file_ensure,
owner => 'sensu',
group => 'sensu',
mode => '0555',
source => $source,
}
} else {
$command_real = $command
}

sensu_handler { $real_key:
ensure => $ensure,
type => $type,
command => "${install_path}/${filename}",
severities => $severities,
notify => $notify_services,
}
sensu_handler { $name:
ensure => $ensure,
type => $type,
command => $command_real,
handlers => $handlers,
severities => $severities,
exchange => $exchange,
mutator => $mutator,
notify => $notify_services,
}

# Handler config
Expand All @@ -73,7 +72,7 @@
}
}

sensu_handler_config { $real_key:
sensu_handler_config { $config_key:
ensure => $config_present,
config => $config,
}
Expand Down
33 changes: 29 additions & 4 deletions spec/defines/sensu_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,40 @@

let(:params) { { :type => 'pipe', :source => 'puppet:///somewhere/mycommand.rb' } }
it { should contain_file('/etc/sensu/handlers/mycommand.rb').with_source('puppet:///somewhere/mycommand.rb')}
it { should contain_sensu_handler('mycommand').with(
it { should contain_sensu_handler('myhandler').with(
'ensure' => 'present',
'type' => 'pipe',
'command' => '/etc/sensu/handlers/mycommand.rb',
'severities' => ['ok', 'warning', 'critical', 'unknown']
) }
it { should contain_sensu_handler_config('mycommand').with_ensure('absent') }
it { should contain_sensu_handler_config('myhandler').with_ensure('absent') }
end

context 'absent' do
let(:facts) { { 'Class[sensu::service::server]' => true } }
let(:params) { { :type => 'pipe', :ensure => 'absent', :source => 'puppet:///somewhere/mycommand.rb' } }
it { should contain_sensu_handler('mycommand').with_ensure('absent') }
it { should contain_sensu_handler_config('mycommand').with_ensure('absent') }
it { should contain_sensu_handler('myhandler').with_ensure('absent') }
it { should contain_sensu_handler_config('myhandler').with_ensure('absent') }
end

context 'install path' do
let(:params) { { :install_path => '/etc', :source => 'puppet:///mycommand.rb'} }
it { should contain_file('/etc/mycommand.rb') }
end

context 'command' do
let(:params) { { :command => '/somewhere/file/script.sh' } }

it { should contain_sensu_handler('myhandler').with_command('/somewhere/file/script.sh') }
end

context 'source' do
let(:params) { { :source => 'puppet:///sensu/handler/script.sh' } }

it { should contain_file('/etc/sensu/handlers/script.sh').with_ensure('file')}
it { should contain_sensu_handler('myhandler').with_command('/etc/sensu/handlers/script.sh') }
end

context 'handlers' do
let(:params) { { :handlers => ['mailer', 'hipchat'] } }
it { should contain_sensu_handler('myhandler').with(
Expand All @@ -38,6 +51,18 @@
) }
end

context 'exchange' do
let(:params) { { :exchange => { 'type' => 'topic' } } }

it { should contain_sensu_handler('myhandler').with_exchange({'type' => 'topic'}) }
end

context 'mutator' do
let(:params) { { :mutator => 'only_check_output' } }

it { should contain_sensu_handler('myhandler').with_mutator('only_check_output') }
end

context 'config' do
let(:params) { { :config => { 'foo' => 'bar' }, :config_key => 'configkey' } }
it { should contain_sensu_handler_config('configkey').with_config({'foo' => 'bar'} ) }
Expand Down