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.
20 default_action :create
22 property :exporter, :kind_of => String, :name_property => true
23 property :github_owner, :kind_of => String, :default => "prometheus"
24 property :github_project, :kind_of => String
25 property :version, :kind_of => String, :required => [:create]
26 property :port, :kind_of => Integer, :required => [:create]
27 property :listen_switch, :kind_of => String, :default => "web.listen-address"
28 property :options, :kind_of => [String, Array]
31 package "prometheus-#{new_resource.exporter}-exporter" do
35 remote_file archive_file do
36 action :create_if_missing
44 execute archive_file do
46 command "tar -xf #{archive_file}"
50 subscribes :run, "remote_file[#{archive_file}]"
53 systemd_service service_name do
54 description "Prometheus #{new_resource.exporter} exporter"
57 exec_start "#{executable_path} #{executable_options}"
59 protect_system "strict"
61 no_new_privileges true
64 service service_name do
65 action [:enable, :start]
66 subscribes :restart, "systemd_service[#{service_name}]"
69 firewall_rule "accept-prometheus-#{new_resource.exporter}" do
74 dest_ports new_resource.port
75 only_if { node[:prometheus][:mode] == "external" }
78 node.default[:prometheus][:exporters][new_resource.exporter] = listen_address
82 service service_name do
83 action [:disable, :stop]
86 package package_name do
93 new_resource.github_project || "#{new_resource.exporter}_exporter"
97 "https://github.com/#{new_resource.github_owner}/#{github_project}/releases/download/v#{new_resource.version}/#{github_project}-#{new_resource.version}.linux-amd64.tar.gz"
101 "#{Chef::Config[:file_cache_path]}/prometheus-#{new_resource.exporter}-exporter-#{new_resource.version}.tar.gz"
105 "prometheus-#{new_resource.exporter}-exporter"
109 "/opt/prometheus/#{github_project}-#{new_resource.version}.linux-amd64/#{github_project}"
112 def executable_options
113 "--#{new_resource.listen_switch}=#{listen_address} #{Array(new_resource.options).join(" ")}"
117 if node[:prometheus][:mode] == "wireguard"
118 "[#{node[:prometheus][:address]}]:#{new_resource.port}"
120 "#{node[:prometheus][:address]}:#{new_resource.port}"