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