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 aws_credentials = data_bag_item("prometheus", "aws")
28 admins = data_bag_item("apache", "admins")
30 prometheus_exporter "fastly" do
32 listen_switch "listen"
33 environment "FASTLY_API_TOKEN" => tokens["fastly"]
36 prometheus_exporter "fastly_healthcheck" do
40 environment "FASTLY_API_TOKEN" => tokens["fastly"]
43 prometheus_exporter "statuscake" do
47 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
50 template "/etc/prometheus/cloudwatch.yml" do
51 source "cloudwatch.yml.erb"
57 prometheus_exporter "cloudwatch" do
60 listen_switch "listen-address"
62 --config.file=/etc/prometheus/cloudwatch.yml
63 --enable-feature=aws-sdk-v2
64 --enable-feature=always-return-info-metrics
66 environment "AWS_ACCESS_KEY_ID" => aws_credentials["cloudwatch_access_key_id"],
67 "AWS_SECRET_ACCESS_KEY" => aws_credentials["cloudwatch_secret_access_key"]
68 subscribes :restart, "template[/etc/prometheus/cloudwatch.yml]"
71 cache_dir = Chef::Config[:file_cache_path]
73 prometheus_version = "3.7.3"
74 alertmanager_version = "0.29.0"
75 karma_version = "0.122"
77 directory "/opt/prometheus-server" do
83 prometheus_arch = if arm?
89 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
90 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
97 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
99 destination "/opt/prometheus-server/prometheus"
104 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
107 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
108 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
115 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
117 destination "/opt/prometheus-server/alertmanager"
122 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
125 remote_file "#{cache_dir}/karma-linux.tar.gz" do
126 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
133 archive_file "#{cache_dir}/karma-linux.tar.gz" do
135 destination "/opt/prometheus-server/karma"
139 subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
142 search(:node, "roles:gateway") do |gateway|
143 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
145 node.default[:networking][:wireguard][:peers] << {
146 :public_key => gateway[:networking][:wireguard][:public_key],
147 :allowed_ips => allowed_ips,
148 :endpoint => "#{gateway.name}:51820"
156 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
157 if client[:prometheus][:mode] == "wireguard"
158 node.default[:networking][:wireguard][:peers] << {
159 :public_key => client[:networking][:wireguard][:public_key],
160 :allowed_ips => client[:networking][:wireguard][:address],
161 :endpoint => "#{client.name}:51820"
165 client[:prometheus][:exporters].each do |key, exporter|
166 if exporter.is_a?(Hash)
167 name = exporter[:name]
168 address = exporter[:address]
170 labels = Array(exporter[:labels])
171 scrape_interval = exporter[:scrape_interval]
172 scrape_timeout = exporter[:scrape_timeout]
173 metric_relabel = exporter[:metric_relabel] || []
179 scrape_interval = nil
188 :instance => client.name.split(".").first,
190 :scrape_interval => scrape_interval,
191 :scrape_timeout => scrape_timeout,
192 :metric_relabel => metric_relabel
196 Hash(client[:prometheus][:junos]).each do |instance, details|
198 :instance => instance,
199 :target => details[:address],
200 :address => client[:prometheus][:addresses]["junos"],
201 :labels => Array(details[:labels])
205 Hash(client[:prometheus][:snmp]).each do |instance, details|
207 :instance => instance,
208 :target => details[:address],
209 :modules => details[:modules],
210 :address => client[:prometheus][:addresses]["snmp"],
211 :labels => Array(details[:labels])
216 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
217 n[:letsencrypt][:certificates].each do |name, details|
218 c[name] ||= details.merge(:nodes => [])
222 :address => n.external_ipaddress || n.internal_ipaddress
227 template "/etc/prometheus/ssl.yml" do
232 variables :certificates => certificates
235 prometheus_exporter "ssl" do
238 options "--config.file=/etc/prometheus/ssl.yml"
239 register_target false
244 systemd_service "prometheus-executable" do
247 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"
249 notifies :restart, "service[prometheus]"
252 template "/etc/prometheus/prometheus.yml" do
253 source "prometheus.yml.erb"
257 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
260 template "/etc/prometheus/alert_rules.yml" do
261 source "alert_rules.yml.erb"
267 service "prometheus" do
268 action [:enable, :start]
269 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
270 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
271 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
274 systemd_service "prometheus-alertmanager" do
275 description "Prometheus alert manager"
278 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"
279 exec_reload "/bin/kill -HUP $MAINPID"
282 notifies :restart, "service[prometheus-alertmanager]"
285 link "/usr/local/bin/promtool" do
286 to "/opt/prometheus-server/prometheus/promtool"
289 template "/etc/prometheus/alertmanager.yml" do
290 source "alertmanager.yml.erb"
296 directory "/var/lib/prometheus/alertmanager" do
302 service "prometheus-alertmanager" do
303 action [:enable, :start]
304 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
305 subscribes :restart, "systemd_service[prometheus-alertmanager]"
306 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
309 directory "/etc/amtool" do
315 template "/etc/amtool/config.yml" do
316 source "amtool.yml.erb"
322 link "/usr/local/bin/amtool" do
323 to "/opt/prometheus-server/alertmanager/amtool"
326 template "/etc/prometheus/karma.yml" do
327 source "karma.yml.erb"
333 systemd_service "prometheus-karma" do
334 description "Alert dashboard for Prometheus Alertmanager"
336 exec_start "/opt/prometheus-server/karma/karma-linux-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
337 sandbox :enable_network => true
341 service "prometheus-karma" do
342 action [:enable, :start]
343 subscribes :restart, "template[/etc/prometheus/karma.yml]"
344 subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
345 subscribes :restart, "systemd_service[prometheus-karma]"
348 package "grafana-enterprise"
350 template "/etc/grafana/grafana.ini" do
351 source "grafana.ini.erb"
355 variables :passwords => passwords
358 service "grafana-server" do
359 action [:enable, :start]
360 subscribes :restart, "template[/etc/grafana/grafana.ini]"
363 apache_module "alias"
364 apache_module "proxy_http"
365 apache_module "proxy_wstunnel"
367 ssl_certificate "prometheus.openstreetmap.org" do
368 domains ["prometheus.openstreetmap.org", "prometheus.osm.org", "munin.openstreetmap.org", "munin.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"
389 directory "/var/lib/prometheus/.aws" do
395 template "/var/lib/prometheus/.aws/credentials" do
396 source "aws-credentials.erb"
400 variables :aws_credentials => aws_credentials
404 template "/usr/local/bin/prometheus-backup-data" do
405 source "backup-data.erb"
411 systemd_service "prometheus-backup-data" do
412 description "Backup prometheus data to S3"
414 exec_start "/usr/local/bin/prometheus-backup-data"
416 /var/lib/prometheus/.aws
417 /var/lib/prometheus/metrics2/snapshots
419 sandbox :enable_network => true
422 systemd_timer "prometheus-backup-data" do
423 description "Backup prometheus data to S3"
427 service "prometheus-backup-data.timer" do
428 action [:enable, :start]