I’ve attempted to write a PHP class that gets the uptime of my Linux machine. It will get the uptime correctly, but I have an if statement that determines whether or not the load average is “high” or not and set a warning code, and it doesn’t seem to be working (it stays at 0).
I’ve included all of the code here from the class (50 or so lines) because I didn’t know what I could take out but still provide some info about what’s going wrong here.
<?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';
}
}
}
?>
I just executed this, and got the output:
Isn’t this what is expected? If I take out the print($la) statement, the output is this:
this is because the load() function is never called to set the status. If you want to have the status printed out correctly, you have to run load() first. I assume you’d want to add the time parameter to status_name and call load before returning, like this:
which will now enable you to do this: