Skip to content

Commit 8538469

Browse files
dalesitmajormoses
authored andcommitted
Adding option -j to allow configuration scope to be defined on command
line
1 parent 6af1597 commit 8538469

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang
66
## [Unreleased]
77
### Added
88
- example in readme for using the handlers (@scosist)
9+
### Changed
10+
- Added option -j to handler-graphite-notify.rb and handler-graphite-status.rb to allow configuration scope to be defined on command line.
911

1012
## [2.2.1] - 2017-06-13
1113
### Fixed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* bin/extension-graphite
1717
* bin/handler-graphite-event
1818
* bin/handler-graphite-notify
19+
* bin/handler-graphite-status
1920
* bin/handler-graphite-occurrences
2021
* bin/mutator-graphite
2122

bin/handler-graphite-notify.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,35 @@
55
#
66
# This will send a 1 to a Graphite metric when an event is created and 0 when it's resolved
77
# See http://imansson.wordpress.com/2012/11/26/why-sensu-is-a-monitoring-router-some-cool-handlers/
8+
#
9+
# Config by default is graphite_notify, but can be called with a specific json config
10+
# using the -j option. This allows multiple graphite handlers to be configured.
811

912
require 'sensu-handler'
1013
require 'simple-graphite'
1114

1215
class Resolve < Sensu::Handler
16+
option :json_config,
17+
description: 'Config Name',
18+
short: '-j JsonConfig',
19+
long: '--json_config JsonConfig',
20+
required: false,
21+
default: 'graphite_notify'
22+
1323
def handle
14-
port = settings['graphite_notify']['port'] ? settings['graphite_notify']['port'].to_s : '2003'
15-
graphite = Graphite.new(host: settings['graphite_notify']['host'], port: port)
24+
json_config = config[:json_config]
25+
port = settings[json_config]['port'] ? settings[json_config]['port'].to_s : '2003'
26+
graphite = Graphite.new(host: settings[json_config]['host'], port: port)
1627
return unless graphite
1728
prop = @event['action'] == 'create' ? 1 : 0
18-
message = "#{settings['graphite_notify']['prefix']}.#{@event['client']['name'].tr('.', '_')}.#{@event['check']['name']}"
29+
message = "#{settings[json_config]['prefix']}.#{@event['client']['name'].tr('.', '_')}.#{@event['check']['name']}"
1930
message += " #{prop} #{graphite.time_now + rand(100)}"
2031
begin
2132
graphite.push_to_graphite do |graphite_socket|
2233
graphite_socket.puts message
2334
end
24-
rescue => e
25-
error_msg = "Can't connect to #{settings['graphite_notify']['host']}:#{port} and send message #{message}: #{e}'"
35+
rescue ETIMEDOUT
36+
error_msg = "Can't connect to #{settings[json_config]['host']}:#{port} and send message #{message}'"
2637
raise error_msg
2738
end
2839
end

bin/handler-graphite-status.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@
44
# for details
55
#
66
# This will send the check status (0,1,2,3) to a graphite metric when a check event state changes
7-
# Based on handler-graphite-notify.rb
8-
# See http://imansson.wordpress.com/2012/11/26/why-sensu-is-a-monitoring-router-some-cool-handlers/
7+
# Based on handler-graphite-notify.rb.
8+
# Config by default is graphite_status but can be called with a specific json config
9+
# using the -j option. This allows multiple graphite handlers to be configured.
910

1011
require 'sensu-handler'
1112
require 'simple-graphite'
1213

1314
class Resolve < Sensu::Handler
15+
option :json_config,
16+
description: 'Config Name',
17+
short: '-j JsonConfig',
18+
long: '--json_config JsonConfig',
19+
required: false,
20+
default: 'graphite_status'
1421
# override filters from Sensu::Handler. not appropriate for metric handlers
1522
def filter; end
1623

1724
def handle
18-
port = settings['graphite_notify']['port'] ? settings['graphite_notify']['port'].to_s : '2003'
19-
graphite = Graphite.new(host: settings['graphite_notify']['host'], port: port)
25+
json_config = config[:json_config]
26+
port = settings[json_config]['port'] ? settings[json_config]['port'].to_s : '2003'
27+
graphite = Graphite.new(host: settings[json_config]['host'], port: port)
2028
return unless graphite
2129
prop = @event['check']['status']
22-
message = "#{settings['graphite_notify']['prefix']}.#{@event['client']['name'].tr('.', '_')}.#{@event['check']['name']}"
30+
message = "#{settings[json_config]['prefix']}.#{@event['client']['name'].tr('.', '_')}.#{@event['check']['name']}"
2331
message += " #{prop} #{graphite.time_now + rand(100)}"
2432
begin
2533
graphite.push_to_graphite do |graphite_socket|
2634
graphite_socket.puts message
2735
end
28-
rescue => e
29-
error_msg = "Can't connect to #{settings['graphite_notify']['host']}:#{port} and send message #{message}: #{e}'"
36+
rescue ETIMEDOUT
37+
error_msg = "Can't connect to #{settings[json_config]['host']}:#{port} and send message #{message}'"
3038
raise error_msg
3139
end
3240
end

0 commit comments

Comments
 (0)