Skip to content

Commit

Permalink
[zabbix_agent] Add support for Zabbix Agent 2 (Go)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8dfd729)
  • Loading branch information
drybjed committed Dec 16, 2022
1 parent 014d15b commit a80ff13
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ Added
- The role can now download APT repository GPG keys to separate keyring files,
which can be used to scope a given GPG key to specific APT repositories.

:ref:`debops.zabbix_agent` role
'''''''''''''''''''''''''''''''

- The role now supports management of Zabbix Agent (written in C) as well as
Zabbix Agent 2 (written in Go), available in Debian repositories. Only one
flavor can be managed at a time, but role provides an easy way to switch
between the two flavors.

Changed
~~~~~~~

Expand Down
15 changes: 13 additions & 2 deletions ansible/roles/global_handlers/handlers/zabbix_agent.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
---
# Copyright (C) 2021 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2021 DebOps <https://debops.org/>
# Copyright (C) 2021-2022 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2021-2022 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-or-later

- name: Check zabbix-agent and restart
command: '/usr/sbin/zabbix_agentd --print'
when: zabbix_agent__flavor == 'C'
notify: [ 'Restart zabbix-agent' ]

- name: Check zabbix-agent2 and restart
command: '/usr/sbin/zabbix_agent2 --print'
when: zabbix_agent__flavor == 'Go'
notify: [ 'Restart zabbix-agent2' ]

- name: Restart zabbix-agent
service:
name: 'zabbix-agent'
state: 'restarted'

- name: Restart zabbix-agent2
service:
name: 'zabbix-agent2'
state: 'restarted'
56 changes: 40 additions & 16 deletions ansible/roles/zabbix_agent/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker

# .. Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# .. Copyright (C) 2021 Maciej Delmanowski <drybjed@gmail.com>
# .. Copyright (C) 2021 DebOps <https://debops.org/>
# .. Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# .. Copyright (C) 2021-2022 Maciej Delmanowski <drybjed@gmail.com>
# .. Copyright (C) 2021-2022 DebOps <https://debops.org/>
# .. SPDX-License-Identifier: GPL-3.0-only

# .. _zabbix_agent__ref_defaults:
Expand Down Expand Up @@ -45,7 +45,9 @@ zabbix_agent__deploy_state: 'present'
# .. envvar:: zabbix_agent__base_packages [[[
#
# List of APT packages to install for :command:`zabbix_agent` support.
zabbix_agent__base_packages: [ 'zabbix-agent' ]
zabbix_agent__base_packages: '{{ ["zabbix-agent"]
if (zabbix_agent__flavor == "C")
else ["zabbix-agent2"] }}'

# ]]]
# .. envvar:: zabbix_agent__packages [[[
Expand Down Expand Up @@ -107,13 +109,21 @@ zabbix_agent__additional_groups:
- '{{ (ansible_local.proc_hidepid.group|d("procadmins"))
if (ansible_local.proc_hidepid.enabled|d())|bool
else [] }}'

# ]]]
# .. envvar:: zabbix_agent__conf_file_path [[[
#
# The absolute path to the main Zabbix Agent configuration file.
zabbix_agent__conf_file_path: '{{ "/etc/zabbix/zabbix_agentd.conf"
if (zabbix_agent__flavor == "C")
else "/etc/zabbix/zabbix_agent2.conf" }}'
# ]]]
# ]]]
# Zabbix agent configuration [[[
# ------------------------------

# These variables define the contents of the
# :file:`/etc/zabbix/zabbix_agentd.conf` configuration file.
# These variables define the contents of the main Zabbix Agent configuration
# file.

# .. envvar:: zabbix_agent__tls_psk_identity [[[
#
Expand Down Expand Up @@ -141,7 +151,7 @@ zabbix_agent__original_configuration:
comment: |
Name of PID file.
value: '/var/run/zabbix/zabbix_agentd.pid'
state: 'present'
state: '{{ "present" if (zabbix_agent__flavor == "C") else "comment" }}'

- name: 'LogType'
comment: |
Expand All @@ -157,7 +167,7 @@ zabbix_agent__original_configuration:
Log file name for LogType 'file' parameter.
Mandatory: yes, if LogType is set to file, otherwise no
value: '/var/log/zabbix-agent/zabbix_agentd.log'
state: 'present'
state: '{{ "present" if (zabbix_agent__flavor == "C") else "comment" }}'

- name: 'LogFileSize'
comment: |
Expand All @@ -167,12 +177,6 @@ zabbix_agent__original_configuration:
value: 0
state: 'present'

- name: 'PidFile'
comment: |
Name of PID file.
value: '/var/run/zabbix/zabbix_agentd.pid'
state: 'present'

- name: 'DebugLevel'
comment: |
Specifies debug level:
Expand Down Expand Up @@ -359,7 +363,9 @@ zabbix_agent__original_configuration:
comment: |
You may include individual files or all files in a directory in the configuration file.
Installing Zabbix will create include directory in /etc/zabbix, unless modified during the compile time.
value: '/etc/zabbix/zabbix_agentd.conf.d/*.conf'
value: '{{ "/etc/zabbix/zabbix_agentd.conf.d/*.conf"
if (zabbix_agent__flavor == "C")
else "/etc/zabbix/zabbix_agent2.d/*.conf" }}'
state: 'present'

- name: 'UnsafeUserParameters'
Expand Down Expand Up @@ -468,6 +474,13 @@ zabbix_agent__original_configuration:
Full pathname of a file containing the pre-shared key.
value: ''
state: 'comment'

- name: 'IncludePlugins'
option: 'Include'
comment: 'Include configuration files for plugins'
value: './zabbix_agent2.d/plugins.d/*.conf'
state: '{{ "present" if zabbix_agent__flavor == "Go" else "absent" }}'

# ]]]
# .. envvar:: zabbix_agent__default_configuration [[[
#
Expand All @@ -484,7 +497,7 @@ zabbix_agent__default_configuration:

- name: 'User'
value: '{{ zabbix_agent__user }}'
state: 'present'
state: '{{ "present" if zabbix_agent__flavor == "C" else "ignore" }}'

- name: 'TLSConnect'
value: 'psk'
Expand Down Expand Up @@ -560,5 +573,16 @@ zabbix_agent__dpkg_cleanup__dependent_packages:
- '/etc/zabbix/zabbix_agentd.conf.d'
restart_services:
- 'ferm'
state: '{{ "present" if (zabbix_agent__flavor == "C") else "absent" }}'

- name: 'zabbix-agent2'
ansible_fact: 'zabbix_agent'
revert_files:
- '/etc/zabbix/zabbix_agent2.conf'
remove_files:
- '/etc/zabbix/secret.key'
restart_services:
- 'ferm'
state: '{{ "present" if (zabbix_agent__flavor == "Go") else "absent" }}'
# ]]]
# ]]]
29 changes: 16 additions & 13 deletions ansible/roles/zabbix_agent/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker

# Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# Copyright (C) 2021 DebOps <https://debops.org/>
# Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# Copyright (C) 2021-2022 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2021-2022 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

- name: Import custom Ansible plugins
Expand Down Expand Up @@ -33,26 +34,26 @@
groups: '{{ zabbix_agent__additional_groups | flatten | join(",") }}'
state: 'present'
append: True
notify: [ 'Check zabbix-agent and restart' ]
notify: [ 'Check zabbix-agent and restart', 'Check zabbix-agent2 and restart' ]
when: zabbix_agent__deploy_state != 'absent'

- name: Divert Zabbix agent configuration file
debops.debops.dpkg_divert:
path: '/etc/zabbix/zabbix_agentd.conf'
dpkg_divert:
path: '{{ zabbix_agent__conf_file_path }}'
state: 'present'
notify: [ 'Check zabbix-agent and restart' ]
notify: [ 'Check zabbix-agent and restart', 'Check zabbix-agent2 and restart' ]
when: zabbix_agent__deploy_state != 'absent'

# Zabbix agent configuration [[[1
- name: Generate Zabbix agent configuration file
template:
src: 'etc/zabbix/zabbix_agentd.conf.j2'
dest: '/etc/zabbix/zabbix_agentd.conf'
dest: '{{ zabbix_agent__conf_file_path }}'
owner: 'root'
group: '{{ zabbix_agent__group }}'
mode: '0640'
unsafe_writes: '{{ True if (core__unsafe_writes|d(ansible_local.core.unsafe_writes|d()) | bool) else omit }}'
notify: [ 'Check zabbix-agent and restart' ]
unsafe_writes: '{{ True if (core__unsafe_writes | d(ansible_local.core.unsafe_writes | d()) | bool) else omit }}'
notify: [ 'Check zabbix-agent and restart', 'Check zabbix-agent2 and restart' ]
when: zabbix_agent__deploy_state != 'absent'

- name: Install secret key
Expand All @@ -63,7 +64,7 @@
group: '{{ zabbix_agent__group }}'
mode: '0640'
no_log: '{{ debops__no_log | d(True) }}'
notify: [ 'Check zabbix-agent and restart' ]
notify: [ 'Check zabbix-agent and restart', 'Check zabbix-agent2 and restart' ]
when: zabbix_agent__deploy_state != 'absent'

- name: Ensure configuration folder exists
Expand All @@ -73,7 +74,8 @@
owner: 'root'
group: 'root'
mode: '0755'
when: zabbix_agent__deploy_state != 'absent'
when: zabbix_agent__deploy_state != 'absent' and
zabbix_agent__flavor == 'C'

- name: Ensure log folder exists
file:
Expand All @@ -82,7 +84,8 @@
owner: '{{ zabbix_agent__user }}'
group: '{{ zabbix_agent__group }}'
mode: '0755'
when: zabbix_agent__deploy_state != 'absent'
when: zabbix_agent__deploy_state != 'absent' and
zabbix_agent__flavor == 'C'

# Ansible facts [[[1
- name: Make sure that Ansible local fact directory exists
Expand All @@ -107,7 +110,7 @@
tags: [ 'meta::facts' ]

- name: Prepare cleanup during package removal
ansible.builtin.import_role:
import_role:
name: 'dpkg_cleanup'
vars:
dpkg_cleanup__dependent_packages:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{# Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# Copyright (C) 2021 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2021 DebOps <https://debops.org/>
{# Copyright (C) 2021 Julien Lecomte <julien@lecomte.at>
# Copyright (C) 2021-2022 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2021-2022 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
#}
# {{ ansible_managed }}
Expand All @@ -11,7 +11,7 @@
{% if element.name|d() and element.state|d('present') not in [ 'absent', 'ignore' ] %}

{{ element.comment | regex_replace('\n$','') | comment() }}
{% set element_comment = ('#' if element.state|d('present') == 'comment' else '') %}
{{ '{}{}={}'.format(element_comment, element.name, element.value) }}
{% set element_comment = ('#' if element.state | d('present') == 'comment' else '') %}
{{ '{}{}={}'.format(element_comment, element.option | d(element.name), element.value) }}
{% endif %}
{% endfor %}
5 changes: 5 additions & 0 deletions docs/ansible/roles/zabbix_agent/defaults-detailed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ used, can be defined using specific parameters:
Multiple entries with the same ``name`` parameter are merged together in
order of appearance and can affect each other.

``option``
Optional. Name of the configuration option, which will be used instead of the
``name`` parameter. This is useful when a Zabbix Agent option can be present
multiple times in the configuration file.

``value``
The value of a given configuration option. It can be a string, a number,
or a boolean value - this is used to specify selected
Expand Down

0 comments on commit a80ff13

Please sign in to comment.