2 # Cookbook:: postgresql
 
   5 # Copyright:: 2012, OpenStreetMap Foundation
 
   7 # Licensed under the Apache License, Version 2.0 (the "License");
 
   8 # you may not use this file except in compliance with the License.
 
   9 # You may obtain a copy of the License at
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  13 # Unless required by applicable law or agreed to in writing, software
 
  14 # distributed under the License is distributed on an "AS IS" BASIS,
 
  15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16 # See the License for the specific language governing permissions and
 
  17 # limitations under the License.
 
  20 include_recipe "apt::postgresql"
 
  21 include_recipe "munin"
 
  22 include_recipe "prometheus"
 
  25 package "postgresql-common"
 
  27 node[:postgresql][:versions].each do |version|
 
  28   package "postgresql-#{version}"
 
  29   package "postgresql-client-#{version}"
 
  30   package "postgresql-contrib-#{version}"
 
  31   package "postgresql-server-dev-#{version}"
 
  33   defaults = node[:postgresql][:settings][:defaults] || {}
 
  34   settings = node[:postgresql][:settings][version] || {}
 
  36   standby_mode = settings[:standby_mode] || defaults[:standby_mode]
 
  37   primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
 
  39   passwords = if primary_conninfo
 
  40                 data_bag_item(primary_conninfo[:passwords][:bag],
 
  41                               primary_conninfo[:passwords][:item])
 
  44   template "/etc/postgresql/#{version}/main/postgresql.conf" do
 
  45     source "postgresql.conf.erb"
 
  49     variables :version => version,
 
  50               :defaults => defaults,
 
  51               :settings => settings,
 
  52               :primary_conninfo => primary_conninfo,
 
  53               :passwords => passwords
 
  54     notifies :reload, "service[postgresql]"
 
  55     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
 
  58   template "/etc/postgresql/#{version}/main/pg_hba.conf" do
 
  59     source "pg_hba.conf.erb"
 
  63     variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
 
  64               :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
 
  65     notifies :reload, "service[postgresql]"
 
  66     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
 
  69   template "/etc/postgresql/#{version}/main/pg_ident.conf" do
 
  70     source "pg_ident.conf.erb"
 
  74     variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
 
  75     notifies :reload, "service[postgresql]"
 
  76     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
 
  79   link "/var/lib/postgresql/#{version}/main/server.crt" do
 
  80     to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
 
  81     only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
 
  84   link "/var/lib/postgresql/#{version}/main/server.key" do
 
  85     to "/etc/ssl/private/ssl-cert-snakeoil.key"
 
  86     only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
 
  89   if standby_mode == "on"
 
  90     file "/var/lib/postgresql/#{version}/main/standby.signal" do
 
  96     file "/var/lib/postgresql/#{version}/main/standby.signal" do
 
 102 service "postgresql" do
 
 103   action [:enable, :start]
 
 104   supports :status => true, :restart => true, :reload => true
 
 107 ohai_plugin "postgresql" do
 
 108   template "ohai.rb.erb"
 
 111 template "/etc/prometheus/exporters/postgres_queries.yml" do
 
 112   source "postgres_queries.yml.erb"
 
 119 package "libdbd-pg-perl"
 
 121 clusters = node[:postgresql][:clusters] || []
 
 123 clusters.each do |name, details|
 
 124   prometheus_suffix = name.tr("/", "-")
 
 125   prometheus_database = node[:postgresql][:monitor_database]
 
 127   prometheus_exporter "postgres" do
 
 128     port 10000 + details[:port].to_i
 
 129     service "postgres-#{prometheus_suffix}"
 
 130     labels "cluster" => name
 
 134     options "--no-collector.process_idle --extend.query-path=/etc/prometheus/exporters/postgres_queries.yml"
 
 135     environment "DATA_SOURCE_NAME" => "postgres:///#{prometheus_database}?host=/run/postgresql&port=#{details[:port]}"
 
 136     restrict_address_families "AF_UNIX"
 
 138     subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"
 
 141   munin_suffix = name.tr("/", ":")
 
 143   munin_plugin "postgres_bgwriter_#{munin_suffix}" do
 
 144     target "postgres_bgwriter"
 
 146     conf_variables :port => details[:port]
 
 149   munin_plugin "postgres_checkpoints_#{munin_suffix}" do
 
 150     target "postgres_checkpoints"
 
 152     conf_variables :port => details[:port]
 
 155   munin_plugin "postgres_connections_db_#{munin_suffix}" do
 
 156     target "postgres_connections_db"
 
 158     conf_variables :port => details[:port]
 
 161   munin_plugin "postgres_users_#{munin_suffix}" do
 
 162     target "postgres_users"
 
 164     conf_variables :port => details[:port]
 
 167   munin_plugin "postgres_xlog_#{munin_suffix}" do
 
 168     target "postgres_xlog"
 
 170     conf_variables :port => details[:port]
 
 173   next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
 
 175   munin_plugin "postgres_replication_#{munin_suffix}" do
 
 176     target "postgres_replication"
 
 178     conf_variables :port => details[:port]
 
 182 service "prometheus-postgres-exporter" do
 
 183   action [:stop, :disable]
 
 186 systemd_service "prometheus-postgres-exporter" do