reel-eye 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +32 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +170 -0
  8. data/Rakefile +20 -0
  9. data/bin/eye +322 -0
  10. data/bin/loader_eye +58 -0
  11. data/examples/notify.eye +18 -0
  12. data/examples/process_thin.rb +29 -0
  13. data/examples/processes/em.rb +57 -0
  14. data/examples/processes/forking.rb +20 -0
  15. data/examples/processes/sample.rb +144 -0
  16. data/examples/processes/thin.ru +12 -0
  17. data/examples/puma.eye +34 -0
  18. data/examples/rbenv.eye +11 -0
  19. data/examples/sidekiq.eye +23 -0
  20. data/examples/test.eye +81 -0
  21. data/examples/thin-farm.eye +29 -0
  22. data/examples/unicorn.eye +31 -0
  23. data/eye.gemspec +42 -0
  24. data/lib/eye.rb +28 -0
  25. data/lib/eye/application.rb +74 -0
  26. data/lib/eye/checker.rb +138 -0
  27. data/lib/eye/checker/cpu.rb +27 -0
  28. data/lib/eye/checker/file_ctime.rb +25 -0
  29. data/lib/eye/checker/file_size.rb +34 -0
  30. data/lib/eye/checker/http.rb +98 -0
  31. data/lib/eye/checker/memory.rb +27 -0
  32. data/lib/eye/checker/socket.rb +152 -0
  33. data/lib/eye/child_process.rb +101 -0
  34. data/lib/eye/client.rb +32 -0
  35. data/lib/eye/config.rb +88 -0
  36. data/lib/eye/control.rb +2 -0
  37. data/lib/eye/controller.rb +53 -0
  38. data/lib/eye/controller/commands.rb +73 -0
  39. data/lib/eye/controller/helpers.rb +61 -0
  40. data/lib/eye/controller/load.rb +214 -0
  41. data/lib/eye/controller/options.rb +48 -0
  42. data/lib/eye/controller/send_command.rb +115 -0
  43. data/lib/eye/controller/show_history.rb +62 -0
  44. data/lib/eye/controller/status.rb +131 -0
  45. data/lib/eye/dsl.rb +48 -0
  46. data/lib/eye/dsl/application_opts.rb +33 -0
  47. data/lib/eye/dsl/chain.rb +12 -0
  48. data/lib/eye/dsl/child_process_opts.rb +8 -0
  49. data/lib/eye/dsl/config_opts.rb +48 -0
  50. data/lib/eye/dsl/group_opts.rb +27 -0
  51. data/lib/eye/dsl/helpers.rb +12 -0
  52. data/lib/eye/dsl/main.rb +40 -0
  53. data/lib/eye/dsl/opts.rb +140 -0
  54. data/lib/eye/dsl/process_opts.rb +21 -0
  55. data/lib/eye/dsl/pure_opts.rb +110 -0
  56. data/lib/eye/dsl/validation.rb +59 -0
  57. data/lib/eye/group.rb +134 -0
  58. data/lib/eye/group/chain.rb +81 -0
  59. data/lib/eye/http.rb +31 -0
  60. data/lib/eye/http/router.rb +25 -0
  61. data/lib/eye/loader.rb +23 -0
  62. data/lib/eye/logger.rb +80 -0
  63. data/lib/eye/notify.rb +86 -0
  64. data/lib/eye/notify/jabber.rb +30 -0
  65. data/lib/eye/notify/mail.rb +44 -0
  66. data/lib/eye/process.rb +86 -0
  67. data/lib/eye/process/child.rb +58 -0
  68. data/lib/eye/process/commands.rb +256 -0
  69. data/lib/eye/process/config.rb +70 -0
  70. data/lib/eye/process/controller.rb +76 -0
  71. data/lib/eye/process/data.rb +47 -0
  72. data/lib/eye/process/monitor.rb +95 -0
  73. data/lib/eye/process/notify.rb +32 -0
  74. data/lib/eye/process/scheduler.rb +78 -0
  75. data/lib/eye/process/states.rb +86 -0
  76. data/lib/eye/process/states_history.rb +66 -0
  77. data/lib/eye/process/system.rb +97 -0
  78. data/lib/eye/process/trigger.rb +54 -0
  79. data/lib/eye/process/validate.rb +23 -0
  80. data/lib/eye/process/watchers.rb +69 -0
  81. data/lib/eye/reason.rb +20 -0
  82. data/lib/eye/server.rb +52 -0
  83. data/lib/eye/settings.rb +46 -0
  84. data/lib/eye/system.rb +154 -0
  85. data/lib/eye/system_resources.rb +86 -0
  86. data/lib/eye/trigger.rb +53 -0
  87. data/lib/eye/trigger/flapping.rb +28 -0
  88. data/lib/eye/utils.rb +14 -0
  89. data/lib/eye/utils/alive_array.rb +31 -0
  90. data/lib/eye/utils/celluloid_chain.rb +70 -0
  91. data/lib/eye/utils/leak_19.rb +7 -0
  92. data/lib/eye/utils/tail.rb +20 -0
  93. metadata +390 -0
@@ -0,0 +1,29 @@
1
+ RUBY = 'ruby'
2
+ BUNDLE = 'bundle'
3
+
4
+ Eye.load("process_thin.rb")
5
+
6
+ Eye.config do
7
+ logger "/tmp/eye.log"
8
+ end
9
+
10
+ Eye.app 'thin-farm' do
11
+ working_dir File.expand_path(File.join(File.dirname(__FILE__), %w[ processes ]))
12
+ env "RAILS_ENV" => "production"
13
+
14
+ stop_on_delete true # this option means, when we change pids and load config,
15
+ # deleted processes will be stops
16
+
17
+ triggers :flapping, :times => 10, :within => 1.minute
18
+ checks :memory, :below => 60.megabytes, :every => 30.seconds, :times => 5
19
+
20
+ group :web do
21
+ chain :action => :restart, :grace => 5.seconds
22
+ chain :action => :start, :grace => 0.2.seconds
23
+
24
+ (5555..5560).each do |port|
25
+ thin self, port
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,31 @@
1
+ # Example: now to run unicorn, and monitor its childs processes
2
+
3
+ RUBY = '/usr/local/ruby/1.9.3/bin/ruby' # ruby on the server
4
+ RAILS_ENV = 'production'
5
+
6
+ Eye.application "rails_unicorn" do
7
+ env "RAILS_ENV" => RAILS_ENV, "PATH" => "#{File.dirname(RUBY)}:#{ENV['PATH']}"
8
+ working_dir "/projects/rails_unicorn"
9
+
10
+ process("unicorn") do
11
+ pid_file "tmp/pids/unicorn.pid"
12
+ start_command "#{RUBY} ./bin/unicorn -Dc ./config/unicorn.rb -E #{RAILS_ENV}"
13
+ stop_command "kill -QUIT {{PID}}"
14
+ restart_command "kill -USR2 {{PID}}"
15
+ stdall "log/unicorn.log"
16
+
17
+ checks :cpu, :every => 30, :below => 80, :times => 3
18
+ checks :memory, :every => 30, :below => 150.megabytes, :times => [3,5]
19
+
20
+ start_timeout 30.seconds
21
+ stop_grace 5.seconds
22
+ restart_grace 30.seconds
23
+
24
+ monitor_children do
25
+ stop_command "kill -QUIT {{PID}}"
26
+ checks :cpu, :every => 30, :below => 80, :times => 3
27
+ checks :memory, :every => 30, :below => 150.megabytes, :times => [3,5]
28
+ end
29
+ end
30
+
31
+ end
data/eye.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../lib/eye', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = "Konstantin Makarchev"
5
+ gem.email = "kostya27@gmail.com"
6
+
7
+ gem.description = gem.summary = \
8
+ %q{Process monitoring tool. An alternative to God and Bluepill. With Bluepill like config syntax. Requires MRI Ruby >= 1.9.2. Uses Celluloid and Celluloid::IO.}
9
+ gem.homepage = "http://github.com/kostya/eye"
10
+
11
+ gem.files = `git ls-files`.split($\).reject{|n| n =~ %r[png|gif\z]}.reject{|n| n =~ %r[^(test|spec|features)/]}
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ #gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "reel-eye"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Eye::VERSION
17
+ gem.license = "MIT"
18
+
19
+ gem.required_ruby_version = '>= 1.9.2' # because of celluloid
20
+ gem.required_rubygems_version = '>= 1.3.6'
21
+
22
+ gem.add_dependency 'celluloid', '~> 0.13.0'
23
+ gem.add_dependency 'celluloid-io', '~> 0.13.0'
24
+ gem.add_dependency 'state_machine', '< 1.2'
25
+ gem.add_dependency 'activesupport', '~> 3.2.0'
26
+ gem.add_dependency 'thor'
27
+
28
+ gem.add_dependency 'reel', '~> 0.4.0.pre'
29
+ gem.add_dependency 'cuba'
30
+
31
+ gem.add_development_dependency 'rake'
32
+ gem.add_development_dependency 'rspec'
33
+ gem.add_development_dependency 'simplecov'
34
+ gem.add_development_dependency 'rr'
35
+ gem.add_development_dependency 'ruby-graphviz'
36
+ gem.add_development_dependency 'forking'
37
+ gem.add_development_dependency 'fakeweb'
38
+ gem.add_development_dependency 'eventmachine', ">= 1.0.3"
39
+ gem.add_development_dependency 'sinatra'
40
+ gem.add_development_dependency 'thin'
41
+ gem.add_development_dependency 'xmpp4r'
42
+ end
data/lib/eye.rb ADDED
@@ -0,0 +1,28 @@
1
+ module Eye
2
+ VERSION = "0.3.1"
3
+ ABOUT = "ReelEye v#{VERSION} (c) 2012-2013 @kostya"
4
+ PROCLINE = "reel-eye monitoring v#{VERSION}"
5
+
6
+ autoload :Process, 'eye/process'
7
+ autoload :ChildProcess, 'eye/child_process'
8
+ autoload :Server, 'eye/server'
9
+ autoload :Logger, 'eye/logger'
10
+ autoload :System, 'eye/system'
11
+ autoload :SystemResources,'eye/system_resources'
12
+ autoload :Checker, 'eye/checker'
13
+ autoload :Trigger, 'eye/trigger'
14
+ autoload :Group, 'eye/group'
15
+ autoload :Dsl, 'eye/dsl'
16
+ autoload :Application, 'eye/application'
17
+ autoload :Settings, 'eye/settings'
18
+ autoload :Client, 'eye/client'
19
+ autoload :Utils, 'eye/utils'
20
+ autoload :Notify, 'eye/notify'
21
+ autoload :Config, 'eye/config'
22
+ autoload :Reason, 'eye/reason'
23
+
24
+ autoload :Controller, 'eye/controller'
25
+ autoload :Control, 'eye/control'
26
+
27
+ autoload :Http, 'eye/http'
28
+ end
@@ -0,0 +1,74 @@
1
+ class Eye::Application
2
+
3
+ attr_reader :groups, :name
4
+
5
+ include Eye::Logger::Helpers
6
+
7
+ def initialize(name, config = {})
8
+ @groups = Eye::Utils::AliveArray.new
9
+ @name = name
10
+ @logger = Eye::Logger.new(full_name)
11
+ @config = config
12
+ debug 'created'
13
+ end
14
+
15
+ def full_name
16
+ @name
17
+ end
18
+
19
+ def update_config(cfg)
20
+ @config = cfg
21
+ end
22
+
23
+ def add_group(group)
24
+ @groups << group
25
+ end
26
+
27
+ # sort processes in name order
28
+ def resort_groups
29
+ @groups = @groups.sort_by{|gr| gr.name == '__default__' ? 'zzzzz' : gr.name }
30
+ end
31
+
32
+ def status_data(debug = false)
33
+ h = { name: @name, type: :application, subtree: @groups.map{|gr| gr.status_data(debug) }}
34
+ h.merge!(debug: debug_data) if debug
35
+ h
36
+ end
37
+
38
+ def status_data_short
39
+ h = Hash.new 0
40
+ @groups.each do |c|
41
+ c.processes.each do |p|
42
+ h[p.state] += 1
43
+ end
44
+ end
45
+ str = h.sort_by{|a,b| a}.map{|k, v| "#{k}:#{v}" } * ', '
46
+ { name: @name, type: :application, state: str}
47
+ end
48
+
49
+ def debug_data
50
+ end
51
+
52
+ def send_command(command, *args)
53
+ debug "send_command #{command} #{args}"
54
+
55
+ @groups.each do |group|
56
+ group.send_command(command, *args)
57
+ end
58
+ end
59
+
60
+ def alive?
61
+ true # emulate celluloid actor method
62
+ end
63
+
64
+ def sub_object?(obj)
65
+ res = @groups.include?(obj)
66
+ res = @groups.any?{|gr| gr.sub_object?(obj)} if !res
67
+ res
68
+ end
69
+
70
+ def processes
71
+ Eye::Utils::AliveArray.new(@groups.map{|gr| gr.processes.to_a }.flatten)
72
+ end
73
+
74
+ end
@@ -0,0 +1,138 @@
1
+ class Eye::Checker
2
+ include Eye::Logger::Helpers
3
+
4
+ autoload :Memory, 'eye/checker/memory'
5
+ autoload :Cpu, 'eye/checker/cpu'
6
+ autoload :Http, 'eye/checker/http'
7
+ autoload :FileCTime, 'eye/checker/file_ctime'
8
+ autoload :FileSize, 'eye/checker/file_size'
9
+ autoload :Socket, 'eye/checker/socket'
10
+
11
+ TYPES = {:memory => "Memory", :cpu => "Cpu", :http => "Http",
12
+ :ctime => "FileCTime", :fsize => "FileSize", :socket => "Socket"}
13
+
14
+ attr_accessor :value, :values, :options, :pid, :type, :check_count
15
+
16
+ def self.get_class(type)
17
+ klass = eval("Eye::Checker::#{TYPES[type]}") rescue nil
18
+ raise "Unknown checker #{type}" unless klass
19
+ klass
20
+ end
21
+
22
+ def self.create(pid, options = {}, logger_prefix = nil)
23
+ get_class(options[:type]).new(pid, options, logger_prefix)
24
+ end
25
+
26
+ def self.validate!(options)
27
+ get_class(options[:type]).validate(options)
28
+ end
29
+
30
+ def initialize(pid, options = {}, logger_prefix = nil)
31
+ @pid = pid
32
+ @options = options
33
+ @type = options[:type]
34
+
35
+ @logger = Eye::Logger.new(logger_prefix, "check:#{check_name}")
36
+ debug "create checker, with #{options}"
37
+
38
+ @value = nil
39
+ @values = Eye::Utils::Tail.new(max_tries)
40
+ @check_count = 0
41
+ end
42
+
43
+ def last_human_values
44
+ h_values = @values.map do |v|
45
+ sign = v[:good] ? '' : '*'
46
+ sign + human_value(v[:value]).to_s
47
+ end
48
+
49
+ '[' + h_values * ', ' + ']'
50
+ end
51
+
52
+ def check
53
+ @value = get_value_safe
54
+ @values << {:value => @value, :good => good?(value)}
55
+
56
+ result = true
57
+ @check_count += 1
58
+
59
+ if @values.size == max_tries
60
+ bad_count = @values.count{|v| !v[:good] }
61
+ result = false if bad_count >= min_tries
62
+ end
63
+
64
+ info "#{last_human_values} => #{result ? 'OK' : 'Fail'}"
65
+ result
66
+ end
67
+
68
+ def get_value_safe
69
+ get_value
70
+ end
71
+
72
+ def get_value
73
+ raise 'Realize me'
74
+ end
75
+
76
+ def human_value(value)
77
+ value.to_s
78
+ end
79
+
80
+ # true if check ok
81
+ # false if check bad
82
+ def good?(value)
83
+ raise 'Realize me'
84
+ end
85
+
86
+ def check_name
87
+ @check_name ||= @type.to_s
88
+ end
89
+
90
+ def max_tries
91
+ @max_tries ||= if times
92
+ if times.is_a?(Array)
93
+ times[-1].to_i
94
+ else
95
+ times.to_i
96
+ end
97
+ else
98
+ 1
99
+ end
100
+ end
101
+
102
+ def min_tries
103
+ @min_tries ||= if times
104
+ if times.is_a?(Array)
105
+ times[0].to_i
106
+ else
107
+ max_tries
108
+ end
109
+ else
110
+ max_tries
111
+ end
112
+ end
113
+
114
+ def previous_value
115
+ @values[-1][:value] if @values.present?
116
+ end
117
+
118
+ extend Eye::Dsl::Validation
119
+ param :every, [Fixnum, Float], false, 5
120
+ param :times, [Fixnum, Array]
121
+ param :fire, Symbol, nil, nil, [:stop, :restart, :unmonitor, :nothing]
122
+
123
+ class Defer < Eye::Checker
124
+ def get_value_safe
125
+ Celluloid::Future.new{ get_value }.value
126
+ end
127
+ end
128
+
129
+ class Custom < Defer
130
+ def self.inherited(base)
131
+ super
132
+ name = base.to_s
133
+ type = name.underscore.to_sym
134
+ Eye::Checker::TYPES[type] = name
135
+ Eye::Checker.const_set(name, base)
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,27 @@
1
+ class Eye::Checker::Cpu < Eye::Checker
2
+
3
+ # checks :cpu, :every => 3.seconds, :below => 80, :times => [3,5]
4
+
5
+ param :below, [Fixnum, Float], true
6
+
7
+ def check_name
8
+ @check_name ||= "cpu(#{human_value(below)})"
9
+ end
10
+
11
+ def get_value
12
+ Eye::SystemResources.cpu(@pid).to_i # nil => 0
13
+ end
14
+
15
+ def human_value(value)
16
+ "#{value}%"
17
+ end
18
+
19
+ def good?(value)
20
+ if below
21
+ value < below
22
+ else
23
+ true
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,25 @@
1
+ class Eye::Checker::FileCTime < Eye::Checker
2
+
3
+ # Check that file changes (log for example)
4
+
5
+ # checks :ctime, :every => 5.seconds, :file => "/tmp/1.log", :times => [3,5]
6
+
7
+ param :file, [String], true
8
+
9
+ def get_value
10
+ File.ctime(file) rescue nil
11
+ end
12
+
13
+ def human_value(value)
14
+ if value == nil
15
+ 'Err'
16
+ else
17
+ value.strftime('%H:%M')
18
+ end
19
+ end
20
+
21
+ def good?(value)
22
+ value.to_i > previous_value.to_i
23
+ end
24
+
25
+ end
@@ -0,0 +1,34 @@
1
+ class Eye::Checker::FileSize < Eye::Checker
2
+
3
+ # Check that file size changed (log for example)
4
+
5
+ # checks :fsize, :every => 5.seconds, :file => "/tmp/1.log", :times => [3,5],
6
+ # :below => 30.kilobytes, :above => 10.kilobytes
7
+
8
+ param :file, [String], true
9
+ param :below, [Fixnum, Float]
10
+ param :above, [Fixnum, Float]
11
+
12
+ def get_value
13
+ File.size(file) rescue nil
14
+ end
15
+
16
+ def human_value(value)
17
+ "#{value.to_i / 1024}Kb"
18
+ end
19
+
20
+ def good?(value)
21
+ return true unless previous_value
22
+
23
+ diff = value.to_i - previous_value.to_i
24
+
25
+ return true if diff < 0 # case when logger nulled
26
+
27
+ return false if below && diff > below
28
+ return false if above && diff < above
29
+ return false if diff == 0
30
+
31
+ true
32
+ end
33
+
34
+ end
@@ -0,0 +1,98 @@
1
+ require 'net/http'
2
+
3
+ class Eye::Checker::Http < Eye::Checker::Defer
4
+
5
+ # checks :http, :every => 5.seconds, :times => 1,
6
+ # :url => "http://127.0.0.1:3000/", :kind => :success, :pattern => /OK/, :timeout => 3.seconds
7
+
8
+ param :url, String, true
9
+ param :pattern, [String, Regexp]
10
+ param :kind
11
+ param :timeout, [Fixnum, Float]
12
+ param :open_timeout, [Fixnum, Float]
13
+ param :read_timeout, [Fixnum, Float]
14
+
15
+ attr_reader :uri
16
+
17
+ def initialize(*args)
18
+ super
19
+
20
+ @uri = URI.parse(url) rescue URI.parse('http://127.0.0.1')
21
+ @pattern = pattern
22
+ @kind = case kind
23
+ when Fixnum then Net::HTTPResponse::CODE_TO_OBJ[kind]
24
+ when String, Symbol then Net.const_get("HTTP#{kind.to_s.camelize}") rescue Net::HTTPSuccess
25
+ else
26
+ Net::HTTPSuccess
27
+ end
28
+ @open_timeout = (open_timeout || 3).to_f
29
+ @read_timeout = (read_timeout || timeout || 15).to_f
30
+ end
31
+
32
+ def get_value
33
+ res = session.start{ |http| http.get(@uri.request_uri) }
34
+ {:result => res}
35
+
36
+ rescue Timeout::Error => ex
37
+ debug ex.inspect
38
+
39
+ if defined?(Net::OpenTimeout) # for ruby 2.0
40
+ mes = ex.class.is_a?(Net::OpenTimeout) ? "OpenTimeout<#{@open_timeout}>" : "ReadTimeout<#{@read_timeout}>"
41
+ {:exception => mes}
42
+ else
43
+ error "#{ex.message}"
44
+ {:exception => "Timeout<#{@open_timeout},#{@read_timeout}>"}
45
+ end
46
+
47
+ rescue => ex
48
+ {:exception => "Error<#{ex.message}>"}
49
+ end
50
+
51
+ def good?(value)
52
+ return false unless value[:result]
53
+
54
+ unless value[:result].kind_of?(@kind)
55
+ return false
56
+ end
57
+
58
+ if @pattern
59
+ matched = if @pattern.is_a?(Regexp)
60
+ @pattern === value[:result].body
61
+ else
62
+ value[:result].body.include?(@pattern.to_s)
63
+ end
64
+ value[:notice] = "missing '#{@pattern.to_s}'" unless matched
65
+ matched
66
+ else
67
+ true
68
+ end
69
+ end
70
+
71
+ def human_value(value)
72
+ if !value.is_a?(Hash)
73
+ '-'
74
+ elsif value[:exception]
75
+ value[:exception]
76
+ else
77
+ body_size = value[:result].body.size / 1024
78
+ msg = "#{value[:result].code}=#{body_size}Kb"
79
+ msg += "<#{value[:notice]}>" if value[:notice]
80
+ msg
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def session
87
+ Net::HTTP.new(@uri.host, @uri.port).tap do |session|
88
+ if @uri.scheme == 'https'
89
+ require 'net/https'
90
+ session.use_ssl = true
91
+ session.verify_mode = OpenSSL::SSL::VERIFY_NONE
92
+ end
93
+ session.open_timeout = @open_timeout
94
+ session.read_timeout = @read_timeout
95
+ end
96
+ end
97
+
98
+ end