From 297350de6e3edf745f04239b98411f8588bfd099 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Mon, 4 Mar 2013 19:02:16 -0700 Subject: [PATCH] install handler scripts and config files --- .../provider/sensu_handler_config/json.rb | 43 +++++++++++ lib/puppet/type/sensu_handler_config.rb | 35 +++++++++ manifests/handler.pp | 71 ++++++++++++++++--- manifests/package.pp | 7 ++ manifests/server.pp | 6 -- spec/classes/sensu_package_spec.rb | 2 + spec/classes/sensu_server_spec.rb | 13 ---- spec/defines/sensu_handler_spec.rb | 37 +++++++--- 8 files changed, 179 insertions(+), 35 deletions(-) create mode 100644 lib/puppet/provider/sensu_handler_config/json.rb create mode 100644 lib/puppet/type/sensu_handler_config.rb diff --git a/lib/puppet/provider/sensu_handler_config/json.rb b/lib/puppet/provider/sensu_handler_config/json.rb new file mode 100644 index 0000000000..93a73e071b --- /dev/null +++ b/lib/puppet/provider/sensu_handler_config/json.rb @@ -0,0 +1,43 @@ +require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.features.rubygems? +require 'json' if Puppet.features.json? + +Puppet::Type.type(:sensu_handler_config).provide(:json) do + confine :feature => :json + + def initialize(*args) + super + + begin + @conf = JSON.parse(File.read("/etc/sensu/conf.d/#{resource[:name]}.json")) + rescue + @conf = {} + end + end + + def flush + File.open("/etc/sensu/conf.d/#{resource[:name]}.json", 'w') do |f| + f.puts JSON.pretty_generate(@conf) + end + end + + def create + @conf[resource[:name]] = {} + self.config = resource[:config] + end + + def destroy + @conf = nil + end + + def exists? + @conf.has_key?(resource[:name]) + end + + def config + @conf[resource[:name]] + end + + def config=(value) + @conf[resource[:name]] = value + end +end diff --git a/lib/puppet/type/sensu_handler_config.rb b/lib/puppet/type/sensu_handler_config.rb new file mode 100644 index 0000000000..01c6079bd9 --- /dev/null +++ b/lib/puppet/type/sensu_handler_config.rb @@ -0,0 +1,35 @@ +Puppet::Type.newtype(:sensu_handler_config) 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 key name of the handler" + end + + newproperty(:config) do + desc "Configuration for the handler" + end + + autorequire(:package) do + ['sensu'] + end + + autorequire(:file) do + ['/etc/sensu/handlers'] + end +end diff --git a/manifests/handler.pp b/manifests/handler.pp index 7701afa131..e2e6a2ef31 100644 --- a/manifests/handler.pp +++ b/manifests/handler.pp @@ -6,9 +6,14 @@ # define sensu::handler( - $type, - $command, - $ensure = 'present' + $source = '', + $type = 'pipe', + $handlers = [], + $install_path = '/etc/sensu/handlers', + $config = '', + $config_key = '', + $ensure = 'present', + $severities = ['ok', 'warning', 'critical', 'unknown'] ) { if defined(Class['sensu::service::server']) { @@ -17,10 +22,60 @@ $notify_services = [] } - sensu_handler_config { $name: - ensure => $ensure, - type => $type, - command => $command, - notify => $notify_services + $filename = inline_template("<%= scope.lookupvar('source').split('/').last %>") + + $real_key = $config_key ? { + '' => inline_template("<%= File.basename(scope.lookupvar('filename')).split('.').first %>"), + default => $config_key + } + + if $handlers != [] { + sensu_handler { $name: + ensure => $ensure, + type => $type, + handlers => $handlers, + severities => $severities, + notify => $notify_services, + } + } else { + $file_ensure = $ensure ? { + 'absent' => 'abasent', + default => 'file' + } + + file { "${install_path}/${filename}": + ensure => $file_file, + owner => 'sensu', + group => 'sensu', + mode => '0555', + source => $source, + } + + sensu_handler { $real_key: + ensure => $ensure, + type => $type, + command => "${install_path}/${filename}", + severities => $severities, + notify => $notify_services, + } } + + # Handler config + case $ensure { + 'present': { + $config_present = $config ? { + '' => 'absent', + default => 'present' + } + } + default: { + $config_present = 'absent' + } + } + + sensu_handler_config { $real_key: + ensure => $config_present, + config => $config, + } + } diff --git a/manifests/package.pp b/manifests/package.pp index 3d4d4e5363..1cf3e6eb9b 100644 --- a/manifests/package.pp +++ b/manifests/package.pp @@ -19,6 +19,13 @@ ensure => $version, notify => $notify_services } + + file { ['/etc/sensu/plugins', '/etc/sensu/handlers']: + ensure => directory, + mode => '0555', + owner => 'sensu', + group => 'sensu', + } file { '/etc/sensu/config.json': ensure => absent } } diff --git a/manifests/server.pp b/manifests/server.pp index 860a600196..da3a885d4e 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -42,10 +42,4 @@ user => $dashboard_user, password => $dashboard_password, } - - sensu::handler { 'default': - ensure => $ensure, - type => 'pipe', - command => '/etc/sensu/handlers/default', - } } diff --git a/spec/classes/sensu_package_spec.rb b/spec/classes/sensu_package_spec.rb index 584a70e350..f8c300c0cc 100644 --- a/spec/classes/sensu_package_spec.rb +++ b/spec/classes/sensu_package_spec.rb @@ -7,6 +7,8 @@ it { should create_class('sensu::package') } it { should include_class('sensu::repo') } it { should contain_package('sensu').with_ensure('latest') } + it { should contain_file('/etc/sensu/handlers').with_ensure('directory') } + it { should contain_file('/etc/sensu/plugins').with_ensure('directory') } it { should contain_file('/etc/sensu/config.json').with_ensure('absent') } end diff --git a/spec/classes/sensu_server_spec.rb b/spec/classes/sensu_server_spec.rb index 9d26441574..f2af55f812 100644 --- a/spec/classes/sensu_server_spec.rb +++ b/spec/classes/sensu_server_spec.rb @@ -8,7 +8,6 @@ it { should contain_sensu_redis_config('testhost.domain.com').with_ensure('absent') } it { should contain_sensu_api_config('testhost.domain.com').with_ensure('absent') } it { should contain_sensu_dashboard_config('testhost.domain.com').with_ensure('absent') } - it { should contain_sensu__handler('default').with_ensure('absent') } end context 'defaults (enabled)' do @@ -35,12 +34,6 @@ 'ensure' => 'present' ) } - it { should contain_sensu__handler('default').with( - 'type' => 'pipe', - 'command' => '/etc/sensu/handlers/default', - 'ensure' => 'present' - ) } - end # Defaults context 'setting params (enabled)' do @@ -76,12 +69,6 @@ 'password' => 'mypass', 'ensure' => 'present' ) } - - it { should contain_sensu__handler('default').with( - 'type' => 'pipe', - 'command' => '/etc/sensu/handlers/default', - 'ensure' => 'present' - ) } end # setting params end diff --git a/spec/defines/sensu_handler_spec.rb b/spec/defines/sensu_handler_spec.rb index 1dd3b72ec4..794049f804 100644 --- a/spec/defines/sensu_handler_spec.rb +++ b/spec/defines/sensu_handler_spec.rb @@ -5,20 +5,41 @@ context 'default (present)' do - let(:params) { { :type => 'pipe', :command => '/etc/sensu/mycommand.rb' } } - it { should contain_sensu_handler_config('myhandler').with( - 'type' => 'pipe', - 'command' => '/etc/sensu/mycommand.rb', - 'ensure' => 'present' + 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( + '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') } end context 'absent' do let(:facts) { { 'Class[sensu::service::server]' => true } } - let(:params) { { :type => 'pipe', :command => '/etc/sensu/mycommand.rb', :ensure => 'absent' } } - it { should contain_sensu_handler_config('myhandler').with_ensure('absent').with_notify([]) } + 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') } + end + + context 'install path' do + let(:params) { { :install_path => '/etc', :source => 'puppet:///mycommand.rb'} } + it { should contain_file('/etc/mycommand.rb') } + end + context 'handlers' do + let(:params) { { :handlers => ['mailer', 'hipchat'] } } + it { should contain_sensu_handler('myhandler').with( + 'ensure' => 'present', + 'type' => 'pipe', + 'handlers' => ['mailer', 'hipchat'], + 'severities' => ['ok', 'warning', 'critical', 'unknown'] + ) } end + context 'config' do + let(:params) { { :config => { 'foo' => 'bar' }, :config_key => 'configkey' } } + it { should contain_sensu_handler_config('configkey').with_config({'foo' => 'bar'} ) } + end end