]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/postgresql/recipes/default.rb
Make postgres exporter connect as the prometheus user
[chef.git] / cookbooks / postgresql / recipes / default.rb
index 9f713b60e9834f28300df03169dd31a7d323fe92..d2c94d1ed5dae55bcdbda2a9c2bac0e14092c874 100644 (file)
@@ -17,6 +17,8 @@
 # limitations under the License.
 #
 
+require "uri"
+
 include_recipe "apt::postgresql"
 include_recipe "munin"
 include_recipe "prometheus"
@@ -108,26 +110,29 @@ ohai_plugin "postgresql" do
   template "ohai.rb.erb"
 end
 
-file "/etc/prometheus/exporters/postgres_queries.yml" do
-  action :delete
-end
-
 package "pgtop"
 package "libdbd-pg-perl"
 
 clusters = node[:postgresql][:clusters] || []
+passwords = data_bag_item("postgresql", "passwords")
 
 clusters.each do |name, details|
   prometheus_suffix = name.tr("/", "-")
   prometheus_database = node[:postgresql][:monitor_database]
 
+  postgresql_user "prometheus" do
+    cluster name
+    password passwords["prometheus"]
+    roles "pg_monitor"
+    not_if { ::File.exist?("/var/lib/postgresql/#{name}/standby.signal") }
+  end
+
   prometheus_exporter "postgres" do
     port 10000 + details[:port].to_i
     service "postgres-#{prometheus_suffix}"
     labels "cluster" => name
     scrape_interval "1m"
     scrape_timeout "1m"
-    user "postgres"
     options %w[
       --collector.database_wraparound
       --collector.long_running_transactions
@@ -136,9 +141,8 @@ clusters.each do |name, details|
       --collector.stat_wal_receiver
       --collector.statio_user_indexes
     ]
-    environment "DATA_SOURCE_NAME" => "postgres:///#{prometheus_database}?host=/run/postgresql&port=#{details[:port]}"
+    environment "DATA_SOURCE_NAME" => "postgres:///#{prometheus_database}?host=/run/postgresql&port=#{details[:port]}&user=prometheus&password=#{URI.encode_www_form_component(passwords['prometheus'])}"
     restrict_address_families "AF_UNIX"
-    remove_ipc false
     subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"
   end
 
@@ -182,11 +186,3 @@ clusters.each do |name, details|
     conf_variables :port => details[:port]
   end
 end
-
-service "prometheus-postgres-exporter" do
-  action [:stop, :disable]
-end
-
-systemd_service "prometheus-postgres-exporter" do
-  action :delete
-end