2010-01-30 120 views
2

我试图写一个PHP类,得到我的Linux机器的正常运行时间。它得到正确的运行时间,但我有一个if声明,确定平均负载是否为“高”与否并设置警告代码,它似乎并没有工作(它停留在0) 。PHP类变量设置/获取问题

我已经包括了所有在这里的代码从类(50行左右),因为我不知道我会采取什么样的了,但还是提供了这是怎么回事错在这里的一些信息。

<?php 
class loadavg { 

    private $divisor, $status; 

    public function __construct($set_divisor = 1){ 
     $this->divisor = $set_divisor; 
    } 

    public function __toString(){ 
     return $this->load(1).', '.$this->load(5).', '.$this->load(15)."\n"; 
    } 

    public function load($time = 1){ 
     $loadfile = shell_exec('cat /proc/loadavg'); 
     $split = preg_split('/ /', $loadfile); 

     if ($split[1] > (2 * $this->divisor)){ 
      $this->status = 3; 
     } else if ($split[1] > $this->divisor){ 
      $this->status = 2; 
     } else { 
      $this->status = 1; 
     } 

     switch($time){ 
      case 1: 
       return $split[0]; 
      case 5: 
       return $split[1]; 
      case 15: 
      return $split[2]; 
     } 
    } 

    public function status_name(){ 
     switch ($this->status){ 
      case 3: 
       return 'critical'; 
      case 2: 
       return 'warn'; 
      case 1: 
       return 'ok'; 
      case 0: 
       return 'error'; 
     } 
    } 
} 
?> 
+0

发布的输出' print_r($ split)' – Pentium10 2010-01-30 20:53:29

+0

你在调用status_name之前调用load吗? – Pentium10 2010-01-30 20:55:30

回答

1
<? 
class loadavg { 

    private $divisor, $status; 

    public function __construct($set_divisor = 1){ 
     $this->divisor = $set_divisor; 
    } 

    public function __toString(){ 
     return $this->load(1).', '.$this->load(5).', '.$this->load(15)."\n"; 
    } 

    public function load($time = 1){ 
     //$loadfile = shell_exec('cat /proc/loadavg'); 
     $loadfile = '1.5 1.5 1.5'; 
     $split = preg_split('/ /', $loadfile); 

     if ($split[1] > (2 * $this->divisor)){ 
      $this->status = 3; 
     } else if ($split[1] > $this->divisor){ 
      $this->status = 2; 
     } else { 
      $this->status = 1; 
     } 

     switch($time){ 
      case 1: 
       return $split[0]; 
      case 5: 
       return $split[1]; 
      case 15: 
      return $split[2]; 
     } 
    } 

    public function status_name(){ 
     switch ($this->status){ 
      case 3: 
       return 'critical'; 
      case 2: 
       return 'warn'; 
      case 1: 
       return 'ok'; 
      case 0: 
       return 'error'; 
     } 
    } 
} 

$la = new loadavg(); 
print($la); 
print($la->status_name()); 

我只是执行这一点,并得到了输出:

1.5, 1.5, 1.5 
warn 

这不是什么预期?如果我拿出打印($ LA)声明,输出是这样的:

error 

这是因为load()函数没有被调用设置状态。如果您想要正确打印状态,则必须先运行load()。我假设你要添加的时间参数status_name并在返回之前调用负载,就像这样:

public function status_name($time =1){ 
    $this->load($time); 
    switch ($this->status){ 
     case 3: 
      return 'critical'; 
     case 2: 
      return 'warn'; 
     case 1: 
      return 'ok'; 
     case 0: 
      return 'error'; 
    } 
} 

现在将使你能够做到这一点:

$la = new loadavg(); 
print($la->status_name(5)); 
+0

我看到,load()需要在status_name()内运行才能工作。谢谢。 – ZeroUptime 2010-01-30 21:23:38

1

您的代码似乎做工精细,虽然设计使得它的使用不太直观,它可能是。我把你的类定义放在一个文件中,在底部添加下面几行,并在运行时打印出'ok'。

$load = new loadavg(); 
$load->load (5); 
echo $load->status_name() . "\n"; 

你的类的设计的奇怪的事情,对我来说,它不会允许“装”在初始化,但是当你尝试打印loadavg对象只自动调用负载。如果您希望能够访问status_name()而不明确调用load(),你可以调用__construct负载(或许还有一个合理的默认是不能修改的,就像除数...)