Skip to content

Commit

Permalink
Bootstrap agent2 support
Browse files Browse the repository at this point in the history
* Add param binary_location for specify zabbix agent binary (only for
  systemd)
* Remove some parameters in zabbix agent configuration if it's zabbix
  agent2 package
    * LogRemoteCommands
    * StartAgents
    * MaxLinesPerSecond
    * AllowRoot
    * User
    * LoadModulePath
* Fix EnableRemoteCommands still use on zabbix agentd > 5 but remove on
  agent2

Conflicts:
	manifests/agent.pp
	spec/classes/agent_spec.rb
	templates/zabbix_agentd.conf.erb
  • Loading branch information
fe80 authored and Valantin committed Mar 7, 2024
1 parent db01009 commit f7ff08d
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 12 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
5. [Usage - Configuration options and additional functionality](#usage)
* [zabbix-server](#usage-zabbix-server)
* [zabbix-agent](#usage-zabbix-agent)
* [zabbix-agent2](#usage-zabbix-agent2)
* [zabbix-proxy](#usage-zabbix-proxy)
* [zabbix-javagateway](#usage-zabbix-javagateway)
* [zabbix-sender](#usage-zabbix-sender)
Expand Down Expand Up @@ -200,6 +201,21 @@ class { 'zabbix::agent':
}
```

### Usage zabbix-agent2

To use Zabbix agent2 you need to define some parameters

```ruby
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
}
```

### Usage zabbix-proxy

Like the zabbix-server, the zabbix-proxy can also be used in 2 ways:
Expand Down
32 changes: 32 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,20 @@ class { 'zabbix::agent':
}
```

##### Using Zabbix Agent 2

```puppet
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
agent_include => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
}
```

#### Parameters

The following parameters are available in the `zabbix::agent` class:
Expand Down Expand Up @@ -1427,6 +1441,7 @@ The following parameters are available in the `zabbix::agent` class:
* [`userparameter`](#-zabbix--agent--userparameter)
* [`loadmodulepath`](#-zabbix--agent--loadmodulepath)
* [`loadmodule`](#-zabbix--agent--loadmodule)
* [`binary_location`](#-zabbix--agent--binary_location)
* [`manage_startup_script`](#-zabbix--agent--manage_startup_script)

##### <a name="-zabbix--agent--zabbix_version"></a>`zabbix_version`
Expand Down Expand Up @@ -2099,6 +2114,14 @@ Module to load at agent startup.

Default value: `$zabbix::params::agent_loadmodule`

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

Data type: `Optional[Stdlib::Absolutepath]`

Location of th binary file, this feature is available only for systemd startup script

Default value: `$zabbix::params::agent_binary_location`

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

Data type: `Boolean`
Expand Down Expand Up @@ -5719,6 +5742,7 @@ The following parameters are available in the `zabbix::startup` defined type:
* [`service_type`](#-zabbix--startup--service_type)
* [`manage_database`](#-zabbix--startup--manage_database)
* [`service_name`](#-zabbix--startup--service_name)
* [`binary_location`](#-zabbix--startup--binary_location)

##### <a name="-zabbix--startup--pidfile"></a>`pidfile`

Expand Down Expand Up @@ -5794,6 +5818,14 @@ Name of the service. Defaults to the resource name

Default value: `$name`

##### <a name="-zabbix--startup--binary_location"></a>`binary_location`

Data type: `Optional[Stdlib::Absolutepath]`

This params is use for define a specific binary location. This is actually only available for zabbix agent and systemd

Default value: `undef`

### <a name="zabbix--template"></a>`zabbix::template`

This will upload an Zabbix Template (XML format)
Expand Down
17 changes: 17 additions & 0 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
# @param userparameter User-defined parameter to monitor.
# @param loadmodulepath Full path to location of agent modules.
# @param loadmodule Module to load at agent startup.
# @param binary_location Location of th binary file, this feature is available only for systemd startup script
# @param manage_startup_script
# If the init script should be managed by this module. Attention: This might
# cause problems with some config options of this module (e.g
Expand All @@ -134,6 +135,18 @@
# monitored_by_proxy => 'my_proxy_host',
# server => '192.168.1.1',
# }
#
# @example Using Zabbix Agent 2
# class { 'zabbix::agent':
# package_name => 'zabbix-agent2',
# agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
# agent_include => '/etc/zabbix/zabbix_agent2.d',
# pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
# servicename => 'zabbix-agent2',
# zabbix_package_agent => 'zabbix-agent2',
# binary_location => '/usr/sbin/zabbix_agent2'
# }
#
# @author Werner Dijkerman [email protected]
class zabbix::agent (
$zabbix_version = $zabbix::params::zabbix_version,
Expand Down Expand Up @@ -218,7 +231,10 @@
String $additional_service_params = $zabbix::params::additional_service_params,
String $service_type = $zabbix::params::service_type,
Boolean $manage_startup_script = $zabbix::params::manage_startup_script,
Optional[Stdlib::Absolutepath] $binary_location = $zabbix::params::agent_binary_location,
) inherits zabbix::params {
$agent2 = $zabbix_package_agent == 'zabbix-agent2'

# 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 @@ -305,6 +321,7 @@
additional_service_params => $additional_service_params,
service_type => $service_type,
service_name => 'zabbix-agent',
binary_location => $binary_location,
require => Package[$zabbix_package_agent],
}
}
Expand Down
3 changes: 3 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,21 @@
# @param saml_sp_cert The location of the SAML Service Provider Certificate.
# @param saml_idp_cert The location of the SAML Identity Provider Certificate.
# @param saml_settings A hash of additional SAML SSO settings.
#
# @example Single host setup:
# class { 'zabbix':
# zabbix_url => 'zabbix.dj-wasabi.nl',
# }
#
#
# @example This assumes that you want to use the postgresql database. If not and you want to supply your own database crendentials:
# class { 'zabbix':
# zabbix_url => 'zabbix.dj-wasabi.nl',
# database_type => 'mysql',
# database_user => 'zabbix',
# database_password => 'ThisIsVeryDifficult.nl',
# }
#
# @author Werner Dijkerman [email protected]
class zabbix (
$zabbix_url = '',
Expand Down
4 changes: 4 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@
$server_nodeaddress = undef

# Agent specific params
$agent_binary_location = $facts['kernel'] ? {
'Linux' => '/usr/sbin/zabbix_agentd',
default => undef,
}
$agent_allowroot = '0'
$agent_buffersend = '5'
$agent_buffersize = '100'
Expand Down
6 changes: 5 additions & 1 deletion manifests/startup.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# @param service_type Systemd service type
# @param manage_database When true, it will configure the database and execute the sql scripts.
# @param service_name Name of the service. Defaults to the resource name
# @param binary_location This params is use for define a specific binary location. This is actually only available for zabbix agent and systemd
# @example
# zabbix::startup { 'agent': }
#
Expand All @@ -27,6 +28,7 @@
String $service_type = 'simple',
Optional[Boolean] $manage_database = undef,
Optional[String] $service_name = $name,
Optional[Stdlib::Absolutepath] $binary_location = undef,
) {
case $title.downcase {
/agent/: {
Expand All @@ -41,8 +43,10 @@
fail('we currently only support a title that contains agent or server')
}
}
# provided by camp2camp/systemd
if $facts['systemd'] {
if $name =~ /^zabbix-agent2?$/ {
assert_type(Stdlib::Absolutepath, $binary_location)
}
contain systemd
systemd::unit_file { "${name}.service":
content => template("zabbix/${service_name}-systemd.init.erb"),
Expand Down
116 changes: 116 additions & 0 deletions spec/acceptance/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,120 @@ class { 'zabbix::agent':
end
end
end

describe "zabbix::agent class with agent2 and zabbix_version #{version}" do
# <6.0 agent2 packages are not available for ubuntu 22.04
next if zabbix_version < '6.0' && default[:platform] =~ %r{ubuntu-22}

it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
agent_include => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
server => '192.168.20.11',
zabbix_package_state => 'latest',
zabbix_version => '#{version}',
}
EOS

prepare_host

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

# do some basic checks
describe package('zabbix-agent2') do
it { is_expected.to be_installed }
end

describe service('zabbix-agent2') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end

describe file('/etc/zabbix/zabbix_agentd2.conf') do
its(:content) { is_expected.not_to match %r{ListenIP=} }
end

context 'With ListenIP set to an IP-Address' do
it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
agent_include => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe file('/etc/zabbix/zabbix_agent2.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
end

context 'With ListenIP set to lo' do
it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
agent_include => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => 'lo',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

context 'With ListenIP set to an IP-Address' do
it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
package_name => 'zabbix-agent2',
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
agent_include => '/etc/zabbix/zabbix_agent2.d',
pidfile => '/var/run/zabbix/zabbix_agentd2.pid',
servicename => 'zabbix-agent2',
zabbix_package_agent => 'zabbix-agent2',
binary_location => '/usr/sbin/zabbix_agent2'
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe file('/etc/zabbix/zabbix_agent2.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
end
end
end
end
30 changes: 30 additions & 0 deletions spec/classes/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,36 @@
it { is_expected.to contain_systemd__unit_file('zabbix-agent.service') }
end
end

context 'when binary_location is define', if: facts[:kernel] == 'Linux' do
it do
is_expected.to contain_zabbix__startup(service_name).with(
binary_location: '/usr/sbin/zabbix_agentd'
)
end
end

context 'when zabbix_package_agent is zabbix-agent2' do
let :params do
{
zabbix_package_agent: 'zabbix-agent2', startagents: 1,
maxlinespersecond: 1, allowroot: 1, zabbix_user: 'root',
loadmodulepath: '/tmp', allowkey: 'system.run[*]',
denykey: 'system.run[*]', enableremotecommands: 1,
logremotecommands: 1
}
end

it { is_expected.to contain_package('zabbix-agent2') }

it do
is_expected.not_to contain_file(config_path).with_content(
%r{^(LogRemoteCommands|StartAgents|MaxLinesPerSecond
|AllowRoot|User|LoadModulePath|AllowKey|DenyKey|
EnableRemoteCommands|LogRemoteCommands)}
)
end
end
end
end
end
4 changes: 1 addition & 3 deletions spec/classes/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
next if facts[:os]['name'] == 'windows'

context "on #{os}" do
let :facts do
facts
end
let(:facts) { facts }

let :pre_condition do
<<-EOS
Expand Down
3 changes: 2 additions & 1 deletion spec/defines/startup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
{
agent_configfile_path: '/something',
pidfile: '/somethingelse',
additional_service_params: '--foreground'
additional_service_params: '--foreground',
binary_location: '/usr/sbin/zabbix_agentd'
}
end

Expand Down
2 changes: 1 addition & 1 deletion templates/zabbix-agent-systemd.init.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ After=network.target
Restart=on-failure
<% if @pidfile %>PIDFile=<%= @pidfile %><% end %>
KillMode=control-group
ExecStart=/usr/sbin/zabbix_agentd <%= @additional_service_params %> -c <%= @agent_configfile_path %>
ExecStart=<%= @binary_location %> <%= @additional_service_params %> -c <%= @agent_configfile_path %>
<% if @zabbix_user %>User=<%= @zabbix_user %><% end %>
RestartSec=10s

Expand Down
Loading

0 comments on commit f7ff08d

Please sign in to comment.