forked from morbz-archive/munin-php-fpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp-fpm.php
212 lines (194 loc) · 4.71 KB
/
php-fpm.php
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
#!/usr/bin/php
<?php
/**
* @author realhidden
* @author Devlopnet
* @author MorbZ
* @licence CC
*/
//grab all pools
exec('find /etc/php5/fpm/pool.d/*.conf -type f -printf "%f\n"',$allpool);
foreach($allpool as &$pool)
{
$pool=substr($pool,0,-5);
}
//grab processes
exec("ps -eo %cpu,etime,rss,command | grep php-fpm", $result);
//iterate through processes
$groups = array();
foreach ($result as $line) {
//split fields
$line = trim($line);
$args = preg_split('/\s+/', $line);
if (strpos($args[3], 'php-fpm') === false) {
continue;
}
list($cpu, $time, $ram, $type, $poolWord, $poolName) = $args;
//which group
if ($poolWord == 'master') {
continue;
}
$groupName = $poolName;
//add group
if (!isset($groups[$groupName])) {
$groups[$groupName] = array(
'count' => 0,
'memory' => 0,
'cpu' => 0,
'time' => 0
);
}
//add values
$groups[$groupName]['count']++;
$groups[$groupName]['cpu'] += $cpu;
$groups[$groupName]['time'] += timeToSeconds($time);
$groups[$groupName]['memory'] += $ram / 1024;
}
//add missing pools
foreach($allpool as $groupName)
{
if (isset($groups[$groupName]))
continue;
$groups[$groupName] = array(
'count' => 0,
'memory' => 0,
'cpu' => 0,
'time' => 0
);
}
//check args
if(!isset($argv) || !isset($argv[0])) {
die("Error: No Plugin name provided\n");
}
$fileCalled = basename($argv[0]);
$isConfig = isset($argv[1]) && $argv[1] == 'config';
//which plugin?
switch ($fileCalled) {
// ------------------------------------------------------
case 'php-fpm-memory':
// ------------------------------------------------------
$elements = array();
foreach ($groups as $name=>$array) {
$ramMb = $array['memory'] / $array['count'];
$label = 'Pool ' . $name;
$elements[$name] = array(
'label' => $label,
'type' => 'GAUGE',
'value' => $ramMb
);
}
$config = array(
'params' => array(
'graph_title' => 'PHP-FPM Average Process Memory',
'graph_vlabel' => 'MB'
),
'elements' => $elements
);
break;
// ------------------------------------------------------
case 'php-fpm-cpu':
// ------------------------------------------------------
$elements = array();
foreach ($groups as $name=>$array) {
$cpu = $array['cpu'];
$label = 'Pool ' . $name;
$elements[$name] = array(
'label' => $label,
'type' => 'GAUGE',
'value' => $cpu
);
}
$config = array(
'params' => array(
'graph_title' => 'PHP-FPM CPU',
'graph_vlabel' => '%',
'graph_scale' => 'no'
),
'elements' => $elements
);
break;
// ------------------------------------------------------
case 'php-fpm-count':
// ------------------------------------------------------
$elements = array();
foreach ($groups as $name=>$array) {
$label = 'Pool ' . $name;
$elements[$name] = array(
'label' => $label,
'type' => 'GAUGE',
'value' => $array['count']
);
}
$config = array(
'params' => array(
'graph_title' => 'PHP-FPM Processes',
'graph_vlabel' => 'processes'
),
'elements' => $elements
);
break;
// ------------------------------------------------------
case 'php-fpm-time':
// ------------------------------------------------------
$elements = array();
foreach ($groups as $name=>$array) {
$time = round($array['time'] / $array['count']);
$label = 'Pool ' . $name;
$elements[$name] = array(
'label' => $label,
'type' => 'GAUGE',
'value' => $time
);
}
$config = array(
'params' => array(
'graph_title' => 'PHP-FPM Average Process Age',
'graph_vlabel' => 'seconds',
'graph_scale' => 'no'
),
'elements' => $elements
);
break;
// ------------------------------------------------------
default:
die("Error: Unrecognized Plugin name $fileCalled\n");
}
//output
ksort($config['elements']);
if ($isConfig) {
//graph params
echo "graph_category PHP-FPM\n";
foreach($config['params'] as $key=>$value) {
echo $key . ' ' . $value . "\n";
}
//element params
foreach($config['elements'] as $element=>$data) {
foreach ($data as $key=>$value) {
if ($key == 'value') continue;
echo $element . '.' . $key . ' ' . $value . "\n";
}
}
} else {
//element values
foreach ($config['elements'] as $pool=>$element) {
echo $pool . '.value ' . $element['value'] . "\n";
}
}
//functions
function timeToSeconds ($time) {
$seconds = 0;
//days
$parts = explode('-', $time);
if(count($parts) == 2) {
$seconds += $parts[0] * 86400;
$time = $parts[1];
}
//hours
$parts = explode(':', $time);
if(count($parts) == 3) {
$seconds += array_shift($parts) * 3600;
}
//minutes/seconds
$seconds += $parts[0] * 60 + $parts[1];
return $seconds;
}