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