]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/recipes/server.rb
Fix cookstyle warnings
[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"
22 include_recipe "networking"
23 include_recipe "timescaledb"
24
25 passwords = data_bag_item("prometheus", "passwords")
26 tokens = data_bag_item("prometheus", "tokens")
27
28 prometheus_exporter "fastly" do
29   port 8080
30   listen_switch "endpoint"
31   listen_type "url"
32   environment "FASTLY_API_TOKEN" => tokens["fastly"]
33 end
34
35 package "prometheus"
36
37 promscale_version = "0.1.4"
38
39 database_cluster = node[:timescaledb][:cluster]
40
41 postgresql_user "prometheus" do
42   cluster database_cluster
43   createrole true
44 end
45
46 postgresql_database "promscale" do
47   cluster database_cluster
48   owner "prometheus"
49 end
50
51 postgresql_extension "timescaledb" do
52   cluster database_cluster
53   database "promscale"
54 end
55
56 directory "/opt/promscale" do
57   owner "root"
58   group "root"
59   mode "755"
60 end
61
62 package %w[
63   make
64   gcc
65   clang-9
66   llvm-9
67   cargo
68 ]
69
70 git "/opt/promscale/extension" do
71   action :sync
72   repository "https://github.com/timescale/promscale_extension.git"
73   revision "0.1.1"
74   user "root"
75   group "root"
76 end
77
78 execute "/opt/promscale/extension/Makefile" do
79   action :nothing
80   command "make install"
81   cwd "/opt/promscale/extension"
82   user "root"
83   group "root"
84   subscribes :run, "git[/opt/promscale/extension]", :immediately
85   notifies :restart, "service[postgresql]", :immediately
86 end
87
88 directory "/opt/promscale/bin" do
89   owner "root"
90   group "root"
91   mode "755"
92 end
93
94 remote_file "/opt/promscale/bin/promscale" do
95   action :create
96   source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
97   owner "root"
98   group "root"
99   mode "755"
100 end
101
102 systemd_service "promscale" do
103   description "Promscale Connector"
104   type "simple"
105   user "prometheus"
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" }
108   private_tmp true
109   protect_system "strict"
110   protect_home true
111   no_new_privileges true
112 end
113
114 service "promscale" do
115   action [:enable, :start]
116   subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
117   subscribes :restart, "systemd_service[promscale]"
118 end
119
120 postgresql_extension "promscale" do
121   cluster database_cluster
122   database "promscale"
123   notifies :restart, "service[promscale]"
124 end
125
126 systemd_service "promscale-maintenance" do
127   description "Promscale Maintenace"
128   type "simple"
129   user "prometheus"
130   exec_start "/usr/bin/psql --command='CALL prom_api.execute_maintenance()' promscale"
131   private_tmp true
132   protect_system "strict"
133   protect_home true
134   no_new_privileges true
135 end
136
137 systemd_timer "promscale-maintenance" do
138   description "Promscale Maintenace"
139   on_active_sec 1800
140   on_unit_inactive_sec 1800
141 end
142
143 jobs = {}
144
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"
151     }
152   end
153
154   client[:prometheus][:exporters].each do |key, exporter|
155     if exporter.is_a?(Hash)
156       name = exporter[:name]
157       address = exporter[:address]
158     else
159       name = key
160       address = exporter
161     end
162
163     jobs[name] ||= []
164     jobs[name] << { :address => address, :name => client.name }
165   end
166 end
167
168 template "/etc/prometheus/prometheus.yml" do
169   source "prometheus.yml.erb"
170   owner "root"
171   group "root"
172   mode "644"
173   variables :jobs => jobs
174 end
175
176 service "prometheus" do
177   action [:enable, :start]
178   subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
179 end
180
181 package "grafana-enterprise"
182
183 template "/etc/grafana/grafana.ini" do
184   source "grafana.ini.erb"
185   owner "root"
186   group "grafana"
187   mode "640"
188   variables :passwords => passwords
189 end
190
191 service "grafana-server" do
192   action [:enable, :start]
193   subscribes :restart, "template[/etc/grafana/grafana.ini]"
194 end
195
196 apache_module "alias"
197 apache_module "proxy_http"
198
199 ssl_certificate "prometheus.openstreetmap.org" do
200   domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
201   notifies :reload, "service[apache2]"
202 end
203
204 apache_site "prometheus.openstreetmap.org" do
205   template "apache.erb"
206 end