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