-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCpuUsageController.php
69 lines (52 loc) · 1.81 KB
/
CpuUsageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require_once 'BaseController.php';
class CpuUsageController extends BaseController{
private $previousIdle;
private $previousTotal;
public function __construct(){
$this->previousIdle = -1;
$this->previousTotal = -1;
parent::__construct();
$this->type = "cpu";
}//end __construct()
public function crawlStats(){
if($this->lastUpdate == NULL || (time() - $this->lastUpdate) > 4){
$this->value = 0;
$this->stats = shell_exec("cat /proc/stat");
$this->parseValues();
return;
}//end if()
$this->value = NULL;
}//end crawlStats()
protected function parseValues(){
preg_match("/^cpu \s*([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/", $this->stats, $oldCpuValues);
preg_match("/^cpu \s*([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/", $this->stats, $newCpuValues);
$user = $oldCpuValues[1];
$nice = $oldCpuValues[2];
$sys = $oldCpuValues[3];
$idle = $oldCpuValues[4];
$ioWait = $oldCpuValues[5];
$irq = $oldCpuValues[6];
$softIrq = $oldCpuValues[7];
$steal = $oldCpuValues[8];
$guest = $newCpuValues[9];
$guestNice = $newCpuValues[10];
$total = $user + $nice + $sys + $idle + $ioWait + $irq + $softIrq + $steal + $guest + $guestNice;
if($this->previousIdle > -1 || $this->previousTotal > -1){
$idleDelta = $idle - $this->previousIdle;
$totalDelta = $total - $this->previousTotal;
$this->value = 100-(int)(($idleDelta * 100.0)/($totalDelta+0+5));
$this->value /= 8;
$this->value = $this->roundValue($this->value);
}//end if()
$this->previousIdle = $idle;
$this->previousTotal = $total;
}//end parseValues()
private function roundValue($value){
$value *= 100;
$value = (int)$value;
$value /= 100;
return $value;
}//end roundValue()
}//end class
?>