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 promscale_version = "0.1.4"
39 database_cluster = node[:timescaledb][:cluster]
41 postgresql_user "prometheus" do
42 cluster database_cluster
46 postgresql_database "promscale" do
47 cluster database_cluster
51 postgresql_extension "timescaledb" do
52 cluster database_cluster
56 directory "/opt/promscale" do
70 git "/opt/promscale/extension" do
72 repository "https://github.com/timescale/promscale_extension.git"
78 execute "/opt/promscale/extension/Makefile" do
80 command "make install"
81 cwd "/opt/promscale/extension"
84 subscribes :run, "git[/opt/promscale/extension]", :immediate
85 notifies :restart, "service[postgresql]", :immediate
88 directory "/opt/promscale/bin" do
94 remote_file "/opt/promscale/bin/promscale" do
96 source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
102 systemd_service "promscale" do
103 description "Promscale Connector"
106 exec_start "/opt/promscale/bin/promscale --db-host /run/postgresql --db-port 5432 --db-user prometheus --db-name promscale --db-connections-max 400"
107 # 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" }
109 protect_system "strict"
111 no_new_privileges true
114 service "promscale" do
115 action [:enable, :start]
116 subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
117 subscribes :restart, "systemd_service[promscale]"
120 postgresql_extension "promscale" do
121 cluster database_cluster
123 notifies :restart, "service[promscale]"
126 systemd_service "promscale-maintenance" do
127 description "Promscale Maintenace"
130 exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
132 protect_system "strict"
134 no_new_privileges true
137 systemd_timer "promscale-maintenance" do
138 description "Promscale Maintenace"
140 on_unit_inactive_sec 1800
145 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
146 if client[:prometheus][:mode] == "wireguard"
147 node.default[:networking][:wireguard][:peers] << {
148 :public_key => client[:networking][:wireguard][:public_key],
149 :allowed_ips => client[:networking][:wireguard][:address],
150 :endpoint => "#{client.name}:51820"
154 client[:prometheus][:exporters].each do |key, exporter|
155 if exporter.is_a?(Hash)
156 name = exporter[:name]
157 address = exporter[:address]
164 jobs[name] << { :address => address, :name => client.name }
168 template "/etc/prometheus/prometheus.yml" do
169 source "prometheus.yml.erb"
173 variables :jobs => jobs
176 service "prometheus" do
177 action [:enable, :start]
178 subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
181 package "grafana-enterprise"
183 template "/etc/grafana/grafana.ini" do
184 source "grafana.ini.erb"
188 variables :passwords => passwords
191 service "grafana-server" do
192 action [:enable, :start]
193 subscribes :restart, "template[/etc/grafana/grafana.ini]"
196 apache_module "alias"
197 apache_module "proxy_http"
199 ssl_certificate "prometheus.openstreetmap.org" do
200 domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
201 notifies :reload, "service[apache2]"
204 apache_site "prometheus.openstreetmap.org" do
205 template "apache.erb"