Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow uninstalling the Zabbix agent #959

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ class { 'zabbix::agent':

The following parameters are available in the `zabbix::agent` class:

* [`ensure`](#-zabbix--agent--ensure)
* [`zabbix_version`](#-zabbix--agent--zabbix_version)
* [`zabbix_package_state`](#-zabbix--agent--zabbix_package_state)
* [`zabbix_package_agent`](#-zabbix--agent--zabbix_package_agent)
Expand Down Expand Up @@ -1442,6 +1443,14 @@ The following parameters are available in the `zabbix::agent` class:
* [`loadmodule`](#-zabbix--agent--loadmodule)
* [`manage_startup_script`](#-zabbix--agent--manage_startup_script)

##### <a name="-zabbix--agent--ensure"></a>`ensure`

Data type: `Enum['absent', 'present']`

Ensure that the agent is either present or absent

Default value: `'present'`

##### <a name="-zabbix--agent--zabbix_version"></a>`zabbix_version`

Data type: `Any`
Expand Down
31 changes: 22 additions & 9 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @summary This will install and configure the zabbix-agent deamon
# @param ensure Ensure that the agent is either present or absent
# @param zabbix_version This is the zabbix version.
# @param zabbix_package_state The state of the package that needs to be installed: present or latest.
# @param zabbix_package_agent The name of the agent package that we manage
Expand Down Expand Up @@ -147,6 +148,7 @@
#
# @author Werner Dijkerman [email protected]
class zabbix::agent (
Enum['absent', 'present'] $ensure = 'present',
$zabbix_version = $zabbix::params::zabbix_version,
$zabbix_package_state = $zabbix::params::zabbix_package_state,
$zabbix_package_agent = $zabbix::params::zabbix_package_agent,
Expand Down Expand Up @@ -232,6 +234,16 @@
) inherits zabbix::params {
$agent2 = $zabbix_package_agent == 'zabbix-agent2'

# Set up overrides if the ensure parameter is set to 'absent'
$dir_ensure = $ensure ? { 'absent' => $ensure, default => 'directory' }
$file_ensure = $ensure ? { 'absent' => $ensure, default => 'file' }
$include_dir_force = $ensure ? { 'absent' => true, default => undef }
$_include_dir_purge = $ensure ? { 'absent' => true, default => $include_dir_purge }
$_manage_repo = $ensure ? { 'absent' => false, default => $manage_repo }
$_service_enable = $ensure ? { 'absent' => false, default => $service_enable }
$_service_ensure = $ensure ? { 'absent' => 'stopped', default => $service_ensure }
$_zabbix_package_state = $ensure ? { 'absent' => $ensure, default => $zabbix_package_state }

# Find if listenip is set. If not, we can set to specific ip or
# to network name. If more than 1 interfaces are available, we
# can find the ipaddress of this specific interface if listenip
Expand Down Expand Up @@ -276,7 +288,7 @@
# Only include the repo class if it has not yet been included
unless defined(Class['Zabbix::Repo']) {
class { 'zabbix::repo':
manage_repo => $manage_repo,
manage_repo => $_manage_repo,
zabbix_version => $zabbix_version,
}
}
Expand All @@ -291,7 +303,7 @@
} else {
assert_type(Stdlib::Windowspath, $zabbix_package_source)
package { $zabbix_package_agent:
ensure => $zabbix_package_state,
ensure => $_zabbix_package_state,
tag => 'zabbix',
provider => $zabbix_package_provider,
source => $zabbix_package_source,
Expand All @@ -302,7 +314,7 @@
else {
# Installing the package
package { $zabbix_package_agent:
ensure => $zabbix_package_state,
ensure => $_zabbix_package_state,
require => Class['zabbix::repo'],
tag => 'zabbix',
provider => $zabbix_package_provider,
Expand Down Expand Up @@ -336,8 +348,8 @@

# Controlling the 'zabbix-agent' service
service { $servicename:
ensure => $service_ensure,
enable => $service_enable,
ensure => $_service_ensure,
enable => $_service_enable,
require => $service_require,
}

Expand All @@ -352,7 +364,7 @@

# Configuring the zabbix-agent configuration file
file { $agent_configfile_path:
ensure => file,
ensure => $file_ensure,
owner => $agent_config_owner,
group => $agent_config_group,
mode => '0644',
Expand All @@ -364,11 +376,12 @@

# Include dir for specific zabbix-agent checks.
file { $include_dir:
ensure => directory,
ensure => $dir_ensure,
owner => $agent_config_owner,
group => $agent_config_group,
force => $include_dir_force,
recurse => true,
purge => $include_dir_purge,
purge => $_include_dir_purge,
notify => Service[$servicename],
require => File[$agent_configfile_path],
}
Expand All @@ -394,7 +407,7 @@
# https://support.zabbix.com/browse/ZBX-11631
if fact('os.selinux.enabled') == true and $manage_selinux {
selinux::module { 'zabbix-agent':
ensure => 'present',
ensure => $ensure,
content_te => template('zabbix/selinux/zabbix-agent.te.erb'),
before => Service[$servicename],
}
Expand Down
26 changes: 26 additions & 0 deletions spec/classes/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@
it { is_expected.to contain_file(config_path).with_content %r{^Alias=name2$} }
end

context 'when declaring ensure is absent' do
let :params do
{
ensure: 'absent'
}
end

it { is_expected.to contain_package(package_name).with_ensure('absent') } if facts[:kernel] == 'Linux'

it { is_expected.to contain_class('zabbix::repo').with_manage_repo(false) }

it do
is_expected.to contain_service(service_name).
with_ensure('stopped').
with_enable(false).
that_requires("Package[#{package_name}]")
end

it do
is_expected.to contain_file(include_dir).
with_ensure('absent').
with_force(true).
with_purge(true)
end
end

context 'configuration file with full options' do
if facts[:kernel] == 'Linux'
let :params do
Expand Down
Loading