Site Reliability Team Lead at News UK

View on GitHub
26 September 2011

Gearman Admin Interface

by Mike

I have recently been playing with gearman for a big project for one of our clients here at Sysdom. Getting it up and running was super easy, but we needed to integrate monitoring into our admin pages, so here is a little piece of code to get you up and running…


echo "Queues\n";

$service_port = 4730;

//$address = gethostbyname('www.example.com');

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

//echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";

$in = "status\n";
$out = '';

socket_write($socket, $in, strlen($in));

while ($out = socket_read($socket, 2048,PHP_NORMAL_READ)) {
    echo "$out";
    if ($out==".\n") break;


Yes, it is a quick and dirty proof of concept, but I hope this will help other people with monitoring gearman in their own apps.

Output is pretty simple:

[email protected]:~# php gmstatus.php
test    1    0    0
reverse    0    0    1

with the function, total jobs in queue, running jobs and available workers seperated by tabs.

Note: yes, I know there is the PEAR library for doing the same, but adding it when you are using the PECL extension is an extra dependency that we’d rather not have.

The gearman admin protocol is available here, but I’ll repost the snippet so people have an easy reference to it.

Administrative Protocol

The Gearman job server also supports a text-based protocol to pull
information and run some administrative tasks. This runs on the same
port as the binary protocol, and the server differentiates between
the two by looking at the first character. If it is a NULL (\0),
then it is binary, if it is non-NULL, that it attempts to parse it
as a text command. The following commands are supported:


This sends back a list of all workers, their file descriptors,
their IPs, their IDs, and a list of registered functions they can
perform. The list is terminated with a line containing a single
'.' (period). The format is:


- None.


This sends back a list of all registered functions. Next to
each function is the number of jobs in the queue, the number of
running jobs, and the number of capable workers. The columns are
tab separated, and the list is terminated with a line containing
a single '.' (period). The format is:


- None.


This sets the maximum queue size for a function. If no size is
given, the default is used. If the size is negative, then the queue
is set to be unlimited. This sends back a single line with "OK".

- Function name.
- Optional maximum queue size.


Shutdown the server. If the optional "graceful" argument is used,
close the listening socket and let all existing connections

- Optional "graceful" mode.


Send back the version of the server.

- None.

The Perl version also has a 'gladiator' command that uses the
'Devel::Gladiator' Perl module and is used for debugging.

I hope someone finds this useful and feel free to send any comments to mike at technomonk dot com