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"
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]"
85 prometheus-alertmanager
88 promscale_version = "0.6.2"
90 database_version = node[:timescaledb][:database_version]
91 database_cluster = "#{database_version}/main"
93 postgresql_user "prometheus" do
94 cluster database_cluster
98 postgresql_database "promscale" do
99 cluster database_cluster
103 directory "/opt/promscale" do
109 cookbook_file "/usr/lib/postgresql/#{database_version}/lib/promscale.so" do
110 source "postgresql-#{database_version}-promscale.so"
116 directory "/opt/promscale/bin" do
122 remote_file "/opt/promscale/bin/promscale" do
124 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
130 systemd_service "promscale" do
131 description "Promscale Connector"
134 exec_start "/opt/promscale/bin/promscale --db-uri postgresql:///promscale?host=/run/postgresql&port=5432 --db-connections-max 400"
135 # 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" }
138 protect_system "strict"
140 no_new_privileges true
143 systemd_service "promscale-maintenance" do
144 description "Promscale Maintenance"
147 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
149 protect_system "strict"
151 no_new_privileges true
154 systemd_timer "promscale-maintenance" do
155 description "Promscale Maintenace"
157 on_unit_inactive_sec 1800
160 if node[:prometheus][:promscale]
161 service "promscale" do
162 action [:enable, :start]
163 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
164 subscribes :restart, "systemd_service[promscale]"
167 service "promscale-maintenance.timer" do
168 action [:enable, :start]
171 service "promscale" do
172 action [:disable, :stop]
175 service "promscale-maintenance.timer" do
176 action [:disable, :stop]
180 search(:node, "roles:gateway") do |gateway|
181 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
182 "#{interface[:network]}/#{interface[:prefix]}"
185 node.default[:networking][:wireguard][:peers] << {
186 :public_key => gateway[:networking][:wireguard][:public_key],
187 :allowed_ips => allowed_ips,
188 :endpoint => "#{gateway.name}:51820"
195 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
196 if client[:prometheus][:mode] == "wireguard"
197 node.default[:networking][:wireguard][:peers] << {
198 :public_key => client[:networking][:wireguard][:public_key],
199 :allowed_ips => client[:networking][:wireguard][:address],
200 :endpoint => "#{client.name}:51820"
204 client[:prometheus][:exporters].each do |key, exporter|
205 if exporter.is_a?(Hash)
206 name = exporter[:name]
207 address = exporter[:address]
208 metric_relabel = exporter[:metric_relabel] || []
218 :instance => client.name.split(".").first,
219 :metric_relabel => metric_relabel
223 Hash(client[:prometheus][:snmp]).each do |instance, details|
225 :instance => instance,
226 :target => details[:address],
227 :module => details[:module],
228 :address => client[:prometheus][:addresses]["snmp"],
229 :labels => Array(details[:labels])
234 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
235 n[:letsencrypt][:certificates].each do |name, details|
236 c[name] ||= details.merge(:nodes => [])
240 :address => n.external_ipaddress || n.internal_ipaddress
245 template "/etc/prometheus/ssl.yml" do
250 variables :certificates => certificates
253 prometheus_exporter "ssl" do
256 options "--config.file=/etc/prometheus/ssl.yml"
257 register_target false
260 systemd_service "prometheus-executable" do
263 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"
264 notifies :restart, "service[prometheus]"
267 template "/etc/prometheus/prometheus.yml" do
268 source "prometheus.yml.erb"
272 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
275 template "/etc/prometheus/alert_rules.yml" do
276 source "alert_rules.yml.erb"
282 service "prometheus" do
283 action [:enable, :start]
284 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
285 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
288 systemd_service "prometheus-alertmanager-executable" do
289 service "prometheus-alertmanager"
291 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"
292 notifies :restart, "service[prometheus-alertmanager]"
295 template "/etc/prometheus/alertmanager.yml" do
296 source "alertmanager.yml.erb"
302 service "prometheus-alertmanager" do
303 action [:enable, :start]
304 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
307 template "/etc/prometheus/amtool.yml" do
308 source "amtool.yml.erb"
314 package "grafana-enterprise"
316 template "/etc/grafana/grafana.ini" do
317 source "grafana.ini.erb"
321 variables :passwords => passwords
324 service "grafana-server" do
325 action [:enable, :start]
326 subscribes :restart, "template[/etc/grafana/grafana.ini]"
329 apache_module "alias"
330 apache_module "proxy_http"
332 ssl_certificate "prometheus.openstreetmap.org" do
333 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
334 notifies :reload, "service[apache2]"
337 apache_site "prometheus.openstreetmap.org" do
338 template "apache.erb"
339 variables :admin_hosts => admins["hosts"]
342 template "/etc/cron.daily/prometheus-backup" do
343 source "backup.cron.erb"