-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphpfpm.py
executable file
·102 lines (85 loc) · 2.74 KB
/
phpfpm.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
#!/usr/bin/env python
import sys
import urllib
import json
COLLECTD_ENABLED = True
try:
if COLLECTD_ENABLED:
import collectd
except ImportError:
# We're not running in CollectD, set this to False so we can make some changes
# accordingly for testing/development.
COLLECTD_ENABLED = False
import re
# default config
URLS = []
# gauge: store as is
# derive: store the change over time
TYPES = {
'start since': 'gauge',
'accepted conn': 'derive',
'listen queue': 'gauge',
'max listen queue': 'gauge',
'listen queue len': 'gauge',
'idle processes': 'gauge',
'active processes': 'gauge',
'total processes': 'gauge',
'max active processes': 'gauge',
'max children reached': 'gauge',
'slow requests': 'requests',
'requests': 'requests',
'request duration': 'gauge',
'content length': 'gauge',
'last request cpu': 'gauge',
'last request memory': 'gauge',
}
def configure_callback(conf):
global URLS
for node in conf.children:
key = node.key.lower()
value = node.values[0]
if key == 'url':
URLS.append(value)
else:
collectd.warning('phpfpm plugin: Unknown config key: %s.' % key)
def dispatch(pool, metric, value, metric_type, process=None):
instance = pool
if process is not None:
instance += '.process-{}'.format(process)
metric = metric.replace(' ', '_')
if COLLECTD_ENABLED:
vl = collectd.Values(plugin='phpfpm', plugin_instance=instance,
type=metric_type, type_instance=metric)
vl.dispatch(values=[value])
else:
print 'dispatch: phpfpm.{}.{}.{} value: {}'.format(
instance, metric_type, metric, value)
def read_callback():
global URLS
for url in URLS:
response = urllib.urlopen(url)
data = json.loads(response.read())
# read master metrics
pool = data['pool']
for metric in [m for m in data.keys() if m in TYPES]:
dispatch(pool, metric, data[metric], TYPES[metric])
# read each prcess metrics
if 'processes' in data.keys():
i = 0
for process in data['processes']:
for metric in [m for m in process.keys() if m in TYPES]:
dispatch(pool, metric,
process[metric], TYPES[metric], process=i)
i += 1
if COLLECTD_ENABLED:
collectd.register_read(read_callback)
collectd.register_config(configure_callback)
if __name__ == "__main__" and not COLLECTD_ENABLED:
from pprint import pprint as pp
print "Running in test mode, invoke with"
print sys.argv[0] + " URL"
URLS.append(sys.argv[1])
print "\n\nURLS:"
pp(URLS)
print
read_callback()