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.1.4"
42 database_cluster = node[:timescaledb][:cluster]
44 postgresql_user "prometheus" do
45 cluster database_cluster
49 postgresql_database "promscale" do
50 cluster database_cluster
54 postgresql_extension "timescaledb" do
55 cluster database_cluster
59 directory "/opt/promscale" do
73 git "/opt/promscale/extension" do
75 repository "https://github.com/timescale/promscale_extension.git"
81 execute "/opt/promscale/extension/Makefile" do
83 command "make install"
84 cwd "/opt/promscale/extension"
87 subscribes :run, "git[/opt/promscale/extension]", :immediately
88 notifies :restart, "service[postgresql]", :immediately
91 directory "/opt/promscale/bin" do
97 remote_file "/opt/promscale/bin/promscale" do
99 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
105 systemd_service "promscale" do
106 description "Promscale Connector"
109 exec_start "/opt/promscale/bin/promscale --db-host /run/postgresql --db-port 5432 --db-user prometheus --db-name promscale --db-connections-max 400"
110 # 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" }
113 protect_system "strict"
115 no_new_privileges true
118 service "promscale" do
119 action [:enable, :start]
120 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
121 subscribes :restart, "systemd_service[promscale]"
124 postgresql_extension "promscale" do
125 cluster database_cluster
127 notifies :restart, "service[promscale]"
130 systemd_service "promscale-maintenance" do
131 description "Promscale Maintenace"
134 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
136 protect_system "strict"
138 no_new_privileges true
141 systemd_timer "promscale-maintenance" do
142 description "Promscale Maintenace"
144 on_unit_inactive_sec 1800
147 service "promscale-maintenance.timer" do
148 action [:enable, :start]
153 search(:node, "roles:gateway") do |gateway|
154 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
155 "#{interface[:network]}/#{interface[:prefix]}"
158 node.default[:networking][:wireguard][:peers] << {
159 :public_key => gateway[:networking][:wireguard][:public_key],
160 :allowed_ips => allowed_ips,
161 :endpoint => "#{gateway.name}:51820"
165 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
166 if client[:prometheus][:mode] == "wireguard"
167 node.default[:networking][:wireguard][:peers] << {
168 :public_key => client[:networking][:wireguard][:public_key],
169 :allowed_ips => client[:networking][:wireguard][:address],
170 :endpoint => "#{client.name}:51820"
174 client[:prometheus][:exporters].each do |key, exporter|
175 if exporter.is_a?(Hash)
176 name = exporter[:name]
177 address = exporter[:address]
178 metric_relabel = exporter[:metric_relabel] || []
188 :instance => client.name.split(".").first,
189 :metric_relabel => metric_relabel
194 template "/etc/default/prometheus" do
195 source "default.prometheus.erb"
201 template "/etc/prometheus/prometheus.yml" do
202 source "prometheus.yml.erb"
206 variables :jobs => jobs
209 template "/etc/prometheus/alert_rules.yml" do
210 source "alert_rules.yml.erb"
216 service "prometheus" do
217 action [:enable, :start]
218 subscribes :restart, "template[/etc/default/prometheus]"
219 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
220 subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
223 template "/etc/default/prometheus-alertmanager" do
224 source "default.alertmanager.erb"
230 template "/etc/prometheus/alertmanager.yml" do
231 source "alertmanager.yml.erb"
237 service "prometheus-alertmanager" do
238 action [:enable, :start]
239 subscribes :restart, "template[/etc/default/prometheus-alertmanager]"
240 subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
243 template "/etc/prometheus/amtool.yml" do
244 source "amtool.yml.erb"
250 package "grafana-enterprise"
252 template "/etc/grafana/grafana.ini" do
253 source "grafana.ini.erb"
257 variables :passwords => passwords
260 service "grafana-server" do
261 action [:enable, :start]
262 subscribes :restart, "template[/etc/grafana/grafana.ini]"
265 apache_module "alias"
266 apache_module "proxy_http"
268 ssl_certificate "prometheus.openstreetmap.org" do
269 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
270 notifies :reload, "service[apache2]"
273 apache_site "prometheus.openstreetmap.org" do
274 template "apache.erb"
277 template "/etc/cron.daily/prometheus-backup" do
278 source "backup.cron.erb"