Newer
Older
<?php
namespace App\Sensor;
use \App\AbstractSensor;
/**
* Description of MemInfo
*
* @author tibo
*/
class MemInfo extends AbstractSensor {
public function report() {
return view("agent.meminfo", ["server" => $this->getServer()]);
}
public function usedMemoryPoints() {
$records = $this->getLastRecords("memory", 288);
$used = [];
foreach ($records as $record) {
$meminfo = $this->parseMeminfo($record->memory);
$used[] = new Point(
return $used;
}
public function cachedMemoryPoints() {
$records = $this->getLastRecords("memory", 288);
$points = [];
foreach ($records as $record) {
$meminfo = $this->parseMeminfo($record->memory);
$points[] = new Point(
$record->time * 1000, $meminfo->cached / 1000);
}
return $points;
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
70
71
72
73
74
75
76
77
78
79
80
81
82
}
public function status() {
return self::STATUS_OK;
}
// used = total - free - cached
const MEMTOTAL = "/^MemTotal:\\s+([0-9]+) kB$/m";
const MEMFREE = "/^MemFree:\\s+([0-9]+) kB$/m";
const MEMCACHED = "/^Cached:\\s+([0-9]+) kB$/m";
public function parseMeminfo($string) {
return new Memory(
$this->pregMatchOne(self::MEMTOTAL, $string),
$this->pregMatchOne(self::MEMFREE, $string),
$this->pregMatchOne(self::MEMCACHED, $string));
}
public function pregMatchOne($pattern, $string) {
$matches = array();
preg_match($pattern, $string, $matches);
return $matches[1];
}
}
class Memory {
public $total;
public $free;
public $cached;
public function __construct($total, $free, $cached) {
$this->total = $total;
$this->free = $free;
$this->cached = $cached;
}
public function used() {
return $this->total - $this->free - $this->cached;
}
}