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"]
37 prometheus-alertmanager
40 promscale_version = "0.6.1"
42 database_version = node[:timescaledb][:database_version]
43 database_cluster = "#{database_version}/main"
45 postgresql_user "prometheus" do
46 cluster database_cluster
50 postgresql_database "promscale" do
51 cluster database_cluster
55 directory "/opt/promscale" do
61 cookbook_file "/usr/lib/postgresql/#{database_version}/lib/promscale.so" do
62 source "postgresql-#{database_version}-promscale.so"
68 directory "/opt/promscale/bin" do
74 remote_file "/opt/promscale/bin/promscale" do
76 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
82 systemd_service "promscale" do
83 description "Promscale Connector"
86 exec_start "/opt/promscale/bin/promscale --db-uri postgresql:///promscale?host=/run/postgresql&port=5432 --db-connections-max 400"
87 # 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" }
90 protect_system "strict"
92 no_new_privileges true
95 service "promscale" do
96 action [:enable, :start]
97 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
98 subscribes :restart, "systemd_service[promscale]"
101 systemd_service "promscale-maintenance" do
102 description "Promscale Maintenance"
105 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
107 protect_system "strict"
109 no_new_privileges true
112 systemd_timer "promscale-maintenance" do
113 description "Promscale Maintenace"
115 on_unit_inactive_sec 1800
118 service "promscale-maintenance.timer" do
119 action [:enable, :start]
122 search(:node, "roles:gateway") do |gateway|
123 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
124 "#{interface[:network]}/#{interface[:prefix]}"
127 node.default[:networking][:wireguard][:peers] << {
128 :public_key => gateway[:networking][:wireguard][:public_key],
129 :allowed_ips => allowed_ips,
130 :endpoint => "#{gateway.name}:51820"
137 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
138 if client[:prometheus][:mode] == "wireguard"
139 node.default[:networking][:wireguard][:peers] << {
140 :public_key => client[:networking][:wireguard][:public_key],
141 :allowed_ips => client[:networking][:wireguard][:address],
142 :endpoint => "#{client.name}:51820"
146 client[:prometheus][:exporters].each do |key, exporter|
147 if exporter.is_a?(Hash)
148 name = exporter[:name]
149 address = exporter[:address]
150 metric_relabel = exporter[:metric_relabel] || []
160 :instance => client.name.split(".").first,
161 :metric_relabel => metric_relabel
165 Hash(client[:prometheus][:snmp]).each do |instance, details|
167 :instance => instance,
168 :target => details[:address],
169 :module => details[:module],
170 :address => client[:prometheus][:addresses]["snmp"],
171 :labels => Array(details[:labels])
176 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
177 n[:letsencrypt][:certificates].each do |name, details|
178 c[name] ||= details.merge(:nodes => [])
182 :address => n.external_ipaddress || n.internal_ipaddress
187 template "/etc/prometheus/ssl.yml" do
192 variables :certificates => certificates
195 prometheus_exporter "ssl" do
198 options "--config.file=/etc/prometheus/ssl.yml"
199 register_target false
202 template "/etc/default/prometheus" do
203 source "default.prometheus.erb"
209 template "/etc/prometheus/prometheus.yml" do
210 source "prometheus.yml.erb"
214 variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
217 template "/etc/prometheus/alert_rules.yml" do
218 source "alert_rules.yml.erb"
224 service "prometheus" do
225 action [:enable, :start]
226 subscribes :restart, "template[/etc/default/prometheus]"
227 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
228 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
231 template "/etc/default/prometheus-alertmanager" do
232 source "default.alertmanager.erb"
238 template "/etc/prometheus/alertmanager.yml" do
239 source "alertmanager.yml.erb"
245 service "prometheus-alertmanager" do
246 action [:enable, :start]
247 subscribes :restart, "template[/etc/default/prometheus-alertmanager]"
248 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
251 template "/etc/prometheus/amtool.yml" do
252 source "amtool.yml.erb"
258 package "grafana-enterprise"
260 template "/etc/grafana/grafana.ini" do
261 source "grafana.ini.erb"
265 variables :passwords => passwords
268 service "grafana-server" do
269 action [:enable, :start]
270 subscribes :restart, "template[/etc/grafana/grafana.ini]"
273 apache_module "alias"
274 apache_module "proxy_http"
276 ssl_certificate "prometheus.openstreetmap.org" do
277 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
278 notifies :reload, "service[apache2]"
281 apache_site "prometheus.openstreetmap.org" do
282 template "apache.erb"
285 template "/etc/cron.daily/prometheus-backup" do
286 source "backup.cron.erb"