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 :command, :kind_of => String
31 property :options, :kind_of => [String, Array]
32 property :environment, :kind_of => Hash, :default => {}
33 property :proc_subset, String
34 property :private_devices, [true, false]
35 property :protect_clock, [true, false]
36 property :restrict_address_families, [String, Array]
37 property :system_call_filter, [String, Array]
38 property :service, :kind_of => String
39 property :scrape_interval, :kind_of => String
40 property :scrape_timeout, :kind_of => String
41 property :metric_relabel, :kind_of => Array
42 property :register_target, :kind_of => [TrueClass, FalseClass], :default => true
45 systemd_service service_name do
46 after "network-online.target"
47 wants "network-online.target"
48 description "Prometheus #{new_resource.exporter} exporter"
50 user new_resource.user
51 dynamic_user new_resource.user.nil?
52 environment new_resource.environment
53 exec_start "#{executable_path} #{new_resource.command} #{executable_options}"
54 sandbox :enable_network => true
55 proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset)
56 private_devices new_resource.private_devices if new_resource.property_is_set?(:private_devices)
57 protect_clock new_resource.protect_clock if new_resource.property_is_set?(:protect_clock)
58 restrict_address_families new_resource.restrict_address_families if new_resource.property_is_set?(:restrict_address_families)
59 system_call_filter new_resource.system_call_filter if new_resource.property_is_set?(:system_call_filter)
62 service service_name do
63 action [:enable, :start]
64 subscribes :restart, "systemd_service[#{service_name}]"
67 firewall_rule "accept-prometheus-#{new_resource.exporter}" do
72 dest_ports new_resource.port
73 only_if { node[:prometheus][:mode] == "external" }
76 node.default[:prometheus][:addresses][new_resource.exporter] = listen_address
78 if new_resource.register_target
79 node.default[:prometheus][:exporters][new_resource.port] = {
80 :name => new_resource.exporter,
81 :address => listen_address,
82 :scrape_interval => new_resource.scrape_interval,
83 :scrape_timeout => new_resource.scrape_timeout,
84 :metric_relabel => new_resource.metric_relabel
90 service service_name do
91 action [:disable, :stop]
94 systemd_service service_name do
100 service service_name do
102 only_if { service_exists? }
108 if new_resource.service
109 "prometheus-#{new_resource.service}-exporter"
111 "prometheus-#{new_resource.exporter}-exporter"
116 ::File.exist?("/etc/systemd/system/#{service_name}.service")
120 if ::File.exist?("#{executable_directory}/#{executable_name}_#{executable_architecture}")
121 "#{executable_directory}/#{executable_name}_#{executable_architecture}"
123 "#{executable_directory}/#{executable_name}"
127 def executable_directory
128 "/opt/prometheus-exporters/exporters/#{new_resource.exporter}"
132 "#{new_resource.exporter}_exporter"
135 def executable_architecture
136 node[:kernel][:machine]
139 def executable_options
140 "--#{new_resource.listen_switch}=#{listen_argument} #{Array(new_resource.options).join(' ')}"
144 case new_resource.listen_type
145 when "address" then listen_address
146 when "url" then "http://#{listen_address}/metrics"
151 if new_resource.address
152 "#{new_resource.address}:#{new_resource.port}"
153 elsif node[:prometheus][:mode] == "wireguard"
154 "[#{node[:prometheus][:address]}]:#{new_resource.port}"
156 "#{node[:prometheus][:address]}:#{new_resource.port}"
162 subscribes :restart, "git[/opt/prometheus-exporters]"