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"]
63 cache_dir = Chef::Config[:file_cache_path]
65 prometheus_version = "2.45.0"
66 alertmanager_version = "0.25.0"
67 karma_version = "0.114"
69 directory "/opt/prometheus-server" do
75 prometheus_arch = if arm?
81 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
82 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
89 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
91 destination "/opt/prometheus-server/prometheus"
96 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
99 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
100 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
107 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
109 destination "/opt/prometheus-server/alertmanager"
114 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
117 remote_file "#{cache_dir}/karma-linux.tar.gz" do
118 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
125 archive_file "#{cache_dir}/karma-linux.tar.gz" do
127 destination "/opt/prometheus-server/karma"
131 subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
134 search(:node, "roles:gateway") do |gateway|
135 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
137 node.default[:networking][:wireguard][:peers] << {
138 :public_key => gateway[:networking][:wireguard][:public_key],
139 :allowed_ips => allowed_ips,
140 :endpoint => "#{gateway.name}:51820"
148 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
149 if client[:prometheus][:mode] == "wireguard"
150 node.default[:networking][:wireguard][:peers] << {
151 :public_key => client[:networking][:wireguard][:public_key],
152 :allowed_ips => client[:networking][:wireguard][:address],
153 :endpoint => "#{client.name}:51820"
157 client[:prometheus][:exporters].each do |key, exporter|
158 if exporter.is_a?(Hash)
159 name = exporter[:name]
160 address = exporter[:address]
162 labels = Array(exporter[:labels])
163 scrape_interval = exporter[:scrape_interval]
164 scrape_timeout = exporter[:scrape_timeout]
165 metric_relabel = exporter[:metric_relabel] || []
171 scrape_interval = nil
180 :instance => client.name.split(".").first,
182 :scrape_interval => scrape_interval,
183 :scrape_timeout => scrape_timeout,
184 :metric_relabel => metric_relabel
188 Hash(client[:prometheus][:junos]).each do |instance, details|
190 :instance => instance,
191 :target => details[:address],
192 :address => client[:prometheus][:addresses]["junos"],
193 :labels => Array(details[:labels])
197 Hash(client[:prometheus][:snmp]).each do |instance, details|
199 :instance => instance,
200 :target => details[:address],
201 :modules => details[:modules],
202 :address => client[:prometheus][:addresses]["snmp"],
203 :labels => Array(details[:labels])
208 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
209 n[:letsencrypt][:certificates].each do |name, details|
210 c[name] ||= details.merge(:nodes => [])
214 :address => n.external_ipaddress || n.internal_ipaddress
219 template "/etc/prometheus/ssl.yml" do
224 variables :certificates => certificates
227 prometheus_exporter "ssl" do
230 options "--config.file=/etc/prometheus/ssl.yml"
231 register_target false
236 systemd_service "prometheus-executable" do
239 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"
241 notifies :restart, "service[prometheus]"
244 template "/etc/prometheus/prometheus.yml" do
245 source "prometheus.yml.erb"
249 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
252 template "/etc/prometheus/alert_rules.yml" do
253 source "alert_rules.yml.erb"
259 service "prometheus" do
260 action [:enable, :start]
261 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
262 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
263 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
266 systemd_service "prometheus-alertmanager" do
267 description "Prometheus alert manager"
270 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"
271 exec_reload "/bin/kill -HUP $MAINPID"
274 notifies :restart, "service[prometheus-alertmanager]"
277 link "/usr/local/bin/promtool" do
278 to "/opt/prometheus-server/prometheus/promtool"
281 template "/etc/prometheus/alertmanager.yml" do
282 source "alertmanager.yml.erb"
288 directory "/var/lib/prometheus/alertmanager" do
294 service "prometheus-alertmanager" do
295 action [:enable, :start]
296 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
297 subscribes :restart, "systemd_service[prometheus-alertmanager]"
298 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
301 directory "/etc/amtool" do
307 template "/etc/amtool/config.yml" do
308 source "amtool.yml.erb"
314 link "/usr/local/bin/amtool" do
315 to "/opt/prometheus-server/alertmanager/amtool"
318 template "/etc/prometheus/karma.yml" do
319 source "karma.yml.erb"
325 systemd_service "prometheus-karma" do
326 description "Alert dashboard for Prometheus Alertmanager"
328 exec_start "/opt/prometheus-server/karma/karma-linux-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
329 sandbox :enable_network => true
333 service "prometheus-karma" do
334 action [:enable, :start]
335 subscribes :restart, "template[/etc/prometheus/karma.yml]"
336 subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
337 subscribes :restart, "systemd_service[prometheus-karma]"
340 package "grafana-enterprise"
342 template "/etc/grafana/grafana.ini" do
343 source "grafana.ini.erb"
347 variables :passwords => passwords
350 service "grafana-server" do
351 action [:enable, :start]
352 subscribes :restart, "template[/etc/grafana/grafana.ini]"
355 apache_module "alias"
356 apache_module "proxy_http"
357 apache_module "proxy_wstunnel"
359 ssl_certificate "prometheus.openstreetmap.org" do
360 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
361 notifies :reload, "service[apache2]"
364 apache_site "prometheus.openstreetmap.org" do
365 template "apache.erb"
366 variables :admin_hosts => admins["hosts"]
369 template "/etc/cron.daily/prometheus-backup" do
370 source "backup.cron.erb"