2 # Cookbook:: prometheus
 
   5 # Copyright:: 2020, OpenStreetMap Foundation
 
   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
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  20 include_recipe "apache"
 
  22 include_recipe "networking"
 
  23 include_recipe "timescaledb"
 
  25 passwords = data_bag_item("prometheus", "passwords")
 
  26 tokens = data_bag_item("prometheus", "tokens")
 
  27 admins = data_bag_item("apache", "admins")
 
  29 prometheus_exporter "fastly" do
 
  31   listen_switch "listen"
 
  32   environment "FASTLY_API_TOKEN" => tokens["fastly"]
 
  35 cache_dir = Chef::Config[:file_cache_path]
 
  37 prometheus_version = "2.31.1"
 
  38 alertmanager_version = "0.23.0"
 
  39 karma_version = "0.93"
 
  41 directory "/opt/prometheus-server" do
 
  47 remote_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
 
  48   source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-amd64.tar.gz"
 
  55 archive_file "#{cache_dir}/prometheus.linux-amd64.tar.gz" do
 
  57   destination "/opt/prometheus-server/prometheus"
 
  62   subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
 
  65 remote_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
 
  66   source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-amd64.tar.gz"
 
  73 archive_file "#{cache_dir}/alertmanager.linux-amd64.tar.gz" do
 
  75   destination "/opt/prometheus-server/alertmanager"
 
  80   subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
 
  83 remote_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
 
  84   source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-amd64.tar.gz"
 
  91 archive_file "#{cache_dir}/karma-linux-amd64.tar.gz" do
 
  93   destination "/opt/prometheus-server/karma"
 
  97   subscribes :extract, "remote_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
 
 100 promscale_version = "0.11.0"
 
 102 database_version = node[:timescaledb][:database_version]
 
 103 database_cluster = "#{database_version}/main"
 
 107   prometheus-alertmanager
 
 108   promscale-extension-postgresql-#{database_version}
 
 111 postgresql_user "prometheus" do
 
 112   cluster database_cluster
 
 116 postgresql_database "promscale" do
 
 117   cluster database_cluster
 
 121 directory "/opt/promscale" do
 
 127 directory "/opt/promscale/bin" do
 
 133 remote_file "/opt/promscale/bin/promscale" do
 
 135   source "https://github.com/timescale/promscale/releases/download/#{promscale_version}/promscale_#{promscale_version}_Linux_x86_64"
 
 141 systemd_service "promscale" do
 
 142   description "Promscale Connector"
 
 145   exec_start "/opt/promscale/bin/promscale --db.uri postgresql:///promscale?host=/run/postgresql&port=5432 --db.connections-max 400"
 
 148   protect_system "strict"
 
 150   no_new_privileges true
 
 153 if node[:prometheus][:promscale]
 
 154   service "promscale" do
 
 155     action [:enable, :start]
 
 156     subscribes :restart, "remote_file[/opt/promscale/bin/promscale]"
 
 157     subscribes :restart, "systemd_service[promscale]"
 
 160   service "promscale" do
 
 161     action [:disable, :stop]
 
 165 search(:node, "roles:gateway") do |gateway|
 
 166   allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
 
 167     "#{interface[:network]}/#{interface[:prefix]}"
 
 170   node.default[:networking][:wireguard][:peers] << {
 
 171     :public_key => gateway[:networking][:wireguard][:public_key],
 
 172     :allowed_ips => allowed_ips,
 
 173     :endpoint => "#{gateway.name}:51820"
 
 180 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
 
 181   if client[:prometheus][:mode] == "wireguard"
 
 182     node.default[:networking][:wireguard][:peers] << {
 
 183       :public_key => client[:networking][:wireguard][:public_key],
 
 184       :allowed_ips => client[:networking][:wireguard][:address],
 
 185       :endpoint => "#{client.name}:51820"
 
 189   client[:prometheus][:exporters].each do |key, exporter|
 
 190     if exporter.is_a?(Hash)
 
 191       name = exporter[:name]
 
 192       address = exporter[:address]
 
 194       metric_relabel = exporter[:metric_relabel] || []
 
 206       :instance => client.name.split(".").first,
 
 207       :metric_relabel => metric_relabel
 
 211   Hash(client[:prometheus][:snmp]).each do |instance, details|
 
 213       :instance => instance,
 
 214       :target => details[:address],
 
 215       :module => details[:module],
 
 216       :address => client[:prometheus][:addresses]["snmp"],
 
 217       :labels => Array(details[:labels])
 
 222 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
 
 223   n[:letsencrypt][:certificates].each do |name, details|
 
 224     c[name] ||= details.merge(:nodes => [])
 
 228       :address => n.external_ipaddress || n.internal_ipaddress
 
 233 template "/etc/prometheus/ssl.yml" do
 
 238   variables :certificates => certificates
 
 241 prometheus_exporter "ssl" do
 
 244   options "--config.file=/etc/prometheus/ssl.yml"
 
 245   register_target false
 
 248 systemd_service "prometheus-executable" do
 
 251   exec_start "/opt/prometheus-server/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.external-url=https://prometheus.openstreetmap.org/prometheus --storage.tsdb.path=/var/lib/prometheus/metrics2 --storage.tsdb.retention.time=30d"
 
 253   notifies :restart, "service[prometheus]"
 
 256 template "/etc/prometheus/prometheus.yml" do
 
 257   source "prometheus.yml.erb"
 
 261   variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
 
 264 template "/etc/prometheus/alert_rules.yml" do
 
 265   source "alert_rules.yml.erb"
 
 271 service "prometheus" do
 
 272   action [:enable, :start]
 
 273   subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
 
 274   subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
 
 275   subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux-amd64.tar.gz]"
 
 278 systemd_service "prometheus-alertmanager-executable" do
 
 279   service "prometheus-alertmanager"
 
 281   exec_start "/opt/prometheus-server/alertmanager/alertmanager --config.file=/etc/prometheus/alertmanager.yml --storage.path=/var/lib/prometheus/alertmanager --web.external-url=https://prometheus.openstreetmap.org/alertmanager"
 
 282   notifies :restart, "service[prometheus-alertmanager]"
 
 285 template "/etc/prometheus/alertmanager.yml" do
 
 286   source "alertmanager.yml.erb"
 
 292 service "prometheus-alertmanager" do
 
 293   action [:enable, :start]
 
 294   subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
 
 295   subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux-amd64.tar.gz]"
 
 298 template "/etc/prometheus/amtool.yml" do
 
 299   source "amtool.yml.erb"
 
 305 template "/etc/prometheus/karma.yml" do
 
 306   source "karma.yml.erb"
 
 312 systemd_service "prometheus-karma" do
 
 313   description "Alert dashboard for Prometheus Alertmanager"
 
 315   exec_start "/opt/prometheus-server/karma/karma-linux-amd64 --config.file=/etc/prometheus/karma.yml"
 
 318   protect_system "full"
 
 320   no_new_privileges true
 
 324 service "prometheus-karma" do
 
 325   action [:enable, :start]
 
 326   subscribes :reload, "template[/etc/prometheus/karma.yml]"
 
 327   subscribes :restart, "archive_file[#{cache_dir}/karma-linux-amd64.tar.gz]"
 
 330 package "grafana-enterprise"
 
 332 template "/etc/grafana/grafana.ini" do
 
 333   source "grafana.ini.erb"
 
 337   variables :passwords => passwords
 
 340 service "grafana-server" do
 
 341   action [:enable, :start]
 
 342   subscribes :restart, "template[/etc/grafana/grafana.ini]"
 
 345 apache_module "alias"
 
 346 apache_module "proxy_http"
 
 348 ssl_certificate "prometheus.openstreetmap.org" do
 
 349   domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
 
 350   notifies :reload, "service[apache2]"
 
 353 apache_site "prometheus.openstreetmap.org" do
 
 354   template "apache.erb"
 
 355   variables :admin_hosts => admins["hosts"]
 
 358 template "/etc/cron.daily/prometheus-backup" do
 
 359   source "backup.cron.erb"