荟萃馆

位置:首页 > 计算机 > php语言

PHP构建监视服务的方法

php语言3.18W

PHP监视服务记录程序应该能够支持任意的服务检查(例如,HTTP和FTP服务)并且能够以任意方式(通过电子邮件,输出到一个日志文件,等等)记录事件。你当然想让它以一个守护程序方式运行;所以,你应该请求它输出其完整的当前状态。以下是小编为大家搜索整理的PHP构建监视服务的方法,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!

PHP构建监视服务的方法

一个服务需要实现下列抽象类:

abstract class ServiceCheck {

const FAILURE = 0;

const SUCCESS = 1;

protected $timeout = 30;

protected $next_attempt;

protected $current_status = ServiceCheck::SUCCESS;

protected $previous_status = ServiceCheck::SUCCESS;

protected $frequency = 30;

protected $description;

protected $consecutive_failures = 0;

protected $status_time;

protected $failure_time;

protected $loggers = array();

abstract public function __construct($params);

public function __call($name, $args)

{

if(isset($this->$name)) {

return $this->$name;

}

}

public function set_next_attempt()

{

$this->next_attempt = time() + $this->frequency;

}

public abstract function run();

public function post_run($status)

{

if($status !== $this->current_status) {

$this->previous_status = $this->current_status;

}

if($status === self::FAILURE) {

if( $this->current_status === self::FAILURE ) {

$this->consecutive_failures++;

}

else {

$this->failure_time = time();

}

}

else {

$this->consecutive_failures = 0;

}

$this->status_time = time();

$this->current_status = $status;

$this->log_service_event();

}

public function log_current_status()

{

foreach($this->loggers as $logger) {

$logger->log_current_status($this);

}

}

private function log_service_event()

{

foreach($this->loggers as $logger) {

$logger->log_service_event($this);

}

}

public function register_logger(ServiceLogger $logger)

{

$this->loggers[] = $logger;

}

}

上面的__call()重载方法提供对一个ServiceCheck对象的参数的只读存取操作:

· timeout-在引擎终止检查之前,这一检查能够挂起多长时间

· next_attempt-下次尝试连接到服务器的'时间。

· current_status-服务的当前状态:SUCCESS或FAILURE。

· previous_status-当前状态之前的状态。

· frequency-每隔多长时间检查一次服务。

· description-服务描述。

· consecutive_failures-自从上次成功以来,服务检查连续失败的次数。

· status_time-服务被检查的最后时间。

· failure_time-如果状态为FAILED,则它代表发生失败的时间。

这个类还实现了观察者模式,允许ServiceLogger类型的对象注册自身,然后当调用log_current_status()或log_service_event()时调用它。

这里实现的关键函数是run(),它负责定义应该怎样执行检查。如果检查成功,它应该返回SUCCESS;否则返回FAILURE。

标签:PHP 监视 构建 服务