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 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"
126 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
127 if client[:prometheus][:mode] == "wireguard"
128 node.default[:networking][:wireguard][:peers] << {
129 :public_key => client[:networking][:wireguard][:public_key],
130 :allowed_ips => client[:networking][:wireguard][:address],
131 :endpoint => "#{client.name}:51820"
135 client[:prometheus][:exporters].each do |key, exporter|
136 if exporter.is_a?(Hash)
137 name = exporter[:name]
138 address = exporter[:address]
140 labels = Array(exporter[:labels])
141 scrape_interval = exporter[:scrape_interval]
142 scrape_timeout = exporter[:scrape_timeout]
143 metric_relabel = exporter[:metric_relabel] || []
149 scrape_interval = nil
158 :instance => client.name.split(".").first,
160 :scrape_interval => scrape_interval,
161 :scrape_timeout => scrape_timeout,
162 :metric_relabel => metric_relabel
166 Hash(client[:prometheus][:junos]).each do |instance, details|
168 :instance => instance,
169 :target => details[:address],
170 :address => client[:prometheus][:addresses]["junos"],
171 :labels => Array(details[:labels])
175 Hash(client[:prometheus][:snmp]).each do |instance, details|
177 :instance => instance,
178 :target => details[:address],
179 :modules => details[:modules],
180 :address => client[:prometheus][:addresses]["snmp"],
181 :labels => Array(details[:labels])
186 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
187 n[:letsencrypt][:certificates].each do |name, details|
188 c[name] ||= details.merge(:nodes => [])
192 :address => n.external_ipaddress || n.internal_ipaddress
197 template "/etc/prometheus/ssl.yml" do
202 variables :certificates => certificates
205 prometheus_exporter "ssl" do
208 options "--config.file=/etc/prometheus/ssl.yml"
209 register_target false
214 systemd_service "prometheus-executable" do
217 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"
219 notifies :restart, "service[prometheus]"
222 template "/etc/prometheus/prometheus.yml" do
223 source "prometheus.yml.erb"
227 variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
230 template "/etc/prometheus/alert_rules.yml" do
231 source "alert_rules.yml.erb"
237 service "prometheus" do
238 action [:enable, :start]
239 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
240 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
241 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
244 systemd_service "prometheus-alertmanager" do
245 description "Prometheus alert manager"
248 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"
249 exec_reload "/bin/kill -HUP $MAINPID"
252 notifies :restart, "service[prometheus-alertmanager]"
255 link "/usr/local/bin/promtool" do
256 to "/opt/prometheus-server/prometheus/promtool"
259 template "/etc/prometheus/alertmanager.yml" do
260 source "alertmanager.yml.erb"
266 directory "/var/lib/prometheus/alertmanager" do
272 service "prometheus-alertmanager" do
273 action [:enable, :start]
274 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
275 subscribes :restart, "systemd_service[prometheus-alertmanager]"
276 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
279 directory "/etc/amtool" do
285 template "/etc/amtool/config.yml" do
286 source "amtool.yml.erb"
292 link "/usr/local/bin/amtool" do
293 to "/opt/prometheus-server/alertmanager/amtool"
296 template "/etc/prometheus/karma.yml" do
297 source "karma.yml.erb"
303 systemd_service "prometheus-karma" do
304 description "Alert dashboard for Prometheus Alertmanager"
306 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
307 sandbox :enable_network => true
311 service "prometheus-karma" do
312 action [:enable, :start]
313 subscribes :restart, "template[/etc/prometheus/karma.yml]"
314 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
315 subscribes :restart, "systemd_service[prometheus-karma]"
318 package "grafana-enterprise"
320 template "/etc/grafana/grafana.ini" do
321 source "grafana.ini.erb"
325 variables :passwords => passwords
328 service "grafana-server" do
329 action [:enable, :start]
330 subscribes :restart, "template[/etc/grafana/grafana.ini]"
333 apache_module "alias"
334 apache_module "proxy_http"
335 apache_module "proxy_wstunnel"
337 ssl_certificate "prometheus.openstreetmap.org" do
338 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
339 notifies :reload, "service[apache2]"
342 apache_site "prometheus.openstreetmap.org" do
343 template "apache.erb"
344 variables :admin_hosts => admins["hosts"]
347 template "/etc/cron.daily/prometheus-backup" do
348 source "backup.cron.erb"