This is a php class that connects to a running gearmand process and gets the ‘status’ and and list of ‘workers’. The response can be had as the raw response $gmAdmin->getStatusRaw() or as a structured array $gmAdmin->getStatus().
Usage and Examples
require('gearmandAdmin.php');
//Create object from class sending host and port to connect on.
$gmAdmin = new gearmandAdmin( '127.0.0.1', 4730 );
// Outputs raw response from 'status' command
echo $gmAdmin->getStatusRaw();
//Example output from $gmAdmin->getStatusRaw();
ping 0 0 4
traceroute 0 0 4
whois 0 0 4
dig 0 0 4
// Outputs raw response from 'workers' command
echo $gmAdmin->getWorkersRaw();
//Example output from $gmAdmin->getWorkersRaw();
15 ::3875:6100:0:0%6386984 - :
14 ::3875:6100:0:0%6386984 - : ping traceroute whois dig
13 ::3875:6100:0:0%6386984 - : ping traceroute whois dig
12 ::3875:6100:0:0%6386984 - : ping traceroute whois dig
11 ::3875:6100:0:0%6386984 - : ping traceroute whois dig
// Returns array of status
print_r( $gmAdmin->getStatus() );
//Example output from $gmAdmin->getStatus();
Array
(
[0] => Array
(
[function] => ping
[total] => 0
[running] => 0
[workers] => 4
)
[1] => Array
(
[function] => traceroute
[total] => 0
[running] => 0
[workers] => 4
)
[2] => Array
(
[function] => whois
[total] => 0
[running] => 0
[workers] => 4
)
[3] => Array
(
[function] => dig
[total] => 0
[running] => 0
[workers] => 4
)
)
// Returns array of workers
print_r( $gmAdmin->getWorkers() );
// Example out from $gmAdmin->getWorkers()
Array
(
[0] => Array
(
[descriptor] => 15
[ip] => ::3875:6100:0:0%6386984
[clientid] => -
)
[1] => Array
(
[descriptor] => 14
[ip] => ::3875:6100:0:0%6386984
[clientid] => -
[functions] => Array
(
[0] => ping
[1] => traceroute
[2] => whois
[3] => dig
)
)
[2] => Array
(
[descriptor] => 13
[ip] => ::3875:6100:0:0%6386984
[clientid] => -
[functions] => Array
(
[0] => ping
[1] => traceroute
[2] => whois
[3] => dig
)
)
[3] => Array
(
[descriptor] => 12
[ip] => ::3875:6100:0:0%6386984
[clientid] => -
[functions] => Array
(
[0] => ping
[1] => traceroute
[2] => whois
[3] => dig
)
)
[4] => Array
(
[descriptor] => 11
[ip] => ::3875:6100:0:0%6386984
[clientid] => -
[functions] => Array
(
[0] => ping
[1] => traceroute
[2] => whois
[3] => dig
)
)
)
//Disconnect the TCP socket connection
$gmAdmin->disconnect();
The gearmandAdmin.php class
<?php
/**
*
* A PHP class to interface with gearmand's admin interface
* This is a programable alternative to telneting to the
* gearmand process (default port TCP 4730) and issuing the
* commands 'status' and 'workers'
* It is assumed that you have gearmand installed and running
* as well, that you have the gearman pecl module install and enabled in
* php.ini
*
* @license: GPLv3
* @author: Jonathan Cutrer
* @website: http://www.pronique.com
*
* If you find this tool useful consider linking to http:/www.pronique.com/
* Or donate $5 via paypal, http://www.pronique.com/donate
*
*
* Reference: http://gearman.org/index.php?id=protocol
*
*/
class gearmandAdmin {
private $socketHandle;
private $host = '127.0.0.1';
private $port = '4730';
private $timeout = '5';
function __construct( $host=null, $port=null, $timeout=null ) {
if ( $host ) { $this->host = $host;}
if ( $port ) { $this->port = $port;}
if ( $timeout ) { $this->timeout = $timeout;}
$this->connect();
}
/**
* returns array of status
*
*/
function getStatus() {
$response = $this->getStatusRaw();
//TODO build $response in a structured array
$count = 0;
$lines = explode("\n", $response );
foreach( $lines as $line ) {
if ( $line =='.' ) { break; }
$parts = explode("\t", $line);
$status[$count]['function'] = $parts[0];
$status[$count]['total'] = $parts[1];
$status[$count]['running'] = $parts[2];
$status[$count]['workers'] = $parts[3];
$count++;
}
return $status;
}
/**
* send the 'status' command to the server and return the raw response
*
*/
function getStatusRaw() {
return $this->send('status');
}
/**
* return array of workers
*
*/
function getWorkers() {
$response = $this->getWorkersRaw();
$lines = explode("\n", $response );
//TODO build $response in a structured array
$count = 0;
foreach( $lines as $line ) {
if ( $line =='.' ) { break; }
$parts = explode(" ", $line);
$workers[$count]['descriptor'] = $parts[0];
$workers[$count]['ip'] = $parts[1];
$workers[$count]['clientid'] = $parts[2];
$func_marker = false;
foreach( $parts as $part) {
if ( $func_marker == true ) {
$workers[$count]['functions'][] = $part;
}
if ($part == ':') { $func_marker = 1; }
}
$count++;
}
return $workers;
}
/**
* send the 'workers' command to the server and return the raw response
*
*/
function getWorkersRaw() {
return $this->send('workers');
}
/**
* connect to gearmand using tcp
*
*/
function connect() {
try {
$this->socketHandle = fsockopen( $this->host, $this->port, $errno, $errstr, $this->timeout);
} catch (Exception $e) {
echo "Could not connect to gearmand server\n";
echo "$errstr ($errno)\n";
}
}
/**
* disconnect from gearmand
*
*/
function disconnect() {
fclose( $this->socketHandle );
}
/**
* send a command to the socket
*
*/
function send( $cmd ) {
$data = '';
fwrite($this->socketHandle, $cmd . "\r\n");
while (!feof($this->socketHandle)) {
$data .= fgets($this->socketHandle, 1024);
if ( preg_match("/\n\.$/i", $data ) ) { break; }
}
return $data;
}
}
?>
gearman is a distributed job server consisting of a manager, workers, and clients. Learn more about it at http://gearman.org/. This code is released under the GPLv3 license, feel free to use it in your application. Drop me a line and tell me how and where you are using it.
Keywords:
- gearman cakephp
- cakephp gearman
- mapreduce articles
- hosting intext:gearmand intext:paypal
- gearmand tcp
- gearmand status
- gearmand how to get status example
- gearmand
- gearman exemple php
- traceroute using socket php