2 # Cookbook:: prometheus
5 # Copyright:: 2020, 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 "apache"
22 include_recipe "networking"
23 include_recipe "timescaledb"
25 passwords = data_bag_item("prometheus", "passwords")
26 tokens = data_bag_item("prometheus", "tokens")
28 prometheus_exporter "fastly" do
30 listen_switch "endpoint"
32 environment "FASTLY_API_TOKEN" => tokens["fastly"]
37 prometheus-alertmanager
40 promscale_version = "0.2.0"
42 database_cluster = "#{node[:timescaledb][:database_version]}/main"
44 postgresql_user "prometheus" do
45 cluster database_cluster
49 postgresql_database "promscale" do
50 cluster database_cluster
54 directory "/opt/promscale" do
68 git "/opt/promscale/extension" do
70 repository "https://github.com/timescale/promscale_extension.git"
76 execute "/opt/promscale/extension/Makefile" do
78 command "make install"
79 cwd "/opt/promscale/extension"
82 subscribes :run, "git[/opt/promscale/extension]", :immediately
83 notifies :restart, "service[postgresql]", :immediately
86 directory "/opt/promscale/bin" do
92 remote_file "/opt/promscale/bin/promscale" do
94 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
100 systemd_service "promscale" do
101 description "Promscale Connector"
104 exec_start "/opt/promscale/bin/promscale --db-host /run/postgresql --db-port 5432 --db-user prometheus --db-name promscale --db-connections-max 400"
105 # exec_start lazy { "/opt/promscale/bin/promscale --db-host /run/postgresql --db-port #{node[:postgresql][:clusters][database_cluster][:port]} --db-user prometheus --db-name promscale --db-max-connections 400" }
108 protect_system "strict"
110 no_new_privileges true
113 service "promscale" do
114 action [:enable, :start]
115 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
116 subscribes :restart, "systemd_service[promscale]"
119 systemd_service "promscale-maintenance" do
120 description "Promscale Maintenace"
123 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
125 protect_system "strict"
127 no_new_privileges true
130 systemd_timer "promscale-maintenance" do
131 description "Promscale Maintenace"
133 on_unit_inactive_sec 1800
136 service "promscale-maintenance.timer" do
137 action [:enable, :start]
140 search(:node, "roles:gateway") do |gateway|
141 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
142 "#{interface[:network]}/#{interface[:prefix]}"
145 node.default[:networking][:wireguard][:peers] << {
146 :public_key => gateway[:networking][:wireguard][:public_key],
147 :allowed_ips => allowed_ips,
148 :endpoint => "#{gateway.name}:51820"
154 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
155 if client[:prometheus][:mode] == "wireguard"
156 node.default[:networking][:wireguard][:peers] << {
157 :public_key => client[:networking][:wireguard][:public_key],
158 :allowed_ips => client[:networking][:wireguard][:address],
159 :endpoint => "#{client.name}:51820"
163 client[:prometheus][:exporters].each do |key, exporter|
164 if exporter.is_a?(Hash)
165 name = exporter[:name]
166 address = exporter[:address]
167 metric_relabel = exporter[:metric_relabel] || []
177 :instance => client.name.split(".").first,
178 :metric_relabel => metric_relabel
183 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
184 n[:letsencrypt][:certificates].each do |name, details|
185 c[name] ||= details.merge(:nodes => [])
189 :address => n.external_ipaddress || n.internal_ipaddress
194 template "/etc/prometheus/ssl.yml" do
199 variables :certificates => certificates
202 prometheus_exporter "ssl" do
205 options "--config.file=/etc/prometheus/ssl.yml"
206 register_target false
209 template "/etc/default/prometheus" do
210 source "default.prometheus.erb"
216 template "/etc/prometheus/prometheus.yml" do
217 source "prometheus.yml.erb"
221 variables :jobs => jobs, :certificates => certificates
224 template "/etc/prometheus/alert_rules.yml" do
225 source "alert_rules.yml.erb"
231 service "prometheus" do
232 action [:enable, :start]
233 subscribes :restart, "template[/etc/default/prometheus]"
234 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
235 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
238 template "/etc/default/prometheus-alertmanager" do
239 source "default.alertmanager.erb"
245 template "/etc/prometheus/alertmanager.yml" do
246 source "alertmanager.yml.erb"
252 service "prometheus-alertmanager" do
253 action [:enable, :start]
254 subscribes :restart, "template[/etc/default/prometheus-alertmanager]"
255 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
258 template "/etc/prometheus/amtool.yml" do
259 source "amtool.yml.erb"
265 package "grafana-enterprise"
267 template "/etc/grafana/grafana.ini" do
268 source "grafana.ini.erb"
272 variables :passwords => passwords
275 service "grafana-server" do
276 action [:enable, :start]
277 subscribes :restart, "template[/etc/grafana/grafana.ini]"
280 apache_module "alias"
281 apache_module "proxy_http"
283 ssl_certificate "prometheus.openstreetmap.org" do
284 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
285 notifies :reload, "service[apache2]"
288 apache_site "prometheus.openstreetmap.org" do
289 template "apache.erb"
292 template "/etc/cron.daily/prometheus-backup" do
293 source "backup.cron.erb"