Any way to access Gearman administration? Any way to access Gearman administration? php php

Any way to access Gearman administration?


class Waps_Gearman_Server {    /**     * @var string     */    protected $host = "127.0.0.1";    /**     * @var int     */    protected $port = 4730;    /**     * @param string $host     * @param int $port     */    public function __construct($host=null,$port=null){        if( !is_null($host) ){            $this->host = $host;        }        if( !is_null($port) ){            $this->port = $port;        }    }    /**     * @return array | null     */    public function getStatus(){        $status = null;        $handle = fsockopen($this->host,$this->port,$errorNumber,$errorString,30);        if($handle!=null){            fwrite($handle,"status\n");            while (!feof($handle)) {                $line = fgets($handle, 4096);                if( $line==".\n"){                    break;                }                if( preg_match("~^(.*)[ \t](\d+)[ \t](\d+)[ \t](\d+)~",$line,$matches) ){                    $function = $matches[1];                    $status['operations'][$function] = array(                        'function' => $function,                        'total' => $matches[2],                        'running' => $matches[3],                        'connectedWorkers' => $matches[4],                    );                }            }            fwrite($handle,"workers\n");            while (!feof($handle)) {                $line = fgets($handle, 4096);                if( $line==".\n"){                    break;                }                // FD IP-ADDRESS CLIENT-ID : FUNCTION                if( preg_match("~^(\d+)[ \t](.*?)[ \t](.*?) : ?(.*)~",$line,$matches) ){                    $fd = $matches[1];                    $status['connections'][$fd] = array(                        'fd' => $fd,                        'ip' => $matches[2],                        'id' => $matches[3],                        'function' => $matches[4],                    );                }            }            fclose($handle);        }        return $status;    }}


For quick checking, I use this bash one-liner:

# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730

This opens a connection to a gearman instance running on localhost, and sends the "status" query. This contains the name and number of jobs on that instance. The information can then be processed with grep/awk/wc etc. for reporting and alerting.

I also do the same with the "workers" query which shows all connected workers.

# (echo workers ; sleep 0.1) | netcat 127.0.0.1 4730

The sleep is to keep the connection open long enough for the reply.

The full list of administrative commands, and what the output means is at http://gearman.org/index.php?id=protocol Just search for "Administrative Protocol"


To expand on d5ve's answer, since netcat will sit and wait on the socket, you can add a -w parameter with a maximum number of seconds to run. So if you're querying localhost:

# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1

... otherwise you never get back to a command prompt.