From f28e9bc6f9ce1fec87ea1ce88fec8acadb745c02 Mon Sep 17 00:00:00 2001 From: Daniel Rossbach Date: Sat, 18 May 2013 14:51:04 +0200 Subject: [PATCH] type check for cutom check variables * deletet non default params --- README.md | 18 ++--- lib/puppet/provider/sensu_check/json.rb | 95 ++++++------------------- lib/puppet/type/sensu_check.rb | 25 ------- manifests/check.pp | 16 +---- spec/defines/sensu_check_spec.rb | 14 +--- 5 files changed, 35 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index d723fd2cb8..fa6f9dcc7e 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ A usage example is shown below. sensu::check { "diskspace": command => '/etc/sensu/plugins/system/check-disk.rb', } - + } @@ -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 @@ -183,7 +184,7 @@ This will create the following check definition for Sensu ], "in_array": [ "foo", - "zwei" + "baz" ], "command": "/usr/local/bin/check_file_test.sh", "subscribers": [ @@ -191,9 +192,8 @@ This will create the following check definition for Sensu ], "foo": "bar", "interval": 60, - "sla": [ - "admin:2" - ] + "numval": 6, + "boolval": true } } } diff --git a/lib/puppet/provider/sensu_check/json.rb b/lib/puppet/provider/sensu_check/json.rb index 858da76e32..0cc55b7ea3 100644 --- a/lib/puppet/provider/sensu_check/json.rb +++ b/lib/puppet/provider/sensu_check/json.rb @@ -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) @@ -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 @@ -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 @@ -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 @@ -148,40 +129,8 @@ 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) @@ -189,7 +138,7 @@ def low_flap_threshold=(value) 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) diff --git a/lib/puppet/type/sensu_check.rb b/lib/puppet/type/sensu_check.rb index d431dda0cb..ff5a0bfc83 100644 --- a/lib/puppet/type/sensu_check.rb +++ b/lib/puppet/type/sensu_check.rb @@ -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 @@ -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 @@ -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" diff --git a/manifests/check.pp b/manifests/check.pp index 737c520627..0096e01d23 100644 --- a/manifests/check.pp +++ b/manifests/check.pp @@ -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', ) { @@ -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'], } diff --git a/spec/defines/sensu_check_spec.rb b/spec/defines/sensu_check_spec.rb index d0c3b7080f..312a272654 100644 --- a/spec/defines/sensu_check_spec.rb +++ b/spec/defines/sensu_check_spec.rb @@ -21,16 +21,11 @@ :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( @@ -38,16 +33,11 @@ '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