]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/irs_process_scripts/lib/commands/process/inspector.rb
merge 15807:16012 from rails_port
[rails.git] / vendor / plugins / irs_process_scripts / lib / commands / process / inspector.rb
1 require 'optparse'
2
3 if RUBY_PLATFORM =~ /(:?mswin|mingw)/ then abort("Inspector is only for Unix") end
4
5 OPTIONS = {
6   :pid_path => File.expand_path(RAILS_ROOT + '/tmp/pids'),
7   :pattern  => "dispatch.*.pid",
8   :ps       => "ps -o pid,state,user,start,time,pcpu,vsz,majflt,command -p %s"
9 }
10
11 class Inspector
12   def self.inspect(pid_path, pattern)
13     new(pid_path, pattern).inspect
14   end
15
16   def initialize(pid_path, pattern)
17     @pid_path, @pattern = pid_path, pattern
18   end
19
20   def inspect
21     header = `#{OPTIONS[:ps] % 1}`.split("\n")[0] + "\n"
22     lines  = pids.collect { |pid| `#{OPTIONS[:ps] % pid}`.split("\n")[1] }
23     
24     puts(header + lines.join("\n"))
25   end
26
27   private
28     def pids
29       pid_files.collect do |pid_file|
30         File.read(pid_file).to_i
31       end
32     end
33
34     def pid_files
35       Dir.glob(@pid_path + "/" + @pattern)
36     end
37 end
38
39
40 ARGV.options do |opts|
41   opts.banner = "Usage: inspector [options]"
42
43   opts.separator ""
44
45   opts.on <<-EOF
46   Description:
47     Displays system information about Rails dispatchers (or other processes that use pid files) through
48     the ps command.
49
50   Examples:
51     inspector                                             # default ps on all tmp/pids/dispatch.*.pid files
52     inspector -s 'ps -o user,start,majflt,pcpu,vsz -p %s' # custom ps, %s is where the pid is interleaved
53   EOF
54
55   opts.on("  Options:")
56
57   opts.on("-s", "--ps=command", "default: #{OPTIONS[:ps]}", String)           { |v| OPTIONS[:ps] = v }
58   opts.on("-p", "--pidpath=path", "default: #{OPTIONS[:pid_path]}", String)   { |v| OPTIONS[:pid_path] = v }
59   opts.on("-r", "--pattern=pattern", "default: #{OPTIONS[:pattern]}", String) { |v| OPTIONS[:pattern] = v }
60
61   opts.separator ""
62
63   opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
64
65   opts.parse!
66 end
67
68 Inspector.inspect(OPTIONS[:pid_path], OPTIONS[:pattern])