-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGearmanTelnet.php
executable file
·398 lines (335 loc) · 11.1 KB
/
GearmanTelnet.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Monitoring Gearman over telnet port 4730
*
* So the only way to monitor Gearman is via doing a telnet to port 4730. The
* current monitoring supported commands are fairly basic.
* There are plans to include more set of commands in the next release.
*
* BigTent Design, Inc.
*
* @category BigTent
* @package GearmanTelnet
* @copyright Copyright (c) 2009 BigTent Design, Inc. (http://www.bigtent.com)
* @version 1.1 (Modified by Lior Ben-Kereth)
*/
/**
* A class that contains seperated and aggragated workers and status data from all gearman server in a cluster
* @author liorbk
*
*/
class GearmanClusterAdmin
{
private $accumaltiveJobs = array();
private $accumaltiveWorkers = array();
private $serversJobs = array();
private $serversWorkers = array();
private $hosts;
private $orderFunction;
/**
*
* @param array $hosts - array of host:port strings
* @param closure $orderFunction - a function that gets serversJob array return a manipulated array (for example, change job names, sort, etc)
*/
public function __construct(array $hosts, $orderFunction = null)
{
$this->hosts = $hosts;
$this->orderFunction = $orderFunction;
$this->init();
}
public function getAccumaltiveJobs() {return $this->accumaltiveJobs;}
public function getAccumaltiveWorkers() {return $this->accumaltiveWorkers;}
public function getServersJobs() {return $this->serversJobs;}
public function getServersWorkers() {return $this->serversWorkers;}
private function init()
{
// Run on all gearman servers and collect data and accumalate it
foreach ($this->hosts as $_server)
{
try
{
$gm = new GearmanHost($_server);
}
catch(Exception $ex)
{
continue;
}
$serverWorkers = $gm->getWorkers();
$serverJobs = $gm->getJobs();
if (!empty($this->orderFunction) && is_callable($this->orderFunction))
{
$serverJobs = call_user_func($this->orderFunction,$serverJobs);
}
$this->serversJobs[$_server] = $serverJobs;
$this->serversWorkers[$_server] = $serverWorkers;
foreach ($serverJobs as $jobName => $job)
{
$total = $job[GearmanHost::WORKER_TOTAL];
$running = $job[GearmanHost::WORKER_RUNNING];
$available = $job[GearmanHost::WORKER_AVAILABLE];
if (!isset($this->accumaltiveJobs[$jobName]))
{
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_TOTAL] = 0;
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_RUNNING] = 0;
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_AVAILABLE] = 0;
}
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_TOTAL] += $total;
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_RUNNING] += $running;
$this->accumaltiveJobs[$jobName][GearmanHost::WORKER_AVAILABLE] = max($this->accumaltiveJobs[$jobName][GearmanHost::WORKER_AVAILABLE], $available);
}
foreach ($serverWorkers as $type => $worker)
{
$available = $worker[GearmanHost::WORKER_TOTAL];
$running = $worker[GearmanHost::WORKER_RUNNING];
$free = $worker[GearmanHost::WORKER_AVAILABLE];
$queued = $worker[GearmanHost::WORKER_QUEUED];
if (!isset($this->accumaltiveWorkers[$type]))
{
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_TOTAL] = 0;
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_RUNNING] = 0;
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_AVAILABLE] = 0;
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_QUEUED] = 0;
}
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_TOTAL] = max($available, $this->accumaltiveWorkers[$type][GearmanHost::WORKER_TOTAL]);
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_RUNNING] += $running;
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_QUEUED] += $queued;
}
foreach ($this->accumaltiveWorkers as $type => $worker)
{
$this->accumaltiveWorkers[$type][GearmanHost::WORKER_AVAILABLE] = ($worker[GearmanHost::WORKER_TOTAL] - $worker[GearmanHost::WORKER_RUNNING]);
}
}
}
}
class GearmanHost
{
private $host;
private $port = 4730;
private $jobs = array();
private $workers = array();
private $rawStatus;
private $rawWorkers;
const WORKER_AVAILABLE = "AVAILABLE";
const WORKER_RUNNING = "RUNNING";
const WORKER_TOTAL = "TOTAL";
const WORKER_QUEUED = "QUEUED";
const FACER_WORKER = "facer";
const OTHER_WORKER = "other";
public function __construct($host, $port = null)
{
if (strstr($host,":") !== false)
{
$server = explode(":", $host);
$this->host = $server[0];
$this->port = $server[1];
}
else
{
$this->host = $host;
}
if ($port != null)
$this->port = $port;
$gearman_telnet = new GearmanTelnet($this->host, $this->port);
$this->rawStatus = $gearman_telnet->getStatus();
$this->rawStatus = explode(PHP_EOL, $this->rawStatus);
$this->rawWorkers = $gearman_telnet->getWorkers();
$this->rawWorkers = explode(PHP_EOL, $this->rawWorkers);
$this->initWorkers();
$this->initJobs();
}
public function getJobs()
{
if (empty($this->jobs) || count($this->jobs) == 0)
$this->initJobs();
return $this->jobs;
}
public function getWorkers()
{
if (empty($this->workers) || count($this->workers) == 0)
$this->initWorkers();
return $this->workers;
}
private function initJobs()
{
$this->workers = $this->getWorkers();
$status = $this->rawStatus;
for($i=0; $i<count($status); $i++)
{
@list($job, $total, $running, $available) = explode(" ", $status[$i]);
if (!empty($job))
{
$available = trim($available);
$total = trim($total);
$running = trim($running);
$this->jobs[$job] = array(
GearmanHost::WORKER_AVAILABLE => $available,
GearmanHost::WORKER_TOTAL => $total,
GearmanHost::WORKER_RUNNING=> $running
);
$workerType = self::OTHER_WORKER;
if (strpos($job, self::FACER_WORKER) !== false)
$workerType = self::FACER_WORKER;
$this->workers[$workerType][GearmanHost::WORKER_RUNNING] += $running;
$this->workers[$workerType][GearmanHost::WORKER_AVAILABLE] -= $running;
$this->workers[$workerType][GearmanHost::WORKER_QUEUED] += ($total - $running);
}
}
// print_r($this->workers);
}
private function initWorkers()
{
$workers = $this->rawWorkers;
$this->workers[self::FACER_WORKER] = array(
GearmanHost::WORKER_AVAILABLE => 0,
GearmanHost::WORKER_TOTAL => 0,
GearmanHost::WORKER_RUNNING => 0,
GearmanHost::WORKER_QUEUED => 0
);
$this->workers[self::OTHER_WORKER] = array(
GearmanHost::WORKER_AVAILABLE => 0,
GearmanHost::WORKER_TOTAL => 0,
GearmanHost::WORKER_RUNNING => 0,
GearmanHost::WORKER_QUEUED => 0
);
for($i=0; $i<count($workers); $i++)
{
@list($ip, $jobTypes) = explode(" : ", $workers[$i]);
if (!empty($jobTypes))
{
$workerType = self::OTHER_WORKER;
if (strpos($jobTypes, self::FACER_WORKER) !== false)
$workerType = self::FACER_WORKER;
$this->workers[$workerType][GearmanHost::WORKER_TOTAL] += 1;
$this->workers[$workerType][GearmanHost::WORKER_AVAILABLE] += 1;
}
}
}
}
class GearmanTelnet {
/**
* Default Values
*/
private $_gearmand = null;
private $_gearmand_host = 'localhost';
private $_gearmand_port = 4730;
private $_timeout = 3;
private $_errno = null;
private $_errstr = '';
private $_buffer = array();
public function __construct($gearmand_host = null, $gearmand_port = null, $timeout = null) {
$this->_gearmand_host = (!empty($gearmand_host)) ? $gearmand_host : $this->_gearmand_host;
$this->_gearmand_port = (!empty($gearmand_port)) ? $gearmand_port : $this->_gearmand_port;
$this->_timeout = (!empty($timeout)) ? $timeout : $this->_timeout;
$this->_connect();
}
private function sendAdminEmail()
{
}
private function _connect() {
$this->_gearmand = @fsockopen($this->_gearmand_host, $this->_gearmand_port, $this->_errno, $this->_errstr, $this->_timeout);
if(!$this->_gearmand) {
// echo "Send the email to admin";
$this->sendAdminEmail();
throw new Exception("Failed to connect to gearmand_host at " . $this->_gearmand_host);
}
}
/**
* Command: STATUS
*
* The output format of this function is tab separated columns as follows,
* below are the columns shown:
*
* - Function name : A string denoting the name of the function of the job
* - Number in queue : A positive integer indicating the total number of
* jobs for this function in the queue. This includes currently running ones
* as well (next column)
* - Number of jobs running : A positive integer showing how many jobs of
* this function are currently running
* - Number of capable workers : A positive integer denoting the maximum
* possible count of workers that could be doing this job. Though they may
* not all be working on it due to other tasks holding them busy.
*
*/
/* GEARMAN_COMMAND_GET_STATUS */
public function getStatus() {
if ($this->exec('STATUS')) {
$get_status = $this->_getBuffer();
return implode("", $get_status);
}
}
/**
* Command : Workers
*
* This command show the details of various clients registered with the
* gearmand server. For each worker it shows the following info:
*
* - Peer IP: Client remote host
* - Client ID: Unique ID assigned to client
* - Functions: List of functions this client has registered for.
*/
/* GEARMAN_COMMAND_WORK_STATUS */
public function getWorkers() {
if ($this->exec('WORKERS')) {
$get_status = $this->_getBuffer();
return implode("", $get_status);
}
}
private function _getBuffer() {
return $this->_buffer;
}
/**
* The output format of this function is tab separated columns,
* followed by a line consisting of a full stop and a newline (".\n") to
* indicate the end of output.
*
* Any other command text throws a error "ERR unknown_command Unknown+server+command"
*
*/
private function exec($command) {
$this->_buffer = array();
if($this->_gearmand) {
fputs ($this->_gearmand, $command . PHP_EOL);
while (!feof($this->_gearmand)) {
$l = fgets($this->_gearmand, 128);
if ($l === '.' . PHP_EOL) {
break;
}
else {
$this->_buffer[] = $l;
}
}
return true;
}
else {
throw new Exception("Failed to connect to gearmand_host at " . $this->_gearmand_host);
}
}
private function _disconnect() {
fclose($this->_gearmand);
}
/**
* Destructor. Cleans up socket connection and command buffer
*
* @return void
*/
public function __destruct() {
// cleanup resources
$this->_disconnect();
$this->_buffer = array();
}
}
/// startiing
//$test=new GearmanClusterAdmin(array('localhost:4730','localhost:4734'));
$test=new GearmanClusterAdmin(array('localhost:4730'));
print_r ($test->getAccumaltiveJobs());
//echo "displaying server workers ";
//print_r ($test->getAccumaltiveWorkers());
/*
echo "displaying server jobs ";
print_r ($test->getServersJobs());
echo "displaying server workers ";
print_r ($test->getServersWorkers());
*/
//print_r ($test->getServersWorkers());
//echo $test->getStatus();
?>