-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
php_fpm.py
219 lines (180 loc) · 8.11 KB
/
php_fpm.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
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
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import json
import random
import socket
import time
from flup.client.fcgi_app import FCGIApp
from six import PY3, StringIO, iteritems, string_types
from six.moves.urllib.parse import urlparse
from datadog_checks.base import AgentCheck, is_affirmative
# Relax param filtering
FCGIApp._environPrefixes.extend(('DOCUMENT_', 'SCRIPT_'))
# Flup as of 1.0.3 is not fully compatible with Python 3 yet.
# This fixes that for our use case.
# https://hg.saddi.com/flup-py3.0/file/tip/flup/client/fcgi_app.py
if PY3:
def get_connection(self):
if self._connect is not None:
if isinstance(self._connect, string_types):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(self._connect)
elif hasattr(socket, 'create_connection'):
sock = socket.create_connection(self._connect)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self._connect)
return sock
FCGIApp._getConnection = get_connection
DEFAULT_TIMEOUT = 10
class BadConfigError(Exception):
pass
class PHPFPMCheck(AgentCheck):
"""
Tracks basic php-fpm metrics via the status module
Requires php-fpm pools to have the status option.
See http://www.php.net/manual/de/install.fpm.configuration.php#pm.status-path for more details
"""
SERVICE_CHECK_NAME = 'php_fpm.can_ping'
GAUGES = {
'listen queue': 'php_fpm.listen_queue.size',
'idle processes': 'php_fpm.processes.idle',
'active processes': 'php_fpm.processes.active',
'total processes': 'php_fpm.processes.total',
}
MONOTONIC_COUNTS = {
'accepted conn': 'php_fpm.requests.accepted',
'max children reached': 'php_fpm.processes.max_reached',
'slow requests': 'php_fpm.requests.slow',
}
HTTP_CONFIG_REMAPPER = {
'user': {'name': 'username'},
'disable_ssl_validation': {'name': 'tls_verify', 'invert': True, 'default': False},
}
def __init__(self, name, init_config, instances):
super(PHPFPMCheck, self).__init__(name, init_config, instances)
if 'http_host' in self.instance:
self.http.options['headers']['Host'] = self.instance['http_host']
def check(self, instance):
status_url = instance.get('status_url')
ping_url = instance.get('ping_url')
use_fastcgi = is_affirmative(instance.get('use_fastcgi', False))
ping_reply = instance.get('ping_reply')
sock_timeout = instance.get('timeout', DEFAULT_TIMEOUT)
tags = instance.get('tags', [])
http_host = instance.get('http_host')
if status_url is None and ping_url is None:
raise BadConfigError("No status_url or ping_url specified for this instance")
socket.setdefaulttimeout(sock_timeout)
pool = None
if status_url is not None:
try:
pool = self._process_status(status_url, tags, http_host, use_fastcgi)
except Exception as e:
self.log.error("Error running php_fpm check: %s", e)
if ping_url is not None:
self._process_ping(ping_url, ping_reply, tags, pool, http_host, use_fastcgi)
def _process_status(self, status_url, tags, http_host, use_fastcgi):
data = {}
try:
if use_fastcgi:
data = json.loads(self.request_fastcgi(status_url, query='json'))
else:
# TODO: adding the 'full' parameter gets you per-process detailed
# information, which could be nice to parse and output as metrics
max_attempts = 3
for i in range(max_attempts):
resp = self.http.get(status_url, params={'json': True})
# Exponential backoff, wait at most (max_attempts - 1) times in case we get a 503.
# Delay in seconds is (2^i + random amount of seconds between 0 and 1)
# 503s originated here: https://github.com/php/php-src/blob/d84ef96/sapi/fpm/fpm/fpm_status.c#L96
if resp.status_code == 503 and i < max_attempts - 1:
# retry
time.sleep(2 ** i + random.random())
continue
resp.raise_for_status()
data = resp.json()
# successfully got a response, exit the backoff system
break
except Exception as e:
self.log.error("Failed to get metrics from %s: %s", status_url, e)
raise
pool_name = data.get('pool', 'default')
metric_tags = tags + ["pool:{0}".format(pool_name)]
if http_host is not None:
metric_tags += ["http_host:{0}".format(http_host)]
for key, mname in iteritems(self.GAUGES):
if key not in data:
self.log.warning("Gauge metric %s is missing from FPM status", key)
continue
self.gauge(mname, int(data[key]), tags=metric_tags)
for key, mname in iteritems(self.MONOTONIC_COUNTS):
if key not in data:
self.log.warning("Counter metric %s is missing from FPM status", key)
continue
self.monotonic_count(mname, int(data[key]), tags=metric_tags)
# return pool, to tag the service check with it if we have one
return pool_name
def _process_ping(self, ping_url, ping_reply, tags, pool_name, http_host, use_fastcgi):
if ping_reply is None:
ping_reply = 'pong'
sc_tags = ["ping_url:{0}".format(ping_url)] + tags
if http_host is not None:
sc_tags += ["http_host:{0}".format(http_host)]
try:
# TODO: adding the 'full' parameter gets you per-process detailed
# information, which could be nice to parse and output as metrics
if use_fastcgi:
response = self.request_fastcgi(ping_url).decode('utf-8')
else:
resp = self.http.get(ping_url)
resp.raise_for_status()
response = resp.text
if ping_reply not in response:
raise Exception("Received unexpected reply to ping: {}".format(response))
except Exception as e:
self.log.error("Failed to ping FPM pool %s on URL %s: %s", pool_name, ping_url, e)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=sc_tags, message=str(e))
else:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=sc_tags)
@classmethod
def request_fastcgi(cls, url, query=''):
parsed_url = urlparse(url)
if parsed_url.scheme == "unix":
# Example of expected format: unix:///path/to/file.sock/ping
sock_file, _, route = parsed_url.path.partition('.sock')
sock_file += '.sock'
hostname = 'localhost'
port = '80'
fcgi = FCGIApp(connect=sock_file)
else:
hostname = parsed_url.hostname
if hostname == 'localhost':
hostname = '127.0.0.1'
port = str(parsed_url.port or 9000)
route = parsed_url.path
fcgi = FCGIApp(host=hostname, port=port)
env = {
'CONTENT_LENGTH': '0',
'CONTENT_TYPE': '',
'DOCUMENT_ROOT': '/',
'GATEWAY_INTERFACE': 'FastCGI/1.1',
'QUERY_STRING': query,
'REDIRECT_STATUS': '200',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '80',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': route,
'SCRIPT_FILENAME': route,
'SCRIPT_NAME': route,
'SERVER_ADDR': hostname,
'SERVER_NAME': hostname,
'SERVER_PORT': port,
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SOFTWARE': 'datadog-php-fpm',
'wsgi.errors': StringIO(),
'wsgi.input': StringIO(),
}
# Return first response
return fcgi(env, lambda *args, **kwargs: '')[0]