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 "statuscake" do
37 environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
40 cache_dir = Chef::Config[:file_cache_path]
42 prometheus_version = "2.31.1"
43 alertmanager_version = "0.23.0"
44 karma_version = "0.93"
46 directory "/opt/prometheus-server" do
52 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
53 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
60 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
62 destination "/opt/prometheus-server/prometheus"
67 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
70 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
71 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
78 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
80 destination "/opt/prometheus-server/alertmanager"
85 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
88 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
89 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
96 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
98 destination "/opt/prometheus-server/karma"
102 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
105 promscale_version = "0.11.0"
107 database_version = node[:timescaledb][:database_version]
108 database_cluster = "#{database_version}/main"
112 prometheus-alertmanager
113 promscale-extension-postgresql-#{database_version}
116 postgresql_user "prometheus" do
117 cluster database_cluster
121 postgresql_database "promscale" do
122 cluster database_cluster
126 directory "/opt/promscale" do
132 directory "/opt/promscale/bin" do
138 remote_file "/opt/promscale/bin/promscale" do
140 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
146 systemd_service "promscale" do
147 description "Promscale Connector"
150 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
153 protect_system "strict"
155 no_new_privileges true
158 if node[:prometheus][:promscale]
159 service "promscale" do
160 action [:enable, :start]
161 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
162 subscribes :restart, "systemd_service[promscale]"
165 service "promscale" do
166 action [:disable, :stop]
170 search(:node, "roles:gateway") do |gateway|
171 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
172 "#{interface[:network]}/#{interface[:prefix]}"
175 node.default[:networking][:wireguard][:peers] << {
176 :public_key => gateway[:networking][:wireguard][:public_key],
177 :allowed_ips => allowed_ips,
178 :endpoint => "#{gateway.name}:51820"
185 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
186 if client[:prometheus][:mode] == "wireguard"
187 node.default[:networking][:wireguard][:peers] << {
188 :public_key => client[:networking][:wireguard][:public_key],
189 :allowed_ips => client[:networking][:wireguard][:address],
190 :endpoint => "#{client.name}:51820"
194 client[:prometheus][:exporters].each do |key, exporter|
195 if exporter.is_a?(Hash)
196 name = exporter[:name]
197 address = exporter[:address]
199 metric_relabel = exporter[:metric_relabel] || []
211 :instance => client.name.split(".").first,
212 :metric_relabel => metric_relabel
216 Hash(client[:prometheus][:snmp]).each do |instance, details|
218 :instance => instance,
219 :target => details[:address],
220 :modules => details[:modules],
221 :address => client[:prometheus][:addresses]["snmp"],
222 :labels => Array(details[:labels])
227 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
228 n[:letsencrypt][:certificates].each do |name, details|
229 c[name] ||= details.merge(:nodes => [])
233 :address => n.external_ipaddress || n.internal_ipaddress
238 template "/etc/prometheus/ssl.yml" do
243 variables :certificates => certificates
246 prometheus_exporter "ssl" do
249 options "--config.file=/etc/prometheus/ssl.yml"
250 register_target false
253 systemd_service "prometheus-executable" do
256 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"
258 notifies :restart, "service[prometheus]"
261 template "/etc/prometheus/prometheus.yml" do
262 source "prometheus.yml.erb"
266 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
269 template "/etc/prometheus/alert_rules.yml" do
270 source "alert_rules.yml.erb"
276 service "prometheus" do
277 action [:enable, :start]
278 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
279 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
280 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
283 systemd_service "prometheus-alertmanager-executable" do
284 service "prometheus-alertmanager"
286 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"
287 notifies :restart, "service[prometheus-alertmanager]"
290 template "/etc/prometheus/alertmanager.yml" do
291 source "alertmanager.yml.erb"
297 service "prometheus-alertmanager" do
298 action [:enable, :start]
299 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
300 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
303 template "/etc/prometheus/amtool.yml" do
304 source "amtool.yml.erb"
310 template "/etc/prometheus/karma.yml" do
311 source "karma.yml.erb"
317 systemd_service "prometheus-karma" do
318 description "Alert dashboard for Prometheus Alertmanager"
320 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
323 protect_system "full"
325 no_new_privileges true
329 service "prometheus-karma" do
330 action [:enable, :start]
331 subscribes :reload, "template[/etc/prometheus/karma.yml]"
332 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
335 package "grafana-enterprise"
337 template "/etc/grafana/grafana.ini" do
338 source "grafana.ini.erb"
342 variables :passwords => passwords
345 service "grafana-server" do
346 action [:enable, :start]
347 subscribes :restart, "template[/etc/grafana/grafana.ini]"
350 apache_module "alias"
351 apache_module "proxy_http"
353 ssl_certificate "prometheus.openstreetmap.org" do
354 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
355 notifies :reload, "service[apache2]"
358 apache_site "prometheus.openstreetmap.org" do
359 template "apache.erb"
360 variables :admin_hosts => admins["hosts"]
363 template "/etc/cron.daily/prometheus-backup" do
364 source "backup.cron.erb"