]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/recipes/server.rb
Configure cloudwatch exporter to collect S3 metrics
[chef.git] / cookbooks / prometheus / recipes / server.rb
1 #
2 # Cookbook:: prometheus
3 # Recipe:: server
4 #
5 # Copyright:: 2020, OpenStreetMap Foundation
6 #
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
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 include_recipe "apache"
21 include_recipe "apt::grafana"
22 include_recipe "networking"
23
24 passwords = data_bag_item("prometheus", "passwords")
25 tokens = data_bag_item("prometheus", "tokens")
26 admins = data_bag_item("apache", "admins")
27
28 prometheus_exporter "fastly" do
29   port 8080
30   listen_switch "listen"
31   environment "FASTLY_API_TOKEN" => tokens["fastly"]
32 end
33
34 prometheus_exporter "fastly_healthcheck" do
35   port 9696
36   scrape_interval "1m"
37   environment "FASTLY_API_TOKEN" => tokens["fastly"]
38 end
39
40 prometheus_exporter "statuscake" do
41   port 9595
42   scrape_interval "5m"
43   scrape_timeout "2m"
44   environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
45 end
46
47 template "/etc/prometheus/cloudwatch.yml" do
48   source "cloudwatch.yml.erb"
49   owner "root"
50   group "root"
51   mode "644"
52 end
53
54 prometheus_exporter "cloudwatch" do
55   address "127.0.0.1"
56   port 5000
57   listen_switch "listen-address"
58   options "--config.file=/etc/prometheus/cloudwatch.yml"
59   environment "AWS_ACCESS_KEY_ID" => "AKIASQUXHPE7JHG37EA6",
60               "AWS_SECRET_ACCESS_KEY" => tokens["cloudwatch"]
61 end
62
63 cache_dir = Chef::Config[:file_cache_path]
64
65 prometheus_version = "2.45.0"
66 alertmanager_version = "0.25.0"
67 karma_version = "0.114"
68
69 directory "/opt/prometheus-server" do
70   owner "root"
71   group "root"
72   mode "755"
73 end
74
75 prometheus_arch = if arm?
76                     "arm64"
77                   else
78                     "amd64"
79                   end
80
81 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
82   source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
83   owner "root"
84   group "root"
85   mode "644"
86   backup false
87 end
88
89 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
90   action :nothing
91   destination "/opt/prometheus-server/prometheus"
92   overwrite true
93   strip_components 1
94   owner "root"
95   group "root"
96   subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
97 end
98
99 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
100   source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
101   owner "root"
102   group "root"
103   mode "644"
104   backup false
105 end
106
107 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
108   action :nothing
109   destination "/opt/prometheus-server/alertmanager"
110   overwrite true
111   strip_components 1
112   owner "root"
113   group "root"
114   subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
115 end
116
117 remote_file "#{cache_dir}/karma-linux.tar.gz" do
118   source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
119   owner "root"
120   group "root"
121   mode "644"
122   backup false
123 end
124
125 archive_file "#{cache_dir}/karma-linux.tar.gz" do
126   action :nothing
127   destination "/opt/prometheus-server/karma"
128   overwrite true
129   owner "root"
130   group "root"
131   subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
132 end
133
134 search(:node, "roles:gateway") do |gateway|
135   allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
136
137   node.default[:networking][:wireguard][:peers] << {
138     :public_key => gateway[:networking][:wireguard][:public_key],
139     :allowed_ips => allowed_ips,
140     :endpoint => "#{gateway.name}:51820"
141   }
142 end
143
144 jobs = {}
145 junos_targets = []
146 snmp_targets = []
147
148 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
149   if client[:prometheus][:mode] == "wireguard"
150     node.default[:networking][:wireguard][:peers] << {
151       :public_key => client[:networking][:wireguard][:public_key],
152       :allowed_ips => client[:networking][:wireguard][:address],
153       :endpoint => "#{client.name}:51820"
154     }
155   end
156
157   client[:prometheus][:exporters].each do |key, exporter|
158     if exporter.is_a?(Hash)
159       name = exporter[:name]
160       address = exporter[:address]
161       sni = exporter[:sni]
162       labels = Array(exporter[:labels])
163       scrape_interval = exporter[:scrape_interval]
164       scrape_timeout = exporter[:scrape_timeout]
165       metric_relabel = exporter[:metric_relabel] || []
166     else
167       name = key
168       address = exporter
169       sni = nil
170       labels = []
171       scrape_interval = nil
172       scrape_timeout = nil
173       metric_relabel = []
174     end
175
176     jobs[name] ||= []
177     jobs[name] << {
178       :address => address,
179       :sni => sni,
180       :instance => client.name.split(".").first,
181       :labels => labels,
182       :scrape_interval => scrape_interval,
183       :scrape_timeout => scrape_timeout,
184       :metric_relabel => metric_relabel
185     }
186   end
187
188   Hash(client[:prometheus][:junos]).each do |instance, details|
189     junos_targets << {
190       :instance => instance,
191       :target => details[:address],
192       :address => client[:prometheus][:addresses]["junos"],
193       :labels => Array(details[:labels])
194     }
195   end
196
197   Hash(client[:prometheus][:snmp]).each do |instance, details|
198     snmp_targets << {
199       :instance => instance,
200       :target => details[:address],
201       :modules => details[:modules],
202       :address => client[:prometheus][:addresses]["snmp"],
203       :labels => Array(details[:labels])
204     }
205   end
206 end
207
208 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
209   n[:letsencrypt][:certificates].each do |name, details|
210     c[name] ||= details.merge(:nodes => [])
211
212     c[name][:nodes] << {
213       :name => n[:fqdn],
214       :address => n.external_ipaddress || n.internal_ipaddress
215     }
216   end
217 end
218
219 template "/etc/prometheus/ssl.yml" do
220   source "ssl.yml.erb"
221   owner "root"
222   group "root"
223   mode "644"
224   variables :certificates => certificates
225 end
226
227 prometheus_exporter "ssl" do
228   address "127.0.0.1"
229   port 9219
230   options "--config.file=/etc/prometheus/ssl.yml"
231   register_target false
232 end
233
234 package "prometheus"
235
236 systemd_service "prometheus-executable" do
237   service "prometheus"
238   dropin "executable"
239   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 --storage.tsdb.retention.time=540d"
240   timeout_stop_sec 300
241   notifies :restart, "service[prometheus]"
242 end
243
244 template "/etc/prometheus/prometheus.yml" do
245   source "prometheus.yml.erb"
246   owner "root"
247   group "root"
248   mode "644"
249   variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
250 end
251
252 template "/etc/prometheus/alert_rules.yml" do
253   source "alert_rules.yml.erb"
254   owner "root"
255   group "root"
256   mode "644"
257 end
258
259 service "prometheus" do
260   action [:enable, :start]
261   subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
262   subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
263   subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
264 end
265
266 systemd_service "prometheus-alertmanager" do
267   description "Prometheus alert manager"
268   type "simple"
269   user "prometheus"
270   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"
271   exec_reload "/bin/kill -HUP $MAINPID"
272   timeout_stop_sec 20
273   restart "on-failure"
274   notifies :restart, "service[prometheus-alertmanager]"
275 end
276
277 link "/usr/local/bin/promtool" do
278   to "/opt/prometheus-server/prometheus/promtool"
279 end
280
281 template "/etc/prometheus/alertmanager.yml" do
282   source "alertmanager.yml.erb"
283   owner "root"
284   group "root"
285   mode "644"
286 end
287
288 directory "/var/lib/prometheus/alertmanager" do
289   owner "prometheus"
290   group "prometheus"
291   mode "755"
292 end
293
294 service "prometheus-alertmanager" do
295   action [:enable, :start]
296   subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
297   subscribes :restart, "systemd_service[prometheus-alertmanager]"
298   subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
299 end
300
301 directory "/etc/amtool" do
302   owner "root"
303   group "root"
304   mode "755"
305 end
306
307 template "/etc/amtool/config.yml" do
308   source "amtool.yml.erb"
309   owner "root"
310   group "root"
311   mode "644"
312 end
313
314 link "/usr/local/bin/amtool" do
315   to "/opt/prometheus-server/alertmanager/amtool"
316 end
317
318 template "/etc/prometheus/karma.yml" do
319   source "karma.yml.erb"
320   owner "root"
321   group "root"
322   mode "644"
323 end
324
325 systemd_service "prometheus-karma" do
326   description "Alert dashboard for Prometheus Alertmanager"
327   user "prometheus"
328   exec_start "/opt/prometheus-server/karma/karma-linux-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
329   sandbox :enable_network => true
330   restart "on-failure"
331 end
332
333 service "prometheus-karma" do
334   action [:enable, :start]
335   subscribes :restart, "template[/etc/prometheus/karma.yml]"
336   subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
337   subscribes :restart, "systemd_service[prometheus-karma]"
338 end
339
340 package "grafana-enterprise"
341
342 template "/etc/grafana/grafana.ini" do
343   source "grafana.ini.erb"
344   owner "root"
345   group "grafana"
346   mode "640"
347   variables :passwords => passwords
348 end
349
350 service "grafana-server" do
351   action [:enable, :start]
352   subscribes :restart, "template[/etc/grafana/grafana.ini]"
353 end
354
355 apache_module "alias"
356 apache_module "proxy_http"
357 apache_module "proxy_wstunnel"
358
359 ssl_certificate "prometheus.openstreetmap.org" do
360   domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
361   notifies :reload, "service[apache2]"
362 end
363
364 apache_site "prometheus.openstreetmap.org" do
365   template "apache.erb"
366   variables :admin_hosts => admins["hosts"]
367 end
368
369 template "/etc/cron.daily/prometheus-backup" do
370   source "backup.cron.erb"
371   owner "root"
372   group "root"
373   mode "750"
374 end