Skip to content

Commit

Permalink
DiskUsage Check
Browse files Browse the repository at this point in the history
  • Loading branch information
svilborg committed Jul 17, 2019
1 parent e9a001f commit bd40d7d
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/Checks/Filesystem/DiskSpace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

use Health\Checks\BaseCheck;
use Health\Checks\HealthCheckInterface;
use Health\Checks\Traits\FormatTrait;

class DiskSpace extends BaseCheck implements HealthCheckInterface
{

use FormatTrait;

/**
* Default disk space threshold of 100 MB
*
Expand Down Expand Up @@ -48,29 +51,4 @@ public function call()

return $builder->build();
}

/**
* Format bytes to kb, mb, gb, tb
*
* @param integer $size
* @param integer $precision
* @return string
*/
private function formatBytes($size, $precision = 2)
{
$size = (int) $size;
$base = $size > 0 ? log($size) / log(1024) : 0;

$suffixes = [
' bytes',
' KB',
' MB',
' TB',
' GB'
];

$value = $size > 0 ? round(pow(1024, $base - floor($base)), $precision) : 0;

return $value . $suffixes[floor($base)];
}
}
63 changes: 63 additions & 0 deletions src/Checks/Filesystem/DiskUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Health\Checks\Filesystem;

use Health\Checks\BaseCheck;
use Health\Checks\HealthCheckInterface;
use Health\Checks\Traits\FormatTrait;

class DiskUsage extends BaseCheck implements HealthCheckInterface
{

use FormatTrait;

/**
* Default disk usage threshold of 1 %
*
* @var integer
*/
const DEFAULT_THRESHOLD = 1;

/**
* Default Path
*
* @var string
*/
const DEFAULT_PATH = '/';

/**
*
* {@inheritdoc}
* @see \Health\Checks\HealthCheckInterface::call()
*/
public function call()
{
$builder = $this->getBuilder();

$path = $this->getParam('path', self::DEFAULT_PATH);
$threshold = $this->getParam('threshold', self::DEFAULT_THRESHOLD);

if ($threshold > 100 || $threshold < 0) {
return $builder->down()->withData('error', 'Invalid Threshold - ' . $threshold);
}

$free = disk_free_space($path);
$total = disk_total_space($path);
$usage = $total - $free;
$percentage = ($usage / $total) * 100;

if ($percentage >= $threshold) {
$builder->up();
} else {
$builder->down();
}

$builder->withData('free_bytes', $free)
->withData('free_human', $this->formatBytes($free))
->withData('usage', $usage)
->withData('usage_human', $this->formatBytes($usage))
->withData('path', $path)
->withData('threshold', $threshold);

return $builder->build();
}
}
31 changes: 31 additions & 0 deletions src/Checks/Traits/FormatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Health\Checks\Traits;

trait FormatTrait
{

/**
* Format bytes to kb, mb, gb, tb
*
* @param integer $size
* @param integer $precision
* @return string
*/
public function formatBytes($size, $precision = 2)
{
$size = (int) $size;
$base = $size > 0 ? log($size) / log(1024) : 0;

$suffixes = [
' bytes',
' KB',
' MB',
' TB',
' GB'
];

$value = $size > 0 ? round(pow(1024, $base - floor($base)), $precision) : 0;

return $value . $suffixes[floor($base)];
}
}
1 change: 1 addition & 0 deletions tests/Checks/CheckTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CheckTestCase extends \Orchestra\Testbench\TestCase
protected function assertCheck($health, string $state = 'UP')
{
// dump($health);

$this->assertInstanceOf(HealthCheck::class, $health);
$this->assertNotEmpty($health->getName());
$this->assertEquals($state, $health->getState());
Expand Down
21 changes: 21 additions & 0 deletions tests/Checks/DiskUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Tests\Checks;

use Health\Checks\Filesystem\DiskUsage;

class DiskUsageTest extends CheckTestCase
{

public function testCheckUp()
{
$this->assertCheck($this->runCheck(DiskUsage::class, [
'path' => '/',
'threshold' => 1
]), 'UP');

$this->assertCheck($this->runCheck(DiskUsage::class, [
'path' => '/',
'threshold' => 100
]), 'DOWN');
}
}

0 comments on commit bd40d7d

Please sign in to comment.