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")
27 admins = data_bag_item("apache", "admins")
29 prometheus_exporter "fastly" do
31 listen_switch "endpoint"
33 environment "FASTLY_API_TOKEN" => tokens["fastly"]
36 cache_dir = Chef::Config[:file_cache_path]
38 prometheus_version = "2.31.1"
39 alertmanager_version = "0.23.0"
40 karma_version = "0.93"
42 directory "/opt/prometheus-server" do
48 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
49 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
56 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
58 destination "/opt/prometheus-server/prometheus"
63 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
66 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
67 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
74 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
76 destination "/opt/prometheus-server/alertmanager"
81 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
84 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
85 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
92 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
94 destination "/opt/prometheus-server/karma"
98 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
103 prometheus-alertmanager
106 promscale_version = "0.7.1"
108 database_version = node[:timescaledb][:database_version]
109 database_cluster = "#{database_version}/main"
111 postgresql_user "prometheus" do
112 cluster database_cluster
116 postgresql_database "promscale" do
117 cluster database_cluster
121 directory "/opt/promscale" do
127 cookbook_file "/usr/lib/postgresql/#{database_version}/lib/promscale.so" do
128 source "postgresql-#{database_version}-promscale.so"
134 directory "/opt/promscale/bin" do
140 remote_file "/opt/promscale/bin/promscale" do
142 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
148 systemd_service "promscale" do
149 description "Promscale Connector"
152 exec_start "/opt/promscale/bin/promscale --db-uri postgresql:///promscale?host=/run/postgresql&port=5432 --db-connections-max 400"
153 # 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" }
156 protect_system "strict"
158 no_new_privileges true
161 systemd_service "promscale-maintenance" do
162 description "Promscale Maintenance"
165 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
167 protect_system "strict"
169 no_new_privileges true
172 systemd_timer "promscale-maintenance" do
173 description "Promscale Maintenace"
175 on_unit_inactive_sec 1800
178 if node[:prometheus][:promscale]
179 service "promscale" do
180 action [:enable, :start]
181 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
182 subscribes :restart, "systemd_service[promscale]"
185 service "promscale-maintenance.timer" do
186 action [:enable, :start]
189 service "promscale" do
190 action [:disable, :stop]
193 service "promscale-maintenance.timer" do
194 action [:disable, :stop]
198 search(:node, "roles:gateway") do |gateway|
199 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
200 "#{interface[:network]}/#{interface[:prefix]}"
203 node.default[:networking][:wireguard][:peers] << {
204 :public_key => gateway[:networking][:wireguard][:public_key],
205 :allowed_ips => allowed_ips,
206 :endpoint => "#{gateway.name}:51820"
213 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
214 if client[:prometheus][:mode] == "wireguard"
215 node.default[:networking][:wireguard][:peers] << {
216 :public_key => client[:networking][:wireguard][:public_key],
217 :allowed_ips => client[:networking][:wireguard][:address],
218 :endpoint => "#{client.name}:51820"
222 client[:prometheus][:exporters].each do |key, exporter|
223 if exporter.is_a?(Hash)
224 name = exporter[:name]
225 address = exporter[:address]
226 metric_relabel = exporter[:metric_relabel] || []
236 :instance => client.name.split(".").first,
237 :metric_relabel => metric_relabel
241 Hash(client[:prometheus][:snmp]).each do |instance, details|
243 :instance => instance,
244 :target => details[:address],
245 :module => details[:module],
246 :address => client[:prometheus][:addresses]["snmp"],
247 :labels => Array(details[:labels])
252 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
253 n[:letsencrypt][:certificates].each do |name, details|
254 c[name] ||= details.merge(:nodes => [])
258 :address => n.external_ipaddress || n.internal_ipaddress
263 template "/etc/prometheus/ssl.yml" do
268 variables :certificates => certificates
271 prometheus_exporter "ssl" do
274 options "--config.file=/etc/prometheus/ssl.yml"
275 register_target false
278 systemd_service "prometheus-executable" do
281 exec_start "/opt/prometheus-server/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.external-url=https://prometheus.openstreetmap.org/prometheus --storage.tsdb.path=/var/lib/prometheus/metrics2"
282 notifies :restart, "service[prometheus]"
285 template "/etc/prometheus/prometheus.yml" do
286 source "prometheus.yml.erb"
290 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
293 template "/etc/prometheus/alert_rules.yml" do
294 source "alert_rules.yml.erb"
300 service "prometheus" do
301 action [:enable, :start]
302 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
303 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
304 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
307 systemd_service "prometheus-alertmanager-executable" do
308 service "prometheus-alertmanager"
310 exec_start "/opt/prometheus-server/alertmanager/alertmanager --config.file=/etc/prometheus/alertmanager.yml --storage.path=/var/lib/prometheus/alertmanager --web.external-url=https://prometheus.openstreetmap.org/alertmanager"
311 notifies :restart, "service[prometheus-alertmanager]"
314 template "/etc/prometheus/alertmanager.yml" do
315 source "alertmanager.yml.erb"
321 service "prometheus-alertmanager" do
322 action [:enable, :start]
323 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
324 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
327 template "/etc/prometheus/amtool.yml" do
328 source "amtool.yml.erb"
334 template "/etc/prometheus/karma.yml" do
335 source "karma.yml.erb"
341 systemd_service "prometheus-karma" do
342 description "Alert dashboard for Prometheus Alertmanager"
344 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
347 protect_system "full"
349 no_new_privileges true
353 service "prometheus-karma" do
354 action [:enable, :start]
355 subscribes :reload, "template[/etc/prometheus/karma.yml]"
356 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
359 package "grafana-enterprise"
361 template "/etc/grafana/grafana.ini" do
362 source "grafana.ini.erb"
366 variables :passwords => passwords
369 service "grafana-server" do
370 action [:enable, :start]
371 subscribes :restart, "template[/etc/grafana/grafana.ini]"
374 apache_module "alias"
375 apache_module "proxy_http"
377 ssl_certificate "prometheus.openstreetmap.org" do
378 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
379 notifies :reload, "service[apache2]"
382 apache_site "prometheus.openstreetmap.org" do
383 template "apache.erb"
384 variables :admin_hosts => admins["hosts"]
387 template "/etc/cron.daily/prometheus-backup" do
388 source "backup.cron.erb"