/
passenger-status
executable file
·268 lines (238 loc) · 7.83 KB
/
passenger-status
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env ruby
# Phusion Passenger - https://www.phusionpassenger.com/
# Copyright (c) 2010-2013 Phusion
#
# "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## Magic comment: set locations.ini ##
source_root = File.expand_path("..", File.dirname(__FILE__))
$LOAD_PATH.unshift("#{source_root}/lib")
require 'phusion_passenger'
PhusionPassenger.locate_directories
require 'phusion_passenger/platform_info'
require 'phusion_passenger/admin_tools/server_instance'
require 'phusion_passenger/utils/ansi_colors'
require 'optparse'
include PhusionPassenger::AdminTools
include PhusionPassenger::Utils::AnsiColors
DEFAULT_OPTIONS = { :show => 'pool' }.freeze
##### Show status command #####
def command_show_status(argv, options)
if argv.empty?
server_instance = find_sole_server_instance
else
server_instance = find_server_instance_on_pid(argv[0].to_i)
end
show_status(server_instance, options)
end
def find_sole_server_instance
server_instances = ServerInstance.list
if server_instances.empty?
abort "ERROR: Phusion Passenger doesn't seem to be running."
elsif server_instances.size == 1
return server_instances.first
else
puts "It appears that multiple Passenger instances are running. Please select a"
puts "specific one by running:"
puts
puts " passenger-status <PID>"
puts
puts "The following Passenger instances are running:"
server_instances.each do |instance|
puts " PID: #{instance.pid}"
end
exit 1
end
end
def find_server_instance_on_pid(pid)
if server_instance = ServerInstance.for_pid(pid)
return server_instance
else
abort "ERROR: there doesn't seem to be a Phusion Passenger instance running on PID #{pid}."
end
end
def show_status(server_instance, options)
puts "Version : #{PhusionPassenger::VERSION_STRING}"
puts "Date : #{Time.now}"
puts "Instance: #{server_instance.pid}"
case options[:show]
when 'pool'
client = server_instance.connect(:role => :passenger_status)
begin
puts client.pool_status(:verbose => options[:verbose], :colorize => true)
rescue SystemCallError => e
STDERR.puts "*** ERROR: Cannot query status for Phusion Passenger instance #{server_instance.pid}:"
STDERR.puts e.to_s
exit 2
end
when 'requests'
client = server_instance.connect(:role => :passenger_status)
begin
puts client.helper_agent_requests
rescue SystemCallError => e
STDERR.puts "*** ERROR: Cannot query status for Phusion Passenger instance #{server_instance.pid}:"
STDERR.puts e.to_s
exit 2
end
when 'backtraces'
client = server_instance.connect(:role => :passenger_status)
begin
text = client.helper_agent_backtraces
rescue SystemCallError => e
STDERR.puts "*** ERROR: Cannot query status for Phusion Passenger instance #{control_process.pid}:"
STDERR.puts e.to_s
exit 2
end
# Colorize output
text.gsub!(/^(Thread .*:)$/, BLACK_BG + YELLOW + '\1' + RESET)
text.gsub!(/^( +in '.*? )(.*?)\(/, '\1' + BOLD + '\2' + RESET + '(')
puts text
when 'xml'
client = server_instance.connect(:role => :passenger_status)
begin
xml = client.pool_xml
rescue SystemCallError => e
STDERR.puts "*** ERROR: Cannot query status for Phusion Passenger instance #{control_process.pid}:"
STDERR.puts e.to_s
exit 2
end
indented = format_with_xmllint(xml)
if indented
puts indented
else
puts xml
STDERR.puts "*** Tip: if you install the 'xmllint' command then the XML output will be indented."
end
when 'union_station'
client = server_instance.connect(:role => :passenger_status, :socket_name => 'logging_admin')
begin
response = client.logging_agent_status
rescue SystemCallError => e
STDERR.puts "*** ERROR: Cannot query status for Phusion Passenger instance #{control_process.pid}:"
STDERR.puts e.to_s
exit 2
end
puts response
end
rescue ServerInstance::RoleDeniedError
require 'phusion_passenger/platform_info/ruby'
STDERR.puts "*** ERROR: You are not authorized to query the status for this Phusion " <<
"Passenger instance. Please try again with '#{PhusionPassenger::PlatformInfo.ruby_sudo_command}'."
exit 2
rescue ServerInstance::CorruptedDirectoryError
STDERR.puts "*** ERROR: The server instance directory #{server_instance.path} is corrupted. " <<
"This could have two causes:\n" <<
"\n" <<
" 1. The Phusion Passenger instance is no longer running, but failed to cleanup the directory. " <<
"Please delete this directory and ignore the problem.\n" <<
" 2. An external program corrupted the directory. Please restart Phusion Passenger.\n"
exit 2
ensure
client.close if client
end
def format_with_xmllint(xml)
return nil if !PhusionPassenger::PlatformInfo.find_command('xmllint')
require 'open3'
require 'thread'
ENV['XMLLINT_INDENT'] = ' '
Open3.popen3("xmllint", "--format", "-") do |stdin, stdout, stderr|
stdout_text = nil
stderr_text = nil
thread1 = Thread.new do
stdin.write(xml)
stdin.close
end
thread2 = Thread.new do
stdout_text = stdout.read
stdout.close
end
thread3 = Thread.new do
stderr_text = stderr.read
stderr.close
end
thread1.join
thread2.join
thread3.join
if stdout_text.nil? || stdout_text.empty?
if stderr_text !~ /No such file or directory/ && stderr_text !~ /command not found/
STDERR.puts stderr_text
end
return nil
else
return stdout_text
end
end
end
##### Main command dispatcher #####
def create_option_parser(options)
return OptionParser.new do |opts|
opts.banner = "Usage: passenger-status [options] [Phusion Passenger's PID]"
opts.separator ""
opts.separator "Tool for inspecting Phusion Passenger's internal status."
opts.separator ""
opts.separator "Options:"
opts.on("--show=pool|requests|backtraces|xml|union_station", String,
"Whether to show the pool's contents,\n" <<
"#{' ' * 37}the currently running requests,\n" <<
"#{' ' * 37}the backtraces of all threads or an XML\n" <<
"#{' ' * 37}description of the pool.") do |what|
if what !~ /\A(pool|requests|backtraces|xml|union_station)\Z/
STDERR.puts "Invalid argument for --show."
exit 1
else
options[:show] = what
end
end
opts.on("--verbose", "-v", "Show verbose information.") do
options[:verbose] = true
end
end
end
def parse_argv
options = DEFAULT_OPTIONS.dup
parser = create_option_parser(options)
begin
parser.parse!
rescue OptionParser::ParseError => e
puts e
puts
puts "Please see '--help' for valid options."
exit 1
end
return options
end
def infer_command
if !ARGV[0] || ARGV[0] =~ /\A[0-9]+\Z/
return [:show_status, ARGV.dup]
else
command_name, *argv = ARGV
if respond_to?("command_#{command_name}")
return [command_name, argv]
else
abort "ERROR: unrecognized command '#{command_name}'"
end
end
end
def start
options = parse_argv
command, argv = infer_command
send("command_#{command}", argv, options)
end
start