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

Update checks #40

Merged
merged 1 commit into from
Mar 7, 2013
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
140 changes: 140 additions & 0 deletions lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.features.rubygems?
require 'json' if Puppet.features.json?

Puppet::Type.type(:sensu_check).provide(:json) do
confine :feature => :json

def initialize(*args)
super

@conf = nil
end

def conf
resource[:realname] = resource[:name] if resource[:realname] == nil
begin
@conf ||= JSON.parse(File.read("/etc/sensu/conf.d/check_#{resource[:realname]}.json"))
rescue
@conf ||= {}
end
end

def flush
File.open("/etc/sensu/conf.d/check_#{resource[:realname]}.json", 'w') do |f|
f.puts JSON.pretty_generate(conf)
end
end

def create
conf['checks'] = {}
conf['checks'][resource[:realname]] = {}
self.handlers = resource[:handlers]
self.command = resource[:command]
self.interval = resource[:interval]
self.subscribers = resource[:subscribers]
end

def destroy
conf = nil
end

def exists?
conf.has_key?('checks') and conf['checks'].has_key?(resource[:realname])
end

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

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

def handlers
conf['checks'][resource[:realname]]['handlers'] || []
end

def handlers=(value)
conf['checks'][resource[:realname]]['handlers'] = value
end

def aggregate
conf['checks'][resource[:realname]]['aggregate']
end

def aggregate=(value)
conf['checks'][resource[:realname]]['aggregate'] = value
end

def command
conf['checks'][resource[:realname]]['command']
end

def command=(value)
conf['checks'][resource[:realname]]['command'] = value
end

def subscribers
conf['checks'][resource[:realname]]['subscribers'] || []
end

def subscribers=(value)
conf['checks'][resource[:realname]]['subscribers'] = value
end

def type
conf['checks'][resource[:realname]]['type']
end

def type=(value)
conf['checks'][resource[:realname]]['type'] = value
end

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

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

def refresh
conf['checks'][resource[:realname]]['refresh']
end

def refresh=(value)
conf['checks'][resource[:realname]]['refresh'] = value
end

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

def occurrences=(value)
conf['checks'][resource[:realname]]['occurrences'] = value
end

def low_flap_threshold
conf['checks'][resource[:realname]]['low_flap_threshold']
end

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

def high_flap_threshold
conf['checks'][resource[:realname]]['high_flap_threshold']
end

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

def standalone
conf['checks'][resource[:realname]]['standalone']
end

def standalone=(value)
conf['checks'][resource[:realname]]['standalone'] = value
end
end
120 changes: 12 additions & 108 deletions lib/puppet/provider/sensu_check_config/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,133 +7,37 @@
def initialize(*args)
super

@conf = nil
end

def conf
begin
@conf ||= JSON.parse(File.read("/etc/sensu/conf.d/check_#{resource[:realname]}.json"))
@conf = JSON.parse(File.read("/etc/sensu/conf.d/#{resource[:name]}.json"))
rescue
@conf ||= {}
@conf = {}
end
end

def flush
File.open("/etc/sensu/conf.d/check_#{resource[:realname]}.json", 'w') do |f|
f.puts JSON.pretty_generate(conf)
File.open("/etc/sensu/conf.d/#{resource[:name]}.json", 'w') do |f|
f.puts JSON.pretty_generate(@conf)
end
end

def create
conf['checks'] = {}
conf['checks'][resource[:realname]] = {}
self.handlers = resource[:handlers]
self.command = resource[:command]
self.interval = resource[:interval]
self.subscribers = resource[:subscribers]
@conf[resource[:name]] = {}
self.config = resource[:config]
end

def destroy
conf = nil
@conf = nil
end

def exists?
conf.has_key?('checks') and conf['checks'].has_key?(resource[:realname])
end

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

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

def handlers
conf['checks'][resource[:realname]]['handlers'] || []
end

def handlers=(value)
conf['checks'][resource[:realname]]['handlers'] = value
end

def command
conf['checks'][resource[:realname]]['command']
end

def command=(value)
conf['checks'][resource[:realname]]['command'] = value
end

def subscribers
conf['checks'][resource[:realname]]['subscribers'] || []
end

def subscribers=(value)
conf['checks'][resource[:realname]]['subscribers'] = value
end

def type
conf['checks'][resource[:realname]]['type']
end

def type=(value)
conf['checks'][resource[:realname]]['type'] = value
end

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

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

def refresh
conf['checks'][resource[:realname]]['refresh']
end

def refresh=(value)
conf['checks'][resource[:realname]]['refresh'] = value
end

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

def occurrences=(value)
conf['checks'][resource[:realname]]['occurrences'] = value
end

def low_flap_threshold
conf['checks'][resource[:realname]]['low_flap_threshold']
end

def low_flap_threshold=(value)
conf['checks'][resource[:realname]]['low_flap_threshold'] = value
end

def high_flap_threshold
conf['checks'][resource[:realname]]['high_flap_threshold']
end

def high_flap_threshold=(value)
conf['checks'][resource[:realname]]['high_flap_threshold'] = value
end

def standalone
conf['checks'][resource[:realname]]['standalone']
end

def standalone=(value)
conf['checks'][resource[:realname]]['standalone'] = value
@conf.has_key?(resource[:name])
end

def aggregate
conf['checks'][resource[:realname]]['aggregate']
def config
@conf[resource[:name]]
end

def aggregate=(value)
conf['checks'][resource[:realname]]['aggregate'] = value
def config=(value)
@conf[resource[:name]] = value
end
end
87 changes: 87 additions & 0 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Puppet::Type.newtype(:sensu_check) do
@doc = ""

def initialize(*args)
super

self[:notify] = [
"Service[sensu-client]",
"Service[sensu-server]",
].select { |ref| catalog.resource(ref) }
end

ensurable do
newvalue(:present) do
provider.create
end

newvalue(:absent) do
provider.destroy
end

defaultto :present
end

newparam(:name) do
desc "Some unique name, not the name of the check."
end

newparam(:realname) do
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

newproperty(:handlers, :array_matching => :all) do
desc "List of handlers that responds to this check"
end

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

newproperty(:interval) do
desc "How frequently the check runs in seconds"
end

newproperty(:low_flap_threshold) do
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(:type) do
desc "What type of check is this"
end

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

newvalues(:true, :false)
end

autorequire(:package) do
['sensu']
end
end
Loading