Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions lib/OperatingSystems/DefaultOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,47 @@ public function getNetworkInterfaces() {
}

/**
* Get diskInfo will return a list of disks. Used and Available in bytes.
*
* [
* [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
* ]
*
* @return array
*/
public function getDiskInfo() {
$blacklist = ['', 'Type', 'tmpfs', 'devtmpfs'];
$data = shell_exec('df -TP');
$lines = preg_split('/[\r\n]+/', $data);

foreach ($lines as $line) {
$entry = preg_split('/\s+/', trim($line));
if (isset($entry[1]) && !in_array($entry[1], $blacklist)) {
$items = [];
$items['device'] = $entry[0];
$items['fs'] = $entry[1];
$items['used'] = $entry[3] * 1024;
$items['available'] = $entry[4] * 1024;
$items['percent'] = $entry[5];
$items['mount'] = $entry[6];
$result[] = $items;
public function getDiskInfo(): array {
$data = [];

try {
$disks = $this->executeCommand('df -TP');
} catch (\RuntimeException $e) {
return $data;
}

$matches = [];
$pattern = '/^(?<Filesystem>[\w\/-]+)\s*(?<Type>\w+)\s*(?<Blocks>\d+)\s*(?<Used>\d+)\s*(?<Available>\d+)\s*(?<Capacity>\d+%)\s*(?<Mounted>[\w\/-]+)$/m';

$result = preg_match_all($pattern, $disks, $matches);
if ($result === 0 || $result === false) {
return $data;
}

foreach ($matches['Filesystem'] as $i => $filesystem) {
if (in_array($matches['Type'][$i], ['tmpfs', 'devtmpfs'], false)) {
continue;
}

$data[] = [
'device' => $filesystem,
'fs' => $matches['Type'][$i],
'used' => (int)$matches['Used'][$i] * 1024,
'available' => (int)$matches['Available'][$i] * 1024,
'percent' => $matches['Capacity'][$i],
'mount' => $matches['Mounted'][$i],
];
}
return $result;

return $data;
}

protected function readContent(string $filename): string {
Expand All @@ -239,4 +259,12 @@ protected function readContent(string $filename): string {
}
return '';
}

protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === null || $output === '') {
throw new \RuntimeException('No output for command: "' . $command . '"');
}
return $output;
}
}
13 changes: 9 additions & 4 deletions lib/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@ public function getTimeServers() {
}

/**
* @return string
* Get diskInfo will return a list of disks. Used and Available in bytes.
*
* [
* [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
* ]
*
* @return array
*/
public function getDiskInfo() {
$data = $this->backend->getDiskInfo();
return $data;
public function getDiskInfo(): array {
return $this->backend->getDiskInfo();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/data/df_tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Filesystem Type 1024-blocks Used Available Capacity Mounted on
udev devtmpfs 4052408 0 4052408 0% /dev
tmpfs tmpfs 816800 7592 809208 1% /run
/dev/mapper/homestead--vg-root ext4 56422560 6205468 47321220 12% /
tmpfs tmpfs 4084000 8 4083992 1% /dev/shm
tmpfs tmpfs 5120 0 5120 0% /run/lock
tmpfs tmpfs 4084000 0 4084000 0% /sys/fs/cgroup
/dev/mapper/homestead--vg-mysql--master ext4 65531436 257212 61902400 1% /homestead-vg/master
vagrant vboxsf 958123168 614831132 343292036 65% /vagrant
home_vagrant_code vboxsf 958123168 614831132 343292036 65% /home/vagrant/code
tmpfs tmpfs 816800 0 816800 0% /run/user/1000
61 changes: 60 additions & 1 deletion tests/lib/DefaultOsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUp(): void {
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->setMethods(['readContent'])
->setMethods(['readContent', 'executeCommand'])
->getMock();
}

Expand Down Expand Up @@ -85,4 +85,63 @@ public function testGetUptime(): void {
$this->assertEquals(13278, $this->os->getUptime());
}

public function testGetDiskInfo(): void {
$this->os->method('executeCommand')
->with('df -TP')
->willReturn(file_get_contents(__DIR__ . '/../data/df_tp'));

$disks = [
[
'device' => '/dev/mapper/homestead--vg-root',
'fs' => 'ext4',
'used' => 6354399232,
'available' => 48456929280,
'percent' => '12%',
'mount' => '/',
],
[
'device' => '/dev/mapper/homestead--vg-mysql--master',
'fs' => 'ext4',
'used' => 263385088,
'available' => 63388057600,
'percent' => '1%',
'mount' => '/homestead-vg/master',
],
[
'device' => 'vagrant',
'fs' => 'vboxsf',
'used' => 629587079168,
'available' => 351531044864,
'percent' => '65%',
'mount' => '/vagrant',
],
[
'device' => 'home_vagrant_code',
'fs' => 'vboxsf',
'used' => 629587079168,
'available' => 351531044864,
'percent' => '65%',
'mount' => '/home/vagrant/code',
],
];

$this->assertSame($disks, $this->os->getDiskInfo());
}

public function testGetDiskInfoNoCommandOutput(): void {
$this->os->method('executeCommand')
->with('df -TP')
->willThrowException(new \RuntimeException('No output for command "df -TP"'));

$this->assertSame([], $this->os->getDiskInfo());
}

public function testGetDiskInfoInvalidCommandOutput(): void {
$this->os->method('executeCommand')
->with('df -TP')
->willReturn('invalid_data');

$this->assertSame([], $this->os->getDiskInfo());
}

}