Skip to content

Instantly share code, notes, and snippets.

@octplane
Created August 11, 2011 13:13
Show Gist options
  • Select an option

  • Save octplane/1139612 to your computer and use it in GitHub Desktop.

Select an option

Save octplane/1139612 to your computer and use it in GitHub Desktop.
Nagios server cookbook. Part that dumps the configuration on the disk.
ruby_block "Delete Legacy Files" do
block do
existing = []
Dir.new('/etc/nagios3/conf.d/').each do |f|
next if f == '.' or f == '..'
existing << f
end
reals = search(:node, "monitored:[* TO *]").map{ |n| n[:fqdn] +".cfg" }
(existing - reals).each do |leftover|
next if leftover !~ /#{node[:domain]}\.cfg$/
File.unlink('/etc/nagios3/conf.d/'+leftover)
end
end
end
# get all the monitored servers
monitored = {}
search(:node, "monitored:[* TO *]").map do |nod|
monitored[nod[:hostname]] = nod.to_hash
end
monitored.each do |hostname, n|
begin
services = n['monitored']['current']
rescue Exception=>e
Chef::Log.error("Failed to get current monitored states")
Chef::Log.error("State is "+n.inspect)
raise e
end
services.each do |service, s|
depends = s['depends'] || []
new_depends = []
# For each dependency find the nodes which hosts this service and build the dependency string accordingly
depends.each do |parent|
# Dependencies on "@localhost" are replace by real hostname
if parent.index("@localhost") != nil
new_depends << parent[0..parent.index("@")] + hostname
end
monitored.select { |h,reg|
begin
reg['monitored']['current'].include?(parent)
rescue Exception => e
Chef::Log.error("Unable to look at monitored current in #{h}"+e.to_s)
false
end
}.each { |match|
new_depends << parent + "@" + match[0]
}
Chef::Log.warn("#{parent} not found in services (#{service} depends on it). This is not normal.") if new_depends.length == 0
end
new_depends.uniq!
s['depends'] = new_depends
Chef::Log.debug("Service #{service} on #{hostname} depends on "+new_depends.inspect)
end
template "/etc/nagios3/conf.d/#{n['fqdn']}.cfg" do
owner "nagios"
group "nagios"
mode 0644
backup false
source "services.cfg.erb"
variables (:host => n, :services => services)
notifies :reload, resources('service[nagios3]')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment