]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/resources/exporter.rb
Add infrastructure to support relabelling metrics
[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 default_action :create
21
22 property :exporter, :kind_of => String, :name_property => true
23 property :port, :kind_of => Integer, :required => [:create]
24 property :listen_switch, :kind_of => String, :default => "web.listen-address"
25 property :listen_type, :kind_of => String, :default => "address"
26 property :user, :kind_of => String, :default => "root"
27 property :command, :kind_of => String
28 property :options, :kind_of => [String, Array]
29 property :environment, :kind_of => Hash, :default => {}
30 property :service, :kind_of => String
31 property :metric_relabel, :kind_of => Array
32
33 action :create do
34   systemd_service service_name do
35     description "Prometheus #{new_resource.exporter} exporter"
36     type "simple"
37     user new_resource.user
38     environment new_resource.environment
39     exec_start "#{executable_path} #{new_resource.command} #{executable_options}"
40     private_tmp true
41     protect_system "strict"
42     protect_home true
43     no_new_privileges true
44   end
45
46   service service_name do
47     action [:enable, :start]
48     subscribes :restart, "systemd_service[#{service_name}]"
49   end
50
51   firewall_rule "accept-prometheus-#{new_resource.exporter}" do
52     action :accept
53     source "osm"
54     dest "fw"
55     proto "tcp:syn"
56     dest_ports new_resource.port
57     only_if { node[:prometheus][:mode] == "external" }
58   end
59
60   node.default[:prometheus][:exporters][new_resource.port] = {
61     :name => new_resource.exporter,
62     :address => listen_address,
63     :metric_relabel => new_resource.metric_relabel
64   }
65 end
66
67 action :delete do
68   service service_name do
69     action [:disable, :stop]
70   end
71
72   systemd_service service_name do
73     action :delete
74   end
75 end
76
77 action :restart do
78   service service_name do
79     action :restart
80   end
81 end
82
83 action_class do
84   def service_name
85     if new_resource.service
86       "prometheus-#{new_resource.service}-exporter"
87     else
88       "prometheus-#{new_resource.exporter}-exporter"
89     end
90   end
91
92   def executable_path
93     "/opt/prometheus/exporters/#{new_resource.exporter}/#{new_resource.exporter}_exporter"
94   end
95
96   def executable_options
97     "--#{new_resource.listen_switch}=#{listen_argument} #{Array(new_resource.options).join(' ')}"
98   end
99
100   def listen_argument
101     case new_resource.listen_type
102     when "address" then listen_address
103     when "url" then "http://#{listen_address}/metrics"
104     end
105   end
106
107   def listen_address
108     if node[:prometheus][:mode] == "wireguard"
109       "[#{node[:prometheus][:address]}]:#{new_resource.port}"
110     else
111       "#{node[:prometheus][:address]}:#{new_resource.port}"
112     end
113   end
114 end
115
116 def after_created
117   subscribes :restart, "git[/opt/prometheus]"
118 end