-
Notifications
You must be signed in to change notification settings - Fork 8
/
fpmstatus.py
164 lines (145 loc) · 5.31 KB
/
fpmstatus.py
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
# -*- coding: utf-8 -*-
import gevent
from amplify.agent.common.context import context
from amplify.agent.common.util.timeout import TimeoutException
from amplify.ext.phpfpm.util.inet import INET_IPV4
from amplify.ext.phpfpm.util.fcgi import FCGIApp
__author__ = "Grant Hulegaard"
__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved."
__license__ = ""
__maintainer__ = "Grant Hulegaard"
__email__ = "grant.hulegaard@nginx.com"
class PHPFPMStatus(object):
"""
Query wrapper around FCGIApp. Responsible for properly initializing and
calling FCGIApp with exception handling.
"""
def __init__(self, path=None, host=None, port=None, url=None):
self._path = path
self._host = host
self._port = port
self.connection = None
self._setup_connection()
self.env = {}
self._setup_env(url)
def _setup_connection(self):
"""
Setup connection information. IPV4 or Unix file socket.
Follows a similar pattern from flup.
"""
if self._host:
assert self._port
self.connection = INET_IPV4(self._host, self._port)
if self.connection is None:
assert self._path
self.connection = self._path
def _setup_env(self, url):
"""
Setup environment variables to pass though CGI
"""
self.env = {
'SCRIPT_FILENAME': url,
'QUERY_STRING': '',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': url,
'REQUEST_URI': url,
'GATEWAY_INTERFACE': 'CGI/1.1',
'SERVER_SOFTWARE': 'amplify-agent',
'REDIRECT_STATUS': '200',
'CONTENT_TYPE': '',
'CONTENT_LENGTH': '0',
# 'DOCUMENT_URI': url,
'DOCUMENT_ROOT': '/var/www/',
'SERVER_PROTOCOL': 'HTTP/1.1',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '123'
}
if isinstance(self.connection, INET_IPV4):
self.env.update({
'SERVER_ADDR': self.connection.host,
'SERVER_PORT': str(self.connection.port),
'SERVER_NAME': self.connection.host
})
elif isinstance(self.connection, basestring):
self.env.update({
'SERVER_ADDR': self.connection,
'SERVER_NAME': self.connection
})
def _connect(self):
"""
Initialize an FCGIApp wrapper from flup. Since FCGIApp doesn't open a
socket until call, we don't need try-except handling.
"""
# TODO: Should we cache this FCGIApp object? Only init once instead of
# per call?
if self.connection is not None:
# if _connect is a string, assume it is a string path for a Unix
# File sock
if isinstance(self.connection, basestring):
fcgi = FCGIApp(
connect=self.connection
)
elif isinstance(self.connection, INET_IPV4):
fcgi = FCGIApp(
host=self.connection.host, port=self.connection.port
)
else:
fcgi = FCGIApp(
connect=self.connection
)
# this is a hail mary that will bubble a NotImplemented error
# from FCGIApp if flup can't handle it.
return fcgi
def get_status(self):
"""
Now with meta information all setup, attempt to communicate over socket
and get status page.
Example return::
pool: www
process manager: dynamic
start time: 07/Dec/2016:00:13:21 +0000
start since: 0
accepted conn: 1
listen queue: 0
max listen queue: 0
listen queue len: 0
idle processes: 0
active processes: 1
total processes: 1
max active processes: 1
max children reached: 0
slow requests: 0
"""
try:
with gevent.Timeout(10, TimeoutException):
fcgi = self._connect()
resp = fcgi(self.env, lambda x, y: None)
except TimeoutException:
context.log.error(
'pool communication at "%s" timed out' %
self.connection.__str__()
) # use .__str__() because of namedtuple
context.log.debug('additional info:', exc_info=True)
resp = ('500', [], '', '')
except:
context.log.error(
'failed to communicate with pool at "%s"' %
self.connection.__str__()
) # use .__str__() because of namedtuple
context.log.debug('additional info:', exc_info=True)
resp = ('500', [], '', '')
status, headers, out, err = resp
if status.startswith('200'):
return out
else:
context.log.debug(
'non-success returned by fcgi (status: %s)' % status
)
context.log.debug(
'additional info:\n'
' status: %s\n'
' headers: %s\n'
' out: %s\n'
' err: %s\n'
% resp
)