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]"
101 promscale_version = "0.11.0"
103 database_version = node[:timescaledb][:database_version]
104 database_cluster = "#{database_version}/main"
108 prometheus-alertmanager
109 promscale-extension-postgresql-#{database_version}
112 postgresql_user "prometheus" do
113 cluster database_cluster
117 postgresql_database "promscale" do
118 cluster database_cluster
122 directory "/opt/promscale" do
128 directory "/opt/promscale/bin" do
134 remote_file "/opt/promscale/bin/promscale" do
136 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
142 systemd_service "promscale" do
143 description "Promscale Connector"
146 exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
149 protect_system "strict"
151 no_new_privileges true
154 if node[:prometheus][:promscale]
155 service "promscale" do
156 action [:enable, :start]
157 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
158 subscribes :restart, "systemd_service[promscale]"
161 service "promscale" do
162 action [:disable, :stop]
166 search(:node, "roles:gateway") do |gateway|
167 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
168 "#{interface[:network]}/#{interface[:prefix]}"
171 node.default[:networking][:wireguard][:peers] << {
172 :public_key => gateway[:networking][:wireguard][:public_key],
173 :allowed_ips => allowed_ips,
174 :endpoint => "#{gateway.name}:51820"
181 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
182 if client[:prometheus][:mode] == "wireguard"
183 node.default[:networking][:wireguard][:peers] << {
184 :public_key => client[:networking][:wireguard][:public_key],
185 :allowed_ips => client[:networking][:wireguard][:address],
186 :endpoint => "#{client.name}:51820"
190 client[:prometheus][:exporters].each do |key, exporter|
191 if exporter.is_a?(Hash)
192 name = exporter[:name]
193 address = exporter[:address]
195 metric_relabel = exporter[:metric_relabel] || []
207 :instance => client.name.split(".").first,
208 :metric_relabel => metric_relabel
212 Hash(client[:prometheus][:snmp]).each do |instance, details|
214 :instance => instance,
215 :target => details[:address],
216 :module => details[:module],
217 :address => client[:prometheus][:addresses]["snmp"],
218 :labels => Array(details[:labels])
223 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
224 n[:letsencrypt][:certificates].each do |name, details|
225 c[name] ||= details.merge(:nodes => [])
229 :address => n.external_ipaddress || n.internal_ipaddress
234 template "/etc/prometheus/ssl.yml" do
239 variables :certificates => certificates
242 prometheus_exporter "ssl" do
245 options "--config.file=/etc/prometheus/ssl.yml"
246 register_target false
249 systemd_service "prometheus-executable" do
252 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"
253 notifies :restart, "service[prometheus]"
256 template "/etc/prometheus/prometheus.yml" do
257 source "prometheus.yml.erb"
261 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
264 template "/etc/prometheus/alert_rules.yml" do
265 source "alert_rules.yml.erb"
271 service "prometheus" do
272 action [:enable, :start]
273 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
274 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
275 subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
278 systemd_service "prometheus-alertmanager-executable" do
279 service "prometheus-alertmanager"
281 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"
282 notifies :restart, "service[prometheus-alertmanager]"
285 template "/etc/prometheus/alertmanager.yml" do
286 source "alertmanager.yml.erb"
292 service "prometheus-alertmanager" do
293 action [:enable, :start]
294 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
295 subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
298 template "/etc/prometheus/amtool.yml" do
299 source "amtool.yml.erb"
305 template "/etc/prometheus/karma.yml" do
306 source "karma.yml.erb"
312 systemd_service "prometheus-karma" do
313 description "Alert dashboard for Prometheus Alertmanager"
315 exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
318 protect_system "full"
320 no_new_privileges true
324 service "prometheus-karma" do
325 action [:enable, :start]
326 subscribes :reload, "template[/etc/prometheus/karma.yml]"
327 subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
330 package "grafana-enterprise"
332 template "/etc/grafana/grafana.ini" do
333 source "grafana.ini.erb"
337 variables :passwords => passwords
340 service "grafana-server" do
341 action [:enable, :start]
342 subscribes :restart, "template[/etc/grafana/grafana.ini]"
345 apache_module "alias"
346 apache_module "proxy_http"
348 ssl_certificate "prometheus.openstreetmap.org" do
349 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
350 notifies :reload, "service[apache2]"
353 apache_site "prometheus.openstreetmap.org" do
354 template "apache.erb"
355 variables :admin_hosts => admins["hosts"]
358 template "/etc/cron.daily/prometheus-backup" do
359 source "backup.cron.erb"