gvarela-bluepill 0.0.27

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ .idea/
data/DESIGN.md ADDED
@@ -0,0 +1,10 @@
1
+ ## Bluepill Design
2
+ Here are just some bullet points of the design. We'll add details later.
3
+
4
+ * Each process monitors a single _application_, so you can have multiple bluepill processes on a system
5
+ * Use rotational arrays for storing historical data for monitoring process conditions
6
+ * Memo-ize output of _ps_ per tick as an optimization for applications with many processes
7
+ * Use socket files to communicate between CLI and daemon
8
+ * DSL is a separate layer, the core of the monitoring just uses regular initializers, etc. DSL is simply for ease of use and should not interfere with business logic
9
+ * Sequentially process user issued commands so no weird race cases occur
10
+ * Triggers are notified by the state machine on any process state transitions
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Arya Asemanfar, Rohith Ravi, Gary Tsang
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,221 @@
1
+ # Bluepill
2
+ Bluepill is a simple process monitoring tool written in Ruby.
3
+
4
+ ## Installation
5
+ It's hosted on [gemcutter.org][gemcutter].
6
+
7
+ sudo gem install bluepill
8
+
9
+ In order to take advantage of logging with syslog, you also need to setup your syslog to log the local6 facility. Edit the appropriate config file for your syslogger (/etc/syslog.conf for syslog) and add a line for local6:
10
+
11
+ local6.* /var/log/bluepill.log
12
+
13
+ You'll also want to add _/var/log/bluepill.log_ to _/etc/logrotate.d/syslog_ so that it gets rotated.
14
+
15
+ Lastly, create the _/var/bluepill_ directory for bluepill to store its pid and sock files.
16
+
17
+ ## Usage
18
+ ### Config
19
+ Bluepill organizes processes into 3 levels: application -> group -> process. Each process has a few attributes that tell bluepill how to start, stop, and restart it, where to look or put the pid file, what process conditions to monitor and the options for each of those.
20
+
21
+ The minimum config file looks something like this:
22
+
23
+ Bluepill.application("app_name") do |app|
24
+ app.process("process_name") do |process|
25
+ process.start_command = "/usr/bin/some_start_command"
26
+ process.pid_file = "/tmp/some_pid_file.pid"
27
+ end
28
+ end
29
+
30
+ Note that since we specified a PID file and start command, bluepill assumes the process will daemonize itself. If we wanted bluepill to daemonize it for us, we can do (note we still need to specify a PID file):
31
+
32
+ Bluepill.application("app_name") do |app|
33
+ app.process("process_name") do |process|
34
+ process.start_command = "/usr/bin/some_start_command"
35
+ process.pid_file = "/tmp/some_pid_file.pid"
36
+ process.daemonize = true
37
+ end
38
+ end
39
+
40
+ If you don't specify a stop command, a TERM signal will be sent by default. Similarly, the default restart action is to issue stop and then start.
41
+
42
+ Now if we want to do something more meaningful, like actually monitor the process, we do:
43
+
44
+ Bluepill.application("app_name") do |app|
45
+ app.process("process_name") do |process|
46
+ process.start_command = "/usr/bin/some_start_command"
47
+ process.pid_file = "/tmp/some_pid_file.pid"
48
+
49
+ process.checks :cpu_usage, :every => 10.seconds, :below => 5, :times => 3
50
+ end
51
+ end
52
+
53
+ We added a line that checks every 10 seconds to make sure the cpu usage of this process is below 5 percent; 3 failed checks results in a restart. We can specify a two-element array for the _times_ option to say that it 3 out of 5 failed attempts results in a restart.
54
+
55
+ To watch memory usage, we just add one more line:
56
+
57
+ Bluepill.application("app_name") do |app|
58
+ app.process("process_name") do |process|
59
+ process.start_command = "/usr/bin/some_start_command"
60
+ process.pid_file = "/tmp/some_pid_file.pid"
61
+
62
+ process.checks :cpu_usage, :every => 10.seconds, :below => 5, :times => 3
63
+ process.checks :mem_usage, :every => 10.seconds, :below => 100.megabytes, :times => [3,5]
64
+ end
65
+ end
66
+
67
+ We can tell bluepill to give a process some grace time to start/stop/restart before resuming monitoring:
68
+
69
+ Bluepill.application("app_name") do |app|
70
+ app.process("process_name") do |process|
71
+ process.start_command = "/usr/bin/some_start_command"
72
+ process.pid_file = "/tmp/some_pid_file.pid"
73
+ process.start_grace_time = 3.seconds
74
+ process.stop_grace_time = 5.seconds
75
+ process.restart_grace_time = 8.seconds
76
+
77
+ process.checks :cpu_usage, :every => 10.seconds, :below => 5, :times => 3
78
+ process.checks :mem_usage, :every => 10.seconds, :below => 100.megabytes, :times => [3,5]
79
+ end
80
+ end
81
+
82
+ We can group processes by name:
83
+
84
+ Bluepill.application("app_name") do |app|
85
+ 5.times do |i|
86
+ app.process("process_name_#{i}") do |process|
87
+ process.group = "mongrels"
88
+ process.start_command = "/usr/bin/some_start_command"
89
+ process.pid_file = "/tmp/some_pid_file.pid"
90
+ end
91
+ end
92
+ end
93
+
94
+ If you want to run the process as someone other than root:
95
+
96
+ Bluepill.application("app_name") do |app|
97
+ app.process("process_name") do |process|
98
+ process.start_command = "/usr/bin/some_start_command"
99
+ process.pid_file = "/tmp/some_pid_file.pid"
100
+ process.uid = "deploy"
101
+ process.gid = "deploy"
102
+
103
+ process.checks :cpu_usage, :every => 10.seconds, :below => 5, :times => 3
104
+ process.checks :mem_usage, :every => 10.seconds, :below => 100.megabytes, :times => [3,5]
105
+ end
106
+ end
107
+
108
+ You can also set an app-wide uid/gid:
109
+
110
+ Bluepill.application("app_name") do |app|
111
+ app.uid = "deploy"
112
+ app.gid = "deploy"
113
+ app.process("process_name") do |process|
114
+ process.start_command = "/usr/bin/some_start_command"
115
+ process.pid_file = "/tmp/some_pid_file.pid"
116
+ end
117
+ end
118
+
119
+ To check for flapping:
120
+
121
+ process.checks :flapping, :times => 2, :within => 30.seconds, :retry_in => 7.seconds
122
+
123
+ To set the working directory to _cd_ into when starting the command:
124
+
125
+ Bluepill.application("app_name") do |app|
126
+ app.process("process_name") do |process|
127
+ process.start_command = "/usr/bin/some_start_command"
128
+ process.pid_file = "/tmp/some_pid_file.pid"
129
+ process.working_dir = "/path/to/some_directory"
130
+ end
131
+ end
132
+
133
+ You can also have an app-wide working directory:
134
+
135
+
136
+ Bluepill.application("app_name") do |app|
137
+ app.working_dir = "/path/to/some_directory"
138
+ app.process("process_name") do |process|
139
+ process.start_command = "/usr/bin/some_start_command"
140
+ process.pid_file = "/tmp/some_pid_file.pid"
141
+ end
142
+ end
143
+
144
+ Note: We also set the PWD in the environment to the working dir you specify. This is useful for when the working dir is a symlink. Unicorn in particular will cd into the environment variable in PWD when it re-execs to deal with a change in the symlink.
145
+
146
+
147
+ And lastly, to monitor child processes:
148
+
149
+ process.monitor_children do |child_process|
150
+ child_process.checks :cpu_usage, :every => 10, :below => 5, :times => 3
151
+ child_process.checks :mem_usage, :every => 10, :below => 100.megabytes, :times => [3, 5]
152
+
153
+ child_process.stop_command = "kill -QUIT {{PID}}"
154
+ end
155
+
156
+ Note {{PID}} will be substituted for the pid of process in both the stop and restart commands.
157
+
158
+ ### A Note About Output Redirection
159
+
160
+ While you can specify shell tricks like the following in the start_command of a process:
161
+
162
+ Bluepill.application("app_name") do |app|
163
+ app.process("process_name") do |process|
164
+ process.start_command = "cd /tmp/some_dir && SOME_VAR=1 /usr/bin/some_start_command > /tmp/server.log 2>&1"
165
+ process.pid_file = "/tmp/some_pid_file.pid"
166
+ end
167
+ end
168
+
169
+ We recommend that you _not_ do that and instead use the config options to capture output from your daemons. Like so:
170
+
171
+ Bluepill.application("app_name") do |app|
172
+ app.process("process_name") do |process|
173
+ process.start_command = "/usr/bin/env SOME_VAR=1 /usr/bin/some_start_command"
174
+
175
+ process.working_dir = "/tmp/some_dir"
176
+ process.stdout = process.stderr = "/tmp/server.log"
177
+
178
+ process.pid_file = "/tmp/some_pid_file.pid"
179
+ end
180
+ end
181
+
182
+ The main benefit of using the config options is that Bluepill will be able to monitor the correct process instead of just watching the shell that spawned your actual server.
183
+
184
+ ### CLI
185
+ To start a bluepill process and load a config:
186
+
187
+ sudo bluepill load /path/to/production.pill
188
+
189
+ To act on a process or group:
190
+
191
+ sudo bluepill <start|stop|restart|unmonitor> <process_or_group_name>
192
+
193
+ To view process statuses:
194
+
195
+ sudo bluepill status
196
+
197
+ To view the log for a process or group:
198
+
199
+ sudo bluepill log <process_or_group_name>
200
+
201
+ To quit bluepill:
202
+
203
+ sudo bluepill quit
204
+
205
+ ### Logging
206
+ By default, bluepill uses syslog local6 facility as described in the installation section. But if for any reason you don't want to use syslog, you can use a log file. You can do this by setting the :log\_file option in the config:
207
+
208
+ Bluepill.application("app_name", :log_file => "/path/to/bluepill.log") do |app|
209
+ # ...
210
+ end
211
+
212
+ Keep in mind that you still need to set up log rotation (described in the installation section) to keep the log file from growing huge.
213
+
214
+ ## Links
215
+ Code: [http://github.com/arya/bluepill](http://github.com/arya/bluepill)
216
+ Bugs/Features: [http://github.com/arya/bluepill/issues](http://github.com/arya/bluepill/issues)
217
+ Mailing List: [http://groups.google.com/group/bluepill-rb](http://groups.google.com/group/bluepill-rb)
218
+
219
+
220
+ [gemcutter]: http://gemcutter.org
221
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "gvarela-bluepill"
8
+ gem.summary = %Q{A process monitor written in Ruby with stability and minimalism in mind.}
9
+ gem.description = %Q{Bluepill keeps your daemons up while taking up as little resources as possible. After all you probably want the resources of your server to be used by whatever daemons you are running rather than the thing that's supposed to make sure they are brought back up, should they die or misbehave.}
10
+ gem.email = "entombedvirus@gmail.com"
11
+ gem.homepage = "http://github.com/gvarela/bluepill"
12
+ gem.authors = ["Arya Asemanfar", "Gary Tsang", "Rohith Ravi"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ gem.add_dependency("daemons", ">= 1.0.9")
15
+ gem.add_dependency("blankslate", ">= 2.1.2.2")
16
+ gem.add_dependency("state_machine", ">= 0.8.0")
17
+ gem.add_dependency("activesupport", ">= 2.3.5")
18
+
19
+ gem.files -= ["bin/sample_forking_server"]
20
+ gem.executables = ["bluepill"]
21
+ end
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
24
+ end
25
+
26
+
27
+ require 'rake/rdoctask'
28
+ Rake::RDocTask.new do |rdoc|
29
+ if File.exist?('VERSION')
30
+ version = File.read('VERSION')
31
+ else
32
+ version = ""
33
+ end
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "blue-pill #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
40
+
41
+
42
+ namespace :version do
43
+ task :update_file do
44
+ version = File.read("VERSION").strip
45
+ File.open("lib/bluepill/version.rb", "w") do |file|
46
+ file.write <<-END
47
+ module Bluepill
48
+ VERSION = "#{version}"
49
+ end
50
+ END
51
+ end
52
+ end
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.27
data/bin/bluepill ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'optparse'
4
+ require 'bluepill'
5
+
6
+ # Check for root
7
+ unless ::Process.euid == 0
8
+ $stderr.puts "You must run bluepill as root."
9
+ exit(3)
10
+ end
11
+
12
+
13
+ # Default options
14
+ options = {
15
+ :log_file => "/var/log/bluepill.log",
16
+ :base_dir => "/var/bluepill"
17
+ }
18
+
19
+ OptionParser.new do |opts|
20
+ opts.banner = "Usage: bluepill [app] cmd [options]"
21
+ opts.on('-l', "--logfile LOGFILE", "Path to logfile, defaults to #{options[:log_file]}") do |file|
22
+ options[:log_file] = file
23
+ end
24
+
25
+ opts.on('-c', "--base-dir DIR", "Directory to store bluepill socket and pid files, defaults to #{options[:base_dir]}") do |base_dir|
26
+ options[:base_dir] = base_dir
27
+ end
28
+
29
+ opts.on("-v", "--version") do
30
+ puts "bluepill, version #{Bluepill::VERSION}"
31
+ exit
32
+ end
33
+
34
+ help = lambda do
35
+ puts opts
36
+ puts
37
+ puts "Commands:"
38
+ puts " load CONFIG_FILE\t\tLoads new instance of bluepill using the specified config file"
39
+ puts " status\t\t\tLists the status of the proceses for the specified app"
40
+ puts " start TARGET\t\tIssues the start command for the target process or group"
41
+ puts " stop TARGET\t\t\tIssues the stop command for the target process or group"
42
+ puts " restart TARGET\t\tIssues the restart command for the target process or group"
43
+ puts " unmonitor TARGET\t\tStop monitoring target process or group"
44
+ puts " log [TARGET]\t\tShow the log for the specified process or group, defaults to all for app"
45
+ puts " quit\t\t\tStop bluepill"
46
+ puts
47
+ puts "See http://github.com/arya/bluepill for README"
48
+ exit
49
+ end
50
+
51
+ opts.on_tail('-h','--help', 'Show this message', &help)
52
+ help.call if ARGV.empty?
53
+ end.parse!
54
+
55
+ APPLICATION_COMMANDS = %w(status start stop restart unmonitor quit log)
56
+
57
+ controller = Bluepill::Controller.new(options.slice(:base_dir, :log_file))
58
+
59
+ if controller.running_applications.include?(ARGV.first)
60
+ # the first arg is the application name
61
+ options[:application] = ARGV.shift
62
+ elsif APPLICATION_COMMANDS.include?(ARGV.first)
63
+ if controller.running_applications.length == 1
64
+ # there is only one, let's just use that
65
+ options[:application] = controller.running_applications.first
66
+ elsif controller.running_applications.length > 1
67
+ # There is more than one, tell them the list and exit
68
+ $stderr.puts "You must specify an application name to run that command. Here's the list of running applications:"
69
+ controller.running_applications.each_with_index do |app, index|
70
+ $stderr.puts " #{index + 1}. #{app}"
71
+ end
72
+ $stderr.puts "Usage: bluepill [app] cmd [options]"
73
+ exit(1)
74
+ else
75
+ # There are none running AND they aren't trying to start one
76
+ $stderr.puts "Error: There are no running bluepill daemons.\nTo start a bluepill daemon, use: bluepill load <config file>"
77
+ exit(2)
78
+ end
79
+ end
80
+
81
+ options[:command] = ARGV.shift
82
+
83
+ if options[:command] == "load"
84
+ file = ARGV.shift
85
+ if File.exists?(file)
86
+ # Restart the ruby interpreter for the config file so that anything loaded here
87
+ # does not stay in memory for the daemon
88
+ require 'rbconfig'
89
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
90
+ load_path = File.expand_path("#{File.dirname(__FILE__)}/../lib")
91
+ file_path = File.expand_path(file)
92
+
93
+ exec(ruby, "-I#{load_path}", '-rbluepill', file_path)
94
+
95
+ else
96
+ $stderr.puts "Can't find file: #{file}"
97
+ end
98
+ else
99
+ target = ARGV.shift
100
+ controller.handle_command(options[:application], options[:command], target)
101
+ end
data/bluepill.gemspec ADDED
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bluepill}
8
+ s.version = "0.0.27"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Arya Asemanfar", "Gary Tsang", "Rohith Ravi"]
12
+ s.date = %q{2009-12-04}
13
+ s.default_executable = %q{bluepill}
14
+ s.description = %q{Bluepill keeps your daemons up while taking up as little resources as possible. After all you probably want the resources of your server to be used by whatever daemons you are running rather than the thing that's supposed to make sure they are brought back up, should they die or misbehave.}
15
+ s.email = %q{entombedvirus@gmail.com}
16
+ s.executables = ["bluepill"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "DESIGN.md",
24
+ "LICENSE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/bluepill",
29
+ "bluepill.gemspec",
30
+ "lib/bluepill.rb",
31
+ "lib/bluepill/application.rb",
32
+ "lib/bluepill/application/client.rb",
33
+ "lib/bluepill/application/server.rb",
34
+ "lib/bluepill/condition_watch.rb",
35
+ "lib/bluepill/controller.rb",
36
+ "lib/bluepill/dsl.rb",
37
+ "lib/bluepill/group.rb",
38
+ "lib/bluepill/logger.rb",
39
+ "lib/bluepill/process.rb",
40
+ "lib/bluepill/process_conditions.rb",
41
+ "lib/bluepill/process_conditions/always_true.rb",
42
+ "lib/bluepill/process_conditions/cpu_usage.rb",
43
+ "lib/bluepill/process_conditions/mem_usage.rb",
44
+ "lib/bluepill/process_conditions/process_condition.rb",
45
+ "lib/bluepill/socket.rb",
46
+ "lib/bluepill/system.rb",
47
+ "lib/bluepill/trigger.rb",
48
+ "lib/bluepill/triggers/flapping.rb",
49
+ "lib/bluepill/util/rotational_array.rb",
50
+ "lib/bluepill/version.rb",
51
+ "lib/example.rb"
52
+ ]
53
+ s.homepage = %q{http://github.com/gvarela/bluepill}
54
+ s.rdoc_options = ["--charset=UTF-8"]
55
+ s.require_paths = ["lib"]
56
+ s.rubygems_version = %q{1.3.5}
57
+ s.summary = %q{A process monitor written in Ruby with stability and minimalism in mind.}
58
+
59
+ if s.respond_to? :specification_version then
60
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
61
+ s.specification_version = 3
62
+
63
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
64
+ s.add_runtime_dependency(%q<daemons>, [">= 1.0.9"])
65
+ s.add_runtime_dependency(%q<blankslate>, [">= 2.1.2.2"])
66
+ s.add_runtime_dependency(%q<state_machine>, [">= 0.8.0"])
67
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
68
+ else
69
+ s.add_dependency(%q<daemons>, [">= 1.0.9"])
70
+ s.add_dependency(%q<blankslate>, [">= 2.1.2.2"])
71
+ s.add_dependency(%q<state_machine>, [">= 0.8.0"])
72
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
73
+ end
74
+ else
75
+ s.add_dependency(%q<daemons>, [">= 1.0.9"])
76
+ s.add_dependency(%q<blankslate>, [">= 2.1.2.2"])
77
+ s.add_dependency(%q<state_machine>, [">= 0.8.0"])
78
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
79
+ end
80
+ end
81
+
@@ -0,0 +1,7 @@
1
+ module Bluepill
2
+ module Application
3
+ module Client
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ module Bluepill
2
+ module Application
3
+ module ServerMethods
4
+
5
+ def status
6
+ buffer = ""
7
+ self.processes.each do | process |
8
+ buffer << "#{process.name} #{process.state}\n" +
9
+ end
10
+ buffer
11
+ end
12
+
13
+ def restart
14
+ self.socket = Bluepill::Socket.new(name, base_dir).client
15
+ socket.send("restart\n", 0)
16
+ end
17
+
18
+ def stop
19
+ self.socket = Bluepill::Socket.new(name, base_dir).client
20
+ socket.send("stop\n", 0)
21
+ end
22
+ end
23
+ end
24
+ end