From 13fd6e225e7ee49ba90f5743fdc0e112aec6e994 Mon Sep 17 00:00:00 2001 From: Erik Berg Date: Thu, 18 Jul 2024 18:05:19 +0200 Subject: [PATCH 1/2] Refactor TimescaleDB tasks Query the db to actually see if we've got timescale configured instead of relying on files. Use the same task for importing the hypertable schema, and just conditionally change the path to the schemas. --- .../zabbix_server/tasks/initialize-pgsql.yml | 67 +++++++------------ 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/roles/zabbix_server/tasks/initialize-pgsql.yml b/roles/zabbix_server/tasks/initialize-pgsql.yml index 8c42afd75..108fcd1a1 100644 --- a/roles/zabbix_server/tasks/initialize-pgsql.yml +++ b/roles/zabbix_server/tasks/initialize-pgsql.yml @@ -91,47 +91,32 @@ state: restore target: /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz -- name: "PostgreSQL | Create TimescaleDB hypertables Zabbix < 7.0" - ansible.builtin.shell: | - set -euxo pipefail - cd /usr/share/zabbix-sql-scripts/postgresql && - if [ -f timescaledb.sql.gz ]; then zcat timescaledb.sql.gz > /etc/timescaledb.sql ; else cp -p timescaledb.sql /etc/timescaledb.sql ; fi - cat /etc/timescaledb.sql | psql -h '{{ zabbix_server_dbhost }}' \ - -U '{{ zabbix_server_dbuser }}' \ - -d '{{ zabbix_server_dbname }}' \ - -p '{{ zabbix_server_dbport }}' - touch /etc/zabbix/timescaledb.done - rm -f /tmp/timescaledb.sql - args: - creates: /etc/zabbix/timescaledb.done - executable: /bin/bash - environment: - PGPASSWORD: "{{ zabbix_server_dbpassword }}" - become: true - when: - - zabbix_server_database_timescaledb - - zabbix_server_version is version( '7.0', '<') +- name: "Configure Timescale" + when: zabbix_server_database_timescaledb tags: - database + block: + - name: "PostgreSQL | Check if TimescaleDB has been configured" + community.postgresql.postgresql_query: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + query: "SELECT db_extension FROM config WHERE db_extension = 'timescaledb'" + register: _config_db_extension -- name: "PostgreSQL | Create TimescaleDB hypertables Zabbix >= 7.0" - ansible.builtin.shell: | - set -euxo pipefail - cd /usr/share/zabbix-sql-scripts/postgresql/timescaledb && - cat schema.sql | psql -h '{{ zabbix_server_dbhost }}' \ - -U '{{ zabbix_server_dbuser }}' \ - -d '{{ zabbix_server_dbname }}' \ - -p '{{ zabbix_server_dbport }}' - touch /etc/zabbix/timescaledb.done - rm -f /tmp/timescaledb.sql - args: - creates: /etc/zabbix/timescaledb.done - executable: /bin/bash - environment: - PGPASSWORD: "{{ zabbix_server_dbpassword }}" - become: true - when: - - zabbix_server_database_timescaledb - - zabbix_server_version is version( '7.0', '>=') - tags: - - database + - name: "PostgreSQL | Create TimescaleDB hypertables" + when: (_config_db_extension.query_result | length == 0) + community.postgresql.postgresql_db: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + state: restore + target: "{{ is_legacy_version | ternary(legacy_path, modern_path) }}" + vars: + is_legacy_version: "{{ zabbix_server_version is version('7.0', '<') }}" + legacy_path: /usr/share/zabbix-sql-scripts/postgresql/timescaledb.sql + modern_path: /usr/share/zabbix-sql-scripts/postgresql/timescaledb/schema.sql From 508259089c08e456965fa642b28e48965ac9895f Mon Sep 17 00:00:00 2001 From: Erik Berg Date: Thu, 18 Jul 2024 18:18:14 +0200 Subject: [PATCH 2/2] Run Timescale schema during an upgrade In the case that timescale has already been enabled, and we're upgrading from 6.x to 7.x, the auditlog should also get converted. (Which it was not in the 6.x schemas) --- roles/zabbix_server/tasks/initialize-pgsql.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/roles/zabbix_server/tasks/initialize-pgsql.yml b/roles/zabbix_server/tasks/initialize-pgsql.yml index 108fcd1a1..feb4292f3 100644 --- a/roles/zabbix_server/tasks/initialize-pgsql.yml +++ b/roles/zabbix_server/tasks/initialize-pgsql.yml @@ -106,8 +106,20 @@ query: "SELECT db_extension FROM config WHERE db_extension = 'timescaledb'" register: _config_db_extension + # NOTE: Zabbix 7.0 adds a hypertable for auditlog. + - name: "PostgreSQL | Check for auditlog hypertable" + community.postgresql.postgresql_query: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + query: "SELECT hypertable_name FROM timescaledb_information.hypertables WHERE hypertable_name = 'auditlog'" + register: _hypertable_auditlog + - name: "PostgreSQL | Create TimescaleDB hypertables" - when: (_config_db_extension.query_result | length == 0) + when: (_config_db_extension.query_result | length == 0) or + (zabbix_server_version is version('7.0', '>=') and _hypertable_auditlog.query_result | length == 0) community.postgresql.postgresql_db: login_user: "{{ zabbix_server_dbuser }}" login_password: "{{ zabbix_server_dbpassword }}"