public
Description: Process title support for Mongrel (GemPlugin)
Homepage: http://github.com/rtomayko/mongrel_proctitle/tree/master/README
Clone URL: git://github.com/rtomayko/mongrel_proctitle.git
mongrel_proctitle / bin / mongrel_top
100755 93 lines (80 sloc) 1.875 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
 
$last_update = nil
$clear_char = `tput clear`
 
def usage
  puts "usage: #{File.basename($0)} [-s delay] [-o fields]"
end
 
def die(*message)
  STDERR.puts "fatal: #{format(*message)}" if message.any?
  exit 1
end
 
# clear the terminal
def clear_screen
  STDOUT.write($clear_char)
end
 
# clear the terminal and then write output.
def update_screen(output)
  if output != $last_update
    clear_screen
    puts(output)
  end
end
 
def terminal_columns
  @terminal_columns ||=
    if (cols = `tput cols`) == ""
      80
    else
      cols.to_i
    end
end
 
trap("INT") {
  STDERR.puts "\nbailing ..."
  exit 0
}
 
# time to wait between updates
interval = 0.25
 
# the fields to show in our display (see ps -o)
fields = "pid,command"
 
# parse arguments.
# TODO: use getoptlong
argv = ARGV.map { |a| a.split('=', 2) }.flatten
while argv.any?
  arg = argv.shift
  case arg
  when /^-(s|-delay)$/
    interval = argv.shift.to_f
  when /^-(o|-fields)$/
    fields = argv.shift
  when /^-(h|-help)$/
    usage and exit(0)
  else
    usage and die
  end
end
 
# Builds the process output display.
def process_display(fields, interval)
  now = Time.now.strftime('%a %b %d %H:%M:%S %Y')
  cols = terminal_columns
  processes =
    `ps axww -o #{fields}`.grep(/mongrel_rails/).map do |line|
      line.strip!
      line.sub! /\s+SHELL=.*$/, '' # weird MacOS X issue
      line.sub! /(?:ruby\d*: )?mongrel_rails /, '' # remove $0 prefix
      line.sub! /\(ruby\d*\)/, '' # remove (ruby) suffix
      line[0,cols].chomp
    end
  output = [
    (' ' * (cols - now.length)) + now + "\r" +
    "Mongrel Top (delay: #{interval})",
    "",
    processes,
    "",
    "#{processes.length} process(es)"
  ]
  output.flatten.join("\n")
end
 
while true
  output = process_display(fields, interval)
  update_screen(output)
  sleep(interval)
end