]> git.openstreetmap.org Git - chef.git/blob - cookbooks/prometheus/recipes/default.rb
caac9aeb77dd39e6c8e5eb07158119de6af36a14
[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 if node.internal_ipaddress
24   node.default[:prometheus][:mode] = "internal"
25   node.default[:prometheus][:address] = node.internal_ipaddress
26 elsif node[:networking][:wireguard][:enabled]
27   node.default[:prometheus][:mode] = "wireguard"
28   node.default[:prometheus][:address] = node[:networking][:wireguard][:address]
29
30   search(:node, "roles:prometheus") do |server|
31     node.default[:networking][:wireguard][:peers] << {
32       :public_key => server[:networking][:wireguard][:public_key],
33       :allowed_ips => server[:networking][:wireguard][:address],
34       :endpoint => "#{server.name}:51820"
35     }
36   end
37 else
38   node.default[:prometheus][:mode] = "external"
39   node.default[:prometheus][:address] = node.external_ipaddress(:family => :inet)
40 end
41
42 directory "/opt/prometheus" do
43   action :delete
44   owner "root"
45   group "root"
46   mode "755"
47   recursive true
48   not_if { ::Dir.exist?("/opt/prometheus/.git") }
49 end
50
51 git "/opt/prometheus" do
52   action :sync
53   repository "https://github.com/openstreetmap/prometheus-exporters.git"
54   revision "main"
55   depth 1
56   user "root"
57   group "root"
58 end
59
60 directory "/etc/prometheus/collectors" do
61   owner "root"
62   group "root"
63   mode "755"
64   recursive true
65 end
66
67 directory "/etc/prometheus/exporters" do
68   owner "root"
69   group "root"
70   mode "755"
71   recursive true
72 end
73
74 directory "/var/lib/prometheus/node-exporter" do
75   owner "root"
76   group "adm"
77   mode "775"
78   recursive true
79 end
80
81 template "/var/lib/prometheus/node-exporter/chef.prom" do
82   source "chef.prom.erb"
83   owner "root"
84   group "root"
85   mode "644"
86 end
87
88 prometheus_exporter "node" do
89   port 9100
90   options %w[
91     --collector.textfile.directory=/var/lib/prometheus/node-exporter
92     --collector.interrupts
93     --collector.ntp
94     --collector.processes
95     --collector.systemd
96     --collector.tcpstat
97   ]
98 end