diff --git a/manifests/node.pp b/manifests/node.pp index e7382b5..1728ce5 100644 --- a/manifests/node.pp +++ b/manifests/node.pp @@ -1,185 +1,180 @@ # munin::node - Configure a munin node, and export configuration a # munin master can collect. # # Parameters: # # allow: List of IPv4 and IPv6 addresses and networks to allow to connect. # # config_root: Root directory for munin configuration. # # nodeconfig: List of lines to append to the munin node configuration. # # host_name: The host name munin node identifies as. Defaults to # the $::fqdn fact. # # log_dir: The log directory for the munin node process. Defaults # change according to osfamily, see munin::params::node for details. # # log_file: Appended to "log_dir". Defaults to "munin-node.log". # # log_destination: "file" or "syslog". Defaults to "file". If log_destination # is "syslog", the "log_file" and "log_dir" parameters are ignored, and the # "syslog_*" parameters are used if set. # # purge_configs: Removes all other munin plugins and munin plugin # configuration files. Boolean, defaults to false. # -# syslog_ident: Defaults to undef, which makes munin-node use its -# default of "munin-node". -# # syslog_facility: Defaults to undef, which makes munin-node use the # perl Net::Server module default of "daemon". Possible values are any # syslog facility by number, or lowercase name. # # masterconfig: List of configuration lines to append to the munin # master node definitinon # # mastername: The name of the munin master server which will collect # the node definition. # # mastergroup: The group used on the master to construct a FQN for # this node. Defaults to "", which in turn makes munin master use the # domain. Note: changing this for a node also means you need to move # rrd files on the master, or graph history will be lost. # # plugins: A hash used by create_resources to create munin::plugin # instances. # # address: The address used in the munin master node definition. # # package_name: The name of the munin node package to install. # # service_name: The name of the munin node service. # # service_ensure: Defaults to "". If set to "running" or "stopped", it # is used as parameter "ensure" for the munin node service. # # export_node: "enabled" or "disabled". Defaults to "enabled". # Causes the node config to be exported to puppetmaster. # # file_group: The UNIX group name owning the configuration files, # log files, etc. # # timeout: Used to set the global plugin runtime timeout for this # node. Integer. Defaults to undef, which lets munin-node use its # default of 10 seconds. class munin::node ( $address = $munin::params::node::address, $allow = $munin::params::node::allow, $config_root = $munin::params::node::config_root, $host_name = $munin::params::node::host_name, $log_dir = $munin::params::node::log_dir, $log_file = $munin::params::node::log_file, $masterconfig = $munin::params::node::masterconfig, $mastergroup = $munin::params::node::mastergroup, $mastername = $munin::params::node::mastername, $nodeconfig = $munin::params::node::nodeconfig, $package_name = $munin::params::node::package_name, $plugins = $munin::params::node::plugins, $purge_configs = $munin::params::node::purge_configs, $service_ensure = $munin::params::node::service_ensure, $service_name = $munin::params::node::service_name, $export_node = $munin::params::node::export_node, $file_group = $munin::params::node::file_group, $log_destination = $munin::params::node::log_destination, - $syslog_ident = $munin::params::node::syslog_ident, $syslog_facility = $munin::params::node::syslog_facility, $timeout = $munin::params::node::timeout, ) inherits munin::params::node { validate_array($allow) validate_array($nodeconfig) validate_array($masterconfig) if $mastergroup { validate_string($mastergroup) } if $mastername { validate_string($mastername) } validate_hash($plugins) validate_string($address) validate_absolute_path($config_root) validate_string($package_name) validate_string($service_name) if $service_ensure { validate_re($service_ensure, '^(running|stopped)$') } validate_re($export_node, '^(enabled|disabled)$') validate_absolute_path($log_dir) validate_re($log_destination, '^(?:file|syslog)$') validate_string($log_file) validate_string($file_group) validate_bool($purge_configs) if $timeout { validate_integer($timeout) } case $log_destination { 'file': { $_log_file = "${log_dir}/${log_file}" validate_absolute_path($_log_file) } 'syslog': { $_log_file = 'Sys::Syslog' - if $syslog_ident { validate_string($syslog_ident) } if $syslog_facility { validate_string($syslog_facility) validate_re($syslog_facility, '^(?:\d+|(?:kern|user|mail|daemon|auth|syslog|lpr|news|uucp|authpriv|ftp|cron|local[0-7]))$') } } default: { fail('log_destination is not set') } } if $mastergroup { $fqn = "${mastergroup};${host_name}" } else { $fqn = $host_name } if $service_ensure { $_service_ensure = $service_ensure } else { $_service_ensure = undef } # Defaults File { ensure => present, owner => 'root', group => $file_group, mode => '0444', } package { $package_name: ensure => installed, } service { $service_name: ensure => $_service_ensure, enable => true, require => Package[$package_name], } file { "${config_root}/munin-node.conf": content => template('munin/munin-node.conf.erb'), require => Package[$package_name], notify => Service[$service_name], } # Export a node definition to be collected by the munin master if $export_node == 'enabled' { @@munin::master::node_definition{ $fqn: address => $address, mastername => $mastername, config => $masterconfig, tag => [ "munin::master::${mastername}" ] } } # Generate plugin resources from hiera or class parameter. create_resources(munin::plugin, $plugins, {}) # Purge unmanaged plugins and plugin configuration files. if $purge_configs { file { ["${config_root}/plugins", "${config_root}/plugin-conf.d" ]: ensure => directory, recurse => true, purge => true, } } } diff --git a/manifests/params/node.pp b/manifests/params/node.pp index 60c8dcc..f2ae4ca 100644 --- a/manifests/params/node.pp +++ b/manifests/params/node.pp @@ -1,76 +1,75 @@ class munin::params::node { $message = "Unsupported osfamily: ${::osfamily}" $address = $::fqdn $host_name = $::fqdn $allow = [] $masterconfig = [] $mastergroup = undef $mastername = undef $nodeconfig = [] $plugins = {} $service_ensure = undef $export_node = 'enabled' $log_file = 'munin-node.log' $log_destination = 'file' - $syslog_ident = undef $syslog_facility = undef $purge_configs = false $timeout = undef case $::osfamily { 'RedHat': { $config_root = '/etc/munin' $log_dir = '/var/log/munin-node' $service_name = 'munin-node' $package_name = 'munin-node' $plugin_share_dir = '/usr/share/munin/plugins' $file_group = 'root' } 'Debian': { $config_root = '/etc/munin' $log_dir = '/var/log/munin' $service_name = 'munin-node' $package_name = 'munin-node' $plugin_share_dir = '/usr/share/munin/plugins' $file_group = 'root' } 'Solaris': { case $::operatingsystem { 'SmartOS': { $config_root = '/opt/local/etc/munin' $log_dir = '/var/opt/log/munin' $service_name = 'smf:/munin-node' $package_name = 'munin-node' $plugin_share_dir = '/opt/local/share/munin/plugins' $file_group = 'root' } default: { fail("Unsupported operatingsystem ${::operatingsystem} for osfamily ${::osfamily}") } } } 'FreeBSD', 'DragonFly': { $config_root = '/usr/local/etc/munin' $log_dir = '/var/log/munin' $service_name = 'munin-node' $package_name = 'munin-node' $plugin_share_dir = '/usr/local/share/munin/plugins' $file_group = 'wheel' } 'OpenBSD': { $config_root = '/etc/munin' $log_dir = '/var/log/munin' $service_name = 'munin_node' $package_name = 'munin-node' $plugin_share_dir = '/usr/local/libexec/munin/plugins' $file_group = 'wheel' } default: { fail($message) } } } diff --git a/spec/classes/munin_node_spec.rb b/spec/classes/munin_node_spec.rb index 667feb2..2fbac6f 100644 --- a/spec/classes/munin_node_spec.rb +++ b/spec/classes/munin_node_spec.rb @@ -1,197 +1,195 @@ require 'spec_helper' _conf_dir = {} _conf_dir.default = '/etc/munin' _conf_dir['DragonFly'] = '/usr/local/etc/munin' _conf_dir['FreeBSD'] = '/usr/local/etc/munin' _conf_dir['Solaris'] = '/opt/local/etc/munin' describe 'munin::node' do on_supported_os.each do |os, facts| # Avoid testing on distributions similar to RedHat and Debian next if /^(ubuntu|centos|scientific|oraclelinux)-/.match(os) # No need to test all os versions as long as os version is not # used in the params class next if /^(debian-[67]|redhat-[56]|freebsd-9)-/.match(os) context "on #{os}" do let(:facts) do facts end it { should compile.with_all_deps } it { should contain_package('munin-node') } munin_confdir = _conf_dir[facts[:osfamily]] munin_node_conf = "#{munin_confdir}/munin-node.conf" munin_plugin_dir = "#{munin_confdir}/plugins" munin_plugin_conf_dir = "#{munin_confdir}/plugin-conf.d" case facts[:osfamily] when 'Solaris' munin_node_service = 'smf:/munin-node' else munin_node_service = 'munin-node' end case facts[:osfamily] when 'Solaris' log_dir = '/var/opt/log/munin' when 'RedHat' log_dir = '/var/log/munin-node' else log_dir = '/var/log/munin' end it { should contain_service(munin_node_service) } it { should contain_file(munin_node_conf) } context 'with no parameters' do it { should compile.with_all_deps } it do should contain_service(munin_node_service) .without_ensure() end it do should contain_file(munin_node_conf) .with_content(/host_name\s+foo.example.com/) .with_content(/log_file\s+#{log_dir}\/munin-node.log/) end end context 'with parameter allow' do let(:params) do { allow: ['2001:db8:1::', '2001:db8:2::/64', '192.0.2.129', '192.0.2.0/25', '192\.0\.2'] } end it { should compile.with_all_deps } it do should contain_file(munin_node_conf) .with_content(/^cidr_allow 192.0.2.0\/25$/) .with_content(/^cidr_allow 2001:db8:2::\/64$/) .with_content(/^allow \^192\\.0\\.2\\.129\$$/) .with_content(/^allow 192\\.0\\.2$/) .with_content(/^allow \^2001:db8:1::\$$/) end end context 'with parameter host_name' do let(:params) do { host_name: 'something.example.com' } end it { should compile.with_all_deps } it do should contain_file(munin_node_conf) .with_content(/host_name\s+something.example.com/) end end context 'with parameter service_ensure' do let(:params) do { service_ensure: 'running' } end it { should compile.with_all_deps } it do should contain_service('munin-node') .with_ensure('running') end end context 'logging to syslog' do context 'defaults' do let(:params) do { log_destination: 'syslog' } end it{ should compile.with_all_deps } it do should contain_file(munin_node_conf) .with_content(/log_file\s+Sys::Syslog/) end end context 'with syslog options' do let(:params) do { log_destination: 'syslog', - syslog_ident: 'munin-granbusk', syslog_facility: 'local1', } end it{ should compile.with_all_deps } it do should contain_file(munin_node_conf) .with_content(/log_file\s+Sys::Syslog/) - .with_content(/syslog_ident\s+"munin-granbusk"/) .with_content(/syslog_facility\s+local1/) end end context 'with syslog_facility set to wrong value ' do let(:params) do { log_destination: 'syslog', syslog_facility: 'wrong', } end it { expect { should compile.with_all_deps }.to raise_error(/validate_re/) } end end context 'purge_configs' do context 'set' do let(:params) { { purge_configs: true } } it { should compile.with_all_deps } it do should contain_file(munin_plugin_dir) .with_ensure('directory') .with_recurse(true) .with_purge(true) end it do should contain_file(munin_plugin_conf_dir) .with_ensure('directory') .with_recurse(true) .with_purge(true) end end context 'unset' do it { should compile.with_all_deps } it { should_not contain_file(munin_plugin_dir) } it { should_not contain_file(munin_plugin_conf_dir) } end end context 'timeout' do context 'set' do let(:params) { { timeout: 123 } } it { should compile.with_all_deps } it do should contain_file(munin_node_conf) .with_content(/^timeout 123/) end end context 'unset' do it { should compile.with_all_deps } it do should contain_file(munin_node_conf) .without_content(/^timeout/) end end end end end context 'unsupported' do include_context :unsupported it { expect { should contain_class('munin::node') }.to raise_error(Puppet::Error, /Unsupported osfamily/) } end end diff --git a/templates/munin-node.conf.erb b/templates/munin-node.conf.erb index 8844b07..3e076b4 100644 --- a/templates/munin-node.conf.erb +++ b/templates/munin-node.conf.erb @@ -1,72 +1,69 @@ # /etc/munin/munin-node.conf - config-file for munin-node # # This file is handled by puppet, any local changes will be lost # <% require 'ipaddr' addresses = [] unrecognized = [] Array(@allow).flatten.each do |line| begin a = IPAddr.new(line) if a.ipv4? or a.ipv6? addresses << line end rescue ArgumentError, IPAddr::InvalidAddressError # Treat invalid addresses as "allow" arguments, as they can be # regular expressions. unrecognized << line end end -%> host_name <%= @host_name %> log_level 4 log_file <%= @_log_file %> <% if @log_destination == 'syslog' -%> -<% if @syslog_ident -%> -syslog_ident "<%= @syslog_ident %>" -<% end -%> <% if @syslog_facility -%> syslog_facility <%= @syslog_facility %> <% end -%> <% end -%> port 4949 pid_file /var/run/munin/munin-node.pid background 1 setsid 1 # Which port to bind to; user root group <%= @file_group %> # Regexps for files to ignore ignore_file ~$ ignore_file \.bak$ ignore_file %$ ignore_file \.dpkg-(tmp|new|old|dist)$ ignore_file \.rpm(save|new)$ ignore_file \.puppet-bak$ # Hosts to allow <% addresses.each do |line| -%> <% if line.match(/\//) -%> cidr_allow <%= line %> <% else -%> allow ^<%= Regexp.quote(line) %>$ <% end -%> <% end -%> <% unrecognized.each do |line| -%> allow <%= line %> <% end -%> <% if @timeout -%> timeout <%= @timeout %> <% end -%> <% Array(@nodeconfig).each do |line| -%> <%= line %> <% end -%>