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 "listen"
32 environment "FASTLY_API_TOKEN" => tokens["fastly"]
35 prometheus_exporter "fastly_healthcheck" do
38 environment "FASTLY_API_TOKEN" => tokens["fastly"]
41 prometheus_exporter "statuscake" do
43 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
46 cache_dir = Chef::Config[:file_cache_path]
48 prometheus_version = "2.31.1"
49 alertmanager_version = "0.23.0"
50 karma_version = "0.105"
52 directory "/opt/prometheus-server" do
58 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
59 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
66 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
68 destination "/opt/prometheus-server/prometheus"
73 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
76 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
77 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
84 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
86 destination "/opt/prometheus-server/alertmanager"
91 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
94 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
95 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
102 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
104 destination "/opt/prometheus-server/karma"
108 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
111 promscale_version = "0.11.0"
113 database_version = node[:timescaledb][:database_version]
114 database_cluster = "#{database_version}/main"
118 prometheus-alertmanager
119 promscale-extension-postgresql-#{database_version}
122 postgresql_user "prometheus" do
123 cluster database_cluster
127 postgresql_database "promscale" do
128 cluster database_cluster
132 directory "/opt/promscale" do
138 directory "/opt/promscale/bin" do
144 remote_file "/opt/promscale/bin/promscale" do
146 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
152 systemd_service "promscale" do
153 description "Promscale Connector"
156 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
159 protect_system "strict"
161 no_new_privileges true
164 if node[:prometheus][:promscale]
165 service "promscale" do
166 action [:enable, :start]
167 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
168 subscribes :restart, "systemd_service[promscale]"
171 service "promscale" do
172 action [:disable, :stop]
176 search(:node, "roles:gateway") do |gateway|
177 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
178 "#{interface[:network]}/#{interface[:prefix]}"
181 node.default[:networking][:wireguard][:peers] << {
182 :public_key => gateway[:networking][:wireguard][:public_key],
183 :allowed_ips => allowed_ips,
184 :endpoint => "#{gateway.name}:51820"
191 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
192 if client[:prometheus][:mode] == "wireguard"
193 node.default[:networking][:wireguard][:peers] << {
194 :public_key => client[:networking][:wireguard][:public_key],
195 :allowed_ips => client[:networking][:wireguard][:address],
196 :endpoint => "#{client.name}:51820"
200 client[:prometheus][:exporters].each do |key, exporter|
201 if exporter.is_a?(Hash)
202 name = exporter[:name]
203 address = exporter[:address]
205 scrape_interval = exporter[:scrape_interval]
206 metric_relabel = exporter[:metric_relabel] || []
211 scrape_interval = nil
219 :instance => client.name.split(".").first,
220 :scrape_interval => scrape_interval,
221 :metric_relabel => metric_relabel
225 Hash(client[:prometheus][:snmp]).each do |instance, details|
227 :instance => instance,
228 :target => details[:address],
229 :modules => details[:modules],
230 :address => client[:prometheus][:addresses]["snmp"],
231 :labels => Array(details[:labels])
236 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
237 n[:letsencrypt][:certificates].each do |name, details|
238 c[name] ||= details.merge(:nodes => [])
242 :address => n.external_ipaddress || n.internal_ipaddress
247 template "/etc/prometheus/ssl.yml" do
252 variables :certificates => certificates
255 prometheus_exporter "ssl" do
258 options "--config.file=/etc/prometheus/ssl.yml"
259 register_target false
262 systemd_service "prometheus-executable" do
265 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 --storage.tsdb.retention.time=30d"
267 notifies :restart, "service[prometheus]"
270 template "/etc/prometheus/prometheus.yml" do
271 source "prometheus.yml.erb"
275 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
278 template "/etc/prometheus/alert_rules.yml" do
279 source "alert_rules.yml.erb"
285 service "prometheus" do
286 action [:enable, :start]
287 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
288 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
289 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
292 systemd_service "prometheus-alertmanager-executable" do
293 service "prometheus-alertmanager"
295 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"
296 notifies :restart, "service[prometheus-alertmanager]"
299 template "/etc/prometheus/alertmanager.yml" do
300 source "alertmanager.yml.erb"
306 service "prometheus-alertmanager" do
307 action [:enable, :start]
308 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
309 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
312 template "/etc/prometheus/amtool.yml" do
313 source "amtool.yml.erb"
319 template "/etc/prometheus/karma.yml" do
320 source "karma.yml.erb"
326 systemd_service "prometheus-karma" do
327 description "Alert dashboard for Prometheus Alertmanager"
329 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
332 protect_system "full"
334 no_new_privileges true
338 service "prometheus-karma" do
339 action [:enable, :start]
340 subscribes :reload, "template[/etc/prometheus/karma.yml]"
341 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
344 package "grafana-enterprise"
346 template "/etc/grafana/grafana.ini" do
347 source "grafana.ini.erb"
351 variables :passwords => passwords
354 service "grafana-server" do
355 action [:enable, :start]
356 subscribes :restart, "template[/etc/grafana/grafana.ini]"
359 apache_module "alias"
360 apache_module "proxy_http"
362 ssl_certificate "prometheus.openstreetmap.org" do
363 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
364 notifies :reload, "service[apache2]"
367 apache_site "prometheus.openstreetmap.org" do
368 template "apache.erb"
369 variables :admin_hosts => admins["hosts"]
372 template "/etc/cron.daily/prometheus-backup" do
373 source "backup.cron.erb"