Skip to content

Commit

Permalink
type check for cutom check variables
Browse files Browse the repository at this point in the history
  * deletet non default params
  • Loading branch information
Daniel Rossbach committed May 18, 2013
1 parent 2c262ef commit f28e9bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 133 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ A usage example is shown below.
sensu::check { "diskspace":
command => '/etc/sensu/plugins/system/check-disk.rb',
}


}

Expand All @@ -158,19 +158,20 @@ A usage example is shown below.
command => '/etc/sensu/plugins/system/check-disk.rb',
}
}


## Using custom variables in check definition

sensu::check{ 'check_file_test':
command => '/usr/local/bin/check_file_test.sh',
handlers => 'notifu',
sla => ['admin:2'],
custom => {
'foo' => 'bar',
'in_array' => ['foo','zwei']
'numval' => 6,
'boolval' => true,
'in_array' => ['foo','baz']
},
subscribers => 'sensu-test'
subscribers => 'sensu-test'
}

This will create the following check definition for Sensu
Expand All @@ -183,17 +184,16 @@ This will create the following check definition for Sensu
],
"in_array": [
"foo",
"zwei"
"baz"
],
"command": "/usr/local/bin/check_file_test.sh",
"subscribers": [
"sensu-test"
],
"foo": "bar",
"interval": 60,
"sla": [
"admin:2"
]
"numval": 6,
"boolval": true
}
}
}
Expand Down
95 changes: 22 additions & 73 deletions lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,25 @@ def create
self.interval = resource[:interval]
self.subscribers = resource[:subscribers]
# Optional arguments
self.sla = resource[:sla] unless resource[:sla].nil?
self.type = resource[:type] unless resource[:type].nil?
self.config = resource[:config] unless resource[:config].nil?
self.aggregate = resource[:aggregate] unless resource[:aggregate].nil?
self.standalone = resource[:standalone] unless resource[:standalone].nil?
self.high_flap_threshold = resource[:high_flap_threshold] unless resource[:high_flap_threshold].nil?
self.low_flap_threshold = resource[:low_flap_threshold] unless resource[:low_flap_threshold].nil?
self.occurrences = resource[:occurrences] unless resource[:occurrences].nil?
self.refresh = resource[:refresh] unless resource[:refresh].nil?
self.notification = resource[:notification] unless resource[:notification].nil?
self.custom = resource[:custom] unless resource[:custom].nil?
end

def check_args
['handlers','command','interval','subscribers','sla','type','config','aggregate','standalone','high_flap_threshold','low_flap_threshold','occurrences','refresh','notification']
['handlers','command','interval','subscribers','type','standalone','high_flap_threshold','low_flap_threshold']
end

def custom
tmp = {}
conf['checks'][resource[:name]].each do |k,v|
tmp.merge!( k => v )
if v.is_a?( Fixnum )
tmp.merge!( k => v.to_s )
else
tmp.merge!( k => v )
end
end
check_args.each do | del_arg |
tmp.delete(del_arg)
Expand All @@ -66,7 +64,20 @@ def custom=(value)
conf['checks'][resource[:name]].delete(k) unless check_args.include?(k)
end
value.each do | k, v |
conf['checks'][resource[:name]][ k ] = v
conf['checks'][resource[:name]][ k ] = to_type( v )
end
end

def to_type(value)
case value
when true, 'true', 'True', :true
true
when false, 'false', 'False', :false
false
when /^([0-9])+$/
value.to_i
else
value
end
end

Expand Down Expand Up @@ -94,28 +105,6 @@ def handlers=(value)
conf['checks'][resource[:name]]['handlers'] = value
end

def aggregate
case conf['checks'][resource[:name]]['aggregate']
when true
:true
when false
:false
else
conf['checks'][resource[:name]]['aggregate']
end
end

def aggregate=(value)
case value
when true, 'true', 'True', :true, 1
conf['checks'][resource[:name]]['aggregate'] = true
when false, 'false', 'False', :false, 0
conf['checks'][resource[:name]]['aggregate'] = false
else
conf['checks'][resource[:name]]['aggregate'] = value
end
end

def command
conf['checks'][resource[:name]]['command']
end
Expand All @@ -132,14 +121,6 @@ def subscribers=(value)
conf['checks'][resource[:name]]['subscribers'] = value
end

def sla
conf['checks'][resource[:name]]['sla'] || []
end

def sla=(value)
conf['checks'][resource[:name]]['sla'] = value
end

def type
conf['checks'][resource[:name]]['type']
end
Expand All @@ -148,48 +129,16 @@ def type=(value)
conf['checks'][resource[:name]]['type'] = value
end

def config
conf[resource[:name]]
end

def config=(value)
conf[resource[:name]] = value
end

def notification
conf['checks'][resource[:name]]['notification']
end

def notification=(value)
conf['checks'][resource[:name]]['notification'] = value
end

def refresh
conf['checks'][resource[:name]]['refresh'].to_s
end

def refresh=(value)
conf['checks'][resource[:name]]['refresh'] = value.to_i
end

def occurrences
conf['checks'][resource[:name]]['occurrences']
end

def occurrences=(value)
conf['checks'][resource[:name]]['occurrences'] = value.to_i
end

def low_flap_threshold
conf['checks'][resource[:name]]['low_flap_threshold']
conf['checks'][resource[:name]]['low_flap_threshold'].to_s
end

def low_flap_threshold=(value)
conf['checks'][resource[:name]]['low_flap_threshold'] = value.to_i
end

def high_flap_threshold
conf['checks'][resource[:name]]['high_flap_threshold']
conf['checks'][resource[:name]]['high_flap_threshold'].to_s
end

def high_flap_threshold=(value)
Expand Down
25 changes: 0 additions & 25 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def initialize(*args)
desc "The name of the check."
end

newproperty(:aggregate, :boolean => true) do
desc "Enables check aggregation"
newvalues(:true, :false)
end

newproperty(:command) do
desc "Command to be run by the check"
end
Expand All @@ -51,26 +46,10 @@ def initialize(*args)
desc "A host is determined to be flapping when the percent change is below this threshold."
end

newproperty(:notification) do
desc "Check description used by many handlers in their notification"
end

newproperty(:occurrences) do
desc "Number of occurrences before a notification is sent"
end

newproperty(:refresh) do
desc "Refresh / Interval is how frequently a handler is fired"
end

newproperty(:subscribers, :array_matching => :all) do
desc "Who is subscribed to this check"
end

newproperty(:sla, :array_matching => :all) do
desc "custom variable for notifu"
end

newproperty(:custom) do
desc "custom variable"
end
Expand All @@ -79,10 +58,6 @@ def initialize(*args)
desc "What type of check is this"
end

newproperty(:config) do
desc "Check configuration for the client to use"
end

newproperty(:standalone, :boolean => true) do
desc "Whether this is a standalone check"

Expand Down
16 changes: 2 additions & 14 deletions manifests/check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@
$standalone = undef,
$interval = '60',
$subscribers = [],
$sla = [],
$custom = undef,
$notification = undef,
$low_flap_threshold = undef,
$high_flap_threshold = undef,
$refresh = undef,
$aggregate = undef,
$occurrences = undef,
$config = undef,
$custom = undef,
$purge_config = 'false',
) {

Expand All @@ -37,15 +31,9 @@
handlers => $handlers,
interval => $interval,
subscribers => $subscribers,
sla => $sla,
custom => $custom,
notification => $notification,
low_flap_threshold => $low_flap_threshold,
high_flap_threshold => $high_flap_threshold,
refresh => $refresh,
aggregate => $aggregate,
occurrences => $occurrences,
config => $config,
custom => $custom,
require => File['/etc/sensu/conf.d/checks'],
}

Expand Down
14 changes: 2 additions & 12 deletions spec/defines/sensu_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,23 @@
:handlers => ['/handler1', '/handler2'],
:interval => '10',
:subscribers => ['all'],
:sla => ['admin:2'],
:custom => { 'a' => 'b', 'array' => [ 'c', 'd']},
:type => 'metric',
:standalone => true,
:notification => 'some text',
:low_flap_threshold => 10,
:high_flap_threshold => 15,
:refresh => 1800,
:aggregate => true,
:occurrences => 5,
:high_flap_threshold => 15
} }

it { should contain_sensu_check('mycheck').with(
'command' => '/etc/sensu/command2.rb',
'handlers' => ['/handler1', '/handler2'],
'interval' => '10',
'subscribers' => ['all'],
'sla' => ['admin:2'],
'custom' => { 'a' => 'b', 'array' => [ 'c', 'd']},
'type' => 'metric',
'standalone' => true,
'notification' => 'some text',
'low_flap_threshold' => '10',
'high_flap_threshold' => '15',
'refresh' => '1800',
'aggregate' => true,
'occurrences' => '5'
'high_flap_threshold' => '15'
) }
end

Expand Down

0 comments on commit f28e9bc

Please sign in to comment.