1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EricMakesStuff\ServerMonitor\Monitors; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use EricMakesStuff\ServerMonitor\Events\DiskUsageAlarm; |
7
|
|
|
use EricMakesStuff\ServerMonitor\Events\DiskUsageHealthy; |
8
|
|
|
use EricMakesStuff\ServerMonitor\Exceptions\InvalidPath; |
9
|
|
|
use EricMakesStuff\ServerMonitor\Helpers\Format; |
10
|
|
|
|
11
|
|
|
class DiskUsageMonitor extends BaseMonitor |
12
|
|
|
{ |
13
|
|
|
/** @var int */ |
14
|
|
|
protected $totalSpace; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
protected $freeSpace; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $usedSpace; |
21
|
|
|
|
22
|
|
|
/** @var float */ |
23
|
|
|
protected $percentageUsed; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $path; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
protected $alarmPercentage = 75; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $config |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $config) |
35
|
|
|
{ |
36
|
|
|
$this->path = __DIR__; |
37
|
|
|
|
38
|
|
|
if (!empty($config['path'])) { |
39
|
|
|
$this->path = $config['path']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!empty($config['alarmPercentage'])) { |
43
|
|
|
$this->alarmPercentage = intval($config['alarmPercentage']); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws InvalidPath |
49
|
|
|
*/ |
50
|
|
|
public function runMonitor() |
51
|
|
|
{ |
52
|
|
|
if (! file_exists($this->path)) { |
53
|
|
|
throw InvalidPath::pathDoesNotExist($this->path); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->totalSpace = disk_total_space($this->path); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->freeSpace = disk_free_space($this->path); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->usedSpace = $this->totalSpace - $this->freeSpace; |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->percentageUsed = sprintf('%.2f',($this->usedSpace / $this->totalSpace) * 100); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if ($this->percentageUsed >= $this->alarmPercentage) { |
65
|
|
|
event(new DiskUsageAlarm($this)); |
66
|
|
|
} else { |
67
|
|
|
event(new DiskUsageHealthy($this)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getTotalSpace() |
75
|
|
|
{ |
76
|
|
|
return Format::getHumanReadableSize($this->totalSpace); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getFreeSpace() |
83
|
|
|
{ |
84
|
|
|
return Format::getHumanReadableSize($this->freeSpace); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getUsedSpace() |
91
|
|
|
{ |
92
|
|
|
return Format::getHumanReadableSize($this->usedSpace); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getPercentageUsed() |
99
|
|
|
{ |
100
|
|
|
return $this->percentageUsed . '%'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed|string |
105
|
|
|
*/ |
106
|
|
|
public function getPath() |
107
|
|
|
{ |
108
|
|
|
return $this->path; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getAlarmPercentage() |
115
|
|
|
{ |
116
|
|
|
return $this->alarmPercentage . '%'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.