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
45 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
48 cache_dir = Chef::Config[:file_cache_path]
50 prometheus_version = "2.31.1"
51 alertmanager_version = "0.23.0"
52 karma_version = "0.105"
54 directory "/opt/prometheus-server" do
60 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
61 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
68 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
70 destination "/opt/prometheus-server/prometheus"
75 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
78 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
79 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
86 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
88 destination "/opt/prometheus-server/alertmanager"
93 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
96 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
97 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
104 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
106 destination "/opt/prometheus-server/karma"
110 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
113 promscale_version = "0.13.0"
115 database_version = node[:timescaledb][:database_version]
116 database_cluster = "#{database_version}/main"
120 prometheus-alertmanager
121 promscale-extension-postgresql-#{database_version}
124 postgresql_user "prometheus" do
125 cluster database_cluster
129 postgresql_database "promscale" do
130 cluster database_cluster
134 directory "/opt/promscale" do
140 directory "/opt/promscale/bin" do
146 remote_file "/opt/promscale/bin/promscale" do
148 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
154 systemd_service "promscale" do
155 description "Promscale Connector"
158 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
161 protect_system "strict"
163 no_new_privileges true
166 if node[:prometheus][:promscale]
167 service "promscale" do
168 action [:enable, :start]
169 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
170 subscribes :restart, "systemd_service[promscale]"
173 service "promscale" do
174 action [:disable, :stop]
178 search(:node, "roles:gateway") do |gateway|
179 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
180 "#{interface[:network]}/#{interface[:prefix]}"
183 node.default[:networking][:wireguard][:peers] << {
184 :public_key => gateway[:networking][:wireguard][:public_key],
185 :allowed_ips => allowed_ips,
186 :endpoint => "#{gateway.name}:51820"
193 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
194 if client[:prometheus][:mode] == "wireguard"
195 node.default[:networking][:wireguard][:peers] << {
196 :public_key => client[:networking][:wireguard][:public_key],
197 :allowed_ips => client[:networking][:wireguard][:address],
198 :endpoint => "#{client.name}:51820"
202 client[:prometheus][:exporters].each do |key, exporter|
203 if exporter.is_a?(Hash)
204 name = exporter[:name]
205 address = exporter[:address]
207 scrape_interval = exporter[:scrape_interval]
208 scrape_timeout = exporter[:scrape_timeout]
209 metric_relabel = exporter[:metric_relabel] || []
214 scrape_interval = nil
223 :instance => client.name.split(".").first,
224 :scrape_interval => scrape_interval,
225 :scrape_timeout => scrape_timeout,
226 :metric_relabel => metric_relabel
230 Hash(client[:prometheus][:snmp]).each do |instance, details|
232 :instance => instance,
233 :target => details[:address],
234 :modules => details[:modules],
235 :address => client[:prometheus][:addresses]["snmp"],
236 :labels => Array(details[:labels])
241 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
242 n[:letsencrypt][:certificates].each do |name, details|
243 c[name] ||= details.merge(:nodes => [])
247 :address => n.external_ipaddress || n.internal_ipaddress
252 template "/etc/prometheus/ssl.yml" do
257 variables :certificates => certificates
260 prometheus_exporter "ssl" do
263 options "--config.file=/etc/prometheus/ssl.yml"
264 register_target false
267 systemd_service "prometheus-executable" do
270 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"
272 notifies :restart, "service[prometheus]"
275 template "/etc/prometheus/prometheus.yml" do
276 source "prometheus.yml.erb"
280 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
283 template "/etc/prometheus/alert_rules.yml" do
284 source "alert_rules.yml.erb"
290 service "prometheus" do
291 action [:enable, :start]
292 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
293 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
294 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
297 systemd_service "prometheus-alertmanager-executable" do
298 service "prometheus-alertmanager"
300 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"
301 notifies :restart, "service[prometheus-alertmanager]"
304 template "/etc/prometheus/alertmanager.yml" do
305 source "alertmanager.yml.erb"
311 service "prometheus-alertmanager" do
312 action [:enable, :start]
313 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
314 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
317 template "/etc/prometheus/amtool.yml" do
318 source "amtool.yml.erb"
324 template "/etc/prometheus/karma.yml" do
325 source "karma.yml.erb"
331 systemd_service "prometheus-karma" do
332 description "Alert dashboard for Prometheus Alertmanager"
334 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
337 protect_system "full"
339 no_new_privileges true
343 service "prometheus-karma" do
344 action [:enable, :start]
345 subscribes :reload, "template[/etc/prometheus/karma.yml]"
346 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
349 package "grafana-enterprise"
351 template "/etc/grafana/grafana.ini" do
352 source "grafana.ini.erb"
356 variables :passwords => passwords
359 service "grafana-server" do
360 action [:enable, :start]
361 subscribes :restart, "template[/etc/grafana/grafana.ini]"
364 apache_module "alias"
365 apache_module "proxy_http"
367 ssl_certificate "prometheus.openstreetmap.org" do
368 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
369 notifies :reload, "service[apache2]"
372 apache_site "prometheus.openstreetmap.org" do
373 template "apache.erb"
374 variables :admin_hosts => admins["hosts"]
377 template "/etc/cron.daily/prometheus-backup" do
378 source "backup.cron.erb"