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.
22 include_recipe "apt::postgresql"
23 include_recipe "munin"
24 include_recipe "prometheus"
27 package "postgresql-common"
29 node[:postgresql][:versions].each do |version|
30 package "postgresql-#{version}"
31 package "postgresql-client-#{version}"
32 package "postgresql-contrib-#{version}"
33 package "postgresql-server-dev-#{version}"
35 defaults = node[:postgresql][:settings][:defaults] || {}
36 settings = node[:postgresql][:settings][version] || {}
38 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
39 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
41 passwords = if primary_conninfo
42 data_bag_item(primary_conninfo[:passwords][:bag],
43 primary_conninfo[:passwords][:item])
46 template "/etc/postgresql/#{version}/main/postgresql.conf" do
47 source "postgresql.conf.erb"
51 variables :version => version,
52 :defaults => defaults,
53 :settings => settings,
54 :primary_conninfo => primary_conninfo,
55 :passwords => passwords
56 notifies :reload, "service[postgresql]"
57 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
60 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
61 source "pg_hba.conf.erb"
65 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
66 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
67 notifies :reload, "service[postgresql]"
68 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
71 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
72 source "pg_ident.conf.erb"
76 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
77 notifies :reload, "service[postgresql]"
78 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
81 link "/var/lib/postgresql/#{version}/main/server.crt" do
82 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
83 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
86 link "/var/lib/postgresql/#{version}/main/server.key" do
87 to "/etc/ssl/private/ssl-cert-snakeoil.key"
88 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
91 if standby_mode == "on"
92 file "/var/lib/postgresql/#{version}/main/standby.signal" do
98 file "/var/lib/postgresql/#{version}/main/standby.signal" do
104 service "postgresql" do
105 action [:enable, :start]
106 supports :status => true, :restart => true, :reload => true
109 ohai_plugin "postgresql" do
110 template "ohai.rb.erb"
114 package "libdbd-pg-perl"
116 clusters = node[:postgresql][:clusters] || []
117 passwords = data_bag_item("postgresql", "passwords")
119 clusters.each do |name, details|
120 prometheus_suffix = name.tr("/", "-")
121 prometheus_database = node[:postgresql][:monitor_database]
123 postgresql_user "prometheus" do
125 password passwords["prometheus"]
127 not_if { ::File.exist?("/var/lib/postgresql/#{name}/standby.signal") }
130 prometheus_exporter "postgres" do
131 port 10000 + details[:port].to_i
132 service "postgres-#{prometheus_suffix}"
133 labels "cluster" => name
137 --collector.database_wraparound
138 --collector.long_running_transactions
139 --collector.process_idle
140 --collector.stat_activity_autovacuum
141 --collector.stat_wal_receiver
142 --collector.statio_user_indexes
144 environment "DATA_SOURCE_NAME" => "postgres:///#{prometheus_database}?host=/run/postgresql&port=#{details[:port]}&user=prometheus&password=#{URI.encode_www_form_component(passwords['prometheus'])}"
145 restrict_address_families "AF_UNIX"
146 subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"
149 munin_suffix = name.tr("/", ":")
151 munin_plugin "postgres_bgwriter_#{munin_suffix}" do
152 target "postgres_bgwriter"
154 conf_variables :port => details[:port]
157 munin_plugin "postgres_checkpoints_#{munin_suffix}" do
158 target "postgres_checkpoints"
160 conf_variables :port => details[:port]
163 munin_plugin "postgres_connections_db_#{munin_suffix}" do
164 target "postgres_connections_db"
166 conf_variables :port => details[:port]
169 munin_plugin "postgres_users_#{munin_suffix}" do
170 target "postgres_users"
172 conf_variables :port => details[:port]
175 munin_plugin "postgres_xlog_#{munin_suffix}" do
176 target "postgres_xlog"
178 conf_variables :port => details[:port]
181 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
183 munin_plugin "postgres_replication_#{munin_suffix}" do
184 target "postgres_replication"
186 conf_variables :port => details[:port]