Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save kwilczynski/4cd758b232c02df414a8 to your computer and use it in GitHub Desktop.

Select an option

Save kwilczynski/4cd758b232c02df414a8 to your computer and use it in GitHub Desktop.
execute 'remove-hostgroup-files' do
action :run
command 'rm -f /etc/nagios/conf.d/hostgroup_*'
only_if 'ls /etc/nagios/conf.d/hostgroup_* >/dev/null 2>&1 || false'
end
def search_nodes(query = nil)
nodes = search(:node, query).reject {|n| n.respond_to?(:empty?) && n.empty? }
nodes.sort! {|a,b| a['name'] <=> b['name'] }
end
def search_nodes_with_environment(query = nil)
search_nodes(query).inject({}) do |h,v|
environment = n.chef_environment
(h[environment] ||= []) << v unless environment[/\A_?default\z/]
h
end
end
search_nodes_with_environment.each do |env,hosts|
template "/etc/nagios/conf.d/hostgroup_#{env}.cfg" do
owner 'root'
group 'root'
mode '0644'
source 'hostgroup.cfg.erb'
variables(
hosts: hosts,
environment: env,
hostname: node[:hostname],
fqdn: node[:fqdn],
)
notifies :restart, 'service[nagios]', :delayed
end
end
template '/etc/nagios/conf.d/hosts.cfg' do
owner 'root'
group 'root'
mode '0644'
source 'hosts.cfg.erb'
variables(
hosts: search_nodes,
hostname: node[:hostname],
fqdn: node[:fqdn],
)
notifies :restart, 'service[nagios]', :delayed
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment