diff --git a/lib/puppet/provider/sensu_handler/json.rb b/lib/puppet/provider/sensu_handler/json.rb index a0aebaae21..88a6e34b63 100644 --- a/lib/puppet/provider/sensu_handler/json.rb +++ b/lib/puppet/provider/sensu_handler/json.rb @@ -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 @@ -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 diff --git a/lib/puppet/type/sensu_handler.rb b/lib/puppet/type/sensu_handler.rb index 5c109004e7..e96bb91b96 100644 --- a/lib/puppet/type/sensu_handler.rb +++ b/lib/puppet/type/sensu_handler.rb @@ -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 diff --git a/manifests/handler.pp b/manifests/handler.pp index 7d797ff730..15192bddc9 100644 --- a/manifests/handler.pp +++ b/manifests/handler.pp @@ -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']) { @@ -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 @@ -73,7 +72,7 @@ } } - sensu_handler_config { $real_key: + sensu_handler_config { $config_key: ensure => $config_present, config => $config, } diff --git a/spec/defines/sensu_handler_spec.rb b/spec/defines/sensu_handler_spec.rb index 794049f804..e0bca77ef9 100644 --- a/spec/defines/sensu_handler_spec.rb +++ b/spec/defines/sensu_handler_spec.rb @@ -7,20 +7,20 @@ 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 @@ -28,6 +28,19 @@ 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( @@ -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'} ) }