dylanvaughn-bluepill 0.0.39

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
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,228 @@
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
+ ### Extra options
215
+ You can run bluepill in the foreground:
216
+
217
+ Bluepill.application("app_name", :foreground => true) do |app|
218
+ # ...
219
+ end
220
+
221
+ ## Links
222
+ Code: [http://github.com/arya/bluepill](http://github.com/arya/bluepill)
223
+ Bugs/Features: [http://github.com/arya/bluepill/issues](http://github.com/arya/bluepill/issues)
224
+ Mailing List: [http://groups.google.com/group/bluepill-rb](http://groups.google.com/group/bluepill-rb)
225
+
226
+
227
+ [gemcutter]: http://gemcutter.org
228
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dylanvaughn-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 = "dylancvaughn@gmail.com"
11
+ gem.homepage = "http://github.com/arya/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.4")
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
+ desc "Update version of Bluepill in source code"
44
+ task :update_file do
45
+ version = File.read("VERSION").strip
46
+ File.open("lib/bluepill/version.rb", "w") do |file|
47
+ file.write <<-END
48
+ module Bluepill
49
+ VERSION = "#{version}"
50
+ end
51
+ END
52
+ end
53
+ end
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.39
data/bin/bluepill ADDED
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'bluepill'
4
+
5
+ # Check for root
6
+ unless ::Process.euid == 0
7
+ $stderr.puts "You must run bluepill as root."
8
+ exit(3)
9
+ end
10
+
11
+
12
+ # Default options
13
+ options = {
14
+ :log_file => "/var/log/bluepill.log",
15
+ :base_dir => "/var/bluepill"
16
+ }
17
+
18
+ OptionParser.new do |opts|
19
+ opts.banner = "Usage: bluepill [app] cmd [options]"
20
+ opts.on('-l', "--logfile LOGFILE", "Path to logfile, defaults to #{options[:log_file]}") do |file|
21
+ options[:log_file] = file
22
+ end
23
+
24
+ opts.on('-c', "--base-dir DIR", "Directory to store bluepill socket and pid files, defaults to #{options[:base_dir]}") do |base_dir|
25
+ options[:base_dir] = base_dir
26
+ end
27
+
28
+ opts.on("-v", "--version") do
29
+ puts "bluepill, version #{Bluepill::VERSION}"
30
+ exit
31
+ end
32
+
33
+ help = lambda do
34
+ puts opts
35
+ puts
36
+ puts "Commands:"
37
+ puts " load CONFIG_FILE\t\tLoads new instance of bluepill using the specified config file"
38
+ puts " status\t\t\tLists the status of the proceses for the specified app"
39
+ puts " start [TARGET]\t\tIssues the start command for the target process or group, defaults to all processes"
40
+ puts " stop [TARGET]\t\tIssues the stop command for the target process or group, defaults to all processes"
41
+ puts " restart [TARGET]\t\tIssues the restart command for the target process or group, defaults to all processes"
42
+ puts " unmonitor [TARGET]\t\tStop monitoring target process or group, defaults to all processes"
43
+ puts " log [TARGET]\t\tShow the log for the specified process or group, defaults to all for app"
44
+ puts " quit\t\t\tStop bluepill"
45
+ puts
46
+ puts "See http://github.com/arya/bluepill for README"
47
+ exit
48
+ end
49
+
50
+ opts.on_tail('-h','--help', 'Show this message', &help)
51
+ help.call if ARGV.empty?
52
+ end.parse!
53
+
54
+ APPLICATION_COMMANDS = %w(status start stop restart unmonitor quit log)
55
+
56
+ controller = Bluepill::Controller.new(options.slice(:base_dir, :log_file))
57
+
58
+ if controller.running_applications.include?(File.basename($0)) && File.symlink?($0)
59
+ # bluepill was called as a symlink with the name of the target application
60
+ options[:application] = File.basename($0)
61
+ elsif controller.running_applications.include?(ARGV.first)
62
+ # the first arg is the application name
63
+ options[:application] = ARGV.shift
64
+ elsif APPLICATION_COMMANDS.include?(ARGV.first)
65
+ if controller.running_applications.length == 1
66
+ # there is only one, let's just use that
67
+ options[:application] = controller.running_applications.first
68
+ elsif controller.running_applications.length > 1
69
+ # There is more than one, tell them the list and exit
70
+ $stderr.puts "You must specify an application name to run that command. Here's the list of running applications:"
71
+ controller.running_applications.each_with_index do |app, index|
72
+ $stderr.puts " #{index + 1}. #{app}"
73
+ end
74
+ $stderr.puts "Usage: bluepill [app] cmd [options]"
75
+ exit(1)
76
+ else
77
+ # There are none running AND they aren't trying to start one
78
+ $stderr.puts "Error: There are no running bluepill daemons.\nTo start a bluepill daemon, use: bluepill load <config file>"
79
+ exit(2)
80
+ end
81
+ end
82
+
83
+ options[:command] = ARGV.shift
84
+
85
+ if options[:command] == "load"
86
+ file = ARGV.shift
87
+ if File.exists?(file)
88
+ # Restart the ruby interpreter for the config file so that anything loaded here
89
+ # does not stay in memory for the daemon
90
+ require 'rbconfig'
91
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
92
+ load_path = File.expand_path("#{File.dirname(__FILE__)}/../lib")
93
+ file_path = File.expand_path(file)
94
+
95
+ exec(ruby, "-I#{load_path}", '-rbluepill', file_path)
96
+
97
+ else
98
+ $stderr.puts "Can't find file: #{file}"
99
+ end
100
+ else
101
+ target = ARGV.shift
102
+ controller.handle_command(options[:application], options[:command], target)
103
+ end
data/bin/bpsv ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ target=`basename $0`
3
+ exec bluepill $1 $target
data/bluepill.gemspec ADDED
@@ -0,0 +1,84 @@
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.38"
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{2010-05-31}
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
+ "bin/bpsv",
30
+ "bluepill.gemspec",
31
+ "lib/bluepill.rb",
32
+ "lib/bluepill/application.rb",
33
+ "lib/bluepill/application/client.rb",
34
+ "lib/bluepill/application/server.rb",
35
+ "lib/bluepill/condition_watch.rb",
36
+ "lib/bluepill/controller.rb",
37
+ "lib/bluepill/dsl.rb",
38
+ "lib/bluepill/group.rb",
39
+ "lib/bluepill/logger.rb",
40
+ "lib/bluepill/process.rb",
41
+ "lib/bluepill/process_conditions.rb",
42
+ "lib/bluepill/process_conditions/always_true.rb",
43
+ "lib/bluepill/process_conditions/cpu_usage.rb",
44
+ "lib/bluepill/process_conditions/mem_usage.rb",
45
+ "lib/bluepill/process_conditions/process_condition.rb",
46
+ "lib/bluepill/process_statistics.rb",
47
+ "lib/bluepill/socket.rb",
48
+ "lib/bluepill/system.rb",
49
+ "lib/bluepill/trigger.rb",
50
+ "lib/bluepill/triggers/flapping.rb",
51
+ "lib/bluepill/util/rotational_array.rb",
52
+ "lib/bluepill/version.rb",
53
+ "lib/example.rb",
54
+ "lib/runit_example.rb"
55
+ ]
56
+ s.homepage = %q{http://github.com/arya/bluepill}
57
+ s.rdoc_options = ["--charset=UTF-8"]
58
+ s.require_paths = ["lib"]
59
+ s.rubygems_version = %q{1.3.7}
60
+ s.summary = %q{A process monitor written in Ruby with stability and minimalism in mind.}
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 3
65
+
66
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
67
+ s.add_runtime_dependency(%q<daemons>, [">= 1.0.9"])
68
+ s.add_runtime_dependency(%q<blankslate>, [">= 2.1.2.2"])
69
+ s.add_runtime_dependency(%q<state_machine>, [">= 0.8.0"])
70
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
71
+ else
72
+ s.add_dependency(%q<daemons>, [">= 1.0.9"])
73
+ s.add_dependency(%q<blankslate>, [">= 2.1.2.2"])
74
+ s.add_dependency(%q<state_machine>, [">= 0.8.0"])
75
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
76
+ end
77
+ else
78
+ s.add_dependency(%q<daemons>, [">= 1.0.9"])
79
+ s.add_dependency(%q<blankslate>, [">= 2.1.2.2"])
80
+ s.add_dependency(%q<state_machine>, [">= 0.8.0"])
81
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
82
+ end
83
+ end
84
+