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"
 
  21 include_recipe "apt::grafana"
 
  22 include_recipe "networking"
 
  24 passwords = data_bag_item("prometheus", "passwords")
 
  25 tokens = data_bag_item("prometheus", "tokens")
 
  26 admins = data_bag_item("apache", "admins")
 
  28 prometheus_exporter "fastly" do
 
  30   listen_switch "listen"
 
  31   environment "FASTLY_API_TOKEN" => tokens["fastly"]
 
  34 prometheus_exporter "fastly_healthcheck" do
 
  37   environment "FASTLY_API_TOKEN" => tokens["fastly"]
 
  40 prometheus_exporter "statuscake" do
 
  44   environment "STATUSCAKE_APIKEY" => tokens["statuscake"]
 
  47 template "/etc/prometheus/cloudwatch.yml" do
 
  48   source "cloudwatch.yml.erb"
 
  54 prometheus_exporter "cloudwatch" do
 
  57   listen_switch "listen-address"
 
  59     --config.file=/etc/prometheus/cloudwatch.yml
 
  60     --enable-feature=aws-sdk-v2
 
  61     --enable-feature=always-return-info-metrics
 
  63   environment "AWS_ACCESS_KEY_ID" => "AKIASQUXHPE7JHG37EA6",
 
  64               "AWS_SECRET_ACCESS_KEY" => tokens["cloudwatch"]
 
  65   subscribes :restart, "template[/etc/prometheus/cloudwatch.yml]"
 
  68 cache_dir = Chef::Config[:file_cache_path]
 
  70 prometheus_version = "2.45.0"
 
  71 alertmanager_version = "0.25.0"
 
  72 karma_version = "0.114"
 
  74 directory "/opt/prometheus-server" do
 
  80 prometheus_arch = if arm?
 
  86 remote_file "#{cache_dir}/prometheus.linux.tar.gz" do
 
  87   source "https://github.com/prometheus/prometheus/releases/download/v#{prometheus_version}/prometheus-#{prometheus_version}.linux-#{prometheus_arch}.tar.gz"
 
  94 archive_file "#{cache_dir}/prometheus.linux.tar.gz" do
 
  96   destination "/opt/prometheus-server/prometheus"
 
 101   subscribes :extract, "remote_file[#{cache_dir}/prometheus.linux.tar.gz]", :immediately
 
 104 remote_file "#{cache_dir}/alertmanager.linux.tar.gz" do
 
 105   source "https://github.com/prometheus/alertmanager/releases/download/v#{alertmanager_version}/alertmanager-#{alertmanager_version}.linux-#{prometheus_arch}.tar.gz"
 
 112 archive_file "#{cache_dir}/alertmanager.linux.tar.gz" do
 
 114   destination "/opt/prometheus-server/alertmanager"
 
 119   subscribes :extract, "remote_file[#{cache_dir}/alertmanager.linux.tar.gz]", :immediately
 
 122 remote_file "#{cache_dir}/karma-linux.tar.gz" do
 
 123   source "https://github.com/prymitive/karma/releases/download/v#{karma_version}/karma-linux-#{prometheus_arch}.tar.gz"
 
 130 archive_file "#{cache_dir}/karma-linux.tar.gz" do
 
 132   destination "/opt/prometheus-server/karma"
 
 136   subscribes :extract, "remote_file[#{cache_dir}/karma-linux.tar.gz]", :immediately
 
 139 search(:node, "roles:gateway") do |gateway|
 
 140   allowed_ips = gateway.ipaddresses(:role => :internal).map(&:subnet)
 
 142   node.default[:networking][:wireguard][:peers] << {
 
 143     :public_key => gateway[:networking][:wireguard][:public_key],
 
 144     :allowed_ips => allowed_ips,
 
 145     :endpoint => "#{gateway.name}:51820"
 
 153 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
 
 154   if client[:prometheus][:mode] == "wireguard"
 
 155     node.default[:networking][:wireguard][:peers] << {
 
 156       :public_key => client[:networking][:wireguard][:public_key],
 
 157       :allowed_ips => client[:networking][:wireguard][:address],
 
 158       :endpoint => "#{client.name}:51820"
 
 162   client[:prometheus][:exporters].each do |key, exporter|
 
 163     if exporter.is_a?(Hash)
 
 164       name = exporter[:name]
 
 165       address = exporter[:address]
 
 167       labels = Array(exporter[:labels])
 
 168       scrape_interval = exporter[:scrape_interval]
 
 169       scrape_timeout = exporter[:scrape_timeout]
 
 170       metric_relabel = exporter[:metric_relabel] || []
 
 176       scrape_interval = nil
 
 185       :instance => client.name.split(".").first,
 
 187       :scrape_interval => scrape_interval,
 
 188       :scrape_timeout => scrape_timeout,
 
 189       :metric_relabel => metric_relabel
 
 193   Hash(client[:prometheus][:junos]).each do |instance, details|
 
 195       :instance => instance,
 
 196       :target => details[:address],
 
 197       :address => client[:prometheus][:addresses]["junos"],
 
 198       :labels => Array(details[:labels])
 
 202   Hash(client[:prometheus][:snmp]).each do |instance, details|
 
 204       :instance => instance,
 
 205       :target => details[:address],
 
 206       :modules => details[:modules],
 
 207       :address => client[:prometheus][:addresses]["snmp"],
 
 208       :labels => Array(details[:labels])
 
 213 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
 
 214   n[:letsencrypt][:certificates].each do |name, details|
 
 215     c[name] ||= details.merge(:nodes => [])
 
 219       :address => n.external_ipaddress || n.internal_ipaddress
 
 224 template "/etc/prometheus/ssl.yml" do
 
 229   variables :certificates => certificates
 
 232 prometheus_exporter "ssl" do
 
 235   options "--config.file=/etc/prometheus/ssl.yml"
 
 236   register_target false
 
 241 systemd_service "prometheus-executable" do
 
 244   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=540d"
 
 246   notifies :restart, "service[prometheus]"
 
 249 template "/etc/prometheus/prometheus.yml" do
 
 250   source "prometheus.yml.erb"
 
 254   variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates
 
 257 template "/etc/prometheus/alert_rules.yml" do
 
 258   source "alert_rules.yml.erb"
 
 264 service "prometheus" do
 
 265   action [:enable, :start]
 
 266   subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
 
 267   subscribes :reload, "template[/etc/prometheus/alert_rules.yml]"
 
 268   subscribes :restart, "archive_file[#{cache_dir}/prometheus.linux.tar.gz]"
 
 271 systemd_service "prometheus-alertmanager" do
 
 272   description "Prometheus alert manager"
 
 275   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"
 
 276   exec_reload "/bin/kill -HUP $MAINPID"
 
 279   notifies :restart, "service[prometheus-alertmanager]"
 
 282 link "/usr/local/bin/promtool" do
 
 283   to "/opt/prometheus-server/prometheus/promtool"
 
 286 template "/etc/prometheus/alertmanager.yml" do
 
 287   source "alertmanager.yml.erb"
 
 293 directory "/var/lib/prometheus/alertmanager" do
 
 299 service "prometheus-alertmanager" do
 
 300   action [:enable, :start]
 
 301   subscribes :reload, "template[/etc/prometheus/alertmanager.yml]"
 
 302   subscribes :restart, "systemd_service[prometheus-alertmanager]"
 
 303   subscribes :restart, "archive_file[#{cache_dir}/alertmanager.linux.tar.gz]"
 
 306 directory "/etc/amtool" do
 
 312 template "/etc/amtool/config.yml" do
 
 313   source "amtool.yml.erb"
 
 319 link "/usr/local/bin/amtool" do
 
 320   to "/opt/prometheus-server/alertmanager/amtool"
 
 323 template "/etc/prometheus/karma.yml" do
 
 324   source "karma.yml.erb"
 
 330 systemd_service "prometheus-karma" do
 
 331   description "Alert dashboard for Prometheus Alertmanager"
 
 333   exec_start "/opt/prometheus-server/karma/karma-linux-#{prometheus_arch} --config.file=/etc/prometheus/karma.yml"
 
 334   sandbox :enable_network => true
 
 338 service "prometheus-karma" do
 
 339   action [:enable, :start]
 
 340   subscribes :restart, "template[/etc/prometheus/karma.yml]"
 
 341   subscribes :restart, "archive_file[#{cache_dir}/karma-linux.tar.gz]"
 
 342   subscribes :restart, "systemd_service[prometheus-karma]"
 
 345 package "grafana-enterprise"
 
 347 template "/etc/grafana/grafana.ini" do
 
 348   source "grafana.ini.erb"
 
 352   variables :passwords => passwords
 
 355 service "grafana-server" do
 
 356   action [:enable, :start]
 
 357   subscribes :restart, "template[/etc/grafana/grafana.ini]"
 
 360 apache_module "alias"
 
 361 apache_module "proxy_http"
 
 362 apache_module "proxy_wstunnel"
 
 364 ssl_certificate "prometheus.openstreetmap.org" do
 
 365   domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
 
 366   notifies :reload, "service[apache2]"
 
 369 apache_site "prometheus.openstreetmap.org" do
 
 370   template "apache.erb"
 
 371   variables :admin_hosts => admins["hosts"]
 
 374 template "/etc/cron.daily/prometheus-backup" do
 
 375   source "backup.cron.erb"