From 1f31f1314a2ea1143da7073f613ab64eb7e807db Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Sat, 24 Oct 2020 13:23:45 +0900 Subject: [PATCH] Install `snmp_exporter`. --- cookbooks/prometheus/default.rb | 2 + cookbooks/prometheus/snmp_exporter_install.rb | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 cookbooks/prometheus/snmp_exporter_install.rb diff --git a/cookbooks/prometheus/default.rb b/cookbooks/prometheus/default.rb index 32c478e..393143d 100644 --- a/cookbooks/prometheus/default.rb +++ b/cookbooks/prometheus/default.rb @@ -10,6 +10,8 @@ include_recipe './alertmanager_setup.rb' include_recipe './alertmanager_webhook_install.rb' include_recipe './alertmanager_webhook_setup.rb' +include_recipe './snmp_exporter_install.rb' + # Deploy /etc/hosts file: HOSTNAME = run_command('uname -n').stdout.chomp diff --git a/cookbooks/prometheus/snmp_exporter_install.rb b/cookbooks/prometheus/snmp_exporter_install.rb new file mode 100644 index 0000000..fb75001 --- /dev/null +++ b/cookbooks/prometheus/snmp_exporter_install.rb @@ -0,0 +1,52 @@ +snmp_url = '' +snmp_bin = '' + +vtag = '' +tag = '' + +# Calculate the Download URL: +begin + require 'net/http' + + uri = URI.parse('https://github.com/prometheus/snmp_exporter/releases/latest') + + Timeout.timeout(3) do + response = Net::HTTP.get_response(uri) + + vtag = $1 if response.body =~ %r{tag\/(v\d+\.\d+\.\d+)} + tag = vtag.sub(/^v/, '') + + snmp_bin = "#{node['snmp_exporter']['prefix']}#{tag}#{node['snmp_exporter']['postfix']}" + + snmp_url = "#{node['snmp_exporter']['url']}/#{vtag}/#{snmp_bin}" + end +rescue + # Abort the chef client process: + raise 'Cannot connect to http://github.com.' +end + +# Download: +TMP = "/tmp/#{snmp_bin}" + +execute "wget #{snmp_url} -O #{TMP}" + +# Install: +directory node['snmp_exporter']['storage'] do + owner 'root' + group 'root' + mode '755' +end + +execute "tar zxf #{TMP} -C #{node['snmp_exporter']['storage']} --strip-components 1" + +# Change Owner and Permissions: +file "#{node['snmp_exporter']['storage']}snmp_exporter" do + owner 'root' + group 'root' + mode '755' +end + +# Create Link +link "#{node['snmp_exporter']['location']}snmp_exporter" do + to "#{node['snmp_exporter']['storage']}snmp_exporter" +end