diff --git a/.fixtures.yml b/.fixtures.yml new file mode 100644 index 0000000..76f4e25 --- /dev/null +++ b/.fixtures.yml @@ -0,0 +1,6 @@ +fixtures: + repositories: + stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" + concat: "https://github.com/puppetlabs/puppetlabs-concat.git" + symlinks: + check_mk: "#{source_dir}" diff --git a/.gitignore b/.gitignore index f6dc3f6..6ee20c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ pkg/ -metadata.json *.swp diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..0357065 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--color +--format documentation +progress diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..95fdfaf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +--- +language: ruby +sudo: false +rvm: + - 1.9.3 + - 2.0 + - 2.1 + - 2.2 +notifications: + email: false +env: + - PUPPET_GEM_VERSION="~> 3.7.0" + - PUPPET_GEM_VERSION="~> 3.8.0" + - PUPPET_GEM_VERSION="~> 4.1.0" + - PUPPET_GEM_VERSION="~> 4.2.0" +script: + - puppet --version + - bundle exec rake validate lint spec +matrix: + exclude: + - rvm: 2.2 + env: PUPPET_GEM_VERSION="~> 3.7.0" + - rvm: 2.2 + env: PUPPET_GEM_VERSION="~> 3.8.0" diff --git a/Changelog b/Changelog index 0b2f8a1..107bf9b 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,19 @@ +0.7.1: +* Add check_mk::agent::mrpe, code from Bas Grolleman contributed by gerhardsam. + +0.7.0: +* Rework of check_mk::agent classes, contributed by gerhardsam. + +0.6.2: +* Add rspec tests contributed by gerhardsam. + +0.4.0: + +* Fix all_hosts_static.erb, update xinetd on wheezy and use cron instead of a +scheduled exec. + +* Package a new version has upstream seems unmaintained. + 0.3.0: * Added host tags to agent config so that host groups can be auto-populated diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..40339a9 --- /dev/null +++ b/Gemfile @@ -0,0 +1,23 @@ +source ENV['GEM_SOURCE'] || "https://rubygems.org" + +group :development, :unit_tests do + gem 'rake', :require => false + gem 'rspec-puppet', :require => false + gem 'puppetlabs_spec_helper', :require => false + gem 'puppet-lint', :require => false + gem 'metadata-json-lint', :require => false +end + +if puppetversion = ENV['PUPPET_GEM_VERSION'] + gem 'puppet', puppetversion, :require => false +else + gem 'puppet', :require => false +end + +if rspecpuppetversion = ENV['RSPEC_GEM_VERSION'] + gem 'rspec', rspecpuppetversion, :require => false +else + gem 'rspec', :require => false +end + +# vim:ft=ruby diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ad410e1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/Modulefile b/Modulefile deleted file mode 100644 index 60c355a..0000000 --- a/Modulefile +++ /dev/null @@ -1,10 +0,0 @@ -name 'erwbgy-check_mk' -version '0.3.0' -source 'https://github.com/erwbgy/puppet-check_mk.git' -author 'erwbgy' -license 'Apache License, Version 2.0' -summary 'install and configure check_mk' -description 'Install and configure check_mk agent and Nagios plugin' -project_page 'https://github.com/erwbgy/puppet-check_mk' -dependency 'puppetlabs/stdlib', '>= 2.6.0' -dependency 'ripienaar/concat', '>= 0.2.0' diff --git a/README.md b/README.md index 6e8b11f..9b755a7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # check_mk +[![Puppet Forge](http://img.shields.io/puppetforge/v/gnubilafrance/check_mk.svg)](https://forge.puppetlabs.com/gnubilafrance/check_mk) +[![Build Status](https://travis-ci.org/gnubila-france/puppet-check_mk.png?branch=master)](https://travis-ci.org/gnubila-france/puppet-check_mk) + Puppet module for: * Installing and configuring the Open Monitoring Distribution (OMD) which @@ -10,7 +13,7 @@ Puppet module for: Agent hostnames are automatically added to the server all_hosts configuration using stored configs. -Currently only tested on Redhat-like systems. +Original code by erwbgy, forked as upstream seems unmaintained. ## Server @@ -72,7 +75,7 @@ created making the URL http://hostname/acme/check_mk/ running as the 'acme' user ## Agent -* Installs the check_mk-agent and check_mk-agent-logwatch packages +* Installs the check_mk-agent package. * Configures the /etc/xinetd.d/check_mk configuration file @@ -80,7 +83,7 @@ created making the URL http://hostname/acme/check_mk/ running as the 'acme' user include check_mk::agent -Installs the check_mk and check_mk_logwatch packages from the system repository +Installs the check_mk package from the system repository and configures /etc/xinetd.d/check_mk with no IP whitelist restrictions. ### Example 2 @@ -90,7 +93,7 @@ and configures /etc/xinetd.d/check_mk with no IP whitelist restrictions. ip_whitelist => [ '10.7.96.21', '10.7.96.22' ], } -Installs the specified versions of the check_mk and check_mk_logwatch packages +Installs the specified versions of the check_mk package after retrieving them from the Puppet file store. Configures /etc/xinetd.d/check_mk so that only the specified IPs (and localhost/127.0.0.1) are allowed to connect. @@ -120,6 +123,8 @@ Only required if a filestore is used. *workspace*: The directory to use to store files used during installation. Default: '/root/check_mk' +*mrpe_checks*: Specifies a hash of check_mk::agent::mrpe resources to create. Default: {} + ## Host groups and tags By default check_mk puts all hosts into a group called 'check_mk' but where you diff --git a/Rakefile b/Rakefile index 14f1c24..aa2d928 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,7 @@ require 'rubygems' require 'puppetlabs_spec_helper/rake_tasks' +require 'puppet-lint/tasks/puppet-lint' + +PuppetLint.configuration.fail_on_warnings +PuppetLint.configuration.send('disable_80chars') +PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"] diff --git a/example.yaml b/example.yaml index de82ecc..b7280d0 100644 --- a/example.yaml +++ b/example.yaml @@ -69,25 +69,3 @@ check_mk::agent::host_tags: - '%{kernel}' - '%{role}' - '%{location}' - -check_mk::agent::jolokia::server: '127.0.0.1' -check_mk::agent::jolokia::port: '8080' -check_mk::agent::jolokia::user: 'monitoring' -check_mk::agent::jolokia::password: 'tinstaafl' -check_mk::agent::jolokia::suburi: 'jolokia' - -check_mk::agent::jolokia::instances: - 'My-App': - server: '10.0.0.1' - port: '8190' - -check_mk::agent::logwatch::keep_defaults: 'true' -check_mk::agent::logwatch::logfiles: - '/apps/tomcat1/logs/tomcat/catalina.*.log': - critical: - - '^SERVERE:' - - '^ERROR:' - warning: - - '^WARNING:' - ignore: - - '^INFO' diff --git a/manifests/agent.pp b/manifests/agent.pp index b53882a..3a50356 100644 --- a/manifests/agent.pp +++ b/manifests/agent.pp @@ -8,22 +8,17 @@ $user = 'root', $version = undef, $workspace = '/root/check_mk', + $package = undef, + $mrpe_checks = {}, ) { - class { 'check_mk::agent::install': - version => $version, - filestore => $filestore, - workspace => $workspace, - } - class { 'check_mk::agent::config': - ip_whitelist => $ip_whitelist, - port => $port, - server_dir => $server_dir, - use_cache => $use_cache, - user => $user, - require => Class['check_mk::agent::install'], - } + validate_hash($mrpe_checks) + include check_mk::agent::install + include check_mk::agent::config include check_mk::agent::service + Class['check_mk::agent::install'] -> + Class['check_mk::agent::config'] @@check_mk::host { $::fqdn: host_tags => $host_tags, } + create_resources('check_mk::agent::mrpe', $mrpe_checks) } diff --git a/manifests/agent/config.pp b/manifests/agent/config.pp index dc6808a..dbf7b42 100644 --- a/manifests/agent/config.pp +++ b/manifests/agent/config.pp @@ -1,29 +1,69 @@ +# == Class: check_mk::agent::config +# +# Configure check_mk agent. +# +# === Parameters +# +# [*ip_whitelist*] +# Array of IP allowed to access the check_mk agent. +# Default: undef +# +# [*port*] +# Check_mk port +# Default: undef +# +# [*server_dir*] +# Check_mk server directory. +# Default: undef +# +# [*use_cache*] +# Enable cache. +# Default: undef +# +# [*user*] +# Check_mk user. +# Default: undef +# class check_mk::agent::config ( - $ip_whitelist, - $port, - $server_dir, - $use_cache, - $user, -) { + $ip_whitelist = $check_mk::agent::ip_whitelist, + $port = $check_mk::agent::port, + $server_dir = $check_mk::agent::server_dir, + $use_cache = $check_mk::agent::use_cache, + $user = $check_mk::agent::user, +) inherits check_mk::agent { if $use_cache { $server = "${server_dir}/check_mk_caching_agent" - } - else { + } else { $server = "${server_dir}/check_mk_agent" } + if $ip_whitelist { $only_from = join($ip_whitelist, ' ') - } - else { + } else { $only_from = undef } - file { '/etc/xinetd.d/check_mk': - ensure => present, + + $xinetd_file = $::osfamily ? { + 'RedHat' => '/etc/xinetd.d/check-mk-agent', + default => '/etc/xinetd.d/check_mk', + } + + file { $xinetd_file: + ensure => 'file', owner => 'root', group => 'root', mode => '0444', content => template('check_mk/agent/check_mk.erb'), - require => Package['check_mk-agent','check_mk-agent-logwatch'], + require => Package['check_mk-agent'], notify => Class['check_mk::agent::service'], } + + # Delete file from older check_mk package version + if $::osfamily == 'RedHat' { + file { '/etc/xinetd.d/check_mk': + ensure => 'absent', + } + } } + +# vim: set et sta sw=2 ts=2 sts=2 noci noai:a diff --git a/manifests/agent/install.pp b/manifests/agent/install.pp index 1c8a429..a377e56 100644 --- a/manifests/agent/install.pp +++ b/manifests/agent/install.pp @@ -1,14 +1,19 @@ class check_mk::agent::install ( - $version, - $filestore, - $workspace, -) { + $version = $check_mk::agent::version, + $filestore = undef, + $workspace = $check_mk::agent::workspace, + $package = undef, +) inherits check_mk::agent { if ! defined(Package['xinetd']) { package { 'xinetd': ensure => present, } } if $filestore { + if ! $version { + fail('version must be specified.') + } + if ! defined(File[$workspace]) { file { $workspace: ensure => directory, @@ -19,35 +24,26 @@ source => "${filestore}/check_mk-agent-${version}.noarch.rpm", require => Package['xinetd'], } - file { "${workspace}/check_mk-agent-logwatch-${version}.noarch.rpm": - ensure => present, - source => "${filestore}/check_mk-agent-logwatch-${version}.noarch.rpm", - require => Package['xinetd'], - } package { 'check_mk-agent': ensure => present, provider => 'rpm', source => "${workspace}/check_mk-agent-${version}.noarch.rpm", require => File["${workspace}/check_mk-agent-${version}.noarch.rpm"], } - package { 'check_mk-agent-logwatch': - ensure => present, - provider => 'rpm', - source => "${workspace}/check_mk-agent-logwatch-${version}.noarch.rpm", - require => [ - File["${workspace}/check_mk-agent-logwatch-${version}.noarch.rpm"], - Package['check_mk-agent'], - ], - } } else { + $check_mk_agent = $package ? { + undef => $::osfamily ? { + 'Debian' => 'check-mk-agent', + 'RedHat' => 'check-mk-agent', + default => 'check_mk-agent', + }, + default => $package, + } package { 'check_mk-agent': ensure => present, + name => $check_mk_agent, require => Package['xinetd'], } - package { 'check_mk-agent-logwatch': - ensure => present, - require => Package['check_mk-agent'], - } } } diff --git a/manifests/agent/mrpe.pp b/manifests/agent/mrpe.pp new file mode 100644 index 0000000..eb0c80f --- /dev/null +++ b/manifests/agent/mrpe.pp @@ -0,0 +1,44 @@ +# +# Add entry to the mrpe.cfg file +# +### Parameters +# +### command +# +# The command to run +# +### Example +# +# ``` +# check_mk::agent::mrpe { 'Test': +# command => '/bin/true' +# } +# ``` +# +## Authors +# +# * Bas Grolleman +# +define check_mk::agent::mrpe ( + $command, +) { + $mrpe_config_file = $::operatingsystem ? { + centos => '/etc/check-mk-agent/mrpe.cfg', + redhat => '/etc/check-mk-agent/mrpe.cfg', + default => undef, + } + + if ( $mrpe_config_file ) { + if ! defined(Concat[$mrpe_config_file]) { + concat { $mrpe_config_file: + ensure => 'present', + } + } + concat::fragment { "${name}-mrpe-check": + target => $mrpe_config_file, + content => "${name} ${command}\n", + } + } else { + fail("Creating mrpe.cfg is unsupported for operatingsystem ${::operatingsystem}") + } +} diff --git a/manifests/agent/service.pp b/manifests/agent/service.pp index 0f70708..0d3ff04 100644 --- a/manifests/agent/service.pp +++ b/manifests/agent/service.pp @@ -1,8 +1,17 @@ class check_mk::agent::service { if ! defined(Service['xinetd']) { - service { 'xinetd': - ensure => 'running', - enable => true, + if $::operatingsystem == 'Debian' and + versioncmp($::operatingsystemmajrelease, '7') == 0 { + service { 'xinetd': + ensure => 'running', + enable => true, + hasstatus => false, + } + } else { + service { 'xinetd': + ensure => 'running', + enable => true, + } } } } diff --git a/manifests/config.pp b/manifests/config.pp index 3d751cc..328f3d9 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -1,3 +1,4 @@ + class check_mk::config ( $site, $host_groups = undef, @@ -27,7 +28,7 @@ concat { "${etc_dir}/check_mk/main.mk": owner => 'root', group => 'root', - mode => '0644', + mode => 'u=rw,go=r', notify => Exec['check_mk-refresh'], } # all_hosts @@ -47,9 +48,9 @@ } # local list of hosts is in /omd/sites/${site}/etc/check_mk/all_hosts_static and is appended concat::fragment { 'all-hosts-static': - ensure => "${etc_dir}/check_mk/all_hosts_static", - target => "${etc_dir}/check_mk/main.mk", - order => 18, + source => "${etc_dir}/check_mk/all_hosts_static", + target => "${etc_dir}/check_mk/main.mk", + order => 18, } # host_groups if $host_groups { @@ -68,17 +69,23 @@ } $groups = keys($host_groups) check_mk::hostgroup { $groups: - dir => "${etc_dir}/nagios/local/hostgroups", - hostgroups => $host_groups, - target => "${etc_dir}/check_mk/main.mk", - notify => Exec['check_mk-refresh'] + dir => "${etc_dir}/nagios/local/hostgroups", + hostgroups => $host_groups, + target => "${etc_dir}/check_mk/main.mk", + notify => Exec['check_mk-refresh'] } } # local config is in /omd/sites/${site}/etc/check_mk/main.mk.local and is appended + file { "${etc_dir}/check_mk/main.mk.local": + ensure => file, + owner => 'root', + group => 'root', + mode => 'u=rw,go=r', + } concat::fragment { 'check_mk-local-config': - ensure => "${etc_dir}/check_mk/main.mk.local", - target => "${etc_dir}/check_mk/main.mk", - order => 99, + source => "${etc_dir}/check_mk/main.mk.local", + target => "${etc_dir}/check_mk/main.mk", + order => 99, } # re-read config if it changes exec { 'check_mk-refresh': @@ -90,9 +97,11 @@ command => "/bin/su -l -c '${bin_dir}/check_mk -O' ${site}", refreshonly => true, } - # re-read inventory at least daily - exec { 'check_mk-refresh-inventory-daily': - command => "/bin/su -l -c '${bin_dir}/check_mk -O' ${site}", - schedule => 'daily', + # re-read inventory daily + cron { 'check_mk-refresh-inventory-daily': + user => 'root', + command => "su -l -c '${bin_dir}/check_mk -O' ${site}", + minute => 0, + hour => 0, } } diff --git a/manifests/host.pp b/manifests/host.pp index 4e8d146..a584e71 100644 --- a/manifests/host.pp +++ b/manifests/host.pp @@ -1,5 +1,5 @@ define check_mk::host ( - $target, + $target = undef, $host_tags = [], ) { $host = $title diff --git a/manifests/init.pp b/manifests/init.pp index 24e4e0d..acab396 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,10 +1,13 @@ class check_mk ( - $filestore = undef, - $host_groups = undef, - $package = 'omd-0.56', - $site = 'monitoring', - $workspace = '/root/check_mk', -) { + $checkmk_service = $check_mk::params::checkmk_service, + $filestore = $check_mk::params::filestore, + $host_groups = $check_mk::params::host_groups, + $httpd_service = $check_mk::params::httpd_service, + $package = $check_mk::params::package, + $site = $check_mk::params::site, + $workspace = $check_mk::params::workspace, +) inherits check_mk::params { + class { 'check_mk::install': filestore => $filestore, package => $package, @@ -17,6 +20,8 @@ require => Class['check_mk::install'], } class { 'check_mk::service': - require => Class['check_mk::config'], + checkmk_service => $checkmk_service, + httpd_service => $httpd_service, + require => Class['check_mk::config'], } } diff --git a/manifests/install.pp b/manifests/install.pp index 97f7efa..1943508 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,6 +1,6 @@ class check_mk::install ( - $filestore, - $package, + $filestore = undef, + $package = undef, $site, $workspace, ) { diff --git a/manifests/install_tarball.pp b/manifests/install_tarball.pp index 4a0af23..f09e770 100644 --- a/manifests/install_tarball.pp +++ b/manifests/install_tarball.pp @@ -35,14 +35,14 @@ ensure => present, } file { "${workspace}/check_mk-${version}.tar.gz": - ensure => present, - source => "${filestore}/check_mk-${version}.tar.gz", + ensure => present, + source => "${filestore}/check_mk-${version}.tar.gz", } exec { 'unpack-check_mk-tarball': - command => "/bin/tar -zxf ${workspace}/check_mk-${version}.tar.gz", - cwd => $workspace, - creates => "${workspace}/check_mk-${version}", - require => File["${workspace}/check_mk-${version}.tar.gz"], + command => "/bin/tar -zxf ${workspace}/check_mk-${version}.tar.gz", + cwd => $workspace, + creates => "${workspace}/check_mk-${version}", + require => File["${workspace}/check_mk-${version}.tar.gz"], } exec { 'change-setup-config-location': command => "/usr/bin/perl -pi -e 's#^SETUPCONF=.*?$#SETUPCONF=${workspace}/check_mk_setup.conf#' ${workspace}/check_mk-${version}/setup.sh", diff --git a/manifests/params.pp b/manifests/params.pp new file mode 100644 index 0000000..edd6028 --- /dev/null +++ b/manifests/params.pp @@ -0,0 +1,20 @@ +class check_mk::params { + + # common variables + $checkmk_service = 'omd' + $package = 'omd-0.56' + $filestore = undef + $host_groups= undef + $site = 'monitoring' + $workspace = '/root/check_mk' + + # OS specific variables + case $::osfamily { + 'RedHat': { + $httpd_service = 'httpd' + } + 'Debian': { + $httpd_service = 'apache2' + } + } +} diff --git a/manifests/service.pp b/manifests/service.pp index 4b31df9..f21bd51 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -1,6 +1,9 @@ -class check_mk::service { - if ! defined(Service[httpd]) { - service { 'httpd': +class check_mk::service ( + $checkmk_service, + $httpd_service, +) { + if ! defined(Service[$httpd_service]) { + service { "$httpd_service": ensure => 'running', enable => true, } @@ -11,7 +14,7 @@ enable => true, } } - service { 'omd': + service { $checkmk_service: ensure => 'running', enable => true, } diff --git a/metadata.json b/metadata.json new file mode 100644 index 0000000..f08e1e2 --- /dev/null +++ b/metadata.json @@ -0,0 +1,44 @@ +{ + "name": "gnubilafrance-check_mk", + "version": "0.7.1", + "author": "Baptiste Grenier ", + "license": "Apache-2.0", + "summary": "Check_MK installation and configuration", + + "source": "https://github.com/gnubila-france/puppet-check_mk", + "project_page": "https://github.com/gnubila-france/puppet-check_mk", + "issues_url": "https://github.com/gnubila-france/puppet-check_mk/issues", + + "tags": [ "check_mk", "monitoring", "nagios" ], + + "operatingsystem_support": [ + { + "operatingsystem": "RedHat", + "operatingsystemrelease": [ + "5", + "6", + "7" + ] + }, + { + "operatingsystem": "CentOS", + "operatingsystemrelease": [ + "5", + "6", + "7" + ] + }, + { + "operatingsystem": "Debian", + "operatingsystemrelease": [ + "6", + "7" + ] + } + ], + + "dependencies": [ + { "name": "puppetlabs/concat", "version_requirement": ">= 1.0.0 <5.0.0" }, + { "name": "puppetlabs/stdlib", "version_requirement": ">= 2.3.0 <5.0.0" } + ] +} diff --git a/spec/classes/check_mk_agent_config_spec.rb b/spec/classes/check_mk_agent_config_spec.rb new file mode 100644 index 0000000..68fd2aa --- /dev/null +++ b/spec/classes/check_mk_agent_config_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' +describe 'check_mk::agent::config', :type => :class do + + context 'Redhat Linux' do + let :facts do + { + :osfamily => 'RedHat', + } + end + context 'with defaults for all parameters' do + it { should contain_class('check_mk::agent::config') } + it { should contain_file('/etc/xinetd.d/check-mk-agent'). + with_content(/^\tport\s+ = 6556$/). + with_content(/^\tuser\s+ = root$/). + with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_agent$/). + without_content(/only_from/). + with_notify('Class[Check_mk::Agent::Service]') + } + it { should contain_file('/etc/xinetd.d/check_mk').with_ensure('absent') } + end + context 'with use_cache' do + let :params do + { + :use_cache => true, + } + end + it { should contain_file('/etc/xinetd.d/check-mk-agent'). + with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_caching_agent$/) + } + end + context 'with ip_whitelist' do + let :params do + { + :ip_whitelist => [ + '1.2.3.4', + '5.6.7.8', + ], + } + end + it { should contain_file('/etc/xinetd.d/check-mk-agent'). + with_content(/^\tonly_from\s+= 127.0.0.1 1.2.3.4 5.6.7.8$/) + } + end + context 'with custom user' do + let :params do + { + :user => 'custom', + } + end + it { should contain_file('/etc/xinetd.d/check-mk-agent'). + with_content(/^\tuser\s+ = custom$/) + } + end + end + + context 'Other OS' do + context 'with defaults for all parameters' do + it { should contain_file('/etc/xinetd.d/check_mk'). + with_content(/^\tport\s+ = 6556$/). + with_content(/^\tuser\s+ = root$/). + with_content(/^\tserver\s+ = \/usr\/bin\/check_mk_agent$/). + without_content(/only_from/). + with_notify('Class[Check_mk::Agent::Service]') + } + it { should_not contain_file('/etc/xinetd.d/check_mk').with_ensure('absent') } + end + end +end + + diff --git a/spec/classes/check_mk_agent_install_spec.rb b/spec/classes/check_mk_agent_install_spec.rb new file mode 100644 index 0000000..dce3bd2 --- /dev/null +++ b/spec/classes/check_mk_agent_install_spec.rb @@ -0,0 +1,119 @@ +require 'spec_helper' +describe 'check_mk::agent::install', :type => :class do + + context 'RedHat Linux' do + let :facts do + { + :osfamily => 'Redhat', + } + end + + context 'with default parameters' do + it { should contain_class('check_mk::agent::install').with( + { + :version => nil, + :filestore => nil, + :workspace => '/root/check_mk', + :package => nil, + } + ) + } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('check-mk-agent') } + end + + context 'with custom package' do + let :params do + { + :package => 'custom-package', + } + end + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('custom-package') } + end + + context 'with filestore' do + context 'without version' do + let :params do + { + :filestore => '/filestore', + } + end + it 'should fail' do + expect { catalogue }.to raise_error(Puppet::Error, /version must be specified/) + end + end + context 'with custom parameters' do + let :params do + { + :version => '1.2.3', + :filestore => '/filestore', + :workspace => '/workspace', + } + end + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_file('/workspace').with_ensure('directory') } + it { should contain_File('/workspace/check_mk-agent-1.2.3.noarch.rpm').with( + { + :ensure => 'present', + :source => '/filestore/check_mk-agent-1.2.3.noarch.rpm', + :require => 'Package[xinetd]', + } + ).that_comes_before('Package[check_mk-agent]') + } + it { should contain_package('check_mk-agent').with( + { + :provider => 'rpm', + :source => '/workspace/check_mk-agent-1.2.3.noarch.rpm', + } + ) + } + end + end + end + + context 'Debian Linux' do + let :facts do + { + :osfamily => 'Debian', + } + end + + context 'with default parameters' do + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('check-mk-agent') } + end + + context 'with custom package' do + let :params do + { + :package => 'custom-package', + } + end + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('custom-package') } + end + end + + context 'Unknown OS' do + context 'with default parameters' do + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('check_mk-agent') } + end + context 'with custom package' do + let :params do + { + :package => 'custom-package', + } + end + it { should contain_class('check_mk::agent::install') } + it { should contain_package('xinetd') } + it { should contain_package('check_mk-agent').with_name('custom-package') } + end + end +end diff --git a/spec/classes/check_mk_agent_service_spec.rb b/spec/classes/check_mk_agent_service_spec.rb new file mode 100644 index 0000000..a85b3e9 --- /dev/null +++ b/spec/classes/check_mk_agent_service_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' +describe 'check_mk::agent::service', :type => :class do + + context 'all' do + it { should contain_class('check_mk::agent::service') } + it { should contain_service('xinetd').with({ + :ensure => 'running', + :enable => true, + }) + } + end + + context 'Debian 7' do + let :facts do + { + :operatingsystem => 'Debian', + :operatingsystemmajrelease => '7', + } + end + it { should contain_class('check_mk::agent::service') } + it { should contain_service('xinetd').with({ + :ensure => 'running', + :enable => true, + :hasstatus => false, + }) + } + end + +end diff --git a/spec/classes/check_mk_agent_spec.rb b/spec/classes/check_mk_agent_spec.rb new file mode 100644 index 0000000..6d08a4d --- /dev/null +++ b/spec/classes/check_mk_agent_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' +describe 'check_mk::agent', :type => :class do + context 'Redhat Linux' do + let :facts do + { + :kernel => 'Linux', + :operatingsystem => 'Redhat', + :osfamily => 'Redhat', + } + end + context 'with defaults for all parameters' do + it { should contain_class('check_mk::agent') } + it { should contain_class('check_mk::agent::install').that_comes_before('Class[check_mk::agent::config]') } + it { should contain_class('check_mk::agent::config') } + it { should contain_class('check_mk::agent::service') } + end + context 'with mrpe_checks' do + context 'not a hash' do + let :params do + { + :mrpe_checks => 'not_a_hash', + } + end + it 'should fail' do + expect { catalogue }.to raise_error(Puppet::Error, /\"not_a_hash\" is not a Hash./) + end + end + context 'defined correctly' do + let :params do + { + :mrpe_checks => { + 'check1' => {'command' => 'command1'}, + 'check2' => {'command' => 'command2'}, + } + } + end + it { should contain_class('check_mk::agent') } + it { should contain_check_mk__agent__mrpe('check1').with_command('command1') } + it { should contain_check_mk__agent__mrpe('check2').with_command('command2') } + end + end + end +end diff --git a/spec/classes/check_mk_config_spec.rb b/spec/classes/check_mk_config_spec.rb new file mode 100644 index 0000000..9de298b --- /dev/null +++ b/spec/classes/check_mk_config_spec.rb @@ -0,0 +1,124 @@ +require 'spec_helper' +describe 'check_mk::config', :type => :class do + context 'with site set' do + let :params do + { + :site => 'TEST_SITE' + } + end + it { should contain_class('check_mk::config') } + it { should contain_file('/omd/sites/TEST_SITE/etc/nagios/local').with_ensure_directory. + that_comes_before('File_line[nagios-add-check_mk-cfg_dir]') + } + it { should contain_file_line('nagios-add-check_mk-cfg_dir').with({ + :ensure => 'present', + :line => 'cfg_dir=/omd/sites/TEST_SITE/etc/nagios/local', + :path => '/omd/sites/TEST_SITE/etc/nagios/nagios.cfg', + :notify => 'Class[Check_mk::Service]', + }) + } + it { should contain_file_line('add-guest-users').with({ + :ensure => 'present', + :line => 'guest_users = [ "guest" ]', + :path => '/omd/sites/TEST_SITE/etc/check_mk/multisite.mk', + }) + } + it { should contain_file('/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static').with({ + :ensure => 'file', + :content => '', + }) + } + it { should contain_concat('/omd/sites/TEST_SITE/etc/check_mk/main.mk').with({ + :owner => 'root', + :group => 'root', + :mode => '0644', + :notify => 'Exec[check_mk-refresh]', + }) + } + it { should contain_concat__fragment('all_hosts-header').with({ + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :content => /all_hosts = \[\n/, + :order => 10, + }) + } + it { should contain_concat__fragment('all_hosts-footer').with({ + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :content => /\]\n/, + :order => 19, + }) + } + it { should contain_concat__fragment('all-hosts-static').with({ + :ensure => '/omd/sites/TEST_SITE/etc/check_mk/all_hosts_static', + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :order => 18, + }) + } + it { should_not contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups') } + it { should_not contain_concat__fragment('host_groups-header') } + it { should_not contain_concat__fragment('host_groups-footer') } + it { should_not contain_check_mk__hostgroup } + it { should contain_concat__fragment('check_mk-local-config').with({ + :ensure => '/omd/sites/TEST_SITE/etc/check_mk/main.mk.local', + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :order => 99, + }) + } + it { should contain_exec('check_mk-refresh').with({ + :command => /\/bin\/su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -I' TEST_SITE/, + :refreshonly => true, + }) + } + it { should contain_exec('check_mk-reload').with({ + :command => /\/bin\/su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -O' TEST_SITE/, + :refreshonly => true, + }) + } + it { should contain_cron('check_mk-refresh-inventory-daily').with({ + :user => 'root', + :command => /su -l -c '\/omd\/sites\/TEST_SITE\/bin\/check_mk -O' TEST_SITE/, + :minute => 0, + :hour => 0, + }) + } + end + context 'with host_groups' do + host_groups = { + 'group1' => {'host_tags' => []}, + 'group2' => {'host_tags' => []}, + } + let :params do + { + :site => 'TEST_SITE', + :host_groups => host_groups, + } + end + it { should contain_class('check_mk::config') } + it { should contain_file('/omd/sites/TEST_SITE/etc/nagios/local/hostgroups').with_ensure_directory } + it { should contain_concat__fragment('host_groups-header').with({ + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :content => /host_groups = \[\n/, + :order => 20, + }) + } + it { should contain_concat__fragment('host_groups-footer').with({ + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :content => /\]\n/, + :order => 29, + }) + } + it { should contain_check_mk__hostgroup('group1').with({ + :dir => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', + :hostgroups => host_groups, + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :notify => 'Exec[check_mk-refresh]', + }) + } + it { should contain_check_mk__hostgroup('group2').with({ + :dir => '/omd/sites/TEST_SITE/etc/nagios/local/hostgroups', + :hostgroups => host_groups, + :target => '/omd/sites/TEST_SITE/etc/check_mk/main.mk', + :notify => 'Exec[check_mk-refresh]', + }) + } + end +end diff --git a/spec/classes/check_mk_install_spec.rb b/spec/classes/check_mk_install_spec.rb new file mode 100644 index 0000000..9cf0b09 --- /dev/null +++ b/spec/classes/check_mk_install_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' +describe 'check_mk::install', :type => :class do + context 'with necessary parameters set' do + let :params do + { + :filestore => false, + :package => 'package', + :site => 'site', + :workspace => 'workspace', + } + end + it { should contain_class('check_mk::install') } + it { should contain_package('package').with_ensure('installed').that_comes_before('Exec[omd-create-site]') } + it { should contain_exec('omd-create-site').with({ + :command => '/usr/bin/omd create site', + :creates => '/omd/sites/site/etc', + }) + } + end +end diff --git a/spec/classes/check_mk_install_tarball_spec.rb b/spec/classes/check_mk_install_tarball_spec.rb new file mode 100644 index 0000000..7537d85 --- /dev/null +++ b/spec/classes/check_mk_install_tarball_spec.rb @@ -0,0 +1,102 @@ +require 'spec_helper' +describe 'check_mk::install_tarball', :type => :class do + context 'with necessary parameters set' do + let :params do + { + :filestore => '/filestore', + :version => '1.2.3', + :workspace => '/workspace', + } + end + it { should contain_class('check_mk::install_tarball') } + it { should contain_package('nagios').with_ensure('present').that_comes_before('Package[nagios-plugins-all]') } + installed_packages = [ + 'xinetd', + 'mod_python', + 'make', + 'gcc-c++', + 'tar', + 'gzip' + ] + installed_packages.each do |package| + it { should contain_package(package).with_ensure('present') } + end + it { should contain_file('/etc/nagios/passwd').with({ + :ensure => 'present', + :owner => 'root', + :group => 'apache', + :mode => '0640', + }) + } + it { should contain_exec('set-nagiosadmin-password').with({ + :refreshonly => true, + :require => 'File[/etc/nagios/passwd]', + }) + } + it { should contain_exec('set-guest-password').with({ + :refreshonly => true, + :require => 'File[/etc/nagios/passwd]', + }) + } + it { should contain_exec('add-apache-to-nagios-group').with({ + :refreshonly => true, + }) + } + it { should contain_package('nagios-plugins-all').with({ + :ensure => 'present', + :require => 'Package[nagios]', + }) + } + it { should contain_exec('unpack-check_mk-tarball').with({ + :command => '/bin/tar -zxf /workspace/check_mk-1.2.3.tar.gz', + :cwd => '/workspace', + :creates => '/workspace/check_mk-1.2.3', + :require => 'File[/workspace/check_mk-1.2.3.tar.gz]', + }) + } + it { should contain_exec('change-setup-config-location').with({ + :command => "/usr/bin/perl -pi -e 's#^SETUPCONF=.*?$#SETUPCONF=/workspace/check_mk_setup.conf#' /workspace/check_mk-1.2.3/setup.sh", + :unless => "/bin/egrep '^SETUPCONF=/workspace/check_mk_setup.conf$' /workspace/check_mk-1.2.3/setup.sh", + :require => 'Exec[unpack-check_mk-tarball]', + }) + } + it { should contain_exec('remove-setup-header').with({ + :command => "/usr/bin/perl -pi -e 's#^DIRINFO=.*?$#DIRINFO=#' /workspace/check_mk-1.2.3/setup.sh", + :unless => "/bin/egrep '^DIRINFO=$' /workspace/check_mk-1.2.3/setup.sh", + :require => 'Exec[unpack-check_mk-tarball]', + }) + } + it { should contain_file('/workspace/check_mk_setup.conf').that_notifies('Exec[check_mk-setup]') } + it { should contain_file('/etc/nagios/check_mk').with({ + :ensure => 'directory', + :owner => 'nagios', + :group => 'nagios', + :recurse => true, + :require => 'Package[nagios]', + }) + } + it { should contain_file('/etc/nagios/check_mk/hostgroups').with({ + :ensure => 'directory', + :owner => 'nagios', + :group => 'nagios', + :require => 'File[/etc/nagios/check_mk]', + }) + } + it { should contain_exec('check_mk-setup').with({ + :command => '/workspace/check_mk-1.2.3/setup.sh --yes', + :cwd => '/workspace/check_mk-1.2.3', + :refreshonly => true, + :require => [ + 'Exec[change-setup-config-location]', + 'Exec[remove-setup-header]', + 'Exec[unpack-check_mk-tarball]', + 'File[/workspace/check_mk_setup.conf]', + 'File[/etc/nagios/check_mk]', + 'Package[nagios]', + ], + :notify => 'Class[Check_mk::Service]', + }) + } + + end +end diff --git a/spec/classes/check_mk_service_spec.rb b/spec/classes/check_mk_service_spec.rb new file mode 100644 index 0000000..bb05c3d --- /dev/null +++ b/spec/classes/check_mk_service_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' +describe 'check_mk::service', :type => :class do + context 'with defaults for all parameters' do + it { should contain_class('check_mk::service') } + it { should contain_service('httpd').with({ + :ensure => 'running', + :enable => 'true', + }) + } + it { should contain_service('omd').with({ + :ensure => 'running', + :enable => 'true', + }) + } + end +end diff --git a/spec/classes/check_mk_spec.rb b/spec/classes/check_mk_spec.rb new file mode 100644 index 0000000..169438f --- /dev/null +++ b/spec/classes/check_mk_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' +describe 'check_mk', :type => :class do + + context 'with defaults for all parameters' do + it { should contain_class('check_mk') } + it { should contain_class('check_mk::install').with({ + :filestore => nil, + :package => 'omd-0.56', + :site => 'monitoring', + :workspace => '/root/check_mk', + }).that_comes_before('Class[check_mk::config]') } + it { should contain_class('check_mk::config').with({ + :host_groups => nil, + :site => 'monitoring', + }).that_comes_before('Class[check_mk::service]') } + it { should contain_class('check_mk::service') } + end +end diff --git a/spec/classes/coverage_spec.rb b/spec/classes/coverage_spec.rb new file mode 100644 index 0000000..12513b8 --- /dev/null +++ b/spec/classes/coverage_spec.rb @@ -0,0 +1 @@ +at_exit { RSpec::Puppet::Coverage.report! } diff --git a/spec/defines/check_mk_agent_mrpe_spec.rb b/spec/defines/check_mk_agent_mrpe_spec.rb new file mode 100644 index 0000000..b911d0a --- /dev/null +++ b/spec/defines/check_mk_agent_mrpe_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +describe 'check_mk::agent::mrpe', :type => :define do + let :title do + 'checkname' + end + context 'Unsupported OS' do + context 'with mandatory command' do + let :params do + {:command => 'command'} + end + it 'should fail' do + expect { catalogue }.to raise_error(Puppet::Error, /Creating mrpe.cfg is unsupported for operatingsystem/) + end + end + end + context 'RedHat Linux' do + let :facts do + { + :operatingsystem => 'redhat', + } + end + context 'with mandatory command' do + let :params do + {:command => 'command'} + end + it { should contain_check_mk__agent__mrpe('checkname') } + it { should contain_concat('/etc/check-mk-agent/mrpe.cfg').with_ensure('present') } + it { should contain_concat__fragment('checkname-mrpe-check').with({ + :target => '/etc/check-mk-agent/mrpe.cfg', + :content => /^checkname command\n$/, + }) + } + end + end +end diff --git a/spec/defines/check_mk_host_spec.rb b/spec/defines/check_mk_host_spec.rb new file mode 100644 index 0000000..0a60870 --- /dev/null +++ b/spec/defines/check_mk_host_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' +describe 'check_mk::host', :type => :define do + let :title do + 'host' + end + context 'with empty host_tags array' do + let :params do + {:target => 'target'} + end + it { should contain_check_mk__host('host') } + it { should contain_concat__fragment('check_mk-host').with({ + :target => 'target', + :content => /^ 'host',\n$/, + :order => 11, + }) + } + end + context 'with custom host_tags' do + let :params do + { + :target => 'target', + :host_tags => ['tag1', 'tag2'], + } + end + it { should contain_check_mk__host('host') } + it { should contain_concat__fragment('check_mk-host').with({ + :target => 'target', + :content => /^ 'host|tag1|tag2',\n$/, + :order => 11, + }) + } + end +end diff --git a/spec/defines/check_mk_hostgroup_spec.rb b/spec/defines/check_mk_hostgroup_spec.rb new file mode 100644 index 0000000..d2ba88e --- /dev/null +++ b/spec/defines/check_mk_hostgroup_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' +describe 'check_mk::hostgroup', :type => :define do + + context 'with hostgroups, host_tags and description' do + let :title do + 'TEST_HOSTGROUP' + end + hostgroups = { + 'TEST_HOSTGROUP' => { + 'host_tags' => [ + 'tag1', + 'tag2', + ], + 'description' => 'TEST_DESCRIPTION', + }, + } + let :params do + { + :dir => '/dir', + :hostgroups => hostgroups, + :target => 'target' + } + end + it { should contain_check_mk__hostgroup('TEST_HOSTGROUP') } + it { should contain_concat__fragment('check_mk-hostgroup-TEST_HOSTGROUP').with({ + :target => 'target', + :content => /^ \( 'TEST_HOSTGROUP', \[ 'tag1', 'tag2' \], ALL_HOSTS \),\n$/, + :order => 21, + }) + } + expected_file_content = < 'present', + :content => expected_file_content, + }) + } + end + + context 'with hostgroups without description' do + let :title do + 'TEST_HOUSTGROUP_WITH_UNDERSCORES' + end + hostgroups = { + 'TEST_HOUSTGROUP_WITH_UNDERSCORES' => { + 'host_tags' => [ + 'tag1', + 'tag2', + ], + }, + } + let :params do + { + :dir => '/dir', + :hostgroups => hostgroups, + :target => '/target' + } + end + it { should contain_check_mk__hostgroup('TEST_HOUSTGROUP_WITH_UNDERSCORES') } + it { should contain_concat__fragment('check_mk-hostgroup-TEST_HOUSTGROUP_WITH_UNDERSCORES').with({ + :target => '/target', + :content => /^ \( 'TEST_HOUSTGROUP_WITH_UNDERSCORES', \[ 'tag1', 'tag2' \], ALL_HOSTS \),\n$/, + :order => 21, + }) + } + expected_file_content = < 'present', + :content => expected_file_content, + }) + } + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..2c6f566 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require 'puppetlabs_spec_helper/module_spec_helper' diff --git a/templates/all_hosts_static.erb b/templates/all_hosts_static.erb index 7ba0114..79689c1 100644 --- a/templates/all_hosts_static.erb +++ b/templates/all_hosts_static.erb @@ -1,4 +1,6 @@ <% static_hosts = scope.lookupvar('check_mk::config::all_hosts_static') -%> +<% if static_hosts.is_a? Array -%> <% static_hosts.each do |check_host| -%> '<%= host_name %>', <% end -%> +<% end -%>