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")
28 prometheus_exporter "fastly" do
30 listen_switch "endpoint"
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"
40 directory "/opt/prometheus-server" do
46 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
47 source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
54 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
56 destination "/opt/prometheus-server/prometheus"
61 subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
64 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
65 source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
72 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
74 destination "/opt/prometheus-server/alertmanager"
79 subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
84 prometheus-alertmanager
87 promscale_version = "0.6.2"
89 database_version = node[:timescaledb][:database_version]
90 database_cluster = "#{database_version}/main"
92 postgresql_user "prometheus" do
93 cluster database_cluster
97 postgresql_database "promscale" do
98 cluster database_cluster
102 directory "/opt/promscale" do
108 cookbook_file "/usr/lib/postgresql/#{database_version}/lib/promscale.so" do
109 source "postgresql-#{database_version}-promscale.so"
115 directory "/opt/promscale/bin" do
121 remote_file "/opt/promscale/bin/promscale" do
123 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
129 systemd_service "promscale" do
130 description "Promscale Connector"
133 exec_start "/opt/promscale/bin/promscale --db-uri postgresql:///promscale?host=/run/postgresql&port=5432 --db-connections-max 400"
134 # exec_start lazy { "/opt/promscale/bin/promscale --db-host /run/postgresql --db-port #{node[:postgresql][:clusters][database_cluster][:port]} --db-user prometheus --db-name promscale --db-max-connections 400" }
137 protect_system "strict"
139 no_new_privileges true
142 systemd_service "promscale-maintenance" do
143 description "Promscale Maintenance"
146 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
148 protect_system "strict"
150 no_new_privileges true
153 systemd_timer "promscale-maintenance" do
154 description "Promscale Maintenace"
156 on_unit_inactive_sec 1800
159 if node[:prometheus][:promscale]
160 service "promscale" do
161 action [:enable, :start]
162 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
163 subscribes :restart, "systemd_service[promscale]"
166 service "promscale-maintenance.timer" do
167 action [:enable, :start]
170 service "promscale" do
171 action [:disable, :stop]
174 service "promscale-maintenance.timer" do
175 action [:disable, :stop]
179 search(:node, "roles:gateway") do |gateway|
180 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
181 "#{interface[:network]}/#{interface[:prefix]}"
184 node.default[:networking][:wireguard][:peers] << {
185 :public_key => gateway[:networking][:wireguard][:public_key],
186 :allowed_ips => allowed_ips,
187 :endpoint => "#{gateway.name}:51820"
194 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
195 if client[:prometheus][:mode] == "wireguard"
196 node.default[:networking][:wireguard][:peers] << {
197 :public_key => client[:networking][:wireguard][:public_key],
198 :allowed_ips => client[:networking][:wireguard][:address],
199 :endpoint => "#{client.name}:51820"
203 client[:prometheus][:exporters].each do |key, exporter|
204 if exporter.is_a?(Hash)
205 name = exporter[:name]
206 address = exporter[:address]
207 metric_relabel = exporter[:metric_relabel] || []
217 :instance => client.name.split(".").first,
218 :metric_relabel => metric_relabel
222 Hash(client[:prometheus][:snmp]).each do |instance, details|
224 :instance => instance,
225 :target => details[:address],
226 :module => details[:module],
227 :address => client[:prometheus][:addresses]["snmp"],
228 :labels => Array(details[:labels])
233 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
234 n[:letsencrypt][:certificates].each do |name, details|
235 c[name] ||= details.merge(:nodes => [])
239 :address => n.external_ipaddress || n.internal_ipaddress
244 template "/etc/prometheus/ssl.yml" do
249 variables :certificates => certificates
252 prometheus_exporter "ssl" do
255 options "--config.file=/etc/prometheus/ssl.yml"
256 register_target false
259 systemd_service "prometheus-executable" do
262 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"
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]"
287 systemd_service "prometheus-alertmanager-executable" do
288 service "prometheus-alertmanager"
290 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"
291 notifies :restart, "service[prometheus-alertmanager]"
294 template "/etc/prometheus/alertmanager.yml" do
295 source "alertmanager.yml.erb"
301 service "prometheus-alertmanager" do
302 action [:enable, :start]
303 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
306 template "/etc/prometheus/amtool.yml" do
307 source "amtool.yml.erb"
313 package "grafana-enterprise"
315 template "/etc/grafana/grafana.ini" do
316 source "grafana.ini.erb"
320 variables :passwords => passwords
323 service "grafana-server" do
324 action [:enable, :start]
325 subscribes :restart, "template[/etc/grafana/grafana.ini]"
328 apache_module "alias"
329 apache_module "proxy_http"
331 ssl_certificate "prometheus.openstreetmap.org" do
332 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
333 notifies :reload, "service[apache2]"
336 apache_site "prometheus.openstreetmap.org" do
337 template "apache.erb"
340 template "/etc/cron.daily/prometheus-backup" do
341 source "backup.cron.erb"