Improve phpfpm module.
* Get the fpm stats in json format for easy parsing * Get stats for each process and track maximums and averages for each.
Clint Armstrong committed
Sep 21, 2016 at 11:42 UTC
534303bfd4bd8778b66bd8355e1c2dffb05f7e47
2 files changed
+54
-14
conf.d/python.d/phpfpm.conf
+4
-3
@@ -57,6 +57,7 @@
57
# Additionally to the above, PHP-FPM also supports the following:
58
#
59
# url: 'URL' # the URL to fetch nginx's status stats
60
+# # Be sure and include ?full&status at the end of the url
61
#
62
# if the URL is password protected, the following are supported:
63
#
@@ -70,13 +71,13 @@
71
72
localhost:
73
name : 'local'
73
- url : "http://localhost/status"
74
+ url : "http://localhost/status?full&json"
75
76
localipv4:
77
name : 'local'
77
- url : "http://127.0.0.1/status"
78
+ url : "http://127.0.0.1/status?full&json"
79
80
localipv6:
81
name : 'local'
81
- url : "http://::1/status"
82
+ url : "http://::1/status?full&json"
83
python.d/phpfpm.chart.py
+50
-11
@@ -3,6 +3,7 @@
3
# Author: Pawel Krupa (paulfantom)
4
5
from base import UrlService
6
+import json
7
8
# default module values (can be overridden per job in `config`)
9
# update_every = 2
@@ -18,7 +19,7 @@ retries = 60
19
# }}
20
21
# charts order (can be overridden if you want less charts, or different order)
21
-ORDER = ['connections', 'requests', 'performance']
22
+ORDER = ['connections', 'requests', 'performance', 'request_duration', 'request_cpu', 'request_mem']
23
24
CHARTS = {
25
'connections': {
@@ -38,6 +39,24 @@ CHARTS = {
39
'lines': [
40
["reached", 'max children reached'],
41
["slow", 'slow requests']
42
+ ]},
43
+ 'request_duration': {
44
+ 'options': [None, 'PHP-FPM Request Duration', 'milliseconds', 'phpfpm', 'phpfpm.request_duration', 'line'],
45
+ 'lines': [
46
+ ["maxReqDur", 'max request duration'],
47
+ ["avgReqDur", 'average request duration']
48
+ ]},
49
+ 'request_cpu': {
50
+ 'options': [None, 'PHP-FPM Request CPU', 'percent', 'phpfpm', 'phpfpm.request_cpu', 'line'],
51
+ 'lines': [
52
+ ["maxReqCPU", 'max request cpu'],
53
+ ["avgReqCPU", 'average request cpu']
54
+ ]},
55
+ 'request_mem': {
56
+ 'options': [None, 'PHP-FPM Request Memory', 'kilobytes', 'phpfpm', 'phpfpm.request_mem', 'line'],
57
+ 'lines': [
58
+ ["maxReqMem", 'max request memory'],
59
+ ["avgReqMem", 'average request memory']
60
]}
61
}
62
@@ -46,7 +65,7 @@ class Service(UrlService):
65
def __init__(self, configuration=None, name=None):
66
UrlService.__init__(self, configuration=configuration, name=name)
67
if len(self.url) == 0:
49
- self.url = "http://localhost/status"
68
+ self.url = "http://localhost/status?full&json"
69
self.order = ORDER
70
self.definitions = CHARTS
71
self.assignment = {"active processes": 'active',
@@ -55,6 +74,9 @@ class Service(UrlService):
74
"accepted conn": 'requests',
75
"max children reached": 'reached',
76
"slow requests": 'slow'}
77
+ self.proc_assignment = {"request duration": 'ReqDur',
78
+ "last request cpu": 'ReqCPU',
79
+ "last request memory": 'ReqMem'}
80
81
def _get_data(self):
82
"""
@@ -62,17 +84,34 @@ class Service(UrlService):
84
:return: dict
85
"""
86
try:
65
- raw = self._get_raw_data().split('\n')
87
+ raw = self._get_raw_data()
88
except AttributeError:
89
return None
68
- data = {}
69
- for row in raw:
70
- tmp = row.split(":")
71
- if str(tmp[0]) in self.assignment:
72
- try:
73
- data[self.assignment[tmp[0]]] = int(tmp[1])
74
- except (IndexError, ValueError):
75
- pass
90
+
91
+ try:
92
+ raw_json = json.loads(raw)
93
+ except ValueError:
94
+ return None
95
+
96
+ data = {self.assignment[k]: v for k, v in raw_json.items() if k in self.assignment}
97
+
98
+ c = 0
99
+ for proc in raw_json['processes']:
100
+ if proc['state'] != 'Idle':
101
+ continue
102
+ c += 1
103
+ for k, v in self.proc_assignment.items():
104
+ d = proc[k]
105
+ if v == 'ReqDur':
106
+ d = d/1000
107
+ if v == 'ReqMem':
108
+ d = d/1024
109
+ if 'max' + v not in data or data['max' + v] < d:
110
+ data['max' + v] = d
111
+ if 'avg' + v not in data:
112
+ data['avg' + v] = 0
113
+ data['avg' + v] = (data['avg' + v] + d) / c
114
+
115
if len(data) == 0:
116
return None
117
return data