Skip to content

Commit

Permalink
install handler scripts and config files
Browse files Browse the repository at this point in the history
  • Loading branch information
jlambert121 committed Mar 5, 2013
1 parent 5fa1eba commit 297350d
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 35 deletions.
43 changes: 43 additions & 0 deletions lib/puppet/provider/sensu_handler_config/json.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions lib/puppet/type/sensu_handler_config.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 63 additions & 8 deletions manifests/handler.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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,
}

}
7 changes: 7 additions & 0 deletions manifests/package.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
6 changes: 0 additions & 6 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,4 @@
user => $dashboard_user,
password => $dashboard_password,
}

sensu::handler { 'default':
ensure => $ensure,
type => 'pipe',
command => '/etc/sensu/handlers/default',
}
}
2 changes: 2 additions & 0 deletions spec/classes/sensu_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 0 additions & 13 deletions spec/classes/sensu_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
37 changes: 29 additions & 8 deletions spec/defines/sensu_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 297350d

Please sign in to comment.