Skip to content

Commit

Permalink
Refactor TimescaleDB setup/configuration (#1347)
Browse files Browse the repository at this point in the history
* 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.

* 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)
  • Loading branch information
eb4x authored Jul 22, 2024
1 parent c229ac6 commit c27b5b5
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions roles/zabbix_server/tasks/initialize-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,47 +91,44 @@
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
# 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) 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 }}"
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

0 comments on commit c27b5b5

Please sign in to comment.