Skip to content

Commit fde4be3

Browse files
fe80Valantin
authored andcommitted
Bootstrap agent2 support
* 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 spec/acceptance/agent_spec.rb templates/zabbix_agentd.conf.erb
1 parent 53f5969 commit fde4be3

File tree

7 files changed

+214
-36
lines changed

7 files changed

+214
-36
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
5. [Usage - Configuration options and additional functionality](#usage)
2323
* [zabbix-server](#usage-zabbix-server)
2424
* [zabbix-agent](#usage-zabbix-agent)
25+
* [zabbix-agent2](#usage-zabbix-agent2)
2526
* [zabbix-proxy](#usage-zabbix-proxy)
2627
* [zabbix-javagateway](#usage-zabbix-javagateway)
2728
* [zabbix-sender](#usage-zabbix-sender)
@@ -200,6 +201,21 @@ class { 'zabbix::agent':
200201
}
201202
```
202203

204+
### Usage zabbix-agent2
205+
206+
Basic one way of setup, wheter it is monitored by zabbix-server or zabbix-proxy:
207+
```ruby
208+
class { 'zabbix::agent':
209+
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
210+
include_dir => '/etc/zabbix/zabbix_agent2.d',
211+
include_dir_purge => false,
212+
zabbix_package_agent => 'zabbix-agent2',
213+
servicename => 'zabbix-agent2',
214+
manage_startup_script => false,
215+
server => '192.168.20.11',
216+
}
217+
```
218+
203219
### Usage zabbix-proxy
204220

205221
Like the zabbix-server, the zabbix-proxy can also be used in 2 ways:

REFERENCE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,19 @@ class { 'zabbix::agent':
13421342
}
13431343
```
13441344

1345+
##### Using Zabbix Agent 2
1346+
1347+
```puppet
1348+
class { 'zabbix::agent':
1349+
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
1350+
include_dir => '/etc/zabbix/zabbix_agent2.d',
1351+
include_dir_purge => false,
1352+
zabbix_package_agent => 'zabbix-agent2',
1353+
servicename => 'zabbix-agent2',
1354+
manage_startup_script => false,
1355+
}
1356+
```
1357+
13451358
#### Parameters
13461359

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

manifests/agent.pp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@
134134
# monitored_by_proxy => 'my_proxy_host',
135135
# server => '192.168.1.1',
136136
# }
137+
#
138+
# @example Using Zabbix Agent 2
139+
# class { 'zabbix::agent':
140+
# agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
141+
# include_dir => '/etc/zabbix/zabbix_agent2.d',
142+
# include_dir_purge => false,
143+
# zabbix_package_agent => 'zabbix-agent2',
144+
# servicename => 'zabbix-agent2',
145+
# manage_startup_script => false,
146+
# }
147+
#
137148
# @author Werner Dijkerman [email protected]
138149
class zabbix::agent (
139150
$zabbix_version = $zabbix::params::zabbix_version,
@@ -219,6 +230,8 @@
219230
String $service_type = $zabbix::params::service_type,
220231
Boolean $manage_startup_script = $zabbix::params::manage_startup_script,
221232
) inherits zabbix::params {
233+
$agent2 = $zabbix_package_agent == 'zabbix-agent2'
234+
222235
# Find if listenip is set. If not, we can set to specific ip or
223236
# to network name. If more than 1 interfaces are available, we
224237
# can find the ipaddress of this specific interface if listenip

spec/acceptance/agent_spec.rb

Lines changed: 118 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,36 @@
77
next if version < '6.0' && default[:platform] =~ %r{debian-12}
88

99
describe "zabbix::agent class with zabbix_version #{version}" do
10-
it 'works idempotently with no errors' do
11-
pp = <<-EOS
12-
class { 'zabbix::agent':
13-
server => '192.168.20.11',
14-
zabbix_package_state => 'latest',
15-
zabbix_version => '#{version}',
16-
}
17-
EOS
10+
context 'With minimal parameter' do
11+
it 'works idempotently with no errors' do
12+
pp = <<-EOS
13+
class { 'zabbix::agent':
14+
server => '192.168.20.11',
15+
zabbix_package_state => 'latest',
16+
zabbix_version => '#{version}',
17+
}
18+
EOS
1819

19-
prepare_host
20+
prepare_host
2021

21-
# Run it twice and test for idempotency
22-
apply_manifest(pp, catch_failures: true)
23-
apply_manifest(pp, catch_changes: true)
24-
end
22+
# Run it twice and test for idempotency
23+
apply_manifest(pp, catch_failures: true)
24+
apply_manifest(pp, catch_changes: true)
25+
end
2526

26-
# do some basic checks
27-
describe package('zabbix-agent') do
28-
it { is_expected.to be_installed }
29-
end
27+
# do some basic checks
28+
describe package('zabbix-agent') do
29+
it { is_expected.to be_installed }
30+
end
3031

31-
describe service('zabbix-agent') do
32-
it { is_expected.to be_running }
33-
it { is_expected.to be_enabled }
34-
end
32+
describe service('zabbix-agent') do
33+
it { is_expected.to be_running }
34+
it { is_expected.to be_enabled }
35+
end
3536

36-
describe file('/etc/zabbix/zabbix_agentd.conf') do
37-
its(:content) { is_expected.not_to match %r{ListenIP=} }
37+
describe file('/etc/zabbix/zabbix_agentd.conf') do
38+
its(:content) { is_expected.not_to match %r{ListenIP=} }
39+
end
3840
end
3941

4042
context 'With ListenIP set to an IP-Address' do
@@ -90,4 +92,97 @@ class { 'zabbix::agent':
9092
end
9193
end
9294
end
95+
96+
describe "zabbix::agent class with agent2 and zabbix_version #{version}" do
97+
# <6.0 agent2 packages are not available for ubuntu 22.04
98+
next if version < '6.0' && default[:platform] =~ %r{ubuntu-22}
99+
100+
before(:all) do
101+
prepare_host
102+
end
103+
104+
context 'With minimal parameter' do
105+
it_behaves_like 'an idempotent resource' do
106+
let(:manifest) do
107+
<<-PUPPET
108+
class { 'zabbix::agent':
109+
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
110+
include_dir => '/etc/zabbix/zabbix_agent2.d',
111+
include_dir_purge => false,
112+
zabbix_package_agent => 'zabbix-agent2',
113+
servicename => 'zabbix-agent2',
114+
manage_startup_script => false,
115+
server => '192.168.20.11',
116+
zabbix_package_state => 'latest',
117+
zabbix_version => '#{version}',
118+
}
119+
PUPPET
120+
end
121+
end
122+
123+
# do some basic checks
124+
describe package('zabbix-agent2') do
125+
it { is_expected.to be_installed }
126+
end
127+
128+
describe service('zabbix-agent2') do
129+
it { is_expected.to be_running }
130+
it { is_expected.to be_enabled }
131+
end
132+
133+
describe file('/etc/zabbix/zabbix_agentd2.conf') do
134+
its(:content) { is_expected.not_to match %r{ListenIP=} }
135+
end
136+
end
137+
138+
context 'With ListenIP set to an IP-Address' do
139+
it_behaves_like 'an idempotent resource' do
140+
let(:manifest) do
141+
<<-PUPPET
142+
class { 'zabbix::agent':
143+
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
144+
include_dir => '/etc/zabbix/zabbix_agent2.d',
145+
include_dir_purge => false,
146+
zabbix_package_agent => 'zabbix-agent2',
147+
servicename => 'zabbix-agent2',
148+
manage_startup_script => false,
149+
server => '192.168.20.11',
150+
zabbix_package_state => 'latest',
151+
listenip => '127.0.0.1',
152+
zabbix_version => '#{version}',
153+
}
154+
PUPPET
155+
end
156+
end
157+
158+
describe file('/etc/zabbix/zabbix_agent2.conf') do
159+
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
160+
end
161+
end
162+
163+
context 'With ListenIP set to lo' do
164+
it_behaves_like 'an idempotent resource' do
165+
let(:manifest) do
166+
<<-PUPPET
167+
class { 'zabbix::agent':
168+
agent_configfile_path => '/etc/zabbix/zabbix_agent2.conf',
169+
include_dir => '/etc/zabbix/zabbix_agent2.d',
170+
include_dir_purge => false,
171+
zabbix_package_agent => 'zabbix-agent2',
172+
servicename => 'zabbix-agent2',
173+
manage_startup_script => false,
174+
server => '192.168.20.11',
175+
zabbix_package_state => 'latest',
176+
listenip => 'lo',
177+
zabbix_version => '#{version}',
178+
}
179+
PUPPET
180+
end
181+
182+
describe file('/etc/zabbix/zabbix_agent2.conf') do
183+
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
184+
end
185+
end
186+
end
187+
end
93188
end

spec/classes/agent_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,28 @@
445445
it { is_expected.to contain_systemd__unit_file('zabbix-agent.service') }
446446
end
447447
end
448+
449+
context 'when zabbix_package_agent is zabbix-agent2' do
450+
let :params do
451+
{
452+
zabbix_package_agent: 'zabbix-agent2', startagents: 1,
453+
maxlinespersecond: 1, allowroot: 1, zabbix_user: 'root',
454+
loadmodulepath: '/tmp', allowkey: 'system.run[*]',
455+
denykey: 'system.run[*]', enableremotecommands: 1,
456+
logremotecommands: 1
457+
}
458+
end
459+
460+
it { is_expected.to contain_package('zabbix-agent2') }
461+
462+
it do
463+
is_expected.not_to contain_file(config_path).with_content(
464+
%r{^(LogRemoteCommands|StartAgents|MaxLinesPerSecond
465+
|AllowRoot|User|LoadModulePath|AllowKey|DenyKey|
466+
EnableRemoteCommands|LogRemoteCommands)}
467+
)
468+
end
469+
end
448470
end
449471
end
450472
end

spec/support/acceptance/prepare_host.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ def prepare_host
55

66
apply_manifest <<~PUPPET
77
$services = $facts['os']['family'] ? {
8-
'RedHat' => ['zabbix-server', 'httpd'],
9-
'Debian' => ['zabbix-server', 'apache2'],
10-
default => [],
8+
'RedHat' => ['zabbix-server', 'httpd', 'zabbix-agentd', 'zabbix-agent', 'zabbix-agent2'],
9+
'Debian' => ['zabbix-server', 'apache2', 'zabbix-agentd', 'zabbix-agent', 'zabbix-agent2'],
10+
default => ['zabbix-agentd', 'zabbix-agent', 'zabbix-agent2'],
1111
}
1212
service { $services:
1313
ensure => stopped
1414
}
1515
1616
$packages = $facts['os']['family'] ? {
17-
'RedHat' => ['zabbix-server-pgsql', 'zabbix-server-pgsql-scl', 'zabbix-web', 'zabbix-web-pgsql', 'zabbix-web-pgsql-scl', 'zabbix-frontend-php', 'zabbix-sql-scripts'],
18-
'Debian' => ['zabbix-server-pgsql', 'zabbix-web-pgsql', 'zabbix-frontend-php', 'zabbix-sql-scripts'],
19-
default => [],
17+
'RedHat' => ['zabbix-server-pgsql', 'zabbix-server-pgsql-scl', 'zabbix-web', 'zabbix-web-pgsql', 'zabbix-web-pgsql-scl', 'zabbix-frontend-php', 'zabbix-sql-scripts', 'zabbix-agent', 'zabbix-agent2'],
18+
'Debian' => ['zabbix-server-pgsql', 'zabbix-web-pgsql', 'zabbix-frontend-php', 'zabbix-sql-scripts', 'zabbix-agent', 'zabbix-agent2'],
19+
default => ['zabbix-agent', 'zabbix-agent2'],
2020
}
21+
22+
$pkg_ensure = $facts['os']['family'] ? {
23+
'Archlinux' => absent,
24+
default => purged,
25+
}
26+
2127
package { $packages:
22-
ensure => purged
28+
ensure => $pkg_ensure
2329
}
2430
PUPPET
2531

templates/zabbix_agentd.conf.erb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ DebugLevel=<%= @debuglevel %>
4848
#
4949
<% if @sourceip %>SourceIP=<%= @sourceip %><% end %>
5050

51+
<% unless @agent2 %>
52+
<% if @zabbix_version.to_f >= 5.0 %>
5153
### Option: AllowKey
5254
# Allow execution of item keys matching pattern.
5355
# Multiple keys matching rules may be defined in combination with DenyKey.
@@ -57,7 +59,7 @@ DebugLevel=<%= @debuglevel %>
5759
# If no AllowKey or DenyKey rules defined, all keys are allowed.
5860
#
5961
# Mandatory: no
60-
<% if @allowkey %>AllowKey=<%= @allowkey -%><% end %>
62+
<% if @allowkey %>AllowKey=<%= @allowkey -%><% end %>
6163

6264
### Option: DenyKey
6365
# Deny execution of items keys matching pattern.
@@ -70,14 +72,23 @@ DebugLevel=<%= @debuglevel %>
7072
#
7173
# Mandatory: no
7274
# Default:
73-
<% if @denykey %>DenyKey=<%= @denykey -%><% end %>
75+
<% if @denykey %>DenyKey=<%= @denykey -%><% end %>
76+
<% end %>
77+
78+
### Option: EnableRemoteCommands
79+
# Whether remote commands from Zabbix server are allowed.
80+
# 0 - not allowed
81+
# 1 - allowed
82+
#
83+
EnableRemoteCommands=<%= @enableremotecommands %>
7484

7585
### Option: LogRemoteCommands
7686
# Enable logging of executed shell commands as warnings.
7787
# 0 - disabled
7888
# 1 - enabled
7989
#
8090
LogRemoteCommands=<%= @logremotecommands %>
91+
<% end %>
8192

8293

8394
##### Passive checks related
@@ -100,11 +111,13 @@ ListenPort=<%= @listenport %>
100111
#
101112
<% if @listen_ip %>ListenIP=<%= @listen_ip %><% end %>
102113

114+
<% unless @agent2 %>
103115
### Option: StartAgents
104116
# Number of pre-forked instances of zabbix_agentd that process passive checks.
105117
# If set to 0, disables passive checks and the agent will not listen on any TCP port.
106118
#
107119
StartAgents=<%= @startagents %>
120+
<% end %>
108121

109122
##### Active checks related
110123

@@ -181,13 +194,15 @@ BufferSend=<%= @buffersend %>
181194
#
182195
BufferSize=<%= @buffersize %>
183196

197+
<% unless @agent2 %>
184198
### Option: MaxLinesPerSecond
185199
# Maximum number of new lines the agent will send per second to Zabbix Server
186200
# or Proxy processing 'log' and 'logrt' active checks.
187201
# The provided value will be overridden by the parameter 'maxlines',
188202
# provided in 'log' or 'logrt' item keys.
189203
#
190204
MaxLinesPerSecond=<%= @maxlinespersecond %>
205+
<% end %>
191206

192207
############ ADVANCED PARAMETERS #################
193208

@@ -211,7 +226,7 @@ MaxLinesPerSecond=<%= @maxlinespersecond %>
211226
#
212227
Timeout=<%= @timeout %>
213228

214-
<% if @kernel != 'windows' %>
229+
<% if @kernel != 'windows' and !@agent2 %>
215230
### Option: AllowRoot
216231
# Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent
217232
# will try to switch to the user specified by the User configuration option instead.
@@ -220,9 +235,7 @@ Timeout=<%= @timeout %>
220235
# 1 - allow
221236
#
222237
AllowRoot=<%= @allowroot %>
223-
<% end %>
224238

225-
<% if @kernel != 'windows' %>
226239
### Option: User
227240
# Drop privileges to a specific, existing user on the system.
228241
# Only has effect if run as 'root' and AllowRoot is disabled.
@@ -257,7 +270,7 @@ UnsafeUserParameters=<%= @unsafeuserparameters %>
257270
# Disabled. A configuration file should be placed on directory: <%= @include %>
258271

259272
####### LOADABLE MODULES #######
260-
<% if @kernel != 'windows' %>
273+
<% if @kernel != 'windows' and !@agent2 %>
261274
### Option: LoadModulePath
262275
# Full path to location of agent modules.
263276
# Default depends on compilation options.

0 commit comments

Comments
 (0)