Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TimescaleDB setup/configuration #1347

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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