Last active
December 21, 2016 15:05
-
-
Save richardc/4714701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node demo { | |
nagios::host::service { 'ping': | |
check_command => 'check_ping', | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define nagios::host::service ( | |
$display_name, | |
$check_command, | |
$service_description, | |
$servicegroups = [], | |
$contact_groups = undef, | |
$use = $nagios::params::default_service_template, | |
$ensure = present, | |
$host = $::fqdn, | |
$tag = undef | |
) { | |
#If no tag is specified, do a Hiera lookup to see if we override what resource tag | |
#to use, otherwise fall back on using PoP | |
if ($tag == undef) { | |
$use_tag = hiera('nagios_host_tag', $::pop) | |
} else { | |
$use_tag = $tag | |
} | |
if is_string($servicegroups) { | |
$joined_servicegroups = $servicegroups | |
} else { | |
$joined_servicegroups = join($servicegroups, ',') | |
} | |
# export to the server | |
@@nagios::config::service { "${host}__${name}": | |
ensure => $ensure, | |
host_name => $host, | |
check_command => $check_command, | |
service_description => $service_description, | |
contact_groups => $contact_groups, | |
display_name => $display_name, | |
servicegroups => $joined_servicegroups, | |
use => $use, | |
tag => $use_tag, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define nagios::config::collect_tag() { | |
if ($nagios::use_puppetdb) { | |
# Use 'pdbresourcequery' to quury the exported Nagios::Config::Service | |
# resources directly from puppetdb and reassemble them in | |
# a template. This adds some complexity to the compile, but greatly | |
# reduces the number of resources in the resulting catalog. | |
$services = pdbresourcequery( | |
[ 'and', | |
[ '=', 'tag', $name ], | |
[ '=', 'type', 'Nagios::Config::Service' ], | |
[ '=', 'exported', true ] | |
] | |
) | |
file { "${nagios::params::services_dir}/exported_${name}.cfg": | |
content => template('nagios/nagios_services.cfg.erb'), | |
notify => Exec['nagios_generate_servicegroups'], | |
} | |
} | |
else { | |
# Do it the tradional way - Collect all the Nagios::Config::Service resources. | |
# This results in a catalog with a lot of file resources, which the agent | |
# handles poorly. | |
Nagios::Config::Service <<| tag == $name |>> { notify => Class[nagios::service] } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=nagios::config::service | |
# | |
#Define a Nagios service object. This defined type wraps around the nagios_service | |
#type, providing some safety checking that stops the Nagios Server from breaking. | |
# | |
#Other modules should not use this type directly, instead look at the nagios::host::service | |
#defined type. | |
define nagios::config::service( | |
$display_name, | |
$check_command, | |
$service_description, | |
$hostgroup_name = undef, | |
$host_name = undef, | |
$contact_groups = undef, | |
$servicegroups = undef, | |
$use = "generic-service", | |
$ensure = present) { | |
include nagios::params | |
#Sanity check our parameters | |
if $name == undef { | |
fail("A nagios::config::service resource (description = '${service_description}') has been created with an undefined name!") | |
} | |
if $check_command == undef { | |
fail("The nagios::config::service resource '${name}' cannot have an undefined check_command parameter!") | |
} | |
if $service_description == undef { | |
fail("The nagios::config::service resource '${name}' cannot have an undefined service_description parameter!") | |
} | |
if $display_name == undef { | |
fail("The nagios::config::service resource '${name}' cannot have an undefined display_name parameter!") | |
} | |
#Wrap the nagios_service in a file so it does not get purged. | |
$file_name = "${nagios::params::services_dir}/${name}.cfg" | |
file { $file_name: | |
ensure => $ensure, | |
owner => $nagios::params::config_file_owner, | |
group => $nagios::params::config_file_group, | |
mode => $nagios::params::config_file_mode, | |
notify => Exec['nagios_generate_servicegroups'], | |
} | |
nagios_service { $name: | |
ensure => $ensure, | |
hostgroup_name => $hostgroup_name, | |
host_name => $host_name, | |
check_command => $check_command, | |
service_description => $service_description, | |
display_name => $display_name, | |
contact_groups => $contact_groups, | |
servicegroups => $servicegroups ? { | |
'' => 'ungrouped', | |
default => $servicegroups, | |
}, | |
use => $use, | |
target => $file_name, | |
require => File[$file_name], | |
notify => Exec['nagios_generate_servicegroups'], | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file managed by puppet | |
<% @services.sort_by { |s| s["title"] }.each do |service| | |
# XXX resource parameter defaults don't export | |
service["parameters"]["use"] ||= "generic-service" | |
%> | |
# Nagios::Config::Service['<%= service["title"] %>'] | |
define service { | |
<% service["parameters"].keys.sort.each do |k| | |
next if ['tag', 'ensure'].include?(k) # puppet parameter | |
v = service["parameters"][k] -%> | |
<%= k %> <%= v %> | |
<%- end %> | |
} | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment