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"
21 include_recipe "apt::grafana"
22 include_recipe "awscli"
23 include_recipe "networking"
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 template "/etc/prometheus/cloudwatch.yml" do
49 source "cloudwatch.yml.erb"
55 prometheus_exporter "cloudwatch" do
58 listen_switch "listen-address"
60 --config.file=/etc/prometheus/cloudwatch.yml
61 --enable-feature=aws-sdk-v2
62 --enable-feature=always-return-info-metrics
64 environment "AWS_ACCESS_KEY_ID" => "AKIASQUXHPE7JHG37EA6",
65 "AWS_SECRET_ACCESS_KEY" => tokens["cloudwatch"]
66 subscribes :restart, "template[/etc/prometheus/cloudwatch.yml]"
69 cache_dir = Chef::Config[:file_cache_path]
71 prometheus_version = "2.45.0"
72 alertmanager_version = "0.25.0"
73 karma_version = "0.114"
75 directory "/opt/prometheus-server" do
81 prometheus_arch = if arm?
87 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
88 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
95 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
97 destination "/opt/prometheus-server/prometheus"
102 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
105 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
106 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
113 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
115 destination "/opt/prometheus-server/alertmanager"
120 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
123 remote_file "#{cache_dir}/karma-linux.tar.gz" do
124 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
131 archive_file "#{cache_dir}/karma-linux.tar.gz" do
133 destination "/opt/prometheus-server/karma"
137 subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
140 search(:node, "roles:gateway") do |gateway|
141 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
143 node.default[:networking][:wireguard][:peers] << {
144 :public_key => gateway[:networking][:wireguard][:public_key],
145 :allowed_ips => allowed_ips,
146 :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]
168 labels = Array(exporter[:labels])
169 scrape_interval = exporter[:scrape_interval]
170 scrape_timeout = exporter[:scrape_timeout]
171 metric_relabel = exporter[:metric_relabel] || []
177 scrape_interval = nil
186 :instance => client.name.split(".").first,
188 :scrape_interval => scrape_interval,
189 :scrape_timeout => scrape_timeout,
190 :metric_relabel => metric_relabel
194 Hash(client[:prometheus][:junos]).each do |instance, details|
196 :instance => instance,
197 :target => details[:address],
198 :address => client[:prometheus][:addresses]["junos"],
199 :labels => Array(details[:labels])
203 Hash(client[:prometheus][:snmp]).each do |instance, details|
205 :instance => instance,
206 :target => details[:address],
207 :modules => details[:modules],
208 :address => client[:prometheus][:addresses]["snmp"],
209 :labels => Array(details[:labels])
214 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
215 n[:letsencrypt][:certificates].each do |name, details|
216 c[name] ||= details.merge(:nodes => [])
220 :address => n.external_ipaddress || n.internal_ipaddress
225 template "/etc/prometheus/ssl.yml" do
230 variables :certificates => certificates
233 prometheus_exporter "ssl" do
236 options "--config.file=/etc/prometheus/ssl.yml"
237 register_target false
242 systemd_service "prometheus-executable" do
245 exec_start "/opt/prometheus-server/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api --web.external-url=https://prometheus.openstreetmap.org/prometheus --storage.tsdb.path=/var/lib/prometheus/metrics2 --storage.tsdb.retention.time=540d"
247 notifies :restart, "service[prometheus]"
250 template "/etc/prometheus/prometheus.yml" do
251 source "prometheus.yml.erb"
255 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
258 template "/etc/prometheus/alert_rules.yml" do
259 source "alert_rules.yml.erb"
265 service "prometheus" do
266 action [:enable, :start]
267 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
268 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
269 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
272 systemd_service "prometheus-alertmanager" do
273 description "Prometheus alert manager"
276 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"
277 exec_reload "/bin/kill -HUP $MAINPID"
280 notifies :restart, "service[prometheus-alertmanager]"
283 link "/usr/local/bin/promtool" do
284 to "/opt/prometheus-server/prometheus/promtool"
287 template "/etc/prometheus/alertmanager.yml" do
288 source "alertmanager.yml.erb"
294 directory "/var/lib/prometheus/alertmanager" do
300 service "prometheus-alertmanager" do
301 action [:enable, :start]
302 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
303 subscribes :restart, "systemd_service[prometheus-alertmanager]"
304 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
307 directory "/etc/amtool" do
313 template "/etc/amtool/config.yml" do
314 source "amtool.yml.erb"
320 link "/usr/local/bin/amtool" do
321 to "/opt/prometheus-server/alertmanager/amtool"
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-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
335 sandbox :enable_network => true
339 service "prometheus-karma" do
340 action [:enable, :start]
341 subscribes :restart, "template[/etc/prometheus/karma.yml]"
342 subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
343 subscribes :restart, "systemd_service[prometheus-karma]"
346 package "grafana-enterprise"
348 template "/etc/grafana/grafana.ini" do
349 source "grafana.ini.erb"
353 variables :passwords => passwords
356 service "grafana-server" do
357 action [:enable, :start]
358 subscribes :restart, "template[/etc/grafana/grafana.ini]"
361 apache_module "alias"
362 apache_module "proxy_http"
363 apache_module "proxy_wstunnel"
365 ssl_certificate "prometheus.openstreetmap.org" do
366 domains ["prometheus.openstreetmap.org", "prometheus.osm.org", "munin.openstreetmap.org", "munin.osm.org"]
367 notifies :reload, "service[apache2]"
370 apache_site "prometheus.openstreetmap.org" do
371 template "apache.erb"
372 variables :admin_hosts => admins["hosts"]
375 template "/etc/cron.daily/prometheus-backup" do
376 source "backup.cron.erb"
387 directory "/var/lib/prometheus/.aws" do
393 template "/var/lib/prometheus/.aws/credentials" do
394 source "aws-credentials.erb"
398 variables :passwords => passwords
401 template "/usr/local/bin/prometheus-backup-data" do
402 source "backup-data.erb"
408 systemd_service "prometheus-backup-data" do
409 description "Backup prometheus data to S3"
411 exec_start "/usr/local/bin/prometheus-backup-data"
413 /var/lib/prometheus/.aws
414 /var/lib/prometheus/metrics2/snapshots
416 sandbox :enable_network => true
419 systemd_timer "prometheus-backup-data" do
420 description "Backup prometheus data to S3"
424 service "prometheus-backup-data.timer" do
425 action [:enable, :start]