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.45.0"
50 alertmanager_version = "0.25.0"
51 karma_version = "0.114"
53 directory "/opt/prometheus-server" do
59 prometheus_arch = if arm?
65 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
66 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
73 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
75 destination "/opt/prometheus-server/prometheus"
80 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
83 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
84 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
91 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
93 destination "/opt/prometheus-server/alertmanager"
98 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
101 remote_file "#{cache_dir}/karma-linux.tar.gz" do
102 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
109 archive_file "#{cache_dir}/karma-linux.tar.gz" do
111 destination "/opt/prometheus-server/karma"
115 subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
118 search(:node, "roles:gateway") do |gateway|
119 allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
121 node.default[:networking][:wireguard][:peers] << {
122 :public_key => gateway[:networking][:wireguard][:public_key],
123 :allowed_ips => allowed_ips,
124 :endpoint => "#{gateway.name}:51820"
132 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
133 if client[:prometheus][:mode] == "wireguard"
134 node.default[:networking][:wireguard][:peers] << {
135 :public_key => client[:networking][:wireguard][:public_key],
136 :allowed_ips => client[:networking][:wireguard][:address],
137 :endpoint => "#{client.name}:51820"
141 client[:prometheus][:exporters].each do |key, exporter|
142 if exporter.is_a?(Hash)
143 name = exporter[:name]
144 address = exporter[:address]
146 labels = Array(exporter[:labels])
147 scrape_interval = exporter[:scrape_interval]
148 scrape_timeout = exporter[:scrape_timeout]
149 metric_relabel = exporter[:metric_relabel] || []
155 scrape_interval = nil
164 :instance => client.name.split(".").first,
166 :scrape_interval => scrape_interval,
167 :scrape_timeout => scrape_timeout,
168 :metric_relabel => metric_relabel
172 Hash(client[:prometheus][:junos]).each do |instance, details|
174 :instance => instance,
175 :target => details[:address],
176 :address => client[:prometheus][:addresses]["junos"],
177 :labels => Array(details[:labels])
181 Hash(client[:prometheus][:snmp]).each do |instance, details|
183 :instance => instance,
184 :target => details[:address],
185 :modules => details[:modules],
186 :address => client[:prometheus][:addresses]["snmp"],
187 :labels => Array(details[:labels])
192 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
193 n[:letsencrypt][:certificates].each do |name, details|
194 c[name] ||= details.merge(:nodes => [])
198 :address => n.external_ipaddress || n.internal_ipaddress
203 template "/etc/prometheus/ssl.yml" do
208 variables :certificates => certificates
211 prometheus_exporter "ssl" do
214 options "--config.file=/etc/prometheus/ssl.yml"
215 register_target false
220 systemd_service "prometheus-executable" do
223 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"
225 notifies :restart, "service[prometheus]"
228 template "/etc/prometheus/prometheus.yml" do
229 source "prometheus.yml.erb"
233 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
236 template "/etc/prometheus/alert_rules.yml" do
237 source "alert_rules.yml.erb"
243 service "prometheus" do
244 action [:enable, :start]
245 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
246 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
247 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
250 systemd_service "prometheus-alertmanager" do
251 description "Prometheus alert manager"
254 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"
255 exec_reload "/bin/kill -HUP $MAINPID"
258 notifies :restart, "service[prometheus-alertmanager]"
261 link "/usr/local/bin/promtool" do
262 to "/opt/prometheus-server/prometheus/promtool"
265 template "/etc/prometheus/alertmanager.yml" do
266 source "alertmanager.yml.erb"
272 directory "/var/lib/prometheus/alertmanager" do
278 service "prometheus-alertmanager" do
279 action [:enable, :start]
280 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
281 subscribes :restart, "systemd_service[prometheus-alertmanager]"
282 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
285 directory "/etc/amtool" do
291 template "/etc/amtool/config.yml" do
292 source "amtool.yml.erb"
298 link "/usr/local/bin/amtool" do
299 to "/opt/prometheus-server/alertmanager/amtool"
302 template "/etc/prometheus/karma.yml" do
303 source "karma.yml.erb"
309 systemd_service "prometheus-karma" do
310 description "Alert dashboard for Prometheus Alertmanager"
312 exec_start "/opt/prometheus-server/karma/karma-linux-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
313 sandbox :enable_network => true
317 service "prometheus-karma" do
318 action [:enable, :start]
319 subscribes :restart, "template[/etc/prometheus/karma.yml]"
320 subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
321 subscribes :restart, "systemd_service[prometheus-karma]"
324 package "grafana-enterprise"
326 template "/etc/grafana/grafana.ini" do
327 source "grafana.ini.erb"
331 variables :passwords => passwords
334 service "grafana-server" do
335 action [:enable, :start]
336 subscribes :restart, "template[/etc/grafana/grafana.ini]"
339 apache_module "alias"
340 apache_module "proxy_http"
341 apache_module "proxy_wstunnel"
343 ssl_certificate "prometheus.openstreetmap.org" do
344 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
345 notifies :reload, "service[apache2]"
348 apache_site "prometheus.openstreetmap.org" do
349 template "apache.erb"
350 variables :admin_hosts => admins["hosts"]
353 template "/etc/cron.daily/prometheus-backup" do
354 source "backup.cron.erb"