]> git.openstreetmap.org Git - chef.git/commitdiff
Add prometheus SNMP monitoring of switch and PDUs in Amsterdam
authorTom Hughes <tom@compton.nu>
Sun, 7 Mar 2021 12:14:52 +0000 (12:14 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 7 Mar 2021 12:55:20 +0000 (12:55 +0000)
cookbooks/prometheus/attributes/default.rb
cookbooks/prometheus/recipes/default.rb
cookbooks/prometheus/recipes/server.rb
cookbooks/prometheus/resources/exporter.rb
cookbooks/prometheus/templates/default/prometheus.yml.erb
roles/ironbelly.rb

index 56781b4dcb9c72a8657ac253f569cadf3d96a8bf..0200e1ca199400eb1d98ad7d4d8cdac467c7119c 100644 (file)
@@ -1,4 +1,6 @@
+default[:prometheus][:addresses] = {}
 default[:prometheus][:exporters] = {}
+default[:prometheus][:snmp] = {}
 
 if node[:recipes].include?("prometheus::server")
   default[:apt][:sources] |= ["grafana"]
index 2ad1d69aa7934eb0098f177721f0ce2032a1e966..6e88362a40ba80bb8dc009b7886c832d474afc1f 100644 (file)
@@ -113,3 +113,11 @@ prometheus_exporter "node" do
   ]
   metric_relabel metric_relabel
 end
+
+unless node[:prometheus][:snmp].empty?
+  prometheus_exporter "snmp" do
+    port 9116
+    options "--config.file=/opt/prometheus/exporters/snmp/snmp.yml"
+    register_target false
+  end
+end
index 612b404cb30874089d1219e8375bf758cc63f25c..c8241a1f1d3ed0f5a8adcceee5cd360eef37b05c 100644 (file)
@@ -150,6 +150,7 @@ search(:node, "roles:gateway") do |gateway|
 end
 
 jobs = {}
+snmp_targets = []
 
 search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
   if client[:prometheus][:mode] == "wireguard"
@@ -178,6 +179,15 @@ search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client
       :metric_relabel => metric_relabel
     }
   end
+
+  Hash(client[:prometheus][:snmp]).each do |instance, details|
+    snmp_targets << {
+      :instance => instance,
+      :target => details[:address],
+      :module => details[:module],
+      :address => client[:prometheus][:addresses]["snmp"]
+    }
+  end
 end
 
 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
@@ -218,7 +228,7 @@ template "/etc/prometheus/prometheus.yml" do
   owner "root"
   group "root"
   mode "644"
-  variables :jobs => jobs, :certificates => certificates
+  variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates
 end
 
 template "/etc/prometheus/alert_rules.yml" do
index 5eca43f5cd274a40afbb583f5139af5a9094d679..bab318a4ab92edb2f43b15a91e89d2c009b2925f 100644 (file)
@@ -59,6 +59,8 @@ action :create do
     only_if { node[:prometheus][:mode] == "external" }
   end
 
+  node.default[:prometheus][:addresses][new_resource.exporter] = listen_address
+
   if new_resource.register_target
     node.default[:prometheus][:exporters][new_resource.port] = {
       :name => new_resource.exporter,
index 54f72a88c12efc6f7a9a3fa8745dbbce26bfcdcd..812f3229913fb16fae1686a9f9469d9ec00610b9 100644 (file)
@@ -73,6 +73,28 @@ scrape_configs:
 <% end -%>
 <% end -%>
 <% end -%>
+  - job_name: snmp
+    scrape_interval: 5m
+    scrape_timeout: 1m
+    metrics_path: /snmp
+    static_configs:
+      - targets:
+<% @snmp_targets.sort_by { |t| t[:instance] }.each do |target| -%>
+      - targets:
+          - "<%= target[:target] %>/<%= target[:module] %>/<%= target[:address] %>"
+        labels:
+          instance: <%= target[:instance] %>
+<% end -%>
+    relabel_configs:
+      - source_labels: [__address__]
+        regex: "([^/]+)/[^/]+/.*"
+        target_label: __param_target
+      - source_labels: [__address__]
+        regex: "[^/]+/([^/]+)/.*"
+        target_label: __param_module
+      - source_labels: [__address__]
+        regex: "[^/]+/[^/]+/(.*)"
+        target_label: __address__
 
 remote_write:
   - url: "http://localhost:9201/write"
index 5042c4dd0547547d9d01e28a8abbab4fc2afc662..46bcde332c4055cedd79f4c71139632ab5e6a0d0 100644 (file)
@@ -58,6 +58,13 @@ default_attributes(
   :planet => {
     :replication => "enabled"
   },
+  :prometheus => {
+    :snmp => {
+      "pdu1" => { :address => "10.0.48.100", :module => "apcups" },
+      "pdu2" => { :address => "10.0.48.101", :module => "apcups" },
+      "switch1" => { :address => "130.117.76.2", :module => "if_mib" }
+    }
+  },
   :rsyncd => {
     :modules => {
       :hosts => {