Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transorm input with munge in type rather than in sensu_check/json.rb provider #573

Merged
merged 2 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 21 additions & 31 deletions lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
include PuppetX::Sensu::ToType
include PuppetX::Sensu::ProviderCreate

SENSU_CHECK_PROPERTIES = Puppet::Type.type(:sensu_check).validproperties.reject { |p| p == :ensure }

def conf
begin
@conf ||= JSON.parse(File.read(config_file))
Expand All @@ -29,16 +31,16 @@ def pre_create
conf['checks'][resource[:name]] = {}
end

def check_args
['handlers','command','interval','occurrences','refresh','source','subscribers','type','standalone','high_flap_threshold','low_flap_threshold','timeout','aggregate','handle','publish','dependencies','custom','ttl', 'subdue', 'aggregates']
def is_property?(prop)
SENSU_CHECK_PROPERTIES.map(&:to_s).include? prop
end

def custom
conf['checks'][resource[:name]].reject { |k,v| check_args.include?(k) }
conf['checks'][resource[:name]].reject { |k,v| is_property?(k) }
end

def custom=(value)
conf['checks'][resource[:name]].delete_if { |k,v| not check_args.include?(k) }
conf['checks'][resource[:name]].delete_if { |k,v| not is_property?(k) }
conf['checks'][resource[:name]].merge!(to_type(value))
end

Expand All @@ -51,15 +53,15 @@ def exists?
end

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

def config_file
"#{resource[:base_path]}/#{resource[:name]}.json"
end

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

def handlers
Expand All @@ -71,19 +73,19 @@ def handlers=(value)
end

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

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

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

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

def command
Expand Down Expand Up @@ -120,19 +122,19 @@ def type=(value)
end

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

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

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

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

def source
Expand All @@ -144,31 +146,19 @@ def source=(value)
end

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

def trim num
i, f = num.to_i, num.to_f
i == f ? i : f
conf['checks'][resource[:name]]['timeout']
end

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

def aggregate
conf['checks'][resource[:name]]['aggregate']
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
conf['checks'][resource[:name]]['aggregate'] = value
end

def aggregates
Expand Down Expand Up @@ -204,11 +194,11 @@ def standalone=(value)
end

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

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

def subdue
Expand Down
32 changes: 32 additions & 0 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,30 @@ def insync?(is)

newproperty(:high_flap_threshold) do
desc "A host is determined to be flapping when the percent change exceedes this threshold."
munge do |value|
value.to_i
end
end

newproperty(:interval) do
desc "How frequently the check runs in seconds"
munge do |value|
value.to_i
end
end

newproperty(:occurrences) do
desc "The number of event occurrences before the handler should take action."
munge do |value|
value.to_i
end
end

newproperty(:refresh) do
desc "The number of seconds sensu-plugin-aware handlers should wait before taking second action."
munge do |value|
value.to_i
end
end

newparam(:base_path) do
Expand All @@ -73,6 +85,9 @@ def insync?(is)

newproperty(:low_flap_threshold) do
desc "A host is determined to be flapping when the percent change is below this threshold."
munge do |value|
value.to_i
end
end

newproperty(:source) do
Expand Down Expand Up @@ -124,10 +139,24 @@ def insync?(is)

newproperty(:timeout) do
desc "Check timeout in seconds, after it fails"
munge do |value|
i, f = value.to_i, value.to_f
i == f ? i : f
end
end

newproperty(:aggregate) do
desc "Whether check is aggregate"
munge do |value|
case value
when true, 'true', 'True', :true, 1
true
when false, 'false', 'False', :false, 0
false
else
value
end
end
end

newproperty(:aggregates, :array_matching => :all) do
Expand All @@ -152,6 +181,9 @@ def insync?(is)

newproperty(:ttl) do
desc "Check ttl in seconds"
munge do |value|
value.to_i
end
end

autorequire(:package) do
Expand Down