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"
22 include_recipe "networking"
23 include_recipe "timescaledb"
25 passwords = data_bag_item("prometheus", "passwords")
26 tokens = data_bag_item("prometheus", "tokens")
27 admins = data_bag_item("apache", "admins")
29 prometheus_exporter "fastly" do
31 listen_switch "listen"
32 environment "FASTLY_API_TOKEN" => tokens["fastly"]
35 prometheus_exporter "fastly_healthcheck" do
37 environment "FASTLY_API_TOKEN" => tokens["fastly"]
40 prometheus_exporter "statuscake" do
42 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
45 cache_dir = Chef::Config[:file_cache_path]
47 prometheus_version = "2.31.1"
48 alertmanager_version = "0.23.0"
49 karma_version = "0.93"
51 directory "/opt/prometheus-server" do
57 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
58 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
65 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
67 destination "/opt/prometheus-server/prometheus"
72 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
75 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
76 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
83 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
85 destination "/opt/prometheus-server/alertmanager"
90 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
93 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
94 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
101 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
103 destination "/opt/prometheus-server/karma"
107 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
110 promscale_version = "0.11.0"
112 database_version = node[:timescaledb][:database_version]
113 database_cluster = "#{database_version}/main"
117 prometheus-alertmanager
118 promscale-extension-postgresql-#{database_version}
121 postgresql_user "prometheus" do
122 cluster database_cluster
126 postgresql_database "promscale" do
127 cluster database_cluster
131 directory "/opt/promscale" do
137 directory "/opt/promscale/bin" do
143 remote_file "/opt/promscale/bin/promscale" do
145 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
151 systemd_service "promscale" do
152 description "Promscale Connector"
155 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
158 protect_system "strict"
160 no_new_privileges true
163 if node[:prometheus][:promscale]
164 service "promscale" do
165 action [:enable, :start]
166 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
167 subscribes :restart, "systemd_service[promscale]"
170 service "promscale" do
171 action [:disable, :stop]
175 search(:node, "roles:gateway") do |gateway|
176 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
177 "#{interface[:network]}/#{interface[:prefix]}"
180 node.default[:networking][:wireguard][:peers] << {
181 :public_key => gateway[:networking][:wireguard][:public_key],
182 :allowed_ips => allowed_ips,
183 :endpoint => "#{gateway.name}:51820"
190 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
191 if client[:prometheus][:mode] == "wireguard"
192 node.default[:networking][:wireguard][:peers] << {
193 :public_key => client[:networking][:wireguard][:public_key],
194 :allowed_ips => client[:networking][:wireguard][:address],
195 :endpoint => "#{client.name}:51820"
199 client[:prometheus][:exporters].each do |key, exporter|
200 if exporter.is_a?(Hash)
201 name = exporter[:name]
202 address = exporter[:address]
204 metric_relabel = exporter[:metric_relabel] || []
216 :instance => client.name.split(".").first,
217 :metric_relabel => metric_relabel
221 Hash(client[:prometheus][:snmp]).each do |instance, details|
223 :instance => instance,
224 :target => details[:address],
225 :modules => details[:modules],
226 :address => client[:prometheus][:addresses]["snmp"],
227 :labels => Array(details[:labels])
232 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
233 n[:letsencrypt][:certificates].each do |name, details|
234 c[name] ||= details.merge(:nodes => [])
238 :address => n.external_ipaddress || n.internal_ipaddress
243 template "/etc/prometheus/ssl.yml" do
248 variables :certificates => certificates
251 prometheus_exporter "ssl" do
254 options "--config.file=/etc/prometheus/ssl.yml"
255 register_target false
258 systemd_service "prometheus-executable" do
261 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"
263 notifies :restart, "service[prometheus]"
266 template "/etc/prometheus/prometheus.yml" do
267 source "prometheus.yml.erb"
271 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
274 template "/etc/prometheus/alert_rules.yml" do
275 source "alert_rules.yml.erb"
281 service "prometheus" do
282 action [:enable, :start]
283 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
284 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
285 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
288 systemd_service "prometheus-alertmanager-executable" do
289 service "prometheus-alertmanager"
291 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"
292 notifies :restart, "service[prometheus-alertmanager]"
295 template "/etc/prometheus/alertmanager.yml" do
296 source "alertmanager.yml.erb"
302 service "prometheus-alertmanager" do
303 action [:enable, :start]
304 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
305 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
308 template "/etc/prometheus/amtool.yml" do
309 source "amtool.yml.erb"
315 template "/etc/prometheus/karma.yml" do
316 source "karma.yml.erb"
322 systemd_service "prometheus-karma" do
323 description "Alert dashboard for Prometheus Alertmanager"
325 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
328 protect_system "full"
330 no_new_privileges true
334 service "prometheus-karma" do
335 action [:enable, :start]
336 subscribes :reload, "template[/etc/prometheus/karma.yml]"
337 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
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"
358 ssl_certificate "prometheus.openstreetmap.org" do
359 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
360 notifies :reload, "service[apache2]"
363 apache_site "prometheus.openstreetmap.org" do
364 template "apache.erb"
365 variables :admin_hosts => admins["hosts"]
368 template "/etc/cron.daily/prometheus-backup" do
369 source "backup.cron.erb"