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 "endpoint"
33 environment "FASTLY_API_TOKEN" => tokens["fastly"]
36 cache_dir = Chef::Config[:file_cache_path]
38 prometheus_version = "2.31.1"
39 alertmanager_version = "0.23.0"
40 karma_version = "0.93"
42 directory "/opt/prometheus-server" do
48 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
49 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
56 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
58 destination "/opt/prometheus-server/prometheus"
63 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
66 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
67 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
74 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
76 destination "/opt/prometheus-server/alertmanager"
81 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
84 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
85 source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
92 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
94 destination "/opt/prometheus-server/karma"
98 subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
103 prometheus-alertmanager
106 promscale_version = "0.10.0"
107 promscale_extension_version = "0.3.2"
109 database_version = node[:timescaledb][:database_version]
110 database_cluster = "#{database_version}/main"
112 postgresql_user "prometheus" do
113 cluster database_cluster
117 postgresql_database "promscale" do
118 cluster database_cluster
122 directory "/opt/promscale" do
128 remote_file "#{cache_dir}/promscale_extension.pg#{database_version}.x86_64.deb" do
129 source "https://github.com/timescale/promscale_extension/releases/download/#{promscale_extension_version}/promscale_extension-#{promscale_extension_version}.pg#{database_version}.x86_64.deb"
136 dpkg_package "promscale-extension-postgresql-#{database_version}" do
137 source "#{cache_dir}/promscale_extension.pg#{database_version}.x86_64.deb"
138 version "#{promscale_extension_version}-1"
141 directory "/opt/promscale/bin" do
147 remote_file "/opt/promscale/bin/promscale" do
149 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
155 systemd_service "promscale" do
156 description "Promscale Connector"
159 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
162 protect_system "strict"
164 no_new_privileges true
167 systemd_service "promscale-maintenance" do
168 description "Promscale Maintenance"
171 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
173 protect_system "strict"
175 no_new_privileges true
178 systemd_timer "promscale-maintenance" do
179 description "Promscale Maintenace"
181 on_unit_inactive_sec 1800
184 if node[:prometheus][:promscale]
185 service "promscale" do
186 action [:enable, :start]
187 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
188 subscribes :restart, "systemd_service[promscale]"
191 service "promscale-maintenance.timer" do
192 action [:enable, :start]
195 service "promscale" do
196 action [:disable, :stop]
199 service "promscale-maintenance.timer" do
200 action [:disable, :stop]
204 search(:node, "roles:gateway") do |gateway|
205 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
206 "#{interface[:network]}/#{interface[:prefix]}"
209 node.default[:networking][:wireguard][:peers] << {
210 :public_key => gateway[:networking][:wireguard][:public_key],
211 :allowed_ips => allowed_ips,
212 :endpoint => "#{gateway.name}:51820"
219 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
220 if client[:prometheus][:mode] == "wireguard"
221 node.default[:networking][:wireguard][:peers] << {
222 :public_key => client[:networking][:wireguard][:public_key],
223 :allowed_ips => client[:networking][:wireguard][:address],
224 :endpoint => "#{client.name}:51820"
228 client[:prometheus][:exporters].each do |key, exporter|
229 if exporter.is_a?(Hash)
230 name = exporter[:name]
231 address = exporter[:address]
233 metric_relabel = exporter[:metric_relabel] || []
245 :instance => client.name.split(".").first,
246 :metric_relabel => metric_relabel
250 Hash(client[:prometheus][:snmp]).each do |instance, details|
252 :instance => instance,
253 :target => details[:address],
254 :module => details[:module],
255 :address => client[:prometheus][:addresses]["snmp"],
256 :labels => Array(details[:labels])
261 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
262 n[:letsencrypt][:certificates].each do |name, details|
263 c[name] ||= details.merge(:nodes => [])
267 :address => n.external_ipaddress || n.internal_ipaddress
272 template "/etc/prometheus/ssl.yml" do
277 variables :certificates => certificates
280 prometheus_exporter "ssl" do
283 options "--config.file=/etc/prometheus/ssl.yml"
284 register_target false
287 systemd_service "prometheus-executable" do
290 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"
291 notifies :restart, "service[prometheus]"
294 template "/etc/prometheus/prometheus.yml" do
295 source "prometheus.yml.erb"
299 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
302 template "/etc/prometheus/alert_rules.yml" do
303 source "alert_rules.yml.erb"
309 service "prometheus" do
310 action [:enable, :start]
311 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
312 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
313 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
316 systemd_service "prometheus-alertmanager-executable" do
317 service "prometheus-alertmanager"
319 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"
320 notifies :restart, "service[prometheus-alertmanager]"
323 template "/etc/prometheus/alertmanager.yml" do
324 source "alertmanager.yml.erb"
330 service "prometheus-alertmanager" do
331 action [:enable, :start]
332 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
333 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
336 template "/etc/prometheus/amtool.yml" do
337 source "amtool.yml.erb"
343 template "/etc/prometheus/karma.yml" do
344 source "karma.yml.erb"
350 systemd_service "prometheus-karma" do
351 description "Alert dashboard for Prometheus Alertmanager"
353 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
356 protect_system "full"
358 no_new_privileges true
362 service "prometheus-karma" do
363 action [:enable, :start]
364 subscribes :reload, "template[/etc/prometheus/karma.yml]"
365 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
368 package "grafana-enterprise"
370 template "/etc/grafana/grafana.ini" do
371 source "grafana.ini.erb"
375 variables :passwords => passwords
378 service "grafana-server" do
379 action [:enable, :start]
380 subscribes :restart, "template[/etc/grafana/grafana.ini]"
383 apache_module "alias"
384 apache_module "proxy_http"
386 ssl_certificate "prometheus.openstreetmap.org" do
387 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
388 notifies :reload, "service[apache2]"
391 apache_site "prometheus.openstreetmap.org" do
392 template "apache.erb"
393 variables :admin_hosts => admins["hosts"]
396 template "/etc/cron.daily/prometheus-backup" do
397 source "backup.cron.erb"