I want to be able to query a gearman server to determine how many instances of a worker I have running (basically I want to make sure that RunTaskA
is available and RunTaskB
is available if there are no workers handling those tasks, I want to be able to send an alert out.
我想查询一个gearman服务器来确定我有多少工人的实例运行(基本上我想确保RunTaskA和RunTaskB可用时如果没有工人处理这些任务,我希望能够发送警报。
Is there any way to do this?
有什么办法吗?
Also: Mad props if you know of a PHP way to query the gearman server.
另外:如果您知道一种PHP的查询gearman服务器的方法,那么就需要使用Mad道具。
Edit: I know about the PHP gearman extension that is available natively, but I am not looking for a task submission extension, I need something that allows me to query the gearman server and see how many workers are serving a specific task.
编辑:我知道PHP gearman扩展是本地可用的,但是我并不是在寻找任务提交扩展,我需要一些允许我查询gearman服务器并查看有多少工作人员在为特定任务服务的东西。
9 个解决方案
#1
34
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;
}
}
#2
25
For quick checking, I use this bash one-liner:
为了快速检查,我使用了bash一行程序:
# (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.
这将打开运行在本地主机上的gearman实例的连接,并发送“status”查询。它包含该实例上的作业的名称和数量。然后可以使用grep/awk/wc等处理这些信息,以进行报告和警报。
I also do the same with the "workers" query which shows all connected workers.
我还对“workers”查询做了相同的处理,该查询显示所有连接的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"
管理命令的完整列表,以及输出的含义在http://gearman.org/index.php?id=协议搜索"管理协议"
#3
4
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:
要扩展d5ve的答案,因为netcat将坐在套接字上等待,您可以添加一个-w参数,最大运行时间为秒。如果你在查询localhost:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1
... otherwise you never get back to a command prompt.
…否则永远无法返回命令提示符。
#4
1
As far as i know there are no such extension in gearman, Managing and monitoring worker script is your responsibility you can try one of these for this purpose -
就我所知,gearman中没有这样的扩展,管理和监视worker脚本是您的职责,您可以为此目的尝试其中之一——
Supervisord is a python appliation for running application in background and monitoring them.
monitor sord是用于在后台运行和监视应用程序的python应用程序。
OR you can use brian moon's gearman manager
或者你可以使用brian moon的gearman manager
#7
0
Stumbled on it today, haven't tested it myself but it looks promising.
今天偶然发现它,我还没有亲自测试过,但是看起来很有希望。
https://github.com/yugene/Gearman-Monitor
https://github.com/yugene/Gearman-Monitor
#8
0
In Python you could do the following:
在Python中,您可以执行以下操作:
import gearman
admin_client = gearman.GearmanAdminClient(['127.0.0.1:4730',])
status = admin_client.get_status()
for w in status:
if w["task"] == "YOUR_TASK_NAME":
print(w)
Note: you have to install package named "gearman" using pip or easy_install to avoid any exceptions running the above code.
注意:您必须使用pip或easy_install安装名为“gearman”的包,以避免运行上述代码的任何异常。
Also, Check the following admin clients that is simplifying administering gearman in general.
此外,请检查以下管理客户端,这些客户端通常简化了管理gearman。
- Monitoring and Administerating Gearman servers easily from a Django based interface
- 可以很容易地从Django的接口监视和管理Gearman服务器。
- Monitoring tool for Gearman servers
- 变速箱服务器的监控工具
- PHP daemon for managing gearman workers
- 用于管理gearman worker的PHP守护进程
- PHP monitoring dashboard for Gearman Job Servers
- 用于Gearman作业服务器的PHP监控仪表板
- http server that sits on top of gearman
- 位于gearman之上的http服务器
#9
0
When everything else fails, you can use the gearadmin
tool found in the gearman-tools
package in Ubuntu by calling exec()
to execute it in the new process. Here is a reference to its output format.
当其他一切都失败时,您可以使用在Ubuntu的gearman-tools包中找到的gearadmin工具,通过调用exec()在新的进程中执行它。这里是对其输出格式的引用。
This assumes PHP and Gearman are running on the same server.
假设PHP和Gearman在同一台服务器上运行。
#1
34
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;
}
}
#2
25
For quick checking, I use this bash one-liner:
为了快速检查,我使用了bash一行程序:
# (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.
这将打开运行在本地主机上的gearman实例的连接,并发送“status”查询。它包含该实例上的作业的名称和数量。然后可以使用grep/awk/wc等处理这些信息,以进行报告和警报。
I also do the same with the "workers" query which shows all connected workers.
我还对“workers”查询做了相同的处理,该查询显示所有连接的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"
管理命令的完整列表,以及输出的含义在http://gearman.org/index.php?id=协议搜索"管理协议"
#3
4
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:
要扩展d5ve的答案,因为netcat将坐在套接字上等待,您可以添加一个-w参数,最大运行时间为秒。如果你在查询localhost:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1
... otherwise you never get back to a command prompt.
…否则永远无法返回命令提示符。
#4
1
As far as i know there are no such extension in gearman, Managing and monitoring worker script is your responsibility you can try one of these for this purpose -
就我所知,gearman中没有这样的扩展,管理和监视worker脚本是您的职责,您可以为此目的尝试其中之一——
Supervisord is a python appliation for running application in background and monitoring them.
monitor sord是用于在后台运行和监视应用程序的python应用程序。
OR you can use brian moon's gearman manager
或者你可以使用brian moon的gearman manager
#5
#6
#7
0
Stumbled on it today, haven't tested it myself but it looks promising.
今天偶然发现它,我还没有亲自测试过,但是看起来很有希望。
https://github.com/yugene/Gearman-Monitor
https://github.com/yugene/Gearman-Monitor
#8
0
In Python you could do the following:
在Python中,您可以执行以下操作:
import gearman
admin_client = gearman.GearmanAdminClient(['127.0.0.1:4730',])
status = admin_client.get_status()
for w in status:
if w["task"] == "YOUR_TASK_NAME":
print(w)
Note: you have to install package named "gearman" using pip or easy_install to avoid any exceptions running the above code.
注意:您必须使用pip或easy_install安装名为“gearman”的包,以避免运行上述代码的任何异常。
Also, Check the following admin clients that is simplifying administering gearman in general.
此外,请检查以下管理客户端,这些客户端通常简化了管理gearman。
- Monitoring and Administerating Gearman servers easily from a Django based interface
- 可以很容易地从Django的接口监视和管理Gearman服务器。
- Monitoring tool for Gearman servers
- 变速箱服务器的监控工具
- PHP daemon for managing gearman workers
- 用于管理gearman worker的PHP守护进程
- PHP monitoring dashboard for Gearman Job Servers
- 用于Gearman作业服务器的PHP监控仪表板
- http server that sits on top of gearman
- 位于gearman之上的http服务器
#9
0
When everything else fails, you can use the gearadmin
tool found in the gearman-tools
package in Ubuntu by calling exec()
to execute it in the new process. Here is a reference to its output format.
当其他一切都失败时,您可以使用在Ubuntu的gearman-tools包中找到的gearadmin工具,通过调用exec()在新的进程中执行它。这里是对其输出格式的引用。
This assumes PHP and Gearman are running on the same server.
假设PHP和Gearman在同一台服务器上运行。