diff --git a/manifests/database/postgresql.pp b/manifests/database/postgresql.pp index eb73c6588..4350e1ae8 100644 --- a/manifests/database/postgresql.pp +++ b/manifests/database/postgresql.pp @@ -9,6 +9,7 @@ # @param database_host Hostname to use to connect to the database # @param database_port Database port to be used for the import process. # @param database_path Path to the database executable +# @param manage_database_timescale Import the timescaledb sql file? # @author Werner Dijkerman class zabbix::database::postgresql ( $zabbix_type = '', @@ -20,6 +21,7 @@ $database_host = '', Stdlib::Port::Unprivileged $database_port = 5432, $database_path = $zabbix::params::database_path, + Boolean $manage_database_timescale = false, ) inherits zabbix::params { assert_private() @@ -108,6 +110,28 @@ provider => 'shell', require => Exec['update_pgpass'], } + if $manage_database_timescale { + # Enable timescaledb + $_timescaledb_sql = [ + "cd ${schema_path}", + "&& psql -h '${database_host}'", + "-U '${database_user}'", + "-p ${database_port}", + "-d '${database_name}'", + '-f timescaledb.sql', + '&& touch /etc/zabbix/.timescaledb.done', + ] + exec { 'zabbix_timescaledb.sql': + command => $_timescaledb_sql.join(' '), + path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}", + unless => 'test -f /etc/zabbix/.timescaledb.done', + provider => 'shell', + require => [ + Exec['update_pgpass'], + Exec['zabbix_server_data.sql'], + ], + } + } } default: { fail 'We do not work.' diff --git a/manifests/init.pp b/manifests/init.pp index 7a23864c2..f18c86221 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -32,6 +32,7 @@ # you can use this parameter to add the database_path to the above mentioned # path. # @param manage_database When true, it will configure the database and execute the sql scripts. +# @param manage_database_timescale When true, it will execute the sql scripts needef for timescaledb. # @param manage_repo When true (default) this module will manage the Zabbix repository. # @param manage_firewall When true, it will create iptables rules. # @param manage_service @@ -224,6 +225,7 @@ Zabbix::Databases $database_type = $zabbix::params::database_type, $database_path = $zabbix::params::database_path, $manage_database = $zabbix::params::manage_database, + $manage_database_timescale = $zabbix::params::manage_database_timescale, $default_vhost = $zabbix::params::default_vhost, $manage_vhost = $zabbix::params::manage_vhost, $manage_firewall = $zabbix::params::manage_firewall, @@ -401,6 +403,7 @@ manage_firewall => $manage_firewall, manage_repo => $manage_repo, manage_database => $manage_database, + manage_database_timescale => $manage_database_timescale, manage_service => $manage_service, listenport => $listenport, sourceip => $sourceip, diff --git a/manifests/params.pp b/manifests/params.pp index 07470e85e..b958537ff 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -147,6 +147,7 @@ $zabbix_web = 'localhost' $zabbix_web_ip = '127.0.0.1' $manage_database = true + $manage_database_timescale = false $manage_service = true $default_vhost = false $manage_firewall = false diff --git a/manifests/server.pp b/manifests/server.pp index 41d8f73b0..b659845fe 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -11,6 +11,7 @@ # @param zabbix_version This is the zabbix version. Example: 5.0 # @param manage_repo When true (default) this module will manage the Zabbix repository. # @param manage_database When true, it will configure the database and execute the sql scripts. +# @param manage_database_timescale When true, it will execute the sql scripts needed for timescaledb. # @param zabbix_package_state The state of the package that needs to be installed: present or latest. # @param manage_firewall When true, it will create iptables rules. # @param manage_service @@ -172,6 +173,7 @@ Boolean $manage_firewall = $zabbix::params::manage_firewall, Boolean $manage_repo = $zabbix::params::manage_repo, Boolean $manage_database = $zabbix::params::manage_database, + Boolean $manage_database_timescale = $zabbix::params::manage_database_timescale, Boolean $manage_service = $zabbix::params::manage_service, $server_configfile_path = $zabbix::params::server_configfile_path, $server_config_owner = $zabbix::params::server_config_owner, @@ -319,16 +321,17 @@ if $manage_database { # Execute the postgresql scripts class { 'zabbix::database::postgresql': - zabbix_type => 'server', - zabbix_version => $zabbix_version, - database_schema_path => $database_schema_path, - database_name => $database_name, - database_user => $database_user, - database_password => $database_password, - database_host => $database_host, - database_port => $database_port, - database_path => $database_path, - require => $zabbix_database_require, + zabbix_type => 'server', + zabbix_version => $zabbix_version, + database_schema_path => $database_schema_path, + database_name => $database_name, + database_user => $database_user, + database_password => $database_password, + database_host => $database_host, + database_port => $database_port, + database_path => $database_path, + manage_database_timescale => $manage_database_timescale, + require => $zabbix_database_require, } } } diff --git a/spec/classes/database_postgresql_spec.rb b/spec/classes/database_postgresql_spec.rb index 49603fed5..1960debef 100644 --- a/spec/classes/database_postgresql_spec.rb +++ b/spec/classes/database_postgresql_spec.rb @@ -113,6 +113,53 @@ it { is_expected.to contain_class('zabbix::params') } end + describe "when zabbix_type is server and version is #{zabbix_version} and manage_database_timescale is true" do + let :params do + { + database_name: 'zabbix-server', + database_user: 'zabbix-server', + database_password: 'zabbix-server', + database_host: 'node01.example.com', + zabbix_type: 'server', + zabbix_version: zabbix_version, + manage_database_timescale: true + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:6432:zabbix-server:zabbix-server:zabbix-server >> /root/.pgpass') } + it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-server' -p 6432 -d 'zabbix-server' -f #{sql_server} && touch /etc/zabbix/.schema.done") } + it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') } + it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') } + it { is_expected.to contain_file('/root/.pgpass') } + it { is_expected.to contain_class('zabbix::params') } + it { is_expected.to contain_exec('zabbix_timescaledb.sql').with_command("cd #{path} && psql -h 'node01.example.com' -U 'zabbix-server' -p 5432 -d 'zabbix-server' -f timescaledb.sql && touch /etc/zabbix/.timescaledb.done") } + end + + describe "when zabbix_type is server and version is #{zabbix_version} and manage_database_timescale is true and custom port is defined" do + let :params do + { + database_name: 'zabbix-server', + database_user: 'zabbix-server', + database_password: 'zabbix-server', + database_host: 'node01.example.com', + database_port: 6432, + zabbix_type: 'server', + zabbix_version: zabbix_version, + manage_database_timescale: true + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:6432:zabbix-server:zabbix-server:zabbix-server >> /root/.pgpass') } + it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-server' -p 6432 -d 'zabbix-server' -f #{sql_server} && touch /etc/zabbix/.schema.done") } + it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') } + it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') } + it { is_expected.to contain_file('/root/.pgpass') } + it { is_expected.to contain_class('zabbix::params') } + it { is_expected.to contain_exec('zabbix_timescaledb.sql').with_command("cd #{path} && psql -h 'node01.example.com' -U 'zabbix-server' -p 6432 -d 'zabbix-server' -f timescaledb.sql && touch /etc/zabbix/.timescaledb.done") } + end + describe "when zabbix_type is proxy and version is #{zabbix_version}" do let :params do {