1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MohsenAbrishami\Stethoscope\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use MohsenAbrishami\Stethoscope\Events\TroubleOccurred; |
7
|
|
|
use MohsenAbrishami\Stethoscope\LogRecord\Facades\Record; |
8
|
|
|
use MohsenAbrishami\Stethoscope\Services\Cpu; |
9
|
|
|
use MohsenAbrishami\Stethoscope\Services\HardDisk; |
10
|
|
|
use MohsenAbrishami\Stethoscope\Services\Memory; |
11
|
|
|
use MohsenAbrishami\Stethoscope\Services\Network; |
12
|
|
|
use MohsenAbrishami\Stethoscope\Services\WebServer; |
13
|
|
|
|
14
|
|
|
class MonitorCommand extends Command |
15
|
|
|
{ |
16
|
|
|
public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, HardDisk $hardDisk) |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
$this->cpu = $cpu; |
|
|
|
|
21
|
|
|
$this->memory = $memory; |
|
|
|
|
22
|
|
|
$this->network = $network; |
|
|
|
|
23
|
|
|
$this->webServer = $webServer; |
|
|
|
|
24
|
|
|
$this->hardDisk = $hardDisk; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The name and signature of the console command. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $signature = 'stethoscope:monitor'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The console command description. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $description = 'monitor memory usage, cpu usage, network connection and nginx status'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
|
|
public function handle() |
48
|
|
|
{ |
49
|
|
|
$resourceReports = []; |
50
|
|
|
|
51
|
|
|
$cpuUsage = $this->cpu->check(); |
52
|
|
|
$memoryUsage = $this->memory->check(); |
53
|
|
|
$networkStatus = $this->network->check(); |
54
|
|
|
$hardDiskFreeSpace = $this->hardDisk->check(); |
55
|
|
|
$webServerStatuses = $this->webServer->check(); |
56
|
|
|
|
57
|
|
|
if (config('stethoscope.monitorable_resources.cpu') && $cpuUsage > config(('stethoscope.thresholds.cpu'))) |
58
|
|
|
$resourceReports['cpu'] = $cpuUsage; |
59
|
|
|
|
60
|
|
|
if ($memoryUsage > config(('stethoscope.thresholds.memory')) && config('stethoscope.monitorable_resources.memory')) |
61
|
|
|
$resourceReports['memory'] = $memoryUsage; |
62
|
|
|
|
63
|
|
|
if ($networkStatus == 'false' && config('stethoscope.monitorable_resources.network')) |
64
|
|
|
$resourceReports['network'] = $networkStatus; |
65
|
|
|
|
66
|
|
|
if ($hardDiskFreeSpace < config(('stethoscope.thresholds.hard_disk')) && config('stethoscope.monitorable_resources.hard_disk')) |
67
|
|
|
$resourceReports['hardDisk'] = $hardDiskFreeSpace; |
68
|
|
|
|
69
|
|
|
if ($webServerStatuses != 'active' && config('stethoscope.monitorable_resources.web_server')) |
70
|
|
|
$resourceReports['webServer'] = $webServerStatuses; |
71
|
|
|
|
72
|
|
|
Record::record($resourceReports); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!empty($resourceReports)) |
75
|
|
|
TroubleOccurred::dispatch($resourceReports); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|