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 cache_dir = Chef::Config[:file_cache_path]
49 prometheus_version = "2.44.0"
50 alertmanager_version = "0.25.0"
51 karma_version = "0.114"
53 directory "/opt/prometheus-server" do
59 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
60 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
67 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
69 destination "/opt/prometheus-server/prometheus"
74 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]", :immediately
77 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
78 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
85 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
87 destination "/opt/prometheus-server/alertmanager"
92 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]", :immediately
95 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
96 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
103 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
105 destination "/opt/prometheus-server/karma"
109 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]", :immediately
112 search(:node, "roles:gateway") do |gateway|
113 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
115 node.default[:networking][:wireguard][:peers] << {
116 :public_key => gateway[:networking][:wireguard][:public_key],
117 :allowed_ips => allowed_ips,
118 :endpoint => "#{gateway.name}:51820"
125 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
126 if client[:prometheus][:mode] == "wireguard"
127 node.default[:networking][:wireguard][:peers] << {
128 :public_key => client[:networking][:wireguard][:public_key],
129 :allowed_ips => client[:networking][:wireguard][:address],
130 :endpoint => "#{client.name}:51820"
134 client[:prometheus][:exporters].each do |key, exporter|
135 if exporter.is_a?(Hash)
136 name = exporter[:name]
137 address = exporter[:address]
139 labels = Array(exporter[:labels])
140 scrape_interval = exporter[:scrape_interval]
141 scrape_timeout = exporter[:scrape_timeout]
142 metric_relabel = exporter[:metric_relabel] || []
148 scrape_interval = nil
157 :instance => client.name.split(".").first,
159 :scrape_interval => scrape_interval,
160 :scrape_timeout => scrape_timeout,
161 :metric_relabel => metric_relabel
165 Hash(client[:prometheus][:snmp]).each do |instance, details|
167 :instance => instance,
168 :target => details[:address],
169 :modules => details[:modules],
170 :address => client[:prometheus][:addresses]["snmp"],
171 :labels => Array(details[:labels])
176 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
177 n[:letsencrypt][:certificates].each do |name, details|
178 c[name] ||= details.merge(:nodes => [])
182 :address => n.external_ipaddress || n.internal_ipaddress
187 template "/etc/prometheus/ssl.yml" do
192 variables :certificates => certificates
195 prometheus_exporter "ssl" do
198 options "--config.file=/etc/prometheus/ssl.yml"
199 register_target false
204 systemd_service "prometheus-executable" do
207 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"
209 notifies :restart, "service[prometheus]"
212 template "/etc/prometheus/prometheus.yml" do
213 source "prometheus.yml.erb"
217 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
220 template "/etc/prometheus/alert_rules.yml" do
221 source "alert_rules.yml.erb"
227 service "prometheus" do
228 action [:enable, :start]
229 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
230 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
231 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
234 systemd_service "prometheus-alertmanager" do
235 description "Prometheus alert manager"
238 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"
239 exec_reload "/bin/kill -HUP $MAINPID"
242 notifies :restart, "service[prometheus-alertmanager]"
245 link "/usr/local/bin/promtool" do
246 to "/opt/prometheus-server/prometheus/promtool"
249 template "/etc/prometheus/alertmanager.yml" do
250 source "alertmanager.yml.erb"
256 directory "/var/lib/prometheus/alertmanager" do
262 service "prometheus-alertmanager" do
263 action [:enable, :start]
264 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
265 subscribes :restart, "systemd_service[prometheus-alertmanager]"
266 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
269 directory "/etc/amtool" do
275 template "/etc/amtool/config.yml" do
276 source "amtool.yml.erb"
282 link "/usr/local/bin/amtool" do
283 to "/opt/prometheus-server/alertmanager/amtool"
286 template "/etc/prometheus/karma.yml" do
287 source "karma.yml.erb"
293 systemd_service "prometheus-karma" do
294 description "Alert dashboard for Prometheus Alertmanager"
296 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
297 sandbox :enable_network => true
301 service "prometheus-karma" do
302 action [:enable, :start]
303 subscribes :restart, "template[/etc/prometheus/karma.yml]"
304 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
305 subscribes :restart, "systemd_service[prometheus-karma]"
308 package "grafana-enterprise"
310 template "/etc/grafana/grafana.ini" do
311 source "grafana.ini.erb"
315 variables :passwords => passwords
318 service "grafana-server" do
319 action [:enable, :start]
320 subscribes :restart, "template[/etc/grafana/grafana.ini]"
323 apache_module "alias"
324 apache_module "proxy_http"
325 apache_module "proxy_wstunnel"
327 ssl_certificate "prometheus.openstreetmap.org" do
328 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
329 notifies :reload, "service[apache2]"
332 apache_site "prometheus.openstreetmap.org" do
333 template "apache.erb"
334 variables :admin_hosts => admins["hosts"]
337 template "/etc/cron.daily/prometheus-backup" do
338 source "backup.cron.erb"