olgen-god 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. data/.gitignore +8 -0
  2. data/Announce.txt +135 -0
  3. data/History.txt +345 -0
  4. data/README.txt +59 -0
  5. data/Rakefile +69 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/god +132 -0
  8. data/examples/events.god +84 -0
  9. data/examples/gravatar.god +54 -0
  10. data/examples/single.god +66 -0
  11. data/ext/god/.gitignore +5 -0
  12. data/ext/god/extconf.rb +55 -0
  13. data/ext/god/kqueue_handler.c +123 -0
  14. data/ext/god/netlink_handler.c +168 -0
  15. data/god.gemspec +226 -0
  16. data/ideas/execve/.DS_Store +0 -0
  17. data/ideas/execve/execve.c +29 -0
  18. data/ideas/execve/extconf.rb +11 -0
  19. data/ideas/execve/go.rb +8 -0
  20. data/ideas/future.god +82 -0
  21. data/init/god +42 -0
  22. data/init/lsb_compliant_god +109 -0
  23. data/lib/god/behavior.rb +52 -0
  24. data/lib/god/behaviors/clean_pid_file.rb +21 -0
  25. data/lib/god/behaviors/clean_unix_socket.rb +21 -0
  26. data/lib/god/behaviors/notify_when_flapping.rb +51 -0
  27. data/lib/god/cli/command.rb +255 -0
  28. data/lib/god/cli/run.rb +172 -0
  29. data/lib/god/cli/version.rb +23 -0
  30. data/lib/god/condition.rb +96 -0
  31. data/lib/god/conditions/always.rb +23 -0
  32. data/lib/god/conditions/complex.rb +86 -0
  33. data/lib/god/conditions/cpu_usage.rb +80 -0
  34. data/lib/god/conditions/degrading_lambda.rb +52 -0
  35. data/lib/god/conditions/disk_usage.rb +32 -0
  36. data/lib/god/conditions/file_mtime.rb +28 -0
  37. data/lib/god/conditions/flapping.rb +128 -0
  38. data/lib/god/conditions/http_response_code.rb +168 -0
  39. data/lib/god/conditions/lambda.rb +25 -0
  40. data/lib/god/conditions/memory_usage.rb +82 -0
  41. data/lib/god/conditions/ping.rb +40 -0
  42. data/lib/god/conditions/process_exits.rb +72 -0
  43. data/lib/god/conditions/process_running.rb +74 -0
  44. data/lib/god/conditions/tries.rb +44 -0
  45. data/lib/god/configurable.rb +57 -0
  46. data/lib/god/contact.rb +106 -0
  47. data/lib/god/contacts/campfire.rb +83 -0
  48. data/lib/god/contacts/email.rb +95 -0
  49. data/lib/god/contacts/jabber.rb +127 -0
  50. data/lib/god/contacts/sms.rb +96 -0
  51. data/lib/god/contacts/twitter.rb +39 -0
  52. data/lib/god/contacts/webhook.rb +47 -0
  53. data/lib/god/dependency_graph.rb +41 -0
  54. data/lib/god/diagnostics.rb +37 -0
  55. data/lib/god/driver.rb +207 -0
  56. data/lib/god/errors.rb +24 -0
  57. data/lib/god/event_handler.rb +111 -0
  58. data/lib/god/event_handlers/dummy_handler.rb +13 -0
  59. data/lib/god/event_handlers/kqueue_handler.rb +17 -0
  60. data/lib/god/event_handlers/netlink_handler.rb +13 -0
  61. data/lib/god/logger.rb +124 -0
  62. data/lib/god/metric.rb +59 -0
  63. data/lib/god/process.rb +352 -0
  64. data/lib/god/registry.rb +32 -0
  65. data/lib/god/simple_logger.rb +53 -0
  66. data/lib/god/smtp_tls.rb +55 -0
  67. data/lib/god/socket.rb +96 -0
  68. data/lib/god/sugar.rb +47 -0
  69. data/lib/god/system/portable_poller.rb +42 -0
  70. data/lib/god/system/process.rb +50 -0
  71. data/lib/god/system/slash_proc_poller.rb +92 -0
  72. data/lib/god/task.rb +503 -0
  73. data/lib/god/timeline.rb +25 -0
  74. data/lib/god/trigger.rb +43 -0
  75. data/lib/god/watch.rb +183 -0
  76. data/lib/god.rb +675 -0
  77. data/site/images/banner.jpg +0 -0
  78. data/site/images/bg.gif +0 -0
  79. data/site/images/bg_grey.gif +0 -0
  80. data/site/images/bullet.jpg +0 -0
  81. data/site/images/corner_green.gif +0 -0
  82. data/site/images/corner_green.psd +0 -0
  83. data/site/images/corner_pink.gif +0 -0
  84. data/site/images/god_logo.png +0 -0
  85. data/site/images/header_bg.gif +0 -0
  86. data/site/images/header_bg.jpg +0 -0
  87. data/site/images/red_dot.gif +0 -0
  88. data/site/images/top_bg.gif +0 -0
  89. data/site/index.html +563 -0
  90. data/site/install.html +2 -0
  91. data/site/javascripts/code_highlighter.js +188 -0
  92. data/site/javascripts/ruby.js +18 -0
  93. data/site/stylesheets/layout.css +174 -0
  94. data/test/configs/child_events/child_events.god +44 -0
  95. data/test/configs/child_events/simple_server.rb +3 -0
  96. data/test/configs/child_polls/child_polls.god +37 -0
  97. data/test/configs/child_polls/simple_server.rb +12 -0
  98. data/test/configs/complex/complex.god +59 -0
  99. data/test/configs/complex/simple_server.rb +3 -0
  100. data/test/configs/contact/contact.god +84 -0
  101. data/test/configs/contact/simple_server.rb +3 -0
  102. data/test/configs/daemon_events/daemon_events.god +37 -0
  103. data/test/configs/daemon_events/simple_server.rb +8 -0
  104. data/test/configs/daemon_events/simple_server_stop.rb +11 -0
  105. data/test/configs/daemon_polls/daemon_polls.god +17 -0
  106. data/test/configs/daemon_polls/simple_server.rb +6 -0
  107. data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
  108. data/test/configs/degrading_lambda/tcp_server.rb +15 -0
  109. data/test/configs/lifecycle/lifecycle.god +25 -0
  110. data/test/configs/matias/matias.god +50 -0
  111. data/test/configs/real.rb +59 -0
  112. data/test/configs/running_load/running_load.god +16 -0
  113. data/test/configs/stress/simple_server.rb +3 -0
  114. data/test/configs/stress/stress.god +15 -0
  115. data/test/configs/task/logs/.placeholder +0 -0
  116. data/test/configs/task/task.god +26 -0
  117. data/test/configs/test.rb +61 -0
  118. data/test/helper.rb +139 -0
  119. data/test/suite.rb +6 -0
  120. data/test/test_behavior.rb +18 -0
  121. data/test/test_campfire.rb +41 -0
  122. data/test/test_condition.rb +50 -0
  123. data/test/test_conditions_disk_usage.rb +50 -0
  124. data/test/test_conditions_http_response_code.rb +109 -0
  125. data/test/test_conditions_process_running.rb +40 -0
  126. data/test/test_conditions_tries.rb +67 -0
  127. data/test/test_contact.rb +109 -0
  128. data/test/test_dependency_graph.rb +62 -0
  129. data/test/test_driver.rb +11 -0
  130. data/test/test_email.rb +45 -0
  131. data/test/test_event_handler.rb +80 -0
  132. data/test/test_god.rb +570 -0
  133. data/test/test_handlers_kqueue_handler.rb +16 -0
  134. data/test/test_jabber.rb +36 -0
  135. data/test/test_logger.rb +55 -0
  136. data/test/test_metric.rb +72 -0
  137. data/test/test_process.rb +247 -0
  138. data/test/test_registry.rb +15 -0
  139. data/test/test_socket.rb +34 -0
  140. data/test/test_sugar.rb +42 -0
  141. data/test/test_system_portable_poller.rb +17 -0
  142. data/test/test_system_process.rb +30 -0
  143. data/test/test_task.rb +246 -0
  144. data/test/test_timeline.rb +37 -0
  145. data/test/test_trigger.rb +59 -0
  146. data/test/test_watch.rb +279 -0
  147. data/test/test_webhook.rb +17 -0
  148. metadata +242 -0
data/lib/god/socket.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'drb'
2
+
3
+ module God
4
+
5
+ # The God::Server oversees the DRb server which dishes out info on this God daemon.
6
+ class Socket
7
+ attr_reader :port
8
+
9
+ # The location of the socket for a given port
10
+ # +port+ is the port number
11
+ #
12
+ # Returns String (file location)
13
+ def self.socket_file(port)
14
+ "/tmp/god.#{port}.sock"
15
+ end
16
+
17
+ # The address of the socket for a given port
18
+ # +port+ is the port number
19
+ #
20
+ # Returns String (drb address)
21
+ def self.socket(port)
22
+ "drbunix://#{self.socket_file(port)}"
23
+ end
24
+
25
+ # The location of the socket for this Server
26
+ #
27
+ # Returns String (file location)
28
+ def socket_file
29
+ self.class.socket_file(@port)
30
+ end
31
+
32
+ # The address of the socket for this Server
33
+ #
34
+ # Returns String (drb address)
35
+ def socket
36
+ self.class.socket(@port)
37
+ end
38
+
39
+ # Create a new Server and star the DRb server
40
+ # +port+ is the port on which to start the DRb service (default nil)
41
+ def initialize(port = nil)
42
+ @port = port
43
+ start
44
+ end
45
+
46
+ # Returns true
47
+ def ping
48
+ true
49
+ end
50
+
51
+ # Forward API calls to God
52
+ #
53
+ # Returns whatever the forwarded call returns
54
+ def method_missing(*args, &block)
55
+ God.send(*args, &block)
56
+ end
57
+
58
+ # Stop the DRb server and delete the socket file
59
+ #
60
+ # Returns nothing
61
+ def stop
62
+ DRb.stop_service
63
+ FileUtils.rm_f(self.socket_file)
64
+ end
65
+
66
+ private
67
+
68
+ # Start the DRb server. Abort if there is already a running god instance
69
+ # on the socket.
70
+ #
71
+ # Returns nothing
72
+ def start
73
+ begin
74
+ @drb ||= DRb.start_service(self.socket, self)
75
+ applog(nil, :info, "Started on #{DRb.uri}")
76
+ rescue Errno::EADDRINUSE
77
+ applog(nil, :info, "Socket already in use")
78
+ DRb.start_service
79
+ server = DRbObject.new(nil, self.socket)
80
+
81
+ begin
82
+ Timeout.timeout(5) do
83
+ server.ping
84
+ end
85
+ abort "Socket #{self.socket} already in use by another instance of god"
86
+ rescue StandardError, Timeout::Error
87
+ applog(nil, :info, "Socket is stale, reopening")
88
+ File.delete(self.socket_file) rescue nil
89
+ @drb ||= DRb.start_service(self.socket, self)
90
+ applog(nil, :info, "Started on #{DRb.uri}")
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ end
data/lib/god/sugar.rb ADDED
@@ -0,0 +1,47 @@
1
+ class Numeric
2
+ def seconds
3
+ self
4
+ end
5
+
6
+ alias :second :seconds
7
+
8
+ def minutes
9
+ self * 60
10
+ end
11
+
12
+ alias :minute :minutes
13
+
14
+ def hours
15
+ self * 3600
16
+ end
17
+
18
+ alias :hour :hours
19
+
20
+ def days
21
+ self * 86400
22
+ end
23
+
24
+ alias :day :days
25
+
26
+ def kilobytes
27
+ self
28
+ end
29
+
30
+ alias :kilobyte :kilobytes
31
+
32
+ def megabytes
33
+ self * 1024
34
+ end
35
+
36
+ alias :megabyte :megabytes
37
+
38
+ def gigabytes
39
+ self * (1024 ** 2)
40
+ end
41
+
42
+ alias :gigabyte :gigabytes
43
+
44
+ def percent
45
+ self
46
+ end
47
+ end
@@ -0,0 +1,42 @@
1
+ module God
2
+ module System
3
+ class PortablePoller
4
+ def initialize(pid)
5
+ @pid = pid
6
+ end
7
+ # Memory usage in kilobytes (resident set size)
8
+ def memory
9
+ ps_int('rss')
10
+ end
11
+
12
+ # Percentage memory usage
13
+ def percent_memory
14
+ ps_float('%mem')
15
+ end
16
+
17
+ # Percentage CPU usage
18
+ def percent_cpu
19
+ ps_float('%cpu')
20
+ end
21
+
22
+ private
23
+
24
+ def ps_int(keyword)
25
+ `ps -o #{keyword}= -p #{@pid}`.to_i
26
+ end
27
+
28
+ def ps_float(keyword)
29
+ `ps -o #{keyword}= -p #{@pid}`.to_f
30
+ end
31
+
32
+ def ps_string(keyword)
33
+ `ps -o #{keyword}= -p #{@pid}`.strip
34
+ end
35
+
36
+ def time_string_to_seconds(text)
37
+ _, minutes, seconds, useconds = *text.match(/(\d+):(\d{2}).(\d{2})/)
38
+ (minutes.to_i * 60) + seconds.to_i
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ module God
2
+ module System
3
+
4
+ class Process
5
+ def self.fetch_system_poller
6
+ @@poller ||= if SlashProcPoller.usable?
7
+ SlashProcPoller
8
+ else
9
+ PortablePoller
10
+ end
11
+ end
12
+
13
+ def initialize(pid)
14
+ @pid = pid.to_i
15
+ @poller = self.class.fetch_system_poller.new(@pid)
16
+ end
17
+
18
+ # Return true if this process is running, false otherwise
19
+ def exists?
20
+ !!::Process.kill(0, @pid) rescue false
21
+ end
22
+
23
+ # Memory usage in kilobytes (resident set size)
24
+ def memory
25
+ @poller.memory
26
+ end
27
+
28
+ # Percentage memory usage
29
+ def percent_memory
30
+ @poller.percent_memory
31
+ end
32
+
33
+ # Percentage CPU usage
34
+ def percent_cpu
35
+ @poller.percent_cpu
36
+ end
37
+
38
+ private
39
+
40
+ def fetch_system_poller
41
+ if SlashProcPoller.usable?
42
+ SlashProcPoller
43
+ else
44
+ PortablePoller
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,92 @@
1
+ module God
2
+ module System
3
+ class SlashProcPoller < PortablePoller
4
+ @@kb_per_page = 4 # TODO: Need to make this portable
5
+ @@hertz = 100
6
+ @@total_mem = nil
7
+
8
+ MeminfoPath = '/proc/meminfo'
9
+ UptimePath = '/proc/uptime'
10
+
11
+ RequiredPaths = [MeminfoPath, UptimePath]
12
+
13
+ # FreeBSD has /proc by default, but nothing mounted there!
14
+ # So we should check for the actual required paths!
15
+ # Returns true if +RequiredPaths+ are readable.
16
+ def self.usable?
17
+ RequiredPaths.all? do |path|
18
+ test(?r, path) && readable?(path)
19
+ end
20
+ end
21
+
22
+ def initialize(pid)
23
+ super(pid)
24
+
25
+ unless @@total_mem # in K
26
+ File.open(MeminfoPath) do |f|
27
+ @@total_mem = f.gets.split[1]
28
+ end
29
+ end
30
+ end
31
+
32
+ def memory
33
+ stat[:rss].to_i * @@kb_per_page
34
+ rescue # This shouldn't fail is there's an error (or proc doesn't exist)
35
+ 0
36
+ end
37
+
38
+ def percent_memory
39
+ (memory / @@total_mem.to_f) * 100
40
+ rescue # This shouldn't fail is there's an error (or proc doesn't exist)
41
+ 0
42
+ end
43
+
44
+ # TODO: Change this to calculate the wma instead
45
+ def percent_cpu
46
+ stats = stat
47
+ total_time = stats[:utime].to_i + stats[:stime].to_i # in jiffies
48
+ seconds = uptime - stats[:starttime].to_i / @@hertz
49
+ if seconds == 0
50
+ 0
51
+ else
52
+ ((total_time * 1000 / @@hertz) / seconds) / 10
53
+ end
54
+ rescue # This shouldn't fail is there's an error (or proc doesn't exist)
55
+ 0
56
+ end
57
+
58
+ private
59
+
60
+ # Some systems (CentOS?) have a /proc, but they can hang when trying to
61
+ # read from them. Try to use this sparingly as it is expensive.
62
+ def self.readable?(path)
63
+ begin
64
+ timeout(1) { File.read(path) }
65
+ rescue Timeout::Error
66
+ false
67
+ end
68
+ end
69
+
70
+ # in seconds
71
+ def uptime
72
+ File.read(UptimePath).split[0].to_f
73
+ end
74
+
75
+ def stat
76
+ stats = {}
77
+ stats[:pid], stats[:comm], stats[:state], stats[:ppid], stats[:pgrp],
78
+ stats[:session], stats[:tty_nr], stats[:tpgid], stats[:flags],
79
+ stats[:minflt], stats[:cminflt], stats[:majflt], stats[:cmajflt],
80
+ stats[:utime], stats[:stime], stats[:cutime], stats[:cstime],
81
+ stats[:priority], stats[:nice], _, stats[:itrealvalue],
82
+ stats[:starttime], stats[:vsize], stats[:rss], stats[:rlim],
83
+ stats[:startcode], stats[:endcode], stats[:startstack], stats[:kstkesp],
84
+ stats[:kstkeip], stats[:signal], stats[:blocked], stats[:sigignore],
85
+ stats[:sigcatch], stats[:wchan], stats[:nswap], stats[:cnswap],
86
+ stats[:exit_signal], stats[:processor], stats[:rt_priority],
87
+ stats[:policy] = File.read("/proc/#{@pid}/stat").split
88
+ stats
89
+ end
90
+ end
91
+ end
92
+ end