From: Tom Hughes Date: Fri, 11 Sep 2020 13:45:10 +0000 (+0000) Subject: Add initial version of prometheus cookbook X-Git-Url: https://git.openstreetmap.org./chef.git/commitdiff_plain/524d90d022751710980613df6b943291fc1498d6 Add initial version of prometheus cookbook --- diff --git a/.github/workflows/test-kitchen.yml b/.github/workflows/test-kitchen.yml index d8a39bb19..acb40b6f4 100644 --- a/.github/workflows/test-kitchen.yml +++ b/.github/workflows/test-kitchen.yml @@ -74,6 +74,8 @@ jobs: - planet-notes - planet-replication - postgresql + - prometheus + - prometheus-server - python - rsyncd - serverinfo diff --git a/.kitchen.yml b/.kitchen.yml index b7bff87ba..7c446effc 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -275,6 +275,12 @@ suites: postgresql: versions: - 10 + - name: prometheus + run_list: + - recipe[prometheus::default] + - name: prometheus-server + run_list: + - recipe[prometheus::server] - name: python run_list: - recipe[python::default] diff --git a/cookbooks/prometheus/README.md b/cookbooks/prometheus/README.md new file mode 100644 index 000000000..e24566d0b --- /dev/null +++ b/cookbooks/prometheus/README.md @@ -0,0 +1,12 @@ +# Prometheus Cookbook + +This cookbook configures prometheus, which we use for server monitoring at +[prometheus.openstreetmap.org](https://prometheus.openstreetmap.org). The +cookbook contains teo recipes: + +* default - installs and configures basic prometheus exporters on each machine +* server - configures the central prometheus server + +Additionally two providers are defined - prometheus_exporter and +prometheus_textfile_exporter, for configuring individual prometheus +exporters. diff --git a/cookbooks/prometheus/attributes/default.rb b/cookbooks/prometheus/attributes/default.rb new file mode 100644 index 000000000..bea00dfc6 --- /dev/null +++ b/cookbooks/prometheus/attributes/default.rb @@ -0,0 +1 @@ +default[:prometheus][:exporters] = {} diff --git a/cookbooks/prometheus/metadata.rb b/cookbooks/prometheus/metadata.rb new file mode 100644 index 000000000..ee2a797b7 --- /dev/null +++ b/cookbooks/prometheus/metadata.rb @@ -0,0 +1,9 @@ +name "prometheus" +maintainer "OpenStreetMap Administrators" +maintainer_email "admins@openstreetmap.org" +license "Apache-2.0" +description "Installs and configures prometheus" + +version "1.0.0" +supports "ubuntu" +depends "networking" diff --git a/cookbooks/prometheus/recipes/default.rb b/cookbooks/prometheus/recipes/default.rb new file mode 100644 index 000000000..41d28e58d --- /dev/null +++ b/cookbooks/prometheus/recipes/default.rb @@ -0,0 +1,22 @@ +# +# Cookbook:: prometheus +# Recipe:: default +# +# Copyright:: 2020, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +prometheus_exporter "node" do + port 9100 +end diff --git a/cookbooks/prometheus/recipes/server.rb b/cookbooks/prometheus/recipes/server.rb new file mode 100644 index 000000000..f82fe04bf --- /dev/null +++ b/cookbooks/prometheus/recipes/server.rb @@ -0,0 +1,35 @@ +# +# Cookbook:: prometheus +# Recipe:: server +# +# Copyright:: 2020, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package "prometheus" + +clients = search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name) + +template "/etc/prometheus/prometheus.yml" do + source "prometheus.yml.erb" + owner "root" + group "root" + mode "644" + variables :clients => clients +end + +service "prometheus" do + action [:enable, :start] + subscribes :reload, "template[/etc/prometheus/prometheus.yml]" +end diff --git a/cookbooks/prometheus/resources/exporter.rb b/cookbooks/prometheus/resources/exporter.rb new file mode 100644 index 000000000..d3652d3b8 --- /dev/null +++ b/cookbooks/prometheus/resources/exporter.rb @@ -0,0 +1,74 @@ +# +# Cookbook:: prometheus +# Resource:: prometheus_exporter +# +# Copyright:: 2020, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +default_action :create + +property :exporter, :kind_of => String, :name_property => true +property :port, :kind_of => Integer, :required => [:create] +property :listen_switch, :kind_of => String, :default => "web.listen-address" +property :package, :kind_of => String +property :defaults, :kind_of => String +property :service, :kind_of => String + +action :create do + package package_name + + template defaults_name do + source "defaults.erb" + owner "root" + group "root" + mode "644" + variables new_resource.to_hash.merge(:listen_address => listen_address) + end + + service service_name do + action [:enable, :start] + subscribes :restart, "template[#{defaults_name}]" + end + + node.default[:prometheus][:exporters][new_resource.exporter] = listen_address +end + +action :delete do + service service_name do + action [:disable, :stop] + end + + package package_name do + action :purge + end +end + +action_class do + def package_name + new_resource.package || "prometheus-#{new_resource.exporter}-exporter" + end + + def defaults_name + new_resource.defaults || "/etc/default/prometheus-#{new_resource.exporter}-exporter" + end + + def listen_address + "#{node.internal_ipaddress}:#{new_resource.port}" + end + + def service_name + new_resource.service || "prometheus-#{new_resource.exporter}-exporter" + end +end diff --git a/cookbooks/prometheus/templates/default/defaults.erb b/cookbooks/prometheus/templates/default/defaults.erb new file mode 100644 index 000000000..3760c99f7 --- /dev/null +++ b/cookbooks/prometheus/templates/default/defaults.erb @@ -0,0 +1,3 @@ +# DO NOT EDIT - This file is being maintained by Chef + +ARGS="--<%= @listen_switch %>=<%= @listen_address %>" diff --git a/cookbooks/prometheus/templates/default/prometheus.yml.erb b/cookbooks/prometheus/templates/default/prometheus.yml.erb new file mode 100644 index 000000000..60345ef52 --- /dev/null +++ b/cookbooks/prometheus/templates/default/prometheus.yml.erb @@ -0,0 +1,21 @@ +# DO NOT EDIT - This file is being maintained by Chef + +global: + scrape_interval: 15s + evaluation_interval: 15s + +scrape_configs: + - job_name: prometheus + scrape_interval: 5s + scrape_timeout: 5s + static_configs: + - targets: + - localhost:9090 +<% @clients.each do |client| -%> + - job_name: <%= client.name %> + static_configs: + - targets: +<% client[:prometheus][:exporters].sort.each do |_,address| -%> + - <%= address %> +<% end -%> +<% end -%> diff --git a/test/integration/prometheus-server/serverspec/prometheus_spec.rb b/test/integration/prometheus-server/serverspec/prometheus_spec.rb new file mode 100644 index 000000000..a4377ce53 --- /dev/null +++ b/test/integration/prometheus-server/serverspec/prometheus_spec.rb @@ -0,0 +1,17 @@ +require "serverspec" + +# Required by serverspec +set :backend, :exec + +describe package("prometheus") do + it { should be_installed } +end + +describe service("prometheus") do + it { should be_enabled } + it { should be_running } +end + +describe port(9090) do + it { should be_listening.with("tcp6") } +end diff --git a/test/integration/prometheus/serverspec/prometheus_node_exporter_spec.rb b/test/integration/prometheus/serverspec/prometheus_node_exporter_spec.rb new file mode 100644 index 000000000..c37883d15 --- /dev/null +++ b/test/integration/prometheus/serverspec/prometheus_node_exporter_spec.rb @@ -0,0 +1,17 @@ +require "serverspec" + +# Required by serverspec +set :backend, :exec + +describe package("prometheus-node-exporter") do + it { should be_installed } +end + +describe service("prometheus-node-exporter") do + it { should be_enabled } + it { should be_running } +end + +describe port(9100) do + it { should be_listening.with("tcp6") } +end