From e3574f369b90ad143048354173ec95509289c403 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 2 Aug 2023 22:24:14 +0100 Subject: [PATCH] Add support for junos exporter --- cookbooks/prometheus/attributes/default.rb | 1 + cookbooks/prometheus/files/default/id_rsa.pub | 1 + cookbooks/prometheus/recipes/default.rb | 17 ++++++++++++++++ cookbooks/prometheus/recipes/server.rb | 12 ++++++++++- cookbooks/prometheus/resources/exporter.rb | 20 +++++++++++++++++++ .../templates/default/prometheus.yml.erb | 20 +++++++++++++++++++ roles/fafnir.rb | 3 +++ roles/ironbelly.rb | 3 +++ 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 cookbooks/prometheus/files/default/id_rsa.pub diff --git a/cookbooks/prometheus/attributes/default.rb b/cookbooks/prometheus/attributes/default.rb index 7ea8bfd1d..0fe80895e 100644 --- a/cookbooks/prometheus/attributes/default.rb +++ b/cookbooks/prometheus/attributes/default.rb @@ -1,5 +1,6 @@ default[:prometheus][:addresses] = {} default[:prometheus][:exporters] = {} +default[:prometheus][:junos] = {} default[:prometheus][:snmp] = {} default[:prometheus][:metrics] = {} default[:prometheus][:files] = [] diff --git a/cookbooks/prometheus/files/default/id_rsa.pub b/cookbooks/prometheus/files/default/id_rsa.pub new file mode 100644 index 000000000..41565df1b --- /dev/null +++ b/cookbooks/prometheus/files/default/id_rsa.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCwBRhz6gJsBcf5xf1WWEdhm6DjOnVlB5CHn48qEDYk4Gy5j62ZsKq+pBmHIJ1SeoR2ezlsMgKyPouzRkIQgh3Mc1qcy0fmr/szsDwhswKjODyk7vfnkEH5bK70W2/WrrBmBi1RGp03hCnJqWFG5uLaYiO2MjkyihTSoLM4XWj71aohbmOhIQiHB8td24JS+3tsGqNe+UhtKWjk2BJ0lqIhDvpOmuLjxGPMVGzBaC4a18jMYFxZs1AxXf/veLP5cduxA7KxT5nWnun2QsW/P0AThBGkhTny/a2GdmQ/aKAmYftFwnBUtgJtGuo/GXkd5Up8BienJzbZ16HhKFl23cXAuFIV7EJwu8bClzCJQtMUE+7rhAX9StDris1P9e1ldjalUSFBMzPHkhf3nHAju3E14URH8DrnfA8kNrnYvYwCgeH3mAu+yht/6mzZWkoaF94AkJVCQ8KRxsuUO9tZO/hLONMuG5FCao1dofnNtvc797XYWS4X6pjBMW5BpANrFrk= prometheus@openstreetmap.org diff --git a/cookbooks/prometheus/recipes/default.rb b/cookbooks/prometheus/recipes/default.rb index cecfbc74d..0c4bc59da 100644 --- a/cookbooks/prometheus/recipes/default.rb +++ b/cookbooks/prometheus/recipes/default.rb @@ -115,6 +115,23 @@ prometheus_exporter "node" do metric_relabel metric_relabel end +unless node[:prometheus][:junos].empty? + targets = node[:prometheus][:junos].collect { |_, details| details[:address] }.sort.join(",") + + prometheus_exporter "junos" do + port 9326 + options %W[ + --ssh.user=prometheus + --ssh.keyfile=/var/lib/prometheus/junos-exporter/id_rsa + --ssh.targets=#{targets} + --lacp.enabled=true + --power.enabled=false + ] + ssh true + register_target false + end +end + unless node[:prometheus][:snmp].empty? prometheus_exporter "snmp" do port 9116 diff --git a/cookbooks/prometheus/recipes/server.rb b/cookbooks/prometheus/recipes/server.rb index 4ca2d3909..a480a8323 100644 --- a/cookbooks/prometheus/recipes/server.rb +++ b/cookbooks/prometheus/recipes/server.rb @@ -120,6 +120,7 @@ search(:node, "roles:gateway") do |gateway| end jobs = {} +junos_targets = [] snmp_targets = [] search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client| @@ -162,6 +163,15 @@ search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client } end + Hash(client[:prometheus][:junos]).each do |instance, details| + junos_targets << { + :instance => instance, + :target => details[:address], + :address => client[:prometheus][:addresses]["junos"], + :labels => Array(details[:labels]) + } + end + Hash(client[:prometheus][:snmp]).each do |instance, details| snmp_targets << { :instance => instance, @@ -214,7 +224,7 @@ template "/etc/prometheus/prometheus.yml" do owner "root" group "root" mode "644" - variables :jobs => jobs, :snmp_targets => snmp_targets, :certificates => certificates + variables :jobs => jobs, :junos_targets => junos_targets, :snmp_targets => snmp_targets, :certificates => certificates end template "/etc/prometheus/alert_rules.yml" do diff --git a/cookbooks/prometheus/resources/exporter.rb b/cookbooks/prometheus/resources/exporter.rb index e2d8ca7da..5075c5d6a 100644 --- a/cookbooks/prometheus/resources/exporter.rb +++ b/cookbooks/prometheus/resources/exporter.rb @@ -47,8 +47,27 @@ property :scrape_interval, :kind_of => String property :scrape_timeout, :kind_of => String property :metric_relabel, :kind_of => Array property :register_target, :kind_of => [TrueClass, FalseClass], :default => true +property :ssh, [true, false] action :create do + if new_resource.ssh && new_resource.user.nil? + keys = data_bag_item("prometheus", "keys") + + directory "/var/lib/private/prometheus/#{new_resource.exporter}-exporter" do + mode "700" + recursive true + end + + file "/var/lib/private/prometheus/#{new_resource.exporter}-exporter/id_rsa" do + content keys["ssh"].join("\n") + mode "400" + end + + cookbook_file "/var/lib/private/prometheus/#{new_resource.exporter}-exporter/id_rsa.pub" do + mode "644" + end + end + systemd_service service_name do after "network-online.target" wants "network-online.target" @@ -60,6 +79,7 @@ action :create do environment new_resource.environment exec_start "#{executable_path} #{new_resource.command} #{executable_options}" sandbox :enable_network => true + state_directory "prometheus/#{new_resource.exporter}-exporter" if new_resource.ssh && new_resource.user.nil? protect_proc new_resource.protect_proc if new_resource.property_is_set?(:protect_proc) proc_subset new_resource.proc_subset if new_resource.property_is_set?(:proc_subset) capability_bounding_set new_resource.capability_bounding_set if new_resource.property_is_set?(:capability_bounding_set) diff --git a/cookbooks/prometheus/templates/default/prometheus.yml.erb b/cookbooks/prometheus/templates/default/prometheus.yml.erb index daa10a495..02ade7d8e 100644 --- a/cookbooks/prometheus/templates/default/prometheus.yml.erb +++ b/cookbooks/prometheus/templates/default/prometheus.yml.erb @@ -85,6 +85,26 @@ scrape_configs: <% end -%> <% end -%> <% end -%> + - job_name: junos + scrape_interval: 5m + scrape_timeout: 4m + static_configs: +<% @junos_targets.sort_by { |t| t[:instance] }.each do |target| -%> + - targets: + - "<%= target[:target] %>/<%= target[:address] %>" + labels: + instance: <%= target[:instance] %> +<% target[:labels].sort.each do |name, value| -%> + <%= name %>: <%= value %> +<% end -%> +<% end -%> + relabel_configs: + - source_labels: [__address__] + regex: "([^/]+)/.*" + target_label: __param_target + - source_labels: [__address__] + regex: "[^/]+/(.*)" + target_label: __address__ - job_name: snmp scrape_interval: 5m scrape_timeout: 2m diff --git a/roles/fafnir.rb b/roles/fafnir.rb index 5734379ae..a48f623ca 100644 --- a/roles/fafnir.rb +++ b/roles/fafnir.rb @@ -43,6 +43,9 @@ default_attributes( } }, :prometheus => { + :junos => { + "switch1" => { :address => "184.104.226.97", :labels => { "site" => "dublin" } } + }, :snmp => { "pdu1" => { :address => "10.0.64.100", :modules => %w[apcups], :labels => { "site" => "dublin" } }, "pdu2" => { :address => "10.0.64.101", :modules => %w[apcups], :labels => { "site" => "dublin" } }, diff --git a/roles/ironbelly.rb b/roles/ironbelly.rb index 6cf1f9e79..0cb7817e0 100644 --- a/roles/ironbelly.rb +++ b/roles/ironbelly.rb @@ -52,6 +52,9 @@ default_attributes( } }, :prometheus => { + :junos => { + "switch1" => { :address => "184.104.179.129", :labels => { "site" => "amsterdam" } } + }, :snmp => { "pdu1" => { :address => "10.0.48.100", :modules => %w[apcups], :labels => { "site" => "amsterdam" } }, "pdu2" => { :address => "10.0.48.101", :modules => %w[apcups], :labels => { "site" => "amsterdam" } }, -- 2.39.5