Skip to content

Commit 0e384ae

Browse files
authored
Merge pull request #257 from riemann/custom-user-agent
Override default HTTP User-Agent and make it tuneable
2 parents b7fdd44 + ee0b26a commit 0e384ae

File tree

7 files changed

+17
-12
lines changed

7 files changed

+17
-12
lines changed

lib/riemann/tools/apache_status.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class ApacheStatus
1414
require 'uri'
1515

1616
opt :uri, 'Apache Server Status URI', default: 'http://localhost/server-status'
17+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
1718

1819
def initialize
19-
@uri = "#{URI.parse(opts[:uri])}?auto"
20+
@uri = URI.parse("#{opts[:uri]}?auto")
2021
# Sample Response with ExtendedStatus On
2122
# Total Accesses: 20643
2223
# Total kBytes: 36831
@@ -68,7 +69,7 @@ def report_metrics(metrics)
6869
def connection
6970
response = nil
7071
begin
71-
response = ::Net::HTTP.get(@uri)
72+
response = ::Net::HTTP.new(@uri.host, @uri.port).get(@uri, { 'user-agent' => opts[:user_agent] }).body
7273
rescue StandardError => e
7374
report(
7475
service: 'httpd health',

lib/riemann/tools/cloudant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Cloudant
1212

1313
opt :cloudant_username, 'Cloudant username', type: :string, required: true
1414
opt :cloudant_password, 'Cloudant pasword', type: :string, required: true
15+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
1516

1617
def tick
1718
json.each do |node|
@@ -46,7 +47,7 @@ def json
4647
http = ::Net::HTTP.new('cloudant.com', 443)
4748
http.use_ssl = true
4849
http.start do |h|
49-
get = ::Net::HTTP::Get.new('/api/load_balancer')
50+
get = ::Net::HTTP::Get.new('/api/load_balancer', { 'user-agent' => opts[:user_agent] })
5051
get.basic_auth opts[:cloudant_username], opts[:cloudant_password]
5152
h.request get
5253
end

lib/riemann/tools/consul_health.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ConsulHealth
1616
opt :consul_port, 'Consul API Host (default to 8500)', default: '8500'
1717
opt :prefix, 'prefix to use for all service names when reporting', default: 'consul '
1818
opt :minimum_services_per_node, 'minimum services per node (default: 0)', default: 0
19+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
1920

2021
def initialize
2122
@hostname = opts[:consul_host]
@@ -43,7 +44,7 @@ def alert(hostname, service, state, metric, description)
4344
end
4445

4546
def get(url)
46-
::Net::HTTP.get_response(url).body
47+
::Net::HTTP.new(url.host, url.port).get(url, { 'user-agent' => opts[:user_agent] }).body
4748
end
4849

4950
def tick

lib/riemann/tools/haproxy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Haproxy
1212

1313
opt :stats_url, 'Full url to haproxy stats (eg: https://user:[email protected]:9999/stats)', required: true,
1414
type: :string
15+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
1516

1617
def initialize
1718
@uri = URI("#{opts[:stats_url]};csv")
@@ -45,7 +46,7 @@ def csv
4546
http = ::Net::HTTP.new(@uri.host, @uri.port)
4647
http.use_ssl = true if @uri.scheme == 'https'
4748
res = http.start do |h|
48-
get = ::Net::HTTP::Get.new(@uri.request_uri)
49+
get = ::Net::HTTP::Get.new(@uri.request_uri, { 'user-agent' => opts[:user_agent] })
4950
unless @uri.userinfo.nil?
5051
userinfo = @uri.userinfo.split(':')
5152
get.basic_auth userinfo[0], userinfo[1]

lib/riemann/tools/http_check.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class HttpCheck
2828
opt :checks, 'A list of checks to run.', short: :none, type: :strings, default: %w[consistency connection-latency response-code response-latency]
2929
opt :resolvers, 'Run this number of resolver threads', short: :none, type: :integer, default: 5
3030
opt :workers, 'Run this number of worker threads', short: :none, type: :integer, default: 20
31+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
3132

3233
def initialize
3334
@resolve_queue = Queue.new
@@ -58,9 +59,6 @@ def initialize
5859
Thread.new do
5960
loop do
6061
uri, addresses = @work_queue.pop
61-
request = ::Net::HTTP::Get.new(uri)
62-
request.basic_auth(uri.user, uri.password)
63-
6462
test_uri_addresses(uri, addresses)
6563
end
6664
end
@@ -101,7 +99,7 @@ def tick
10199
end
102100

103101
def test_uri_addresses(uri, addresses)
104-
request = ::Net::HTTP::Get.new(uri)
102+
request = ::Net::HTTP::Get.new(uri, { 'user-agent' => opts[:user_agent] })
105103
request.basic_auth(uri.user, uri.password)
106104

107105
responses = []

lib/riemann/tools/nginx_status.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class NginxStatus
2222
opt :writing_critical, 'Writing connections critical threshold', default: 0
2323
opt :waiting_warning, 'Waiting connections warning threshold', default: 0
2424
opt :waiting_critical, 'Waiting connections critical threshold', default: 0
25+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
2526

2627
def initialize
2728
@uri = URI.parse(opts[:uri])
@@ -53,7 +54,7 @@ def state(key, value)
5354
def tick
5455
response = nil
5556
begin
56-
response = ::Net::HTTP.get(@uri)
57+
response = ::Net::HTTP.new(@uri.host, @uri.port).get(@uri, { 'user-agent' => opts[:user_agent] }).body
5758
rescue StandardError => e
5859
report(
5960
service: 'nginx health',

tools/riemann-riak/lib/riemann/tools/riak.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Riak
2626
opt :get_99_warning, 'FSM 99% get time warning threshold (ms)', default: 10_000
2727
opt :put_99_warning, 'FSM 99% put time warning threshold (ms)', default: 10_000
2828

29+
opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"
30+
2931
def initialize
3032
detect_features
3133

@@ -38,7 +40,7 @@ def initialize
3840
http.use_ssl = uri.scheme == 'https'
3941
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
4042
http.start do |h|
41-
h.get opts[:stats_path]
43+
h.get(opts[:stats_path], { 'user-agent' => opts[:user_agent] })
4244
end
4345
rescue StandardError => _e
4446
@httpstatus = false
@@ -168,7 +170,7 @@ def stats_http
168170
http.use_ssl = uri.scheme == 'https'
169171
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
170172
res = http.start do |h|
171-
h.get opts[:stats_path]
173+
h.get(opts[:stats_path], { 'user-agent' => opts[:user_agent] })
172174
end
173175
rescue StandardError => e
174176
report(

0 commit comments

Comments
 (0)