]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/resources/exporter.rb
3087f9c93084eede68b1104ac035dd7632594862
[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 :private_devices, [true, false]
37 property :protect_clock, [true, false]
38 property :restrict_address_families, [String, Array]
39 property :system_call_filter, [String, Array]
40 property :service, :kind_of => String
41 property :scrape_interval, :kind_of => String
42 property :scrape_timeout, :kind_of => String
43 property :metric_relabel, :kind_of => Array
44 property :register_target, :kind_of => [TrueClass, FalseClass], :default => true
45
46 action :create do
47   systemd_service service_name do
48     after "network-online.target"
49     wants "network-online.target"
50     description "Prometheus #{new_resource.exporter} exporter"
51     type "simple"
52     user new_resource.user
53     dynamic_user new_resource.user.nil?
54     group new_resource.group
55     environment new_resource.environment
56     exec_start "#{executable_path} #{new_resource.command} #{executable_options}"
57     sandbox :enable_network => true
58     protect_proc new_resource.protect_proc if new_resource.property_is_set?(:protect_proc)
59     proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset)
60     private_devices new_resource.private_devices if new_resource.property_is_set?(:private_devices)
61     protect_clock new_resource.protect_clock if new_resource.property_is_set?(:protect_clock)
62     restrict_address_families new_resource.restrict_address_families if new_resource.property_is_set?(:restrict_address_families)
63     system_call_filter new_resource.system_call_filter if new_resource.property_is_set?(:system_call_filter)
64   end
65
66   service service_name do
67     action [:enable, :start]
68     subscribes :restart, "systemd_service[#{service_name}]"
69   end
70
71   firewall_rule "accept-prometheus-#{new_resource.exporter}" do
72     action :accept
73     source "osm"
74     dest "fw"
75     proto "tcp:syn"
76     dest_ports new_resource.port
77     only_if { node[:prometheus][:mode] == "external" }
78   end
79
80   node.default[:prometheus][:addresses][new_resource.exporter] = listen_address
81
82   if new_resource.register_target
83     node.default[:prometheus][:exporters][new_resource.port] = {
84       :name => new_resource.exporter,
85       :address => listen_address,
86       :scrape_interval => new_resource.scrape_interval,
87       :scrape_timeout => new_resource.scrape_timeout,
88       :metric_relabel => new_resource.metric_relabel
89     }
90   end
91 end
92
93 action :delete do
94   service service_name do
95     action [:disable, :stop]
96   end
97
98   systemd_service service_name do
99     action :delete
100   end
101 end
102
103 action :restart do
104   service service_name do
105     action :restart
106     only_if { service_exists? }
107   end
108 end
109
110 action_class do
111   def service_name
112     if new_resource.service
113       "prometheus-#{new_resource.service}-exporter"
114     else
115       "prometheus-#{new_resource.exporter}-exporter"
116     end
117   end
118
119   def service_exists?
120     ::File.exist?("/etc/systemd/system/#{service_name}.service")
121   end
122
123   def executable_path
124     if ::File.exist?("#{executable_directory}/#{executable_name}_#{executable_architecture}")
125       "#{executable_directory}/#{executable_name}_#{executable_architecture}"
126     else
127       "#{executable_directory}/#{executable_name}"
128     end
129   end
130
131   def executable_directory
132     "/opt/prometheus-exporters/exporters/#{new_resource.exporter}"
133   end
134
135   def executable_name
136     "#{new_resource.exporter}_exporter"
137   end
138
139   def executable_architecture
140     node[:kernel][:machine]
141   end
142
143   def executable_options
144     "--#{new_resource.listen_switch}=#{listen_argument} #{Array(new_resource.options).join(' ')}"
145   end
146
147   def listen_argument
148     case new_resource.listen_type
149     when "address" then listen_address
150     when "url" then "http://#{listen_address}/metrics"
151     end
152   end
153
154   def listen_address
155     if new_resource.address
156       "#{new_resource.address}:#{new_resource.port}"
157     elsif node[:prometheus][:mode] == "wireguard"
158       "[#{node[:prometheus][:address]}]:#{new_resource.port}"
159     else
160       "#{node[:prometheus][:address]}:#{new_resource.port}"
161     end
162   end
163 end
164
165 def after_created
166   subscribes :restart, "git[/opt/prometheus-exporters]"
167 end