Skip to content

Commit

Permalink
Add support for junos exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Aug 3, 2023
1 parent f06628a commit 0305831
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions cookbooks/prometheus/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
default[:prometheus][:addresses] = {}
default[:prometheus][:exporters] = {}
default[:prometheus][:junos] = {}
default[:prometheus][:snmp] = {}
default[:prometheus][:metrics] = {}
default[:prometheus][:files] = []
1 change: 1 addition & 0 deletions cookbooks/prometheus/files/default/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCwBRhz6gJsBcf5xf1WWEdhm6DjOnVlB5CHn48qEDYk4Gy5j62ZsKq+pBmHIJ1SeoR2ezlsMgKyPouzRkIQgh3Mc1qcy0fmr/szsDwhswKjODyk7vfnkEH5bK70W2/WrrBmBi1RGp03hCnJqWFG5uLaYiO2MjkyihTSoLM4XWj71aohbmOhIQiHB8td24JS+3tsGqNe+UhtKWjk2BJ0lqIhDvpOmuLjxGPMVGzBaC4a18jMYFxZs1AxXf/veLP5cduxA7KxT5nWnun2QsW/P0AThBGkhTny/a2GdmQ/aKAmYftFwnBUtgJtGuo/GXkd5Up8BienJzbZ16HhKFl23cXAuFIV7EJwu8bClzCJQtMUE+7rhAX9StDris1P9e1ldjalUSFBMzPHkhf3nHAju3E14URH8DrnfA8kNrnYvYwCgeH3mAu+yht/6mzZWkoaF94AkJVCQ8KRxsuUO9tZO/hLONMuG5FCao1dofnNtvc797XYWS4X6pjBMW5BpANrFrk= [email protected]
17 changes: 17 additions & 0 deletions cookbooks/prometheus/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@
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=/etc/prometheus/exporters/junos/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
Expand Down
12 changes: 11 additions & 1 deletion cookbooks/prometheus/recipes/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
end

jobs = {}
junos_targets = []
snmp_targets = []

search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name).each do |client|
Expand Down Expand Up @@ -162,6 +163,15 @@
}
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,
Expand Down Expand Up @@ -214,7 +224,7 @@
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
Expand Down
20 changes: 20 additions & 0 deletions cookbooks/prometheus/resources/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,27 @@
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"
Expand All @@ -60,6 +79,7 @@
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)
Expand Down
20 changes: 20 additions & 0 deletions cookbooks/prometheus/templates/default/prometheus.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions roles/fafnir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
}
},
: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" } },
Expand Down
3 changes: 3 additions & 0 deletions roles/ironbelly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
}
},
: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" } },
Expand Down

0 comments on commit 0305831

Please sign in to comment.