]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/recipes/default.rb
Use prometheus-exporters github releases instead of slow git clone
[chef.git] / cookbooks / prometheus / recipes / default.rb
1 #
2 # Cookbook:: prometheus
3 # Recipe:: default
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 include_recipe "networking"
21
22 package %w[
23   ruby
24   zstd
25 ]
26
27 if node.internal_ipaddress
28   node.default[:prometheus][:mode] = "internal"
29   node.default[:prometheus][:address] = node.internal_ipaddress
30 elsif node[:networking][:wireguard][:enabled]
31   node.default[:prometheus][:mode] = "wireguard"
32   node.default[:prometheus][:address] = node[:networking][:wireguard][:address]
33
34   search(:node, "roles:prometheus") do |server|
35     node.default[:networking][:wireguard][:peers] << {
36       :public_key => server[:networking][:wireguard][:public_key],
37       :allowed_ips => server[:networking][:wireguard][:address],
38       :endpoint => "#{server.name}:51820"
39     }
40   end
41 else
42   node.default[:prometheus][:mode] = "external"
43   node.default[:prometheus][:address] = node.external_ipaddress(:family => :inet)
44 end
45
46 directory "/opt/prometheus" do
47   action :delete
48   recursive true
49 end
50
51 cache_dir = Chef::Config[:file_cache_path]
52
53 remote_file "#{cache_dir}/prometheus-exporters.tar.zst" do
54   source "https://github.com/openstreetmap/prometheus-exporters/releases/latest/download/prometheus-exporters-#{node[:kernel][:machine]}-only.tar.zst"
55   owner "root"
56   group "root"
57   mode "644"
58   backup false
59   ignore_failure true
60 end
61
62 archive_file "/opt/prometheus-exporters" do
63   path "#{cache_dir}/prometheus-exporters.tar.zst"
64   destination "/opt/prometheus-exporters"
65   overwrite true
66   action :nothing
67   subscribes :extract, "remote_file[#{cache_dir}/prometheus-exporters.tar.zst]", :immediately
68 end
69
70 directory "/etc/prometheus/collectors" do
71   owner "root"
72   group "root"
73   mode "755"
74   recursive true
75 end
76
77 directory "/etc/prometheus/exporters" do
78   owner "root"
79   group "root"
80   mode "755"
81   recursive true
82 end
83
84 directory "/var/lib/prometheus/node-exporter" do
85   owner "root"
86   group "adm"
87   mode "775"
88   recursive true
89 end
90
91 template "/var/lib/prometheus/node-exporter/chef.prom" do
92   source "chef.prom.erb"
93   owner "root"
94   group "root"
95   mode "644"
96 end
97
98 metric_relabel = []
99
100 node[:hardware][:hwmon].each do |chip, details|
101   next unless details[:ignore]
102
103   sensors = details[:ignore].join("|")
104
105   metric_relabel << {
106     :source_labels => "chip,sensor",
107     :regex => "#{chip};(#{sensors})",
108     :action => "drop"
109   }
110 end
111
112 prometheus_exporter "node" do
113   port 9100
114   user "root"
115   proc_subset "all"
116   protect_clock false
117   restrict_address_families %w[AF_UNIX AF_NETLINK]
118   system_call_filter ["@system-service", "@clock"]
119   options %w[
120     --collector.textfile.directory=/var/lib/prometheus/node-exporter
121     --collector.interrupts
122     --collector.processes
123     --collector.rapl.enable-zone-label
124     --collector.systemd
125     --collector.tcpstat
126   ]
127   metric_relabel metric_relabel
128 end
129
130 unless node[:prometheus][:junos].empty?
131   targets = node[:prometheus][:junos].collect { |_, details| details[:address] }.sort.join(",")
132
133   prometheus_exporter "junos" do
134     port 9326
135     options %W[
136       --ssh.user=prometheus
137       --ssh.keyfile=/var/lib/prometheus/junos-exporter/id_rsa
138       --ssh.targets=#{targets}
139       --bgp.enabled=false
140       --lacp.enabled=true
141       --ldp.enabled=false
142       --ospf.enabled=false
143       --power.enabled=false
144     ]
145     ssh true
146     register_target false
147   end
148 end
149
150 unless node[:prometheus][:snmp].empty?
151   prometheus_exporter "snmp" do
152     port 9116
153     options "--config.file=/opt/prometheus-exporters/exporters/snmp/snmp.yml"
154     register_target false
155   end
156 end
157
158 if node[:prometheus][:files].empty?
159   prometheus_exporter "filestat" do
160     action :delete
161   end
162
163   file "/etc/prometheus/filestat.yml" do
164     action :delete
165   end
166 else
167   template "/etc/prometheus/filestat.yml" do
168     source "filestat.yml.erb"
169     owner "root"
170     group "root"
171     mode "644"
172   end
173
174   prometheus_exporter "filestat" do
175     port 9943
176     options "--config.file=/etc/prometheus/filestat.yml"
177     subscribes :restart, "template[/etc/prometheus/filestat.yml]"
178   end
179 end