]> git.openstreetmap.org Git - chef.git/commitdiff
Add initial version of prometheus cookbook
authorTom Hughes <tom@compton.nu>
Fri, 11 Sep 2020 13:45:10 +0000 (13:45 +0000)
committerTom Hughes <tom@compton.nu>
Fri, 11 Sep 2020 13:45:10 +0000 (13:45 +0000)
12 files changed:
.github/workflows/test-kitchen.yml
.kitchen.yml
cookbooks/prometheus/README.md [new file with mode: 0644]
cookbooks/prometheus/attributes/default.rb [new file with mode: 0644]
cookbooks/prometheus/metadata.rb [new file with mode: 0644]
cookbooks/prometheus/recipes/default.rb [new file with mode: 0644]
cookbooks/prometheus/recipes/server.rb [new file with mode: 0644]
cookbooks/prometheus/resources/exporter.rb [new file with mode: 0644]
cookbooks/prometheus/templates/default/defaults.erb [new file with mode: 0644]
cookbooks/prometheus/templates/default/prometheus.yml.erb [new file with mode: 0644]
test/integration/prometheus-server/serverspec/prometheus_spec.rb [new file with mode: 0644]
test/integration/prometheus/serverspec/prometheus_node_exporter_spec.rb [new file with mode: 0644]

index d8a39bb1985842713f8f09a0e8d57548df424d25..acb40b6f4f54f97e5ab25a9353e7a134a93edd34 100644 (file)
@@ -74,6 +74,8 @@ jobs:
           - planet-notes
           - planet-replication
           - postgresql
+          - prometheus
+          - prometheus-server
           - python
           - rsyncd
           - serverinfo
index b7bff87ba97a67189e612ace8da7e18ea9f843cf..7c446effcc7dd0376579f18028160f24a2083474 100644 (file)
@@ -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 (file)
index 0000000..e24566d
--- /dev/null
@@ -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 (file)
index 0000000..bea00df
--- /dev/null
@@ -0,0 +1 @@
+default[:prometheus][:exporters] = {}
diff --git a/cookbooks/prometheus/metadata.rb b/cookbooks/prometheus/metadata.rb
new file mode 100644 (file)
index 0000000..ee2a797
--- /dev/null
@@ -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 (file)
index 0000000..41d28e5
--- /dev/null
@@ -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 (file)
index 0000000..f82fe04
--- /dev/null
@@ -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 (file)
index 0000000..d3652d3
--- /dev/null
@@ -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 (file)
index 0000000..3760c99
--- /dev/null
@@ -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 (file)
index 0000000..60345ef
--- /dev/null
@@ -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 (file)
index 0000000..a4377ce
--- /dev/null
@@ -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 (file)
index 0000000..c37883d
--- /dev/null
@@ -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