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 cache_dir = Chef::Config[:file_cache_path]
37 prometheus_version = "2.31.1"
38 alertmanager_version = "0.23.0"
39 karma_version = "0.93"
41 directory "/opt/prometheus-server" do
47 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
48 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
55 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
57 destination "/opt/prometheus-server/prometheus"
62 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
65 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
66 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
73 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
75 destination "/opt/prometheus-server/alertmanager"
80 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
83 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
84 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
91 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
93 destination "/opt/prometheus-server/karma"
97 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
100 promscale_version = "0.11.0"
102 database_version = node[:timescaledb][:database_version]
103 database_cluster = "#{database_version}/main"
107 prometheus-alertmanager
108 promscale-extension-postgresql-#{database_version}
111 postgresql_user "prometheus" do
112 cluster database_cluster
116 postgresql_database "promscale" do
117 cluster database_cluster
121 directory "/opt/promscale" do
127 directory "/opt/promscale/bin" do
133 remote_file "/opt/promscale/bin/promscale" do
135 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
141 systemd_service "promscale" do
142 description "Promscale Connector"
145 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
148 protect_system "strict"
150 no_new_privileges true
153 if node[:prometheus][:promscale]
154 service "promscale" do
155 action [:enable, :start]
156 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
157 subscribes :restart, "systemd_service[promscale]"
160 service "promscale" do
161 action [:disable, :stop]
165 search(:node, "roles:gateway") do |gateway|
166 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
167 "#{interface[:network]}/#{interface[:prefix]}"
170 node.default[:networking][:wireguard][:peers] << {
171 :public_key => gateway[:networking][:wireguard][:public_key],
172 :allowed_ips => allowed_ips,
173 :endpoint => "#{gateway.name}:51820"
180 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
181 if client[:prometheus][:mode] == "wireguard"
182 node.default[:networking][:wireguard][:peers] << {
183 :public_key => client[:networking][:wireguard][:public_key],
184 :allowed_ips => client[:networking][:wireguard][:address],
185 :endpoint => "#{client.name}:51820"
189 client[:prometheus][:exporters].each do |key, exporter|
190 if exporter.is_a?(Hash)
191 name = exporter[:name]
192 address = exporter[:address]
194 metric_relabel = exporter[:metric_relabel] || []
206 :instance => client.name.split(".").first,
207 :metric_relabel => metric_relabel
211 Hash(client[:prometheus][:snmp]).each do |instance, details|
213 :instance => instance,
214 :target => details[:address],
215 :module => details[:module],
216 :address => client[:prometheus][:addresses]["snmp"],
217 :labels => Array(details[:labels])
222 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
223 n[:letsencrypt][:certificates].each do |name, details|
224 c[name] ||= details.merge(:nodes => [])
228 :address => n.external_ipaddress || n.internal_ipaddress
233 template "/etc/prometheus/ssl.yml" do
238 variables :certificates => certificates
241 prometheus_exporter "ssl" do
244 options "--config.file=/etc/prometheus/ssl.yml"
245 register_target false
248 systemd_service "prometheus-executable" do
251 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"
252 notifies :restart, "service[prometheus]"
255 template "/etc/prometheus/prometheus.yml" do
256 source "prometheus.yml.erb"
260 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
263 template "/etc/prometheus/alert_rules.yml" do
264 source "alert_rules.yml.erb"
270 service "prometheus" do
271 action [:enable, :start]
272 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
273 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
274 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
277 systemd_service "prometheus-alertmanager-executable" do
278 service "prometheus-alertmanager"
280 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"
281 notifies :restart, "service[prometheus-alertmanager]"
284 template "/etc/prometheus/alertmanager.yml" do
285 source "alertmanager.yml.erb"
291 service "prometheus-alertmanager" do
292 action [:enable, :start]
293 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
294 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
297 template "/etc/prometheus/amtool.yml" do
298 source "amtool.yml.erb"
304 template "/etc/prometheus/karma.yml" do
305 source "karma.yml.erb"
311 systemd_service "prometheus-karma" do
312 description "Alert dashboard for Prometheus Alertmanager"
314 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
317 protect_system "full"
319 no_new_privileges true
323 service "prometheus-karma" do
324 action [:enable, :start]
325 subscribes :reload, "template[/etc/prometheus/karma.yml]"
326 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
329 package "grafana-enterprise"
331 template "/etc/grafana/grafana.ini" do
332 source "grafana.ini.erb"
336 variables :passwords => passwords
339 service "grafana-server" do
340 action [:enable, :start]
341 subscribes :restart, "template[/etc/grafana/grafana.ini]"
344 apache_module "alias"
345 apache_module "proxy_http"
347 ssl_certificate "prometheus.openstreetmap.org" do
348 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
349 notifies :reload, "service[apache2]"
352 apache_site "prometheus.openstreetmap.org" do
353 template "apache.erb"
354 variables :admin_hosts => admins["hosts"]
357 template "/etc/cron.daily/prometheus-backup" do
358 source "backup.cron.erb"