2 # Cookbook:: prometheus
3 # Resource:: prometheus_exporter
5 # Copyright:: 2020, OpenStreetMap Foundation
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
11 # https://www.apache.org/licenses/LICENSE-2.0
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.
22 default_action :create
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 :scrape_interval, :kind_of => String
46 property :scrape_timeout, :kind_of => String
47 property :metric_relabel, :kind_of => Array
48 property :register_target, :kind_of => [TrueClass, FalseClass], :default => true
51 systemd_service service_name do
52 after "network-online.target"
53 wants "network-online.target"
54 description "Prometheus #{new_resource.exporter} exporter"
56 user new_resource.user
57 dynamic_user new_resource.user.nil?
58 group new_resource.group
59 environment new_resource.environment
60 exec_start "#{executable_path} #{new_resource.command} #{executable_options}"
61 sandbox :enable_network => true
62 protect_proc new_resource.protect_proc if new_resource.property_is_set?(:protect_proc)
63 proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset)
64 capability_bounding_set new_resource.capability_bounding_set if new_resource.property_is_set?(:capability_bounding_set)
65 ambient_capabilities new_resource.ambient_capabilities if new_resource.property_is_set?(:ambient_capabilities)
66 private_devices new_resource.private_devices if new_resource.property_is_set?(:private_devices)
67 private_users new_resource.private_users if new_resource.property_is_set?(:private_users)
68 protect_clock new_resource.protect_clock if new_resource.property_is_set?(:protect_clock)
69 restrict_address_families new_resource.restrict_address_families if new_resource.property_is_set?(:restrict_address_families)
70 remove_ipc new_resource.remove_ipc if new_resource.property_is_set?(:remove_ipc)
71 system_call_filter new_resource.system_call_filter if new_resource.property_is_set?(:system_call_filter)
74 service service_name do
75 action [:enable, :start]
76 subscribes :restart, "systemd_service[#{service_name}]"
79 firewall_rule "accept-prometheus-#{new_resource.exporter}" do
84 dest_ports new_resource.port
85 only_if { node[:prometheus][:mode] == "external" }
88 node.default[:prometheus][:addresses][new_resource.exporter] = listen_address
90 if new_resource.register_target
91 node.default[:prometheus][:exporters][new_resource.port] = {
92 :name => new_resource.exporter,
93 :address => listen_address,
94 :scrape_interval => new_resource.scrape_interval,
95 :scrape_timeout => new_resource.scrape_timeout,
96 :metric_relabel => new_resource.metric_relabel
102 service service_name do
103 action [:disable, :stop]
106 systemd_service service_name do
112 service service_name do
114 only_if { service_exists? }
120 if new_resource.service
121 "prometheus-#{new_resource.service}-exporter"
123 "prometheus-#{new_resource.exporter}-exporter"
128 ::File.exist?("/etc/systemd/system/#{service_name}.service")
132 if ::File.exist?("#{executable_directory}/#{executable_name}_#{executable_architecture}")
133 "#{executable_directory}/#{executable_name}_#{executable_architecture}"
135 "#{executable_directory}/#{executable_name}"
139 def executable_directory
140 "/opt/prometheus-exporters/exporters/#{new_resource.exporter}"
144 "#{new_resource.exporter}_exporter"
147 def executable_architecture
148 node[:kernel][:machine]
151 def executable_options
152 "--#{new_resource.listen_switch}=#{listen_argument} #{Array(new_resource.options).join(' ')}"
156 case new_resource.listen_type
157 when "address" then listen_address
158 when "url" then "http://#{listen_address}/metrics"
163 if new_resource.address
164 "#{new_resource.address}:#{new_resource.port}"
165 elsif node[:prometheus][:mode] == "wireguard"
166 "[#{node[:prometheus][:address]}]:#{new_resource.port}"
168 "#{node[:prometheus][:address]}:#{new_resource.port}"
174 subscribes :restart, "git[/opt/prometheus-exporters]"