Commit a94a5fc1 authored by Tibo's avatar Tibo

Re-enable disk evolution

parent b33276b0
Pipeline #2071 failed with stage
in 39 seconds
<?php
namespace App\Sensor;
class DiskEvolution extends \App\AbstractSensor {
namespace App\Sensor;
class DiskEvolution extends \App\AbstractSensor
{
const REGEXP = "/\\n([A-z\/0-9:\\-\\.]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)%\s*([A-z\/0-9]+)/";
/**
* Compute deltas between two arrays of partitions
* @param Partition[] $newAndOld
* @return \App\Sensor\Delta[]
*/
public function computeEvolution(array $newAndOld, int $timeDifference) : array {
// calculates Deltas for remaining blocks
public function computeEvolution($newAndOld)
{
foreach ($newAndOld[0] as $key => $partition) {
$delta = new Delta();
$delta->filesystem = $partition->filesystem;
if (($partition->used - $newAndOld[1][$key]->used)== 0) {
$delta->timeUntillFull = "storage capacity seems to be constant";
} elseif (($partition->used - $newAndOld[1][$key]->used) < 0) {
$delta->timeUntillFull = "more storage left then last calculation";
} else {
$delta->delta = $partition->used - $newAndOld[1][$key]->used;
$delta->timeUntillFull = ($partition->blocks - $partition->used)/$delta->delta;
}
$delta->delta = $partition->used - $newAndOld[1][$key]->used;
$delta->timeUntillFull = ($partition->blocks - $partition->used) / $delta->delta * $timeDifference;
$deltas[] = $delta;
}
return $deltas;
return $deltas;
}
/** gets the records over a certain time
/**
* Gets the records over a certain time
* then gets the first and the last record
* and uses parse to make them partition object
*
* @param int $timeInterval in hours
*/
public function get2Partitions($timeInterval)
{
$newAndOld = [];
$records = $this->getLastRecords("disks", $timeInterval*12);
$newPartitions = $this->parse($records[0]->disks);
$oldPartitions = $this->parse($records[count($records)-1]->disks);
public function get2Partitions(int $timeInterval) {
$records = $this->getLastRecords("disks", $timeInterval * 12);
$newPartitions = Disks::parse($records[0]->disks);
$oldPartitions = Disks::parse($records[count($records) - 1]->disks);
$newAndOld = [$newPartitions, $oldPartitions];
return $newAndOld;
}
// code to print the results
public function printResults($deltas)
{
public function printResults($deltas) {
$return = "<table class='table table-sm'>";
$return .= "<tr><th>name</th><th>time untill full in hours</th></tr>";
$return .= "<tr><th>name</th><th>time untill full (h)</th></tr>";
foreach ($deltas as $delta) {
$return .= "<tr><td>". $delta->filesystem . "</td><td>" . $delta->timeUntillFull
. "</td></tr>";
$return .= "<tr>"
. "<td>" . $delta->filesystem . "</td>"
. "<td>" . $delta->timeUntillFull . "</td>"
. "</tr>";
}
$return .= "</table>";
return $return;
}
public function report()
{
return $this->printResults($this->computeEvolution($this->get2Partitions(24)));
public function report() {
return $this->printResults(
$this->computeEvolution($this->get2Partitions(24)), 24);
}
public function status()
public function status() {
$deltas = $this->computeEvolution($this->get2Partitions(24), 24);
return $this->computeStatusFromDeltas($deltas);
}
public function computeStatusFromDeltas(array $deltas)
{
$record = $this->getLastRecord("disks");
if ($record == null) {
return self::STATUS_UNKNOWN;
}
foreach ($this->computeEvolution($this->get2Partitions(24)) as $delta) {
foreach ($deltas as $delta) {
$status = self::STATUS_OK;
if ($delta->timeUntillFull == "Not enough messurements") {
$status = self::STATUS_UNKNOWN;
} elseif ($delta->timeUntillFull == "more storage left then last calculation") {
$status = self::STATUS_UNKNOWN;
} elseif ($delta->timeUntillFull < 48) {
$status = self::STATUS_WARNING;
} elseif ($delta->timeUntillFull < 96) {
$status = self::STATUS_WARNING;
}
$all_status[] = $status;
}
return max($all_status);
}
public static $skip_fs = ["none", "tmpfs", "shm"];
public function parse($string)
{
$values = array();
preg_match_all(self::REGEXP, $string, $values);
$partitions = array();
$count = count($values[1]);
for ($i = 0; $i < $count; $i++) {
$fs = $values[1][$i];
if (in_array($fs, self::$skip_fs)) {
continue;
}
$partition = new Partition();
$partition->filesystem = $fs;
$partition->blocks = $values[2][$i];
$partition->used = $values[3][$i];
$partition->mounted = $values[6][$i];
$partitions[] = $partition;
}
return $partitions;
}
}
......@@ -19,7 +19,7 @@ class Disks extends \App\AbstractSensor
return "<p>No data available...</p>";
}
$partitions = $this->parse($record->disks);
$partitions = self::parse($record->disks);
$return = "<table class='table table-sm'>";
$return .= "<tr><th></th><th></th><th>Usage</th></tr>";
foreach ($partitions as $partition) {
......@@ -39,7 +39,7 @@ class Disks extends \App\AbstractSensor
}
$all_status = [];
foreach ($this->parse($record->disks) as $partition) {
foreach (self::parse($record->disks) as $partition) {
/* @var $partition Partition */
$status = self::STATUS_OK;
if ($partition->usedPercent() > 80) {
......@@ -55,7 +55,7 @@ class Disks extends \App\AbstractSensor
public static $skip_fs = ["none", "tmpfs", "shm"];
public function parse($string)
public static function parse(string $string)
{
$values = array();
preg_match_all(self::REGEXP, $string, $values);
......
......@@ -29,7 +29,7 @@ class Server extends Model
\App\Sensor\Date::class,
\App\Sensor\ClientVersion::class,
\App\Sensor\Heartbeat::class,
// \App\Sensor\DiskEvolution::class
\App\Sensor\DiskEvolution::class
];
public function __construct(array $attributes = array())
......
<?php
namespace Tests\Unit;
use App\Sensor\DiskEvolution;
use App\Sensor\Partition;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
/**
* Description of DiskEvolutionTest
*
* @group diskevolution
* @author tibo
*/
class DiskEvolutionTest extends TestCase
{
use RefreshDatabase;
public function testSlowEvolution()
{
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 10;
$oldPartitions = array($p);
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 11;
$newPartitions = array($p);
$newAndOld = array($newPartitions, $oldPartitions);
$sensor = new DiskEvolution(new \App\Server());
$partitions = $sensor->computeEvolution($newAndOld, 24);
// test the result is correct...
$this->assertEquals(1, $partitions[0]->delta);
$this->assertEquals("test", $partitions[0]->filesystem);
$this->assertEquals(216, $partitions[0]->timeUntillFull);
$this->assertEquals(0, $sensor->computeStatusFromDeltas($partitions));
}
public function testQuickFull()
{
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 10;
$oldPartitions = array($p);
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 15;
$newPartitions = array($p);
$newAndOld = array($newPartitions, $oldPartitions);
$sensor = new DiskEvolution(new \App\Server());
$partitions = $sensor->computeEvolution($newAndOld, 24);
// test the result is correct...
$this->assertEquals(5, $partitions[0]->delta);
$this->assertEquals("test", $partitions[0]->filesystem);
$this->assertEquals(24, $partitions[0]->timeUntillFull);
$this->assertEquals(10, $sensor->computeStatusFromDeltas($partitions));
}
}
......@@ -9,7 +9,6 @@ use App\Server;
use App\Sensor\Disks;
use App\Sensor\Ifconfig;
use App\Sensor\Partition;
use App\Sensor\DiskEvolution;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
......@@ -71,46 +70,6 @@ class ExampleTest extends TestCase
$this->assertEquals(2074977132, $interfaces[1]->tx);
}
public function testDiskEvolution()
{
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 10;
$p2 = new Partition();
$p2->filesystem = "test";
$p2->blocks = 15;
$p2->used = 5;
$oldPartitions = array($p, $p2);
$p = new Partition();
$p->filesystem = "test";
$p->blocks = 20;
$p->used = 15;
$p2 = new Partition();
$p2->filesystem = "test";
$p2->blocks = 15;
$p2->used = 10;
$newPartitions = array($p, $p2);
$newAndOld = array($newPartitions, $oldPartitions);
$sensor = new DiskEvolution(new \App\Server());
$result = $sensor->computeEvolution($newAndOld);
// test the result is correct...
$this->assertequals(5, $result[0]->delta);
$this->assertequals(5, $result[1]->delta);
$this->assertequals("test", $result[0]->filesystem);
$this->assertequals("test", $result[1]->filesystem);
$this->assertequals(((20-15)/5), $result[0]->timeUntillFull);
$this->assertequals(((15-10)/5), $result[1]->timeUntillFull);
}
public function testDisksSensor()
{
$string = file_get_contents(__DIR__ . "/df");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment