Skip to content
Snippets Groups Projects
Commit 35ccf9c1 authored by Tibo's avatar Tibo
Browse files

Show graph of cpu load

parent c400d782
No related branches found
No related tags found
No related merge requests found
......@@ -34,4 +34,11 @@ abstract class AbstractSensor implements Sensor {
$field => ['$ne' => null]],
["sort" => ["_id" => -1]]);
}
function getLastRecords($field, $count) {
return \Mongo::get()->monitoring->records->find(
[ "server_id" => $this->server->id,
$field => ['$ne' => null]],
["limit" => $count, "sort" => ["_id" => -1]]);
}
}
......@@ -4,6 +4,17 @@ namespace App\Sensor;
use \App\AbstractSensor;
class Point {
public $t = 0;
public $y = 0;
public function __construct($t, $y) {
$this->t = $t;
$this->y = $y;
}
}
/**
* Description of LoadAvg
*
......@@ -13,8 +24,53 @@ class LoadAvg extends AbstractSensor {
public function report() {
return "<p>Current load: " . $this->getLastValue() . "</p>";
$records = $this->getLastRecords("loadavg", 288);
$points = [];
foreach ($records as $record) {
$points[] = new Point($record->time * 1000, $this->parse($record->loadavg));
}
return "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js\"></script>"
. "<p>Current load: " . $this->getLastValue() . "</p>"
. "<canvas id=\"myChart\" width='400' height='300'></canvas>"
. "<script>"
. "var ctx = document.getElementById('myChart').getContext('2d');"
. "var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Load',
data: " . json_encode($points) . ",
}]
},
options: {
legend: {
display: false,
},
scales: {
xAxes: [{
type: 'time',
display: true,
scaleLabel: {
display: true,
labelString: 'Time'
}
}],
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});"
. "</script>";
}
public function status() {
......@@ -33,4 +89,8 @@ class LoadAvg extends AbstractSensor {
function parse($string) {
return current(explode(" ", $string));
}
public function getLastValues() {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment