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 "apt::timescaledb"
23 include_recipe "networking"
24 include_recipe "timescaledb"
26 passwords = data_bag_item("prometheus", "passwords")
27 tokens = data_bag_item("prometheus", "tokens")
28 admins = data_bag_item("apache", "admins")
30 prometheus_exporter "fastly" do
32 listen_switch "listen"
33 environment "FASTLY_API_TOKEN" => tokens["fastly"]
36 prometheus_exporter "fastly_healthcheck" do
39 environment "FASTLY_API_TOKEN" => tokens["fastly"]
42 prometheus_exporter "statuscake" do
46 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
49 cache_dir = Chef::Config[:file_cache_path]
51 prometheus_version = "2.41.0"
52 alertmanager_version = "0.24.0"
53 karma_version = "0.111"
55 directory "/opt/prometheus-server" do
61 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
62 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
69 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
71 destination "/opt/prometheus-server/prometheus"
76 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]", :immediately
79 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
80 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
87 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
89 destination "/opt/prometheus-server/alertmanager"
94 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]", :immediately
97 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
98 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
105 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
107 destination "/opt/prometheus-server/karma"
111 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]", :immediately
114 promscale_version = "0.17.0"
115 promscale_extension_version = "0.8.0-1"
117 database_version = node[:timescaledb][:database_version]
118 database_cluster = "#{database_version}/main"
122 promscale-extension-postgresql-#{database_version}
125 package "promscale-extension-postgresql-#{database_version}" do
126 version promscale_extension_version
129 apt_preference "promscale-extension-postgresql" do
130 pin "version #{promscale_extension_version}"
134 postgresql_user "prometheus" do
135 cluster database_cluster
139 postgresql_database "promscale" do
140 cluster database_cluster
144 directory "/opt/promscale" do
150 directory "/opt/promscale/bin" do
156 remote_file "/opt/promscale/bin/promscale" do
158 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
164 systemd_service "promscale" do
165 description "Promscale Connector"
168 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
170 sandbox :enable_network => true
171 restrict_address_families "AF_UNIX"
174 if node[:prometheus][:promscale]
175 service "promscale" do
176 action [:enable, :start]
177 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
178 subscribes :restart, "systemd_service[promscale]"
181 service "promscale" do
182 action [:disable, :stop]
186 search(:node, "roles:gateway") do |gateway|
187 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
188 "#{interface[:network]}/#{interface[:prefix]}"
191 node.default[:networking][:wireguard][:peers] << {
192 :public_key => gateway[:networking][:wireguard][:public_key],
193 :allowed_ips => allowed_ips,
194 :endpoint => "#{gateway.name}:51820"
201 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
202 if client[:prometheus][:mode] == "wireguard"
203 node.default[:networking][:wireguard][:peers] << {
204 :public_key => client[:networking][:wireguard][:public_key],
205 :allowed_ips => client[:networking][:wireguard][:address],
206 :endpoint => "#{client.name}:51820"
210 client[:prometheus][:exporters].each do |key, exporter|
211 if exporter.is_a?(Hash)
212 name = exporter[:name]
213 address = exporter[:address]
215 scrape_interval = exporter[:scrape_interval]
216 scrape_timeout = exporter[:scrape_timeout]
217 metric_relabel = exporter[:metric_relabel] || []
222 scrape_interval = nil
231 :instance => client.name.split(".").first,
232 :scrape_interval => scrape_interval,
233 :scrape_timeout => scrape_timeout,
234 :metric_relabel => metric_relabel
238 Hash(client[:prometheus][:snmp]).each do |instance, details|
240 :instance => instance,
241 :target => details[:address],
242 :modules => details[:modules],
243 :address => client[:prometheus][:addresses]["snmp"],
244 :labels => Array(details[:labels])
249 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
250 n[:letsencrypt][:certificates].each do |name, details|
251 c[name] ||= details.merge(:nodes => [])
255 :address => n.external_ipaddress || n.internal_ipaddress
260 template "/etc/prometheus/ssl.yml" do
265 variables :certificates => certificates
268 prometheus_exporter "ssl" do
271 options "--config.file=/etc/prometheus/ssl.yml"
272 register_target false
275 systemd_service "prometheus-executable" do
278 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=30d"
280 notifies :restart, "service[prometheus]"
283 template "/etc/prometheus/prometheus.yml" do
284 source "prometheus.yml.erb"
288 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
291 template "/etc/prometheus/alert_rules.yml" do
292 source "alert_rules.yml.erb"
298 service "prometheus" do
299 action [:enable, :start]
300 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
301 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
302 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
305 systemd_service "prometheus-alertmanager" do
306 description "Prometheus alert manager"
309 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"
310 exec_reload "/bin/kill -HUP $MAINPID"
313 notifies :restart, "service[prometheus-alertmanager]"
316 link "/usr/local/bin/promtool" do
317 to "/opt/prometheus-server/prometheus/promtool"
320 template "/etc/prometheus/alertmanager.yml" do
321 source "alertmanager.yml.erb"
327 directory "/var/lib/prometheus/alertmanager" do
333 service "prometheus-alertmanager" do
334 action [:enable, :start]
335 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
336 subscribes :restart, "systemd_service[prometheus-alertmanager]"
337 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
340 directory "/etc/amtool" do
346 template "/etc/amtool/config.yml" do
347 source "amtool.yml.erb"
353 link "/usr/local/bin/amtool" do
354 to "/opt/prometheus-server/alertmanager/amtool"
357 template "/etc/prometheus/karma.yml" do
358 source "karma.yml.erb"
364 systemd_service "prometheus-karma" do
365 description "Alert dashboard for Prometheus Alertmanager"
367 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
368 sandbox :enable_network => true
372 service "prometheus-karma" do
373 action [:enable, :start]
374 subscribes :restart, "template[/etc/prometheus/karma.yml]"
375 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
376 subscribes :restart, "systemd_service[prometheus-karma]"
379 package "grafana-enterprise"
381 template "/etc/grafana/grafana.ini" do
382 source "grafana.ini.erb"
386 variables :passwords => passwords
389 service "grafana-server" do
390 action [:enable, :start]
391 subscribes :restart, "template[/etc/grafana/grafana.ini]"
394 apache_module "alias"
395 apache_module "proxy_http"
397 ssl_certificate "prometheus.openstreetmap.org" do
398 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
399 notifies :reload, "service[apache2]"
402 apache_site "prometheus.openstreetmap.org" do
403 template "apache.erb"
404 variables :admin_hosts => admins["hosts"]
407 template "/etc/cron.daily/prometheus-backup" do
408 source "backup.cron.erb"