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 "networking"
24 passwords = data_bag_item("prometheus", "passwords")
25 tokens = data_bag_item("prometheus", "tokens")
26 admins = data_bag_item("apache", "admins")
28 prometheus_exporter "fastly" do
30 listen_switch "listen"
31 environment "FASTLY_API_TOKEN" => tokens["fastly"]
34 prometheus_exporter "fastly_healthcheck" do
37 environment "FASTLY_API_TOKEN" => tokens["fastly"]
40 prometheus_exporter "statuscake" do
44 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
47 template "/etc/prometheus/cloudwatch.yml" do
48 source "cloudwatch.yml.erb"
54 prometheus_exporter "cloudwatch" do
57 listen_switch "listen-address"
58 options "--config.file=/etc/prometheus/cloudwatch.yml"
59 environment "AWS_ACCESS_KEY_ID" => "AKIASQUXHPE7JHG37EA6",
60 "AWS_SECRET_ACCESS_KEY" => tokens["cloudwatch"]
61 subscribes :restart, "template[/etc/prometheus/cloudwatch.yml]"
64 cache_dir = Chef::Config[:file_cache_path]
66 prometheus_version = "2.45.0"
67 alertmanager_version = "0.25.0"
68 karma_version = "0.114"
70 directory "/opt/prometheus-server" do
76 prometheus_arch = if arm?
82 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
83 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
90 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
92 destination "/opt/prometheus-server/prometheus"
97 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
100 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
101 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
108 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
110 destination "/opt/prometheus-server/alertmanager"
115 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
118 remote_file "#{cache_dir}/karma-linux.tar.gz" do
119 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
126 archive_file "#{cache_dir}/karma-linux.tar.gz" do
128 destination "/opt/prometheus-server/karma"
132 subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
135 search(:node, "roles:gateway") do |gateway|
136 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
138 node.default[:networking][:wireguard][:peers] << {
139 :public_key => gateway[:networking][:wireguard][:public_key],
140 :allowed_ips => allowed_ips,
141 :endpoint => "#{gateway.name}:51820"
149 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
150 if client[:prometheus][:mode] == "wireguard"
151 node.default[:networking][:wireguard][:peers] << {
152 :public_key => client[:networking][:wireguard][:public_key],
153 :allowed_ips => client[:networking][:wireguard][:address],
154 :endpoint => "#{client.name}:51820"
158 client[:prometheus][:exporters].each do |key, exporter|
159 if exporter.is_a?(Hash)
160 name = exporter[:name]
161 address = exporter[:address]
163 labels = Array(exporter[:labels])
164 scrape_interval = exporter[:scrape_interval]
165 scrape_timeout = exporter[:scrape_timeout]
166 metric_relabel = exporter[:metric_relabel] || []
172 scrape_interval = nil
181 :instance => client.name.split(".").first,
183 :scrape_interval => scrape_interval,
184 :scrape_timeout => scrape_timeout,
185 :metric_relabel => metric_relabel
189 Hash(client[:prometheus][:junos]).each do |instance, details|
191 :instance => instance,
192 :target => details[:address],
193 :address => client[:prometheus][:addresses]["junos"],
194 :labels => Array(details[:labels])
198 Hash(client[:prometheus][:snmp]).each do |instance, details|
200 :instance => instance,
201 :target => details[:address],
202 :modules => details[:modules],
203 :address => client[:prometheus][:addresses]["snmp"],
204 :labels => Array(details[:labels])
209 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
210 n[:letsencrypt][:certificates].each do |name, details|
211 c[name] ||= details.merge(:nodes => [])
215 :address => n.external_ipaddress || n.internal_ipaddress
220 template "/etc/prometheus/ssl.yml" do
225 variables :certificates => certificates
228 prometheus_exporter "ssl" do
231 options "--config.file=/etc/prometheus/ssl.yml"
232 register_target false
237 systemd_service "prometheus-executable" do
240 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=540d"
242 notifies :restart, "service[prometheus]"
245 template "/etc/prometheus/prometheus.yml" do
246 source "prometheus.yml.erb"
250 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
253 template "/etc/prometheus/alert_rules.yml" do
254 source "alert_rules.yml.erb"
260 service "prometheus" do
261 action [:enable, :start]
262 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
263 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
264 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
267 systemd_service "prometheus-alertmanager" do
268 description "Prometheus alert manager"
271 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"
272 exec_reload "/bin/kill -HUP $MAINPID"
275 notifies :restart, "service[prometheus-alertmanager]"
278 link "/usr/local/bin/promtool" do
279 to "/opt/prometheus-server/prometheus/promtool"
282 template "/etc/prometheus/alertmanager.yml" do
283 source "alertmanager.yml.erb"
289 directory "/var/lib/prometheus/alertmanager" do
295 service "prometheus-alertmanager" do
296 action [:enable, :start]
297 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
298 subscribes :restart, "systemd_service[prometheus-alertmanager]"
299 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
302 directory "/etc/amtool" do
308 template "/etc/amtool/config.yml" do
309 source "amtool.yml.erb"
315 link "/usr/local/bin/amtool" do
316 to "/opt/prometheus-server/alertmanager/amtool"
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-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
330 sandbox :enable_network => true
334 service "prometheus-karma" do
335 action [:enable, :start]
336 subscribes :restart, "template[/etc/prometheus/karma.yml]"
337 subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
338 subscribes :restart, "systemd_service[prometheus-karma]"
341 package "grafana-enterprise"
343 template "/etc/grafana/grafana.ini" do
344 source "grafana.ini.erb"
348 variables :passwords => passwords
351 service "grafana-server" do
352 action [:enable, :start]
353 subscribes :restart, "template[/etc/grafana/grafana.ini]"
356 apache_module "alias"
357 apache_module "proxy_http"
358 apache_module "proxy_wstunnel"
360 ssl_certificate "prometheus.openstreetmap.org" do
361 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
362 notifies :reload, "service[apache2]"
365 apache_site "prometheus.openstreetmap.org" do
366 template "apache.erb"
367 variables :admin_hosts => admins["hosts"]
370 template "/etc/cron.daily/prometheus-backup" do
371 source "backup.cron.erb"