1 | <?php |
||
2 | |||
3 | namespace Startwind\Inventorio\Exec; |
||
4 | |||
5 | class System |
||
6 | { |
||
7 | private static ?System $instance = null; |
||
8 | |||
9 | public static function getInstance(): System |
||
10 | { |
||
11 | if (self::$instance === null) { |
||
12 | self::$instance = new self(); |
||
13 | } |
||
14 | |||
15 | return self::$instance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
16 | } |
||
17 | |||
18 | public function getDiskTotalSpace($directory): float |
||
19 | { |
||
20 | $escaped = escapeshellarg($directory); |
||
21 | $cmd = "df -k $escaped | awk 'NR==2 {print \$2 * 1024}'"; |
||
0 ignored issues
–
show
|
|||
22 | $cmd = 'df -k ' . $escaped . '| awk "NR==2 {print \$2 * 1024}"'; |
||
23 | $output = trim(Runner::getInstance()->run($cmd)->getOutput()); |
||
24 | return is_numeric($output) ? (float)$output : 0.0; |
||
25 | } |
||
26 | |||
27 | public function getDiskFreeSpace($directory): float |
||
28 | { |
||
29 | $escaped = escapeshellarg($directory); |
||
30 | $cmd = 'df -k ' . $escaped . '| awk "NR==2 {print \$4 * 1024}"'; |
||
31 | $output = trim(Runner::getInstance()->run($cmd)->getOutput()); |
||
32 | return is_numeric($output) ? (float)$output : 0.0; |
||
33 | } |
||
34 | |||
35 | public function getLoadAverage(): array |
||
36 | { |
||
37 | $output = trim(Runner::getInstance()->run('uptime')->getOutput()); |
||
38 | |||
39 | if (preg_match('/load averages?: ([\d\.,]+)[, ]+([\d\.,]+)[, ]+([\d\.,]+)/', $output, $matches)) { |
||
40 | return [ |
||
41 | (float)str_replace(',', '.', $matches[1]), |
||
42 | (float)str_replace(',', '.', $matches[2]), |
||
43 | (float)str_replace(',', '.', $matches[3]), |
||
44 | ]; |
||
45 | } |
||
46 | |||
47 | return []; |
||
48 | } |
||
49 | |||
50 | public function getEnvironmentVariable($name): string |
||
51 | { |
||
52 | $escaped = escapeshellarg($name); |
||
53 | $cmd = "printenv $escaped"; |
||
54 | return trim(Runner::getInstance()->run($cmd)->getOutput()); |
||
55 | } |
||
56 | |||
57 | public function getPlatform(): string |
||
58 | { |
||
59 | static $platform; |
||
60 | |||
61 | if (!$platform) { |
||
62 | $platform = trim(Runner::getInstance()->run('uname -s')->getOutput()); |
||
63 | } |
||
64 | |||
65 | return $platform; |
||
66 | } |
||
67 | } |