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