]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/resources/exporter.rb
Relax security for sql_exporter to fix crash
[chef.git] / cookbooks / prometheus / resources / exporter.rb
1 #
2 # Cookbook:: prometheus
3 # Resource:: prometheus_exporter
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 unified_mode true
21
22 default_action :create
23
24 property :exporter, :kind_of => String, :name_property => true
25 property :address, :kind_of => String
26 property :port, :kind_of => Integer, :required => [:create]
27 property :listen_switch, :kind_of => String, :default => "web.listen-address"
28 property :listen_type, :kind_of => String, :default => "address"
29 property :user, :kind_of => String
30 property :group, :kind_of => String
31 property :command, :kind_of => String
32 property :options, :kind_of => [String, Array]
33 property :environment, :kind_of => Hash, :default => {}
34 property :protect_proc, String
35 property :proc_subset, String
36 property :capability_bounding_set, [String, Array]
37 property :ambient_capabilities, [String, Array]
38 property :private_devices, [true, false]
39 property :private_users, [true, false]
40 property :protect_clock, [true, false]
41 property :restrict_address_families, [String, Array]
42 property :memory_deny_write_execute, [true, false]
43 property :remove_ipc, [true, false]
44 property :system_call_filter, [String, Array]
45 property :service, :kind_of => String
46 property :labels, :kind_of => Hash, :default => {}
47 property :scrape_interval, :kind_of => String
48 property :scrape_timeout, :kind_of => String
49 property :metric_relabel, :kind_of => Array
50 property :register_target, :kind_of => [TrueClass, FalseClass], :default => true
51 property :ssh, [true, false]
52
53 action :create do
54   if new_resource.ssh && new_resource.user.nil?
55     keys = data_bag_item("prometheus", "keys")
56
57     directory "/var/lib/private/prometheus/#{new_resource.exporter}-exporter" do
58       mode "700"
59       recursive true
60     end
61
62     file "/var/lib/private/prometheus/#{new_resource.exporter}-exporter/id_rsa" do
63       content keys["ssh"].join("\n")
64       mode "400"
65     end
66
67     cookbook_file "/var/lib/private/prometheus/#{new_resource.exporter}-exporter/id_rsa.pub" do
68       mode "644"
69     end
70   end
71
72   systemd_service service_name do
73     after "network-online.target"
74     wants "network-online.target"
75     description "Prometheus #{new_resource.exporter} exporter"
76     type "simple"
77     user new_resource.user
78     dynamic_user new_resource.user.nil?
79     group new_resource.group
80     environment new_resource.environment
81     exec_start "#{executable_path} #{new_resource.command} #{executable_options}"
82     sandbox :enable_network => true
83     state_directory "prometheus/#{new_resource.exporter}-exporter" if new_resource.ssh && new_resource.user.nil?
84     protect_proc new_resource.protect_proc if new_resource.property_is_set?(:protect_proc)
85     proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset)
86     capability_bounding_set new_resource.capability_bounding_set if new_resource.property_is_set?(:capability_bounding_set)
87     ambient_capabilities new_resource.ambient_capabilities if new_resource.property_is_set?(:ambient_capabilities)
88     private_devices new_resource.private_devices if new_resource.property_is_set?(:private_devices)
89     private_users new_resource.private_users if new_resource.property_is_set?(:private_users)
90     protect_clock new_resource.protect_clock if new_resource.property_is_set?(:protect_clock)
91     restrict_address_families new_resource.restrict_address_families if new_resource.property_is_set?(:restrict_address_families)
92     memory_deny_write_execute new_resource.memory_deny_write_execute if new_resource.property_is_set?(:memory_deny_write_execute)
93     remove_ipc new_resource.remove_ipc if new_resource.property_is_set?(:remove_ipc)
94     system_call_filter new_resource.system_call_filter if new_resource.property_is_set?(:system_call_filter)
95   end
96
97   service service_name do
98     action [:enable, :start]
99     subscribes :restart, "systemd_service[#{service_name}]"
100   end
101
102   firewall_rule "accept-prometheus-#{new_resource.exporter}" do
103     action :accept
104     context :incoming
105     protocol :tcp
106     source :osm
107     dest_ports new_resource.port
108     only_if { node[:prometheus][:mode] == "external" }
109   end
110
111   node.default[:prometheus][:addresses][new_resource.exporter] = listen_address
112
113   if new_resource.register_target
114     node.default[:prometheus][:exporters][new_resource.port] = {
115       :name => new_resource.exporter,
116       :address => listen_address,
117       :labels => new_resource.labels,
118       :scrape_interval => new_resource.scrape_interval,
119       :scrape_timeout => new_resource.scrape_timeout,
120       :metric_relabel => new_resource.metric_relabel
121     }
122   end
123 end
124
125 action :delete do
126   service service_name do
127     action [:disable, :stop]
128   end
129
130   systemd_service service_name do
131     action :delete
132   end
133 end
134
135 action :restart do
136   service service_name do
137     action :restart
138     only_if { service_exists? }
139   end
140 end
141
142 action_class do
143   def service_name
144     if new_resource.service
145       "prometheus-#{new_resource.service}-exporter"
146     else
147       "prometheus-#{new_resource.exporter}-exporter"
148     end
149   end
150
151   def service_exists?
152     ::File.exist?("/etc/systemd/system/#{service_name}.service")
153   end
154
155   def executable_path
156     if ::File.exist?("#{executable_directory}/#{executable_name}_#{executable_architecture}")
157       "#{executable_directory}/#{executable_name}_#{executable_architecture}"
158     else
159       "#{executable_directory}/#{executable_name}"
160     end
161   end
162
163   def executable_directory
164     "/opt/prometheus-exporters/exporters/#{new_resource.exporter}"
165   end
166
167   def executable_name
168     "#{new_resource.exporter}_exporter"
169   end
170
171   def executable_architecture
172     node[:kernel][:machine]
173   end
174
175   def executable_options
176     "--#{new_resource.listen_switch}=#{listen_argument} #{Array(new_resource.options).join(' ')}"
177   end
178
179   def listen_argument
180     case new_resource.listen_type
181     when "address" then listen_address
182     when "url" then "http://#{listen_address}/metrics"
183     end
184   end
185
186   def listen_address
187     if new_resource.address
188       "#{new_resource.address}:#{new_resource.port}"
189     elsif node[:prometheus][:mode] == "wireguard"
190       "[#{node[:prometheus][:address]}]:#{new_resource.port}"
191     else
192       "#{node[:prometheus][:address]}:#{new_resource.port}"
193     end
194   end
195 end
196
197 def after_created
198   subscribes :restart, "archive_file[/opt/prometheus-exporters]"
199 end