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 Phil Friderici committed Nov 13, 2023
1 parent d83836e commit 2f9c5f9
Show file tree
Hide file tree
Showing 11 changed files with 127 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
31 changes: 31 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ class { 'zabbix':
}
```

##### Using Zabbix Agent 2

```puppet
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'
}
```

#### Parameters

The following parameters are available in the `zabbix` class:
Expand Down Expand Up @@ -1418,6 +1431,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 @@ -2090,6 +2104,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 @@ -5637,6 +5659,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 @@ -5712,6 +5735,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
5 changes: 5 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 Down Expand Up @@ -218,7 +219,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 +309,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
13 changes: 13 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,31 @@
# @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',
# }
#
# @example Using Zabbix Agent 2
# 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'
# }
#
# @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 @@ -292,6 +292,10 @@
$server_socketdir = 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
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
25 changes: 19 additions & 6 deletions templates/zabbix_agentd.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ DebugLevel=<%= @debuglevel %>
#
<% if @sourceip %>SourceIP=<%= @sourceip %><% end %>
<% unless @agent2 %>
<% if @zabbix_version.to_f >= 5.0 %>
### Option: AllowKey
# Allow execution of item keys matching pattern.
# Multiple keys matching rules may be defined in combination with DenyKey.
Expand All @@ -57,7 +59,7 @@ DebugLevel=<%= @debuglevel %>
# If no AllowKey or DenyKey rules defined, all keys are allowed.
#
# Mandatory: no
<% if @allowkey %>AllowKey=<%= @allowkey -%><% end %>
<% if @allowkey %>AllowKey=<%= @allowkey -%><% end %>

### Option: DenyKey
# Deny execution of items keys matching pattern.
Expand All @@ -70,14 +72,23 @@ DebugLevel=<%= @debuglevel %>
#
# Mandatory: no
# Default:
<% if @denykey %>DenyKey=<%= @denykey -%><% end %>
<% if @denykey %>DenyKey=<%= @denykey -%><% end %>
<% end %>

### Option: EnableRemoteCommands
# Whether remote commands from Zabbix server are allowed.
# 0 - not allowed
# 1 - allowed
#
EnableRemoteCommands=<%= @enableremotecommands %>

### Option: LogRemoteCommands
# Enable logging of executed shell commands as warnings.
# 0 - disabled
# 1 - enabled
#
LogRemoteCommands=<%= @logremotecommands %>
<% end %>


##### Passive checks related
Expand All @@ -100,11 +111,13 @@ ListenPort=<%= @listenport %>
#
<% if @listen_ip %>ListenIP=<%= @listen_ip %><% end %>
<% unless @agent2 %>
### Option: StartAgents
# Number of pre-forked instances of zabbix_agentd that process passive checks.
# If set to 0, disables passive checks and the agent will not listen on any TCP port.
#
StartAgents=<%= @startagents %>
<% end %>

##### Active checks related

Expand Down Expand Up @@ -181,13 +194,15 @@ BufferSend=<%= @buffersend %>
#
BufferSize=<%= @buffersize %>
<% unless @agent2 %>
### Option: MaxLinesPerSecond
# Maximum number of new lines the agent will send per second to Zabbix Server
# or Proxy processing 'log' and 'logrt' active checks.
# The provided value will be overridden by the parameter 'maxlines',
# provided in 'log' or 'logrt' item keys.
#
MaxLinesPerSecond=<%= @maxlinespersecond %>
<% end %>

############ ADVANCED PARAMETERS #################

Expand All @@ -211,7 +226,7 @@ MaxLinesPerSecond=<%= @maxlinespersecond %>
#
Timeout=<%= @timeout %>
<% if @kernel != 'windows' %>
<% if @kernel != 'windows' and !@agent2 %>
### Option: AllowRoot
# Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent
# will try to switch to the user specified by the User configuration option instead.
Expand All @@ -220,9 +235,7 @@ Timeout=<%= @timeout %>
# 1 - allow
#
AllowRoot=<%= @allowroot %>
<% end %>

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

####### LOADABLE MODULES #######
<% if @kernel != 'windows' %>
<% if @kernel != 'windows' and !@agent2 %>
### Option: LoadModulePath
# Full path to location of agent modules.
# Default depends on compilation options.
Expand Down

0 comments on commit 2f9c5f9

Please sign in to comment.