firenxis-god 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Announce.txt +135 -0
- data/History.txt +393 -0
- data/README.txt +59 -0
- data/Rakefile +142 -0
- data/bin/god +132 -0
- data/ext/god/.gitignore +5 -0
- data/ext/god/extconf.rb +55 -0
- data/ext/god/kqueue_handler.c +125 -0
- data/ext/god/netlink_handler.c +168 -0
- data/god.gemspec +164 -0
- data/lib/god.rb +701 -0
- data/lib/god/behavior.rb +52 -0
- data/lib/god/behaviors/clean_pid_file.rb +21 -0
- data/lib/god/behaviors/clean_unix_socket.rb +21 -0
- data/lib/god/behaviors/notify_when_flapping.rb +51 -0
- data/lib/god/cli/command.rb +256 -0
- data/lib/god/cli/run.rb +172 -0
- data/lib/god/cli/version.rb +23 -0
- data/lib/god/compat19.rb +36 -0
- data/lib/god/condition.rb +96 -0
- data/lib/god/conditions/always.rb +23 -0
- data/lib/god/conditions/complex.rb +86 -0
- data/lib/god/conditions/cpu_usage.rb +80 -0
- data/lib/god/conditions/degrading_lambda.rb +52 -0
- data/lib/god/conditions/disk_usage.rb +32 -0
- data/lib/god/conditions/file_mtime.rb +28 -0
- data/lib/god/conditions/flapping.rb +128 -0
- data/lib/god/conditions/http_response_code.rb +168 -0
- data/lib/god/conditions/lambda.rb +25 -0
- data/lib/god/conditions/memory_usage.rb +82 -0
- data/lib/god/conditions/process_exits.rb +72 -0
- data/lib/god/conditions/process_running.rb +74 -0
- data/lib/god/conditions/tries.rb +44 -0
- data/lib/god/configurable.rb +57 -0
- data/lib/god/contact.rb +114 -0
- data/lib/god/contacts/campfire.rb +121 -0
- data/lib/god/contacts/email.rb +136 -0
- data/lib/god/contacts/jabber.rb +75 -0
- data/lib/god/contacts/prowl.rb +57 -0
- data/lib/god/contacts/scout.rb +55 -0
- data/lib/god/contacts/twitter.rb +51 -0
- data/lib/god/contacts/webhook.rb +73 -0
- data/lib/god/dependency_graph.rb +41 -0
- data/lib/god/diagnostics.rb +37 -0
- data/lib/god/driver.rb +206 -0
- data/lib/god/errors.rb +24 -0
- data/lib/god/event_handler.rb +108 -0
- data/lib/god/event_handlers/dummy_handler.rb +13 -0
- data/lib/god/event_handlers/kqueue_handler.rb +17 -0
- data/lib/god/event_handlers/netlink_handler.rb +13 -0
- data/lib/god/logger.rb +109 -0
- data/lib/god/metric.rb +59 -0
- data/lib/god/process.rb +363 -0
- data/lib/god/registry.rb +32 -0
- data/lib/god/simple_logger.rb +59 -0
- data/lib/god/socket.rb +107 -0
- data/lib/god/sugar.rb +47 -0
- data/lib/god/sys_logger.rb +45 -0
- data/lib/god/system/portable_poller.rb +42 -0
- data/lib/god/system/process.rb +50 -0
- data/lib/god/system/slash_proc_poller.rb +92 -0
- data/lib/god/task.rb +503 -0
- data/lib/god/timeline.rb +25 -0
- data/lib/god/trigger.rb +43 -0
- data/lib/god/watch.rb +188 -0
- data/test/configs/child_events/child_events.god +44 -0
- data/test/configs/child_events/simple_server.rb +3 -0
- data/test/configs/child_polls/child_polls.god +37 -0
- data/test/configs/child_polls/simple_server.rb +12 -0
- data/test/configs/complex/complex.god +59 -0
- data/test/configs/complex/simple_server.rb +3 -0
- data/test/configs/contact/contact.god +108 -0
- data/test/configs/contact/simple_server.rb +3 -0
- data/test/configs/daemon_events/daemon_events.god +37 -0
- data/test/configs/daemon_events/simple_server.rb +8 -0
- data/test/configs/daemon_events/simple_server_stop.rb +11 -0
- data/test/configs/daemon_polls/daemon_polls.god +17 -0
- data/test/configs/daemon_polls/simple_server.rb +6 -0
- data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
- data/test/configs/degrading_lambda/tcp_server.rb +15 -0
- data/test/configs/lifecycle/lifecycle.god +25 -0
- data/test/configs/matias/matias.god +50 -0
- data/test/configs/real.rb +59 -0
- data/test/configs/running_load/running_load.god +16 -0
- data/test/configs/stop_options/simple_server.rb +12 -0
- data/test/configs/stop_options/stop_options.god +39 -0
- data/test/configs/stress/simple_server.rb +3 -0
- data/test/configs/stress/stress.god +15 -0
- data/test/configs/task/logs/.placeholder +0 -0
- data/test/configs/task/task.god +26 -0
- data/test/configs/test.rb +61 -0
- data/test/helper.rb +141 -0
- data/test/suite.rb +6 -0
- data/test/test_behavior.rb +18 -0
- data/test/test_campfire.rb +23 -0
- data/test/test_condition.rb +50 -0
- data/test/test_conditions_disk_usage.rb +50 -0
- data/test/test_conditions_http_response_code.rb +109 -0
- data/test/test_conditions_process_running.rb +40 -0
- data/test/test_conditions_tries.rb +67 -0
- data/test/test_contact.rb +109 -0
- data/test/test_dependency_graph.rb +62 -0
- data/test/test_driver.rb +11 -0
- data/test/test_email.rb +34 -0
- data/test/test_event_handler.rb +80 -0
- data/test/test_god.rb +570 -0
- data/test/test_handlers_kqueue_handler.rb +16 -0
- data/test/test_jabber.rb +29 -0
- data/test/test_logger.rb +55 -0
- data/test/test_metric.rb +72 -0
- data/test/test_process.rb +247 -0
- data/test/test_prowl.rb +15 -0
- data/test/test_registry.rb +15 -0
- data/test/test_socket.rb +34 -0
- data/test/test_sugar.rb +42 -0
- data/test/test_system_portable_poller.rb +17 -0
- data/test/test_system_process.rb +30 -0
- data/test/test_task.rb +246 -0
- data/test/test_timeline.rb +37 -0
- data/test/test_trigger.rb +59 -0
- data/test/test_watch.rb +279 -0
- data/test/test_webhook.rb +15 -0
- metadata +362 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
module God
|
2
|
+
module Conditions
|
3
|
+
|
4
|
+
# Condition Symbol :process_running
|
5
|
+
# Type: Poll
|
6
|
+
#
|
7
|
+
# Trigger when a process is running or not running depending on attributes.
|
8
|
+
#
|
9
|
+
# Paramaters
|
10
|
+
# Required
|
11
|
+
# +pid_file+ is the pid file of the process in question. Automatically
|
12
|
+
# populated for Watches.
|
13
|
+
# +running" specifies whether you want to trigger if the process is
|
14
|
+
# running (true) or whether it is not running (false)
|
15
|
+
#
|
16
|
+
# Examples
|
17
|
+
#
|
18
|
+
# Trigger if process IS NOT running (from a Watch):
|
19
|
+
#
|
20
|
+
# on.condition(:process_running) do |c|
|
21
|
+
# c.running = false
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# Trigger if process IS running (from a Watch):
|
25
|
+
#
|
26
|
+
# on.condition(:process_running) do |c|
|
27
|
+
# c.running = true
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# Non-Watch Tasks must specify a PID file:
|
31
|
+
#
|
32
|
+
# on.condition(:process_running) do |c|
|
33
|
+
# c.running = false
|
34
|
+
# c.pid_file = "/var/run/mongrel.3000.pid"
|
35
|
+
# end
|
36
|
+
class ProcessRunning < PollCondition
|
37
|
+
attr_accessor :running, :pid_file
|
38
|
+
|
39
|
+
def pid
|
40
|
+
self.pid_file ? File.read(self.pid_file).strip.to_i : self.watch.pid
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid?
|
44
|
+
valid = true
|
45
|
+
valid &= complain("Attribute 'pid_file' must be specified", self) if self.pid_file.nil? && self.watch.pid_file.nil?
|
46
|
+
valid &= complain("Attribute 'running' must be specified", self) if self.running.nil?
|
47
|
+
valid
|
48
|
+
end
|
49
|
+
|
50
|
+
def test
|
51
|
+
self.info = []
|
52
|
+
|
53
|
+
pid = self.pid
|
54
|
+
active = pid && System::Process.new(pid).exists?
|
55
|
+
|
56
|
+
if (self.running && active)
|
57
|
+
self.info.concat(["process is running"])
|
58
|
+
true
|
59
|
+
elsif (!self.running && !active)
|
60
|
+
self.info.concat(["process is not running"])
|
61
|
+
true
|
62
|
+
else
|
63
|
+
if self.running
|
64
|
+
self.info.concat(["process is not running"])
|
65
|
+
else
|
66
|
+
self.info.concat(["process is running"])
|
67
|
+
end
|
68
|
+
false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module God
|
2
|
+
module Conditions
|
3
|
+
|
4
|
+
class Tries < PollCondition
|
5
|
+
attr_accessor :times, :within
|
6
|
+
|
7
|
+
def prepare
|
8
|
+
@timeline = Timeline.new(self.times)
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset
|
12
|
+
@timeline.clear
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
valid = true
|
17
|
+
valid &= complain("Attribute 'times' must be specified", self) if self.times.nil?
|
18
|
+
valid
|
19
|
+
end
|
20
|
+
|
21
|
+
def test
|
22
|
+
@timeline << Time.now
|
23
|
+
|
24
|
+
concensus = (@timeline.size == self.times)
|
25
|
+
duration = self.within.nil? || (@timeline.last - @timeline.first) < self.within
|
26
|
+
|
27
|
+
if within
|
28
|
+
history = "[#{@timeline.size}/#{self.times} within #{(@timeline.last - @timeline.first).to_i}s]"
|
29
|
+
else
|
30
|
+
history = "[#{@timeline.size}/#{self.times}]"
|
31
|
+
end
|
32
|
+
|
33
|
+
if concensus && duration
|
34
|
+
self.info = "tries exceeded #{history}"
|
35
|
+
return true
|
36
|
+
else
|
37
|
+
self.info = "tries within bounds #{history}"
|
38
|
+
return false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module God
|
2
|
+
|
3
|
+
module Configurable
|
4
|
+
# Override this method in your Configurable (optional)
|
5
|
+
#
|
6
|
+
# Called once after the Configurable has been sent to the block and attributes have been
|
7
|
+
# set. Do any post-processing on attributes here
|
8
|
+
def prepare
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def reset
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
# Override this method in your Configurable (optional)
|
17
|
+
#
|
18
|
+
# Called once during evaluation of the config file. Return true if valid, false otherwise
|
19
|
+
#
|
20
|
+
# A convenience method 'complain' is available that will print out a message and return false,
|
21
|
+
# making it easy to report multiple validation errors:
|
22
|
+
#
|
23
|
+
# def valid?
|
24
|
+
# valid = true
|
25
|
+
# valid &= complain("You must specify the 'pid_file' attribute for :memory_usage") if self.pid_file.nil?
|
26
|
+
# valid &= complain("You must specify the 'above' attribute for :memory_usage") if self.above.nil?
|
27
|
+
# valid
|
28
|
+
# end
|
29
|
+
def valid?
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def base_name
|
34
|
+
x = 1 # fix for MRI's local scope optimization bug DO NOT REMOVE!
|
35
|
+
@base_name ||= self.class.name.split('::').last
|
36
|
+
end
|
37
|
+
|
38
|
+
def friendly_name
|
39
|
+
base_name
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.complain(text, c = nil)
|
43
|
+
watch = c.watch rescue nil
|
44
|
+
msg = ""
|
45
|
+
msg += "#{watch.name}: " if watch
|
46
|
+
msg += text
|
47
|
+
msg += " for #{c.friendly_name}" if c
|
48
|
+
applog(watch, :error, msg)
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
def complain(text, c = nil)
|
53
|
+
Configurable.complain(text, c)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/god/contact.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
module God
|
2
|
+
|
3
|
+
class Contact
|
4
|
+
include Configurable
|
5
|
+
|
6
|
+
attr_accessor :name, :group, :info
|
7
|
+
|
8
|
+
def self.generate(kind)
|
9
|
+
sym = kind.to_s.capitalize.gsub(/_(.)/){$1.upcase}.intern
|
10
|
+
c = God::Contacts.const_get(sym).new
|
11
|
+
|
12
|
+
unless c.kind_of?(Contact)
|
13
|
+
abort "Contact '#{c.class.name}' must subclass God::Contact"
|
14
|
+
end
|
15
|
+
|
16
|
+
c
|
17
|
+
rescue NameError
|
18
|
+
raise NoSuchContactError.new("No Contact found with the class name God::Contacts::#{sym}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.valid?(contact)
|
22
|
+
valid = true
|
23
|
+
valid &= Configurable.complain("Attribute 'name' must be specified", contact) if contact.name.nil?
|
24
|
+
valid
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.defaults
|
28
|
+
yield self
|
29
|
+
end
|
30
|
+
|
31
|
+
def arg(name)
|
32
|
+
self.instance_variable_get("@#{name}") || self.class.instance_variable_get("@#{name}")
|
33
|
+
end
|
34
|
+
|
35
|
+
# Normalize the given notify specification into canonical form.
|
36
|
+
# +spec+ is the notify spec as a String, Array of Strings, or Hash
|
37
|
+
#
|
38
|
+
# Canonical form looks like:
|
39
|
+
# {:contacts => ['fred', 'john'], :priority => '1', :category => 'awesome'}
|
40
|
+
# Where :contacts will be present and point to an Array of Strings. Both
|
41
|
+
# :priority and :category may not be present but if they are, they will each
|
42
|
+
# contain a single String.
|
43
|
+
#
|
44
|
+
# Returns normalized notify spec
|
45
|
+
# Raises ArgumentError on invalid spec (message contains details)
|
46
|
+
def self.normalize(spec)
|
47
|
+
case spec
|
48
|
+
when String
|
49
|
+
{:contacts => Array(spec)}
|
50
|
+
when Array
|
51
|
+
unless spec.select { |x| !x.instance_of?(String) }.empty?
|
52
|
+
raise ArgumentError.new("contains non-String elements")
|
53
|
+
end
|
54
|
+
{:contacts => spec}
|
55
|
+
when Hash
|
56
|
+
copy = spec.dup
|
57
|
+
|
58
|
+
# check :contacts
|
59
|
+
if contacts = copy.delete(:contacts)
|
60
|
+
case contacts
|
61
|
+
when String
|
62
|
+
# valid
|
63
|
+
when Array
|
64
|
+
unless contacts.select { |x| !x.instance_of?(String) }.empty?
|
65
|
+
raise ArgumentError.new("has a :contacts key containing non-String elements")
|
66
|
+
end
|
67
|
+
# valid
|
68
|
+
else
|
69
|
+
raise ArgumentError.new("must have a :contacts key pointing to a String or Array of Strings")
|
70
|
+
end
|
71
|
+
else
|
72
|
+
raise ArgumentError.new("must have a :contacts key")
|
73
|
+
end
|
74
|
+
|
75
|
+
# remove priority and category
|
76
|
+
copy.delete(:priority)
|
77
|
+
copy.delete(:category)
|
78
|
+
|
79
|
+
# check for invalid keys
|
80
|
+
unless copy.empty?
|
81
|
+
raise ArgumentError.new("contains extra elements: #{copy.inspect}")
|
82
|
+
end
|
83
|
+
|
84
|
+
# normalize
|
85
|
+
spec[:contacts] &&= Array(spec[:contacts])
|
86
|
+
spec[:priority] &&= spec[:priority].to_s
|
87
|
+
spec[:category] &&= spec[:category].to_s
|
88
|
+
|
89
|
+
spec
|
90
|
+
else
|
91
|
+
raise ArgumentError.new("must be a String (contact name), Array (of contact names), or Hash (contact specification)")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Abstract
|
96
|
+
# Send the message to the external source
|
97
|
+
# +message+ is the message body returned from the condition
|
98
|
+
# +time+ is the Time at which the notification was made
|
99
|
+
# +priority+ is the arbitrary priority String
|
100
|
+
# +category+ is the arbitrary category String
|
101
|
+
# +host+ is the hostname of the server
|
102
|
+
def notify(message, time, priority, category, host)
|
103
|
+
raise AbstractMethodNotOverriddenError.new("Contact#notify must be overridden in subclasses")
|
104
|
+
end
|
105
|
+
|
106
|
+
# Construct the friendly name of this Contact, looks like:
|
107
|
+
#
|
108
|
+
# Contact FooBar
|
109
|
+
def friendly_name
|
110
|
+
super + " Contact '#{self.name}'"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# Send a notice to a Campfire room (http://campfirenow.com).
|
2
|
+
#
|
3
|
+
# subdomain - The String subdomain of the Campfire account. If your URL is
|
4
|
+
# "foo.campfirenow.com" then your subdomain is "foo".
|
5
|
+
# token - The String token used for authentication.
|
6
|
+
# room - The String room name to which the message should be sent.
|
7
|
+
# ssl - A Boolean determining whether or not to use SSL
|
8
|
+
# (default: false).
|
9
|
+
|
10
|
+
require 'net/http'
|
11
|
+
require 'net/https'
|
12
|
+
|
13
|
+
CONTACT_DEPS[:campfire] = ['json']
|
14
|
+
CONTACT_DEPS[:campfire].each do |d|
|
15
|
+
require d
|
16
|
+
end
|
17
|
+
|
18
|
+
module Marshmallow
|
19
|
+
class Connection
|
20
|
+
def initialize(options)
|
21
|
+
raise "Required option :subdomain not set." unless options[:subdomain]
|
22
|
+
raise "Required option :token not set." unless options[:token]
|
23
|
+
@options = options
|
24
|
+
end
|
25
|
+
|
26
|
+
def base_url
|
27
|
+
scheme = @options[:ssl] ? 'https' : 'http'
|
28
|
+
subdomain = @options[:subdomain]
|
29
|
+
"#{scheme}://#{subdomain}.campfirenow.com"
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_room_id_by_name(room)
|
33
|
+
url = URI.parse("#{base_url}/rooms.json")
|
34
|
+
|
35
|
+
http = Net::HTTP.new(url.host, url.port)
|
36
|
+
http.use_ssl = true if @options[:ssl]
|
37
|
+
|
38
|
+
req = Net::HTTP::Get.new(url.path)
|
39
|
+
req.basic_auth(@options[:token], 'X')
|
40
|
+
|
41
|
+
res = http.request(req)
|
42
|
+
case res
|
43
|
+
when Net::HTTPSuccess
|
44
|
+
rooms = JSON.parse(res.body)
|
45
|
+
room = rooms['rooms'].select { |x| x['name'] == room }
|
46
|
+
rooms.empty? ? nil : room.first['id']
|
47
|
+
else
|
48
|
+
raise res.error!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def speak(room, message)
|
53
|
+
room_id = find_room_id_by_name(room)
|
54
|
+
raise "No such room: #{room}." unless room_id
|
55
|
+
|
56
|
+
url = URI.parse("#{base_url}/room/#{room_id}/speak.json")
|
57
|
+
|
58
|
+
http = Net::HTTP.new(url.host, url.port)
|
59
|
+
http.use_ssl = true if @options[:ssl]
|
60
|
+
|
61
|
+
req = Net::HTTP::Post.new(url.path)
|
62
|
+
req.basic_auth(@options[:token], 'X')
|
63
|
+
req.set_content_type('application/json')
|
64
|
+
req.body = { 'message' => { 'body' => message } }.to_json
|
65
|
+
|
66
|
+
res = http.request(req)
|
67
|
+
case res
|
68
|
+
when Net::HTTPSuccess
|
69
|
+
true
|
70
|
+
else
|
71
|
+
raise res.error!
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module God
|
78
|
+
module Contacts
|
79
|
+
|
80
|
+
class Campfire < Contact
|
81
|
+
class << self
|
82
|
+
attr_accessor :subdomain, :token, :room, :ssl
|
83
|
+
attr_accessor :format
|
84
|
+
end
|
85
|
+
|
86
|
+
self.ssl = false
|
87
|
+
|
88
|
+
self.format = lambda do |message, time, priority, category, host|
|
89
|
+
"[#{time.strftime('%H:%M:%S')}] #{host} - #{message}"
|
90
|
+
end
|
91
|
+
|
92
|
+
attr_accessor :subdomain, :token, :room, :ssl
|
93
|
+
|
94
|
+
def valid?
|
95
|
+
valid = true
|
96
|
+
valid &= complain("Attribute 'subdomain' must be specified", self) unless arg(:subdomain)
|
97
|
+
valid &= complain("Attribute 'token' must be specified", self) unless arg(:token)
|
98
|
+
valid &= complain("Attribute 'room' must be specified", self) unless arg(:room)
|
99
|
+
valid
|
100
|
+
end
|
101
|
+
|
102
|
+
def notify(message, time, priority, category, host)
|
103
|
+
body = Campfire.format.call(message, time, priority, category, host)
|
104
|
+
|
105
|
+
conn = Marshmallow::Connection.new(
|
106
|
+
:subdomain => arg(:subdomain),
|
107
|
+
:token => arg(:token),
|
108
|
+
:ssl => arg(:ssl)
|
109
|
+
)
|
110
|
+
|
111
|
+
conn.speak(arg(:room), body)
|
112
|
+
|
113
|
+
self.info = "notified campfire: #{arg(:subdomain)}"
|
114
|
+
rescue Object => e
|
115
|
+
applog(nil, :info, "failed to notify campfire: #{e.message}")
|
116
|
+
applog(nil, :debug, e.backtrace.join("\n"))
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# Send a notice to an email address.
|
2
|
+
#
|
3
|
+
# to_email - The String email address to which the email will be sent.
|
4
|
+
# to_name - The String name corresponding to the recipient.
|
5
|
+
# from_email - The String email address from which the email will be sent.
|
6
|
+
# from_name - The String name corresponding to the sender.
|
7
|
+
# delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
|
8
|
+
# (default: :smtp).
|
9
|
+
#
|
10
|
+
# === SMTP Options (when delivery_method = :smtp) ===
|
11
|
+
# server_host - The String hostname of the SMTP server (default: localhost).
|
12
|
+
# server_port - The Integer port of the SMTP server (default: 25).
|
13
|
+
# server_auth - The Boolean of whether or not to use authentication
|
14
|
+
# (default: false).
|
15
|
+
#
|
16
|
+
# === SMTP Auth Options (when server_auth = true) ===
|
17
|
+
# server_domain - The String domain.
|
18
|
+
# server_user - The String username.
|
19
|
+
# server_password - The String password.
|
20
|
+
#
|
21
|
+
# === Sendmail Options (when delivery_method = :sendmail) ===
|
22
|
+
# sendmail_path - The String path to the sendmail executable
|
23
|
+
# (default: "/usr/sbin/sendmail").
|
24
|
+
# sendmail_args - The String args to send to sendmail (default "-i -t").
|
25
|
+
|
26
|
+
require 'time'
|
27
|
+
require 'net/smtp'
|
28
|
+
|
29
|
+
module God
|
30
|
+
module Contacts
|
31
|
+
|
32
|
+
class Email < Contact
|
33
|
+
class << self
|
34
|
+
attr_accessor :to_email, :to_name, :from_email, :from_name,
|
35
|
+
:delivery_method, :server_host, :server_port,
|
36
|
+
:server_auth, :server_domain, :server_user,
|
37
|
+
:server_password, :sendmail_path, :sendmail_args, :enable_starttls
|
38
|
+
attr_accessor :format
|
39
|
+
end
|
40
|
+
|
41
|
+
self.from_email = 'god@example.com'
|
42
|
+
self.from_name = 'God Process Monitoring'
|
43
|
+
self.delivery_method = :smtp
|
44
|
+
self.server_auth = false
|
45
|
+
self.server_host = 'localhost'
|
46
|
+
self.server_port = 25
|
47
|
+
self.sendmail_path = '/usr/sbin/sendmail'
|
48
|
+
self.sendmail_args = '-i -t'
|
49
|
+
|
50
|
+
self.format = lambda do |name, from_email, from_name, to_email, to_name, message, time, priority, category, host|
|
51
|
+
<<-EOF
|
52
|
+
From: #{from_name} <#{from_email}>
|
53
|
+
To: #{to_name || name} <#{to_email}>
|
54
|
+
Subject: [god] #{message}
|
55
|
+
Date: #{time.httpdate}
|
56
|
+
Message-Id: <#{rand(1000000000).to_s(36)}.#{$$}.#{from_email}>
|
57
|
+
|
58
|
+
Message: #{message}
|
59
|
+
Host: #{host}
|
60
|
+
Priority: #{priority}
|
61
|
+
Category: #{category}
|
62
|
+
EOF
|
63
|
+
end
|
64
|
+
|
65
|
+
attr_accessor :to_email, :to_name, :from_email, :from_name,
|
66
|
+
:delivery_method, :server_host, :server_port,
|
67
|
+
:server_auth, :server_domain, :server_user,
|
68
|
+
:server_password, :sendmail_path, :sendmail_args, :enable_starttls
|
69
|
+
|
70
|
+
def valid?
|
71
|
+
valid = true
|
72
|
+
valid &= complain("Attribute 'to_email' must be specified", self) unless arg(:to_email)
|
73
|
+
valid &= complain("Attribute 'delivery_method' must be one of [ :smtp | :sendmail ]", self) unless [:smtp, :sendmail].include?(arg(:delivery_method))
|
74
|
+
if arg(:delivery_method) == :smtp
|
75
|
+
valid &= complain("Attribute 'server_host' must be specified", self) unless arg(:server_host)
|
76
|
+
valid &= complain("Attribute 'server_port' must be specified", self) unless arg(:server_port)
|
77
|
+
if arg(:server_auth)
|
78
|
+
valid &= complain("Attribute 'server_domain' must be specified", self) unless arg(:server_domain)
|
79
|
+
valid &= complain("Attribute 'server_user' must be specified", self) unless arg(:server_user)
|
80
|
+
valid &= complain("Attribute 'server_password' must be specified", self) unless arg(:server_password)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
valid
|
84
|
+
end
|
85
|
+
|
86
|
+
def notify(message, time, priority, category, host)
|
87
|
+
body = Email.format.call(self.name, arg(:from_email), arg(:from_name),
|
88
|
+
arg(:to_email), arg(:to_name), message, time,
|
89
|
+
priority, category, host)
|
90
|
+
|
91
|
+
case arg(:delivery_method)
|
92
|
+
when :smtp
|
93
|
+
notify_smtp(body)
|
94
|
+
when :sendmail
|
95
|
+
notify_sendmail(body)
|
96
|
+
end
|
97
|
+
|
98
|
+
self.info = "sent email to #{arg(:to_email)} via #{arg(:delivery_method).to_s}"
|
99
|
+
rescue Object => e
|
100
|
+
applog(nil, :info, "failed to send email to #{arg(:to_email)} via #{arg(:delivery_method).to_s}: #{e.message}")
|
101
|
+
applog(nil, :debug, e.backtrace.join("\n"))
|
102
|
+
end
|
103
|
+
|
104
|
+
def notify_smtp(mail)
|
105
|
+
args = [arg(:server_host), arg(:server_port)]
|
106
|
+
if arg(:server_auth)
|
107
|
+
args << arg(:server_domain)
|
108
|
+
args << arg(:server_user)
|
109
|
+
args << arg(:server_password)
|
110
|
+
args << arg(:server_auth)
|
111
|
+
end
|
112
|
+
|
113
|
+
if arg(:enable_starttls)
|
114
|
+
smtp = Net::SMTP.new(arg(:server_host),arg(:server_port))
|
115
|
+
smtp.enable_starttls
|
116
|
+
|
117
|
+
smtp.start(arg(:server_host), arg(:server_user),arg(:server_password), arg(:server_auth)) do |smtp|
|
118
|
+
smtp.send_message(mail,arg(:from_email), arg(:to_email) )
|
119
|
+
end
|
120
|
+
else
|
121
|
+
Net::SMTP.start(*args) do |smtp|
|
122
|
+
smtp.send_message(mail, arg(:from_email), arg(:to_email))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def notify_sendmail(mail)
|
128
|
+
IO.popen("#{arg(:sendmail_path)} #{arg(:sendmail_args)}","w+") do |sm|
|
129
|
+
sm.print(mail.gsub(/\r/, ''))
|
130
|
+
sm.flush
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|