New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New SENSU handling added to "contacts" #199
Changes from 5 commits
f1c4a1c
82f74c5
273af19
44b63be
b128fb1
f511b4c
7130261
a5a67ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
# Send a notice to a SENSU client socket, port 3030 on 'localhost' only. | ||
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
# [mandatory] | ||
# check_name - a unique check name | ||
# | ||
# [optional] | ||
# status_code - status codes used are 0 for OK, 1 for WARNING, 2 for CRITICAL, and 3 or greater to indicate UNKNOWN or CUSTOM. | ||
# handler - default handler | ||
# | ||
|
||
CONTACT_DEPS[:sensu] = ['json'] | ||
CONTACT_DEPS[:sensu].each do |d| | ||
require d | ||
end | ||
|
||
module God | ||
module Contacts | ||
|
||
class Sensu < Contact | ||
class << self | ||
attr_accessor :check_name, :status_code, :handler, :host, :port | ||
end | ||
|
||
self.status_code = 2 | ||
self.handler = 'default' | ||
self.host = 'localhost' | ||
self.port = 3030 | ||
|
||
def valid? | ||
valid = true | ||
valid &= complain("Attribute 'check_name' must be specified", self) unless arg(:check_name) | ||
valid | ||
end | ||
|
||
attr_accessor :check_name, :status_code, :handler, :host, :port | ||
|
||
def sensu_client_socket(msg) | ||
u = UDPSocket.new | ||
u.send(msg + "\n", 0, arg(:host).nil? ? self.host : arg(:host), arg(:port).nil? ? self.port : arg(:port)) | ||
end | ||
|
||
def notify(message, time, priority, category, host) | ||
data = { | ||
:category => category, | ||
:message => message, | ||
:priority => priority, | ||
:host => host, | ||
:time => time, | ||
} | ||
parcel = { 'name' => eval("\"" + arg(:check_name) + "\""), 'status' => arg(:status_code).nil? ? self.status_code : arg(:status_code), 'output' => data.to_json, 'handler' => arg(:handler).empty? ? self.handler : arg(:handler) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry but I didn't notice this |
||
sensu_client_socket parcel.to_json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the message is sent, update |
||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env ruby | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
require File.dirname(__FILE__) + '/helper' | ||
|
||
class TestSensu < Test::Unit::TestCase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're using minitest for this project, so you'll need to change |
||
def test_sensu_notify | ||
sensu = God::Contacts::Sensu.new | ||
sensu.check_name = "TestSensuContact" | ||
|
||
sensu.notify("Test", Time.now, "Test", "Test", "") | ||
assert_equal "sent sensu #{sensu.check_name} notification with status code #{sensu.status_code}", "sent sensu #{sensu.check_name} notification with status code #{sensu.status_code}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this UDPSocket.any_instance.expects(:send)
sensu.notify("Test", Time.now, "Test", "Test", "") |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to close the
UDPSocket
once you're done using it to prevent leaking sockets.