Skip to content

Commit

Permalink
add custom HTTP headers to metrics-http-json.rb
Browse files Browse the repository at this point in the history
Added -H/--header flag to pass custom HTTP headers in metrics-http-json.rb.
This makes it possible to use authentication token or any other function
based on HTTP headers.

Co-Authored-By: Ben Abrams <[email protected]>
  • Loading branch information
mblaettler and majormoses committed Mar 19, 2020
1 parent bc01c20 commit 128b6a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md)

## [Unreleased]
### Added
- `metrics-http-json.rb`: Added `-H/--header` flag to pass custom HTTP headers.


## [6.0.1] - 2020-01-30
Expand Down
17 changes: 16 additions & 1 deletion bin/metrics-http-json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class HttpJsonGraphite < Sensu::Plugin::Metric::CLI::Graphite
short: '-m METRIC::JSONKEY',
long: '--metric METRIC::JSONKEY'

option :headers,
description: 'Additional HTTP request headers to send. Example: Authorization:XYZ,User-Agent:ABC',
short: '-H HEADER1:val,HEADER2:val',
long: '--headers HEADER1:val,HEADER2:val'

option :object,
description: 'The JSON object containing the data',
short: '-o OBJECT',
Expand All @@ -85,13 +90,23 @@ def run
if config[:object]
object = config[:object].to_s
end

header_dict = {}
if config[:headers]
config[:headers].split(',').each do |header|
h, v = header.split(':', 2)
header_dict[h] = v.strip
end
end

# TODO: figure out what to do here
url = URI.encode(config[:url].to_s) # rubocop:disable Lint/UriEscapeUnescape
begin
r = RestClient::Request.execute(
url: url,
method: :get,
verify_ssl: !config[:insecure]
verify_ssl: !config[:insecure],
headers: header_dict
)

puts "Http response: #{r}" if config[:debug]
Expand Down

0 comments on commit 128b6a4

Please sign in to comment.