dguettler-god 0.7.13.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/History.txt +293 -0
  2. data/Manifest.txt +102 -0
  3. data/README.txt +59 -0
  4. data/Rakefile +67 -0
  5. data/bin/god +128 -0
  6. data/examples/events.god +84 -0
  7. data/examples/gravatar.god +54 -0
  8. data/examples/single.god +66 -0
  9. data/ext/god/extconf.rb +55 -0
  10. data/ext/god/kqueue_handler.c +123 -0
  11. data/ext/god/netlink_handler.c +168 -0
  12. data/init/god +42 -0
  13. data/lib/god/behavior.rb +52 -0
  14. data/lib/god/behaviors/clean_pid_file.rb +21 -0
  15. data/lib/god/behaviors/clean_unix_socket.rb +21 -0
  16. data/lib/god/behaviors/notify_when_flapping.rb +51 -0
  17. data/lib/god/cli/command.rb +230 -0
  18. data/lib/god/cli/run.rb +176 -0
  19. data/lib/god/cli/version.rb +23 -0
  20. data/lib/god/condition.rb +96 -0
  21. data/lib/god/conditions/always.rb +23 -0
  22. data/lib/god/conditions/complex.rb +86 -0
  23. data/lib/god/conditions/cpu_usage.rb +80 -0
  24. data/lib/god/conditions/degrading_lambda.rb +52 -0
  25. data/lib/god/conditions/disk_usage.rb +27 -0
  26. data/lib/god/conditions/file_mtime.rb +28 -0
  27. data/lib/god/conditions/flapping.rb +128 -0
  28. data/lib/god/conditions/http_response_code.rb +168 -0
  29. data/lib/god/conditions/lambda.rb +25 -0
  30. data/lib/god/conditions/memory_usage.rb +82 -0
  31. data/lib/god/conditions/process_exits.rb +72 -0
  32. data/lib/god/conditions/process_running.rb +74 -0
  33. data/lib/god/conditions/tries.rb +44 -0
  34. data/lib/god/configurable.rb +57 -0
  35. data/lib/god/contact.rb +106 -0
  36. data/lib/god/contacts/campfire.rb +82 -0
  37. data/lib/god/contacts/email.rb +95 -0
  38. data/lib/god/contacts/jabber.rb +65 -0
  39. data/lib/god/contacts/twitter.rb +39 -0
  40. data/lib/god/contacts/webhook.rb +47 -0
  41. data/lib/god/dependency_graph.rb +41 -0
  42. data/lib/god/diagnostics.rb +37 -0
  43. data/lib/god/driver.rb +207 -0
  44. data/lib/god/errors.rb +24 -0
  45. data/lib/god/event_handler.rb +111 -0
  46. data/lib/god/event_handlers/dummy_handler.rb +13 -0
  47. data/lib/god/event_handlers/kqueue_handler.rb +17 -0
  48. data/lib/god/event_handlers/netlink_handler.rb +13 -0
  49. data/lib/god/logger.rb +124 -0
  50. data/lib/god/metric.rb +59 -0
  51. data/lib/god/process.rb +341 -0
  52. data/lib/god/registry.rb +32 -0
  53. data/lib/god/simple_logger.rb +53 -0
  54. data/lib/god/socket.rb +96 -0
  55. data/lib/god/sugar.rb +47 -0
  56. data/lib/god/system/portable_poller.rb +42 -0
  57. data/lib/god/system/process.rb +42 -0
  58. data/lib/god/system/slash_proc_poller.rb +92 -0
  59. data/lib/god/task.rb +491 -0
  60. data/lib/god/timeline.rb +25 -0
  61. data/lib/god/trigger.rb +43 -0
  62. data/lib/god/watch.rb +183 -0
  63. data/lib/god.rb +670 -0
  64. data/test/configs/child_events/child_events.god +44 -0
  65. data/test/configs/child_events/simple_server.rb +3 -0
  66. data/test/configs/child_polls/child_polls.god +37 -0
  67. data/test/configs/child_polls/simple_server.rb +12 -0
  68. data/test/configs/complex/complex.god +59 -0
  69. data/test/configs/complex/simple_server.rb +3 -0
  70. data/test/configs/contact/contact.god +84 -0
  71. data/test/configs/contact/simple_server.rb +3 -0
  72. data/test/configs/daemon_events/daemon_events.god +37 -0
  73. data/test/configs/daemon_events/simple_server.rb +8 -0
  74. data/test/configs/daemon_events/simple_server_stop.rb +11 -0
  75. data/test/configs/daemon_polls/daemon_polls.god +17 -0
  76. data/test/configs/daemon_polls/simple_server.rb +6 -0
  77. data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
  78. data/test/configs/degrading_lambda/tcp_server.rb +15 -0
  79. data/test/configs/matias/matias.god +50 -0
  80. data/test/configs/real.rb +59 -0
  81. data/test/configs/running_load/running_load.god +16 -0
  82. data/test/configs/stress/simple_server.rb +3 -0
  83. data/test/configs/stress/stress.god +15 -0
  84. data/test/configs/task/logs/.placeholder +0 -0
  85. data/test/configs/task/task.god +26 -0
  86. data/test/configs/test.rb +61 -0
  87. data/test/helper.rb +151 -0
  88. data/test/suite.rb +6 -0
  89. data/test/test_behavior.rb +21 -0
  90. data/test/test_campfire.rb +41 -0
  91. data/test/test_condition.rb +50 -0
  92. data/test/test_conditions_disk_usage.rb +56 -0
  93. data/test/test_conditions_http_response_code.rb +109 -0
  94. data/test/test_conditions_process_running.rb +44 -0
  95. data/test/test_conditions_tries.rb +67 -0
  96. data/test/test_contact.rb +109 -0
  97. data/test/test_dependency_graph.rb +62 -0
  98. data/test/test_driver.rb +11 -0
  99. data/test/test_email.rb +45 -0
  100. data/test/test_event_handler.rb +80 -0
  101. data/test/test_god.rb +598 -0
  102. data/test/test_handlers_kqueue_handler.rb +16 -0
  103. data/test/test_logger.rb +66 -0
  104. data/test/test_metric.rb +72 -0
  105. data/test/test_process.rb +246 -0
  106. data/test/test_registry.rb +15 -0
  107. data/test/test_socket.rb +42 -0
  108. data/test/test_sugar.rb +42 -0
  109. data/test/test_system_portable_poller.rb +17 -0
  110. data/test/test_system_process.rb +30 -0
  111. data/test/test_task.rb +262 -0
  112. data/test/test_timeline.rb +37 -0
  113. data/test/test_trigger.rb +59 -0
  114. data/test/test_watch.rb +279 -0
  115. data/test/test_webhook.rb +17 -0
  116. metadata +206 -0
data/lib/god/driver.rb ADDED
@@ -0,0 +1,207 @@
1
+ module God
2
+ class TimedEvent
3
+ include Comparable
4
+
5
+ attr_accessor :at
6
+
7
+ # Instantiate a new TimedEvent that will be triggered after the specified delay
8
+ # +delay+ is the number of seconds from now at which to trigger
9
+ #
10
+ # Returns TimedEvent
11
+ def initialize(delay = 0)
12
+ self.at = Time.now + delay
13
+ end
14
+
15
+ def due?
16
+ Time.now >= self.at
17
+ end
18
+
19
+ def <=>(other)
20
+ self.at <=> other.at
21
+ end
22
+ end # DriverEvent
23
+
24
+ class DriverEvent < TimedEvent
25
+ attr_accessor :condition, :task
26
+
27
+ def initialize(delay, task, condition)
28
+ super delay
29
+ self.task = task
30
+ self.condition = condition
31
+ end
32
+
33
+ def handle_event
34
+ @task.handle_poll(@condition)
35
+ end
36
+ end # DriverEvent
37
+
38
+ class DriverOperation < TimedEvent
39
+ attr_accessor :task, :name, :args
40
+
41
+ def initialize(task, name, args)
42
+ super(0)
43
+ self.task = task
44
+ self.name = name
45
+ self.args = args
46
+ end
47
+
48
+ # Handle the next queued operation that was issued asynchronously
49
+ #
50
+ # Returns nothing
51
+ def handle_event
52
+ @task.send(@name, *@args)
53
+ end
54
+ end
55
+
56
+ class DriverEventQueue
57
+ def initialize
58
+ @shutdown = false
59
+ @waiting = []
60
+ @events = []
61
+ @waiting.taint
62
+ @events.taint
63
+ self.taint
64
+ @mutex = Mutex.new
65
+ end
66
+
67
+ #
68
+ # Wake any sleeping threads after setting the sentinel
69
+ #
70
+ def shutdown
71
+ @shutdown = true
72
+ begin
73
+ @mutex.lock
74
+ @waiting.each do |t|
75
+ t.run
76
+ end
77
+ ensure
78
+ @mutex.unlock
79
+ end
80
+ end
81
+
82
+ #
83
+ # Sleep until the queue has something due
84
+ #
85
+ def pop
86
+ begin
87
+ while (@mutex.lock; @events.empty? or !@events.first.due?)
88
+ @waiting.push Thread.current
89
+ if @events.empty?
90
+ raise ThreadError, "queue empty" if @shutdown
91
+ Thread.stop
92
+ else
93
+ @mutex.unlock
94
+ sleep @events.first.at - Time.now
95
+ @mutex.lock
96
+ end
97
+ end
98
+ @events.shift
99
+ ensure
100
+ @mutex.unlock
101
+ end
102
+ end
103
+
104
+ alias shift pop
105
+ alias deq pop
106
+
107
+ #
108
+ # Add an event to the queue, wake any waiters if what we added needs to
109
+ # happen sooner than the next pending event
110
+ #
111
+ def push(event)
112
+ @mutex.lock
113
+ @events << event
114
+ @events.sort!
115
+ begin
116
+ t = @waiting.shift if @events.first == event
117
+ t.wakeup if t
118
+ rescue ThreadError
119
+ retry
120
+ ensure
121
+ @mutex.unlock
122
+ end
123
+ begin
124
+ t.run if t
125
+ rescue ThreadError
126
+ end
127
+ end
128
+
129
+ alias << push
130
+ alias enq push
131
+
132
+ def empty?
133
+ @que.empty?
134
+ end
135
+
136
+ def clear
137
+ @events.clear
138
+ end
139
+
140
+ def length
141
+ @events.length
142
+ end
143
+
144
+ alias size length
145
+
146
+ def num_waiting
147
+ @waiting.size
148
+ end
149
+ end
150
+
151
+
152
+ class Driver
153
+ attr_reader :thread
154
+
155
+ # Instantiate a new Driver and start the scheduler loop to handle events
156
+ # +task+ is the Task this Driver belongs to
157
+ #
158
+ # Returns Driver
159
+ def initialize(task)
160
+ @task = task
161
+ @events = God::DriverEventQueue.new
162
+
163
+ @thread = Thread.new do
164
+ loop do
165
+ begin
166
+ @events.pop.handle_event
167
+ rescue ThreadError => e
168
+ # queue is empty
169
+ break
170
+ rescue Exception => e
171
+ message = format("Unhandled exception in driver loop - (%s): %s\n%s",
172
+ e.class, e.message, e.backtrace.join("\n"))
173
+ applog(nil, :fatal, message)
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ # Clear all events for this Driver
180
+ #
181
+ # Returns nothing
182
+ def clear_events
183
+ @events.clear
184
+ end
185
+
186
+ # Queue an asynchronous message
187
+ # +name+ is the Symbol name of the operation
188
+ # +args+ is an optional Array of arguments
189
+ #
190
+ # Returns nothing
191
+ def message(name, args = [])
192
+ @events.push(DriverOperation.new(@task, name, args))
193
+ end
194
+
195
+ # Create and schedule a new DriverEvent
196
+ # +condition+ is the Condition
197
+ # +delay+ is the number of seconds to delay (default: interval defined in condition)
198
+ #
199
+ # Returns nothing
200
+ def schedule(condition, delay = condition.interval)
201
+ applog(nil, :debug, "driver schedule #{condition} in #{delay} seconds")
202
+
203
+ @events.push(DriverEvent.new(delay, @task, condition))
204
+ end
205
+ end # Driver
206
+
207
+ end # God
data/lib/god/errors.rb ADDED
@@ -0,0 +1,24 @@
1
+ module God
2
+
3
+ class AbstractMethodNotOverriddenError < StandardError
4
+ end
5
+
6
+ class NoSuchWatchError < StandardError
7
+ end
8
+
9
+ class NoSuchConditionError < StandardError
10
+ end
11
+
12
+ class NoSuchBehaviorError < StandardError
13
+ end
14
+
15
+ class NoSuchContactError < StandardError
16
+ end
17
+
18
+ class InvalidCommandError < StandardError
19
+ end
20
+
21
+ class EventRegistrationFailedError < StandardError
22
+ end
23
+
24
+ end
@@ -0,0 +1,111 @@
1
+ module God
2
+ class EventHandler
3
+ @@actions = {}
4
+ @@handler = nil
5
+ @@loaded = false
6
+
7
+ def self.loaded?
8
+ @@loaded
9
+ end
10
+
11
+ def self.event_system
12
+ @@handler::EVENT_SYSTEM
13
+ end
14
+
15
+ def self.load
16
+ begin
17
+ case RUBY_PLATFORM
18
+ when /darwin/i, /bsd/i
19
+ require 'god/event_handlers/kqueue_handler'
20
+ @@handler = KQueueHandler
21
+ when /linux/i
22
+ require 'god/event_handlers/netlink_handler'
23
+ @@handler = NetlinkHandler
24
+ else
25
+ raise NotImplementedError, "Platform not supported for EventHandler"
26
+ end
27
+ @@loaded = true
28
+ rescue Exception
29
+ require 'god/event_handlers/dummy_handler'
30
+ @@handler = DummyHandler
31
+ @@loaded = false
32
+ end
33
+ end
34
+
35
+ def self.register(pid, event, &block)
36
+ @@actions[pid] ||= {}
37
+ @@actions[pid][event] = block
38
+ @@handler.register_process(pid, @@actions[pid].keys)
39
+ end
40
+
41
+ def self.deregister(pid, event=nil)
42
+ if watching_pid? pid
43
+ running = ::Process.kill(0, pid.to_i) rescue false
44
+ if event.nil?
45
+ @@actions.delete(pid)
46
+ @@handler.register_process(pid, []) if running
47
+ else
48
+ @@actions[pid].delete(event)
49
+ @@handler.register_process(pid, @@actions[pid].keys) if running
50
+ end
51
+ end
52
+ end
53
+
54
+ def self.call(pid, event, extra_data = {})
55
+ @@actions[pid][event].call(extra_data) if watching_pid?(pid) && @@actions[pid][event]
56
+ end
57
+
58
+ def self.watching_pid?(pid)
59
+ @@actions[pid]
60
+ end
61
+
62
+ def self.start
63
+ Thread.new do
64
+ loop do
65
+ begin
66
+ @@handler.handle_events
67
+ rescue Exception => e
68
+ message = format("Unhandled exception (%s): %s\n%s",
69
+ e.class, e.message, e.backtrace.join("\n"))
70
+ applog(nil, :fatal, message)
71
+ end
72
+ end
73
+ end
74
+
75
+ # do a real test to make sure events are working properly
76
+ @@loaded = self.operational?
77
+ end
78
+
79
+ def self.operational?
80
+ com = [false]
81
+
82
+ Thread.new do
83
+ begin
84
+ event_system = God::EventHandler.event_system
85
+
86
+ pid = fork do
87
+ loop { sleep(1) }
88
+ end
89
+
90
+ self.register(pid, :proc_exit) do
91
+ com[0] = true
92
+ end
93
+
94
+ ::Process.kill('KILL', pid)
95
+
96
+ sleep(0.1)
97
+
98
+ self.deregister(pid, :proc_exit) rescue nil
99
+ rescue => e
100
+ puts e.message
101
+ puts e.backtrace.join("\n")
102
+ end
103
+ end.join
104
+
105
+ sleep(0.1)
106
+
107
+ com.first
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,13 @@
1
+ module God
2
+ class DummyHandler
3
+ EVENT_SYSTEM = "none"
4
+
5
+ def self.register_process
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def self.handle_events
10
+ raise NotImplementedError
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ require 'kqueue_handler_ext'
2
+
3
+ module God
4
+ class KQueueHandler
5
+ EVENT_SYSTEM = "kqueue"
6
+
7
+ def self.register_process(pid, events)
8
+ monitor_process(pid, events_mask(events))
9
+ end
10
+
11
+ def self.events_mask(events)
12
+ events.inject(0) do |mask, event|
13
+ mask |= event_mask(event)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'netlink_handler_ext'
2
+
3
+ module God
4
+ class NetlinkHandler
5
+ EVENT_SYSTEM = "netlink"
6
+
7
+ def self.register_process(pid, events)
8
+ # netlink doesn't need to do this
9
+ # it just reads from the eventhandler actions to see if the pid
10
+ # matches the list we're looking for -- Kev
11
+ end
12
+ end
13
+ end
data/lib/god/logger.rb ADDED
@@ -0,0 +1,124 @@
1
+ module God
2
+
3
+ class Logger < SimpleLogger
4
+ SYSLOG_EQUIVALENTS = {:fatal => :crit,
5
+ :error => :err,
6
+ :warn => :debug,
7
+ :info => :debug,
8
+ :debug => :debug}
9
+
10
+ attr_accessor :logs
11
+
12
+ class << self
13
+ attr_accessor :syslog
14
+ end
15
+
16
+ self.syslog ||= true
17
+
18
+ # Instantiate a new Logger object
19
+ def initialize
20
+ super($stdout)
21
+ self.logs = {}
22
+ @mutex = Mutex.new
23
+ @capture = nil
24
+ @spool = Time.now - 10
25
+ @templogio = StringIO.new
26
+ @templog = SimpleLogger.new(@templogio)
27
+ @templog.level = Logger::INFO
28
+ load_syslog
29
+ end
30
+
31
+ # If Logger.syslog is true then attempt to load the syslog bindings. If syslog
32
+ # cannot be loaded, then set Logger.syslog to false and continue.
33
+ #
34
+ # Returns nothing
35
+ def load_syslog
36
+ return unless Logger.syslog
37
+
38
+ begin
39
+ require 'syslog'
40
+
41
+ # Ensure that Syslog is open
42
+ begin
43
+ Syslog.open('god')
44
+ rescue RuntimeError
45
+ Syslog.reopen('god')
46
+ end
47
+ rescue Exception
48
+ Logger.syslog = false
49
+ end
50
+ end
51
+
52
+ # Log a message
53
+ # +watch+ is the String name of the Watch (may be nil if not Watch is applicable)
54
+ # +level+ is the log level [:debug|:info|:warn|:error|:fatal]
55
+ # +text+ is the String message
56
+ #
57
+ # Returns nothing
58
+ def log(watch, level, text)
59
+ # initialize watch log if necessary
60
+ self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch
61
+
62
+ # push onto capture and timeline for the given watch
63
+ @templogio.truncate(0)
64
+ @templogio.rewind
65
+ @templog.send(level, text % [])
66
+ @mutex.synchronize do
67
+ @capture.puts(@templogio.string.dup) if @capture
68
+ if watch && (Time.now - @spool < 2)
69
+ self.logs[watch.name] << [Time.now, @templogio.string.dup]
70
+ end
71
+ end
72
+
73
+ # send to regular logger
74
+ self.send(level, text % [])
75
+
76
+ # send to syslog
77
+ Syslog.send(SYSLOG_EQUIVALENTS[level], text) if Logger.syslog
78
+ end
79
+
80
+ # Get all log output for a given Watch since a certain Time.
81
+ # +watch_name+ is the String name of the Watch
82
+ # +since+ is the Time since which to fetch log lines
83
+ #
84
+ # Returns String
85
+ def watch_log_since(watch_name, since)
86
+ # initialize watch log if necessary
87
+ self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
88
+
89
+ # get and join lines since given time
90
+ @mutex.synchronize do
91
+ @spool = Time.now
92
+ self.logs[watch_name].select do |x|
93
+ x.first > since
94
+ end.map do |x|
95
+ x[1]
96
+ end.join
97
+ end
98
+ end
99
+
100
+ # private
101
+
102
+ # Enable capturing of log
103
+ #
104
+ # Returns nothing
105
+ def start_capture
106
+ @mutex.synchronize do
107
+ @capture = StringIO.new
108
+ end
109
+ end
110
+
111
+ # Disable capturing of log and return what was captured since
112
+ # capturing was enabled with Logger#start_capture
113
+ #
114
+ # Returns String
115
+ def finish_capture
116
+ @mutex.synchronize do
117
+ cap = @capture.string
118
+ @capture = nil
119
+ cap
120
+ end
121
+ end
122
+ end
123
+
124
+ end
data/lib/god/metric.rb ADDED
@@ -0,0 +1,59 @@
1
+ module God
2
+
3
+ class Metric
4
+ attr_accessor :watch, :destination, :conditions
5
+
6
+ def initialize(watch, destination = nil)
7
+ self.watch = watch
8
+ self.destination = destination
9
+ self.conditions = []
10
+ end
11
+
12
+ # Instantiate a Condition of type +kind+ and pass it into the optional
13
+ # block. Attributes of the condition must be set in the config file
14
+ def condition(kind)
15
+ # create the condition
16
+ begin
17
+ c = Condition.generate(kind, self.watch)
18
+ rescue NoSuchConditionError => e
19
+ abort e.message
20
+ end
21
+
22
+ # send to block so config can set attributes
23
+ yield(c) if block_given?
24
+
25
+ # call prepare on the condition
26
+ c.prepare
27
+
28
+ # test generic and specific validity
29
+ unless Condition.valid?(c) && c.valid?
30
+ abort "Exiting on invalid condition"
31
+ end
32
+
33
+ # inherit interval from watch if no poll condition specific interval was set
34
+ if c.kind_of?(PollCondition) && !c.interval
35
+ if self.watch.interval
36
+ c.interval = self.watch.interval
37
+ else
38
+ abort "No interval set for Condition '#{c.class.name}' in Watch '#{self.watch.name}', and no default Watch interval from which to inherit"
39
+ end
40
+ end
41
+
42
+ # remember
43
+ self.conditions << c
44
+ end
45
+
46
+ def enable
47
+ self.conditions.each do |c|
48
+ self.watch.attach(c)
49
+ end
50
+ end
51
+
52
+ def disable
53
+ self.conditions.each do |c|
54
+ self.watch.detach(c)
55
+ end
56
+ end
57
+ end
58
+
59
+ end