]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/resources/collector.rb
Run taginfo collector via bundler
[chef.git] / cookbooks / prometheus / resources / collector.rb
1 #
2 # Cookbook:: prometheus
3 # Resource:: prometheus_collector
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 :collector, :kind_of => String, :name_property => true
25 property :interval, :kind_of => [Integer, String], :required => [:create]
26 property :user, :kind_of => String
27 property :path, :kind_of => String
28 property :options, :kind_of => [String, Array]
29 property :environment, :kind_of => Hash, :default => {}
30 property :working_directory, String
31 property :proc_subset, String
32 property :capability_bounding_set, [String, Array]
33 property :private_devices, [true, false]
34 property :private_users, [true, false]
35 property :protect_clock, [true, false]
36 property :protect_kernel_modules, [true, false]
37
38 action :create do
39   systemd_service service_name do
40     description "Prometheus #{new_resource.collector} collector"
41     type "oneshot"
42     user new_resource.user
43     dynamic_user new_resource.user.nil?
44     group "adm"
45     environment new_resource.environment
46     working_directory new_resource.working_directory
47     standard_output "file:/var/lib/prometheus/node-exporter/#{new_resource.collector}.new"
48     standard_error "journal"
49     exec_start "#{executable_path} #{executable_options}"
50     exec_start_post "/bin/mv /var/lib/prometheus/node-exporter/#{new_resource.collector}.new /var/lib/prometheus/node-exporter/#{new_resource.collector}.prom"
51     sandbox true
52     proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset)
53     capability_bounding_set new_resource.capability_bounding_set if new_resource.property_is_set?(:capability_bounding_set)
54     private_devices new_resource.private_devices if new_resource.property_is_set?(:private_devices)
55     private_users new_resource.private_users if new_resource.property_is_set?(:private_users)
56     protect_clock new_resource.protect_clock if new_resource.property_is_set?(:protect_clock)
57     protect_kernel_modules new_resource.protect_kernel_modules if new_resource.property_is_set?(:protect_kernel_modules)
58     read_write_paths ["/var/lib/prometheus/node-exporter", "/var/lock", "/var/log"]
59   end
60
61   systemd_timer service_name do
62     description "Prometheus #{new_resource.collector} collector"
63     on_boot_sec 60
64     on_unit_active_sec new_resource.interval
65   end
66
67   service "#{service_name}.timer" do
68     action [:enable, :start]
69     subscribes :restart, "systemd_timer[#{service_name}]"
70   end
71 end
72
73 action :delete do
74   service "#{service_name}.timer" do
75     action [:disable, :stop]
76   end
77
78   systemd_timer service_name do
79     action :delete
80   end
81
82   systemd_service service_name do
83     action :delete
84   end
85
86   file "/var/lib/prometheus/node-exporter/#{new_resource.collector}.prom" do
87     action :delete
88   end
89 end
90
91 action_class do
92   def service_name
93     "prometheus-#{new_resource.collector}-collector"
94   end
95
96   def executable_path
97     new_resource.path || "/opt/prometheus-exporters/collectors/#{new_resource.collector}/#{new_resource.collector}_collector"
98   end
99
100   def executable_options
101     Array(new_resource.options).join(" ")
102   end
103 end