passenger_monit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Roman Shterenzon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ PassengerMonit
2
+ ==============
3
+
4
+ This plugin provides means for monitoring Rails application which run under [Passenger][1]
5
+ with [Monit][2].
6
+
7
+ The pid file name (hardcoded) is /var/tmp/rack.X.pid, where X denotes
8
+ the instance number.
9
+
10
+ [1]: http://www.modrails.com/ "Phusion Passenger"
11
+ [2]: http://mmonit.com/monit/ "Monit"
12
+
13
+
14
+ Example Monit configuration
15
+ ===========================
16
+
17
+ <pre>
18
+ check process instance1 with pidfile /var/tmp/rack.1.pid
19
+ group web
20
+ start program = "/bin/true" with timeout 30 seconds
21
+ stop program = "/bin/sh -c 'kill `cat /var/tmp/rack.1.pid`'"
22
+ if totalmem > 120.0 MB for 5 cycles then restart
23
+ if changed pid 2 times within 2 cycles then alert
24
+ if changed ppid 2 times within 2 cycles then alert
25
+ </pre>
26
+
27
+ Bugs and missing features
28
+ =========================
29
+
30
+ 1. Only tested with nginx
31
+ 2. The pid file location is hardcoded
32
+ 3. Sometimes there are more passenger instances in the air than anticipated,
33
+ so when one goes down, there might be first pid file missing, e.g.:
34
+ from [rack.1.pid, rack.2.pid] to [rack.1.pid, rack.2.pid, rack.3.pid] to
35
+ [rack.2.pid, rack.3.pid] - monit will complain that the first instance is missing.
36
+
37
+ Copyright (c) 2011 Roman Shterenzon, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Default: create gemspec'
5
+ task :default => :gemspec
6
+
7
+ desc 'Generate documentation for the passenger_monit plugin.'
8
+ Rake::RDocTask.new(:rdoc) do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'PassengerMonit'
11
+ rdoc.options << '--line-numbers' << '--inline-source'
12
+ rdoc.rdoc_files.include('README')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ begin
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ gem.name = "passenger_monit"
20
+ gem.summary = %Q{Support for monitoring Passenger with Monit}
21
+ gem.description = gem.summary
22
+ gem.email = "romanbsd@yahoo.com"
23
+ gem.homepage = "http://github.com/romanbsd/passenger_monit"
24
+ gem.authors = ["Roman Shterenzon"]
25
+ end
26
+ Jeweler::GemcutterTasks.new
27
+ rescue LoadError
28
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'passenger_monit'
@@ -0,0 +1,50 @@
1
+ module PassengerMonit::PidfileManager
2
+ extend self
3
+
4
+ BASENAME = '/var/tmp/rack.*.pid'
5
+
6
+ def write_pid_file
7
+ pid = Process.pid
8
+ count = 1
9
+ pidfile = nil
10
+ go_over_pid_files do |file, saved_pid|
11
+ file_id = file[/(\d+)/,1].to_i
12
+ # Increase counter only if we met the same file id
13
+ count += 1 if file_id == count
14
+ # We're already there
15
+ return if saved_pid == pid
16
+ # Check if the process is alive
17
+ res = begin
18
+ Process.kill(0, saved_pid)
19
+ rescue Errno::ESRCH
20
+ nil
21
+ end
22
+ # It's dead, reuse
23
+ unless res
24
+ pidfile = file
25
+ break
26
+ end
27
+ end
28
+ pidfile ||= BASENAME.sub('*', count.to_s)
29
+ File.open(pidfile, 'w') {|f| f.write(pid.to_s)}
30
+ end
31
+
32
+ def remove_pid_file
33
+ pid = Process.pid
34
+ go_over_pid_files do |file, saved_pid|
35
+ if pid == saved_pid
36
+ File.unlink(file)
37
+ break
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+ def go_over_pid_files
44
+ Dir[BASENAME].each do |file|
45
+ saved_pid = File.read(file).to_i
46
+ yield file, saved_pid
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,16 @@
1
+ module PassengerMonit
2
+ class Railtie < Rails::Railtie
3
+ config.before_initialize do
4
+ if defined?(PhusionPassenger)
5
+ require 'passenger_monit/pidfile_manager'
6
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
7
+ PidfileManager.write_pid_file if forked
8
+ end
9
+
10
+ PhusionPassenger.on_event(:stopping_worker_process) do
11
+ PidfileManager.remove_pid_file
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{passenger_monit}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Roman Shterenzon"]
12
+ s.date = %q{2011-04-29}
13
+ s.description = %q{Support for monitoring Passenger with Monit}
14
+ s.email = %q{romanbsd@yahoo.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/passenger_monit.rb",
25
+ "lib/passenger_monit/pidfile_manager.rb",
26
+ "passenger_monit.gemspec"
27
+ ]
28
+ s.homepage = %q{http://github.com/romanbsd/passenger_monit}
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.7}
31
+ s.summary = %q{Support for monitoring Passenger with Monit}
32
+
33
+ if s.respond_to? :specification_version then
34
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
+ else
39
+ end
40
+ else
41
+ end
42
+ end
43
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passenger_monit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Roman Shterenzon
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-29 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Support for monitoring Passenger with Monit
22
+ email: romanbsd@yahoo.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.md
29
+ files:
30
+ - MIT-LICENSE
31
+ - README.md
32
+ - Rakefile
33
+ - VERSION
34
+ - init.rb
35
+ - lib/passenger_monit.rb
36
+ - lib/passenger_monit/pidfile_manager.rb
37
+ - passenger_monit.gemspec
38
+ has_rdoc: true
39
+ homepage: http://github.com/romanbsd/passenger_monit
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Support for monitoring Passenger with Monit
70
+ test_files: []
71
+