Skip to content

Commit

Permalink
Rework zabbix agent2 (windows) (#433)
Browse files Browse the repository at this point in the history
* Rework zabbix agent2 (windows)
  • Loading branch information
david-sieg authored Jul 29, 2021
1 parent a5df477 commit 763d892
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/ZABBIX_AGENT_ROLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ _Supporting Windows is a best effort (I don't have the possibility to either tes
* `zabbix_win_download_link`: The download url to the `win.zip` file.
* `zabbix_win_install_dir`: The directory where Zabbix needs to be installed.
* `zabbix_agent_win_logfile`: The full path to the logfile for the Zabbix Agent.
* `zabbix_agent_win_include`: The directory in which the Zabbix specific configuration files are stored.
* `zabbix_agent_win_include`: The directory in which the Zabbix Agent specific configuration files are stored.
* `zabbix_agent_win_svc_recovery`: Enable Zabbix Agent service auto-recovery settings.

## macOS Variables
Expand Down
2 changes: 1 addition & 1 deletion roles/zabbix_agent/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

- name: restart win zabbix agent
win_service:
name: "{{ zabbix_win_agent_service }}"
name: "{{ zabbix_win_svc_name }}"
state: restarted
when:
- zabbix_agent_os_family == "Windows"
Expand Down
63 changes: 53 additions & 10 deletions roles/zabbix_agent/tasks/Windows.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

- name: "Set default ip address for zabbix_agent_ip"
set_fact:
zabbix_agent_ip: "{{ hostvars[inventory_hostname]['ansible_ip_addresses'][0] }}"
Expand All @@ -21,13 +20,20 @@
set_fact:
zabbix_win_exe_path: '{{ zabbix_win_install_dir }}\bin\win{{ windows_arch }}\zabbix_agentd.exe'

- name: "Windows | Set variables specific to Zabbix 4.0"
- name: "Windows | Set variables specific to Zabbix >= 4"
set_fact:
zabbix_win_package: zabbix_agent-{{ zabbix_version_long }}-windows-amd64.zip
zabbix_win_svc_name: Zabbix Agent
zabbix_win_exe_path: '{{ zabbix_win_install_dir }}\bin\zabbix_agentd.exe'
when:
- zabbix_version_long is version('4.0.0', '>=')

- name: "Windows | Set variables specific to Zabbix (agent 2)"
set_fact:
zabbix_win_svc_name: Zabbix Agent 2
zabbix_win_exe_path: '{{ zabbix_win_install_dir }}\bin\zabbix_agent2.exe'
when:
- zabbix_agent2 | bool

- name: "Windows | Check if Zabbix agent is present"
win_stat:
path: '{{ zabbix_win_exe_path }}'
Expand All @@ -54,9 +60,9 @@
- zabbix_win_exe_info.win_file_version.product_version is version(zabbix_version_long, '<')
- zabbix_agent_package_state == 'latest'

- name: Windows | Check Zabbix service
- name: "Windows | Check Zabbix service"
win_service:
name: Zabbix Agent
name: "{{ zabbix_win_svc_name }}"
register: zabbix_service_info

- name: Windows | Check firewall service
Expand All @@ -66,18 +72,28 @@

- name: "Windows | Stop Zabbix (Update)"
win_service:
name: Zabbix Agent
name: "{{ zabbix_win_svc_name }}"
start_mode: auto
state: stopped
when:
- zabbix_service_info.exists
- update_zabbix_agent | default(False) | bool
- zabbix_service_info.state == 'running'

- name: "Windows | Uninstall Zabbix (Update)"
win_command: '"{{ zabbix_win_exe_path }}" --config "{{ zabbix_win_install_dir }}\zabbix_agentd.conf" --uninstall'
register: zabbix_windows_install
when:
- zabbix_service_info.exists
- update_zabbix_agent | default(False) | bool

- name: "Windows | Uninstall Zabbix (Update) (agent 2)"
win_command: '"{{ zabbix_win_exe_path }}" --config "{{ zabbix_win_install_dir }}\zabbix_agent2.conf" --uninstall'
register: zabbix_windows_install
when:
- zabbix_service_info.exists
- update_zabbix_agent | default(False) | bool
- zabbix_agent2 | bool

- name: "Windows | Removing Zabbix Directory (Update)"
win_file:
Expand Down Expand Up @@ -140,17 +156,44 @@
dest: "{{ zabbix_win_install_dir }}"
creates: '{{ zabbix_win_exe_path }}'

- name: "Windows | Cleanup downloaded Zabbix Agent Zip file"
win_file:
path: '{{ zabbix_win_install_dir }}\{{ zabbix_win_package }}'
state: absent
when:
- zabbix_agent_win_download_zip.changed

- name: "Windows | Configure zabbix-agent"
win_template:
src: zabbix_agentd.conf.j2
dest: '{{ zabbix_win_install_dir }}\zabbix_agentd.conf'
notify: restart win zabbix agent
when:
- not zabbix_agent2

- name: "Windows | Configure zabbix-agent2"
win_template:
src: zabbix_agent2.conf.j2
dest: '{{ zabbix_win_install_dir }}\zabbix_agent2.conf'
notify: restart win zabbix agent
when:
- zabbix_agent2 | bool

- name: "Windows | Register Service"
win_command: '"{{ zabbix_win_exe_path }}" --config "{{ zabbix_win_install_dir }}\zabbix_agentd.conf" --install'
register: zabbix_windows_install
args:
creates: '{{ zabbix_win_install_dir }}\.installed'
when:
- not zabbix_agent2

- name: "Windows | Register Service (agent 2)"
win_command: '"{{ zabbix_win_exe_path }}" --config "{{ zabbix_win_install_dir }}\zabbix_agent2.conf" --install'
register: zabbix_windows_install
args:
creates: '{{ zabbix_win_install_dir }}\.installed'
when:
- zabbix_agent2 | bool

- name: "Windows | Create done file so it won't register itself again"
win_file:
Expand All @@ -160,26 +203,26 @@

- name: "Windows | Set service startup mode to auto and ensure it is started"
win_service:
name: Zabbix Agent
name: "{{ zabbix_win_svc_name }}"
start_mode: auto
state: started

- name: "Windows | Getting Zabbix Service Recovery Settings"
win_shell: sc.exe qfailure "Zabbix Agent" 1100
win_shell: 'sc.exe qfailure "{{ zabbix_win_svc_name }}" 1100'
register: svc_recovery
changed_when: false
check_mode: false
when: zabbix_agent_win_svc_recovery

- name: "Windows | Setting Zabbix Service Recovery"
win_shell: sc.exe failure "Zabbix Agent" actions= restart/5000/restart/10000/restart/20000 reset= 86400
win_shell: 'sc.exe failure "{{ zabbix_win_svc_name }}" actions= restart/5000/restart/10000/restart/20000 reset= 86400'
when:
- "'RESTART -- Delay' not in svc_recovery.stdout"
- zabbix_agent_win_svc_recovery

- name: "Windows | Firewall rule"
win_firewall_rule:
name: Zabbix Agent
name: "{{ zabbix_win_svc_name }}"
localport: "{{ zabbix_agent_listenport }}"
action: allow
direction: in
Expand Down
8 changes: 6 additions & 2 deletions roles/zabbix_agent/templates/zabbix_agent2.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
# Mandatory: no
# Default:

{% if zabbix_agent_os_family != "Windows" %}
PidFile={{ zabbix_agent2_pidfile }}
{% endif %}

### Option: LogType
# Specifies where log messages are written to:
Expand Down Expand Up @@ -52,13 +54,15 @@ LogFileSize={{ zabbix_agent2_logfilesize }}
# Mandatory: no
# Range: 0-5
# Default:

DebugLevel={{ zabbix_agent2_debuglevel }}

### Option: SourceIP
# Source IP address for outgoing connections.
#
# Mandatory: no
# Default:

{% if zabbix_agent2_sourceip is defined and zabbix_agent2_sourceip %}
SourceIP={{ zabbix_agent2_sourceip }}
{% endif %}
Expand Down Expand Up @@ -303,7 +307,6 @@ Timeout={{ zabbix_agent2_timeout }}
#
# Mandatory: no
# Default:

{% if zabbix_agent_os_family == "Windows" %}
Include={{ zabbix_agent_win_include }}
{% else %}
Expand Down Expand Up @@ -341,8 +344,9 @@ UnsafeUserParameters={{ zabbix_agent2_unsafeuserparameters }}
# Mandatory: no
# Default:
# ControlSocket=

{% if zabbix_agent_os_family != "Windows" %}
ControlSocket={{ zabbix_agent2_controlsocket }}
{% endif %}

####### TLS-RELATED PARAMETERS #######

Expand Down
2 changes: 0 additions & 2 deletions roles/zabbix_agent/vars/Windows.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
---
# vars file for zabbix agent (Windows)

zabbix_win_agent_service: "zabbix agent"

0 comments on commit 763d892

Please sign in to comment.