1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MohsenAbrishami\Stethoscope\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use MohsenAbrishami\Stethoscope\Events\TroubleOccurred; |
8
|
|
|
use MohsenAbrishami\Stethoscope\Services\Cpu; |
9
|
|
|
use MohsenAbrishami\Stethoscope\Services\Storage as StorageService; |
10
|
|
|
use MohsenAbrishami\Stethoscope\Services\Memory; |
11
|
|
|
use MohsenAbrishami\Stethoscope\Services\Network; |
12
|
|
|
use MohsenAbrishami\Stethoscope\Services\WebServer; |
13
|
|
|
use MohsenAbrishami\Stethoscope\Traits\MessageCreatorTrait; |
14
|
|
|
|
15
|
|
|
class ListenCommand extends Command |
16
|
|
|
{ |
17
|
|
|
use MessageCreatorTrait; |
18
|
|
|
|
19
|
|
|
public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, StorageService $storage) |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
|
23
|
|
|
$this->cpu = $cpu; |
|
|
|
|
24
|
|
|
$this->memory = $memory; |
|
|
|
|
25
|
|
|
$this->network = $network; |
|
|
|
|
26
|
|
|
$this->webServer = $webServer; |
|
|
|
|
27
|
|
|
$this->storageService = $storage; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->storage = Storage::disk(config('stethoscope.storage.driver')); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The name and signature of the console command. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $signature = 'stethoscope:listen {resources?*} {--notif}'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The console command description. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $description = 'monitor memory usage, cpu usage, network connection and nginx status'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the console command. |
48
|
|
|
* |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function handle() |
52
|
|
|
{ |
53
|
|
|
$resources = collect($this->argument('resources')); |
54
|
|
|
|
55
|
|
|
$resourcesIsEmpty = $resources->isEmpty(); |
56
|
|
|
|
57
|
|
|
$logs = []; |
58
|
|
|
$logs['signature'] = $this->signature; |
59
|
|
|
|
60
|
|
|
$this->line( |
61
|
|
|
$this->timeMessage() |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if ($resources->contains('cpu') || $resourcesIsEmpty) { |
65
|
|
|
$logs['cpu'] = $this->cpu->check(); |
66
|
|
|
$this->info( |
67
|
|
|
$this->cpuMessage($logs['cpu']) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($resources->contains('memory') || $resourcesIsEmpty) { |
72
|
|
|
$logs['memory'] = $this->memory->check(); |
73
|
|
|
$this->info( |
74
|
|
|
$this->memoryMessage($logs['memory']) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($resources->contains('network') || $resourcesIsEmpty) { |
79
|
|
|
$logs['network'] = $this->network->check(); |
80
|
|
|
$this->info( |
81
|
|
|
$this->networkMessage($logs['network']) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($resources->contains('web-server') || $resourcesIsEmpty) { |
86
|
|
|
$logs['webServer'] = $this->webServer->check(); |
87
|
|
|
$this->info( |
88
|
|
|
$this->webServerMessage($logs['webServer']) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($resources->contains('storage') || $resourcesIsEmpty) { |
93
|
|
|
$logs['storage'] = $this->storageService->check(); |
94
|
|
|
$this->info( |
95
|
|
|
$this->storageMessage($logs['storage']) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->option('notif')){ |
100
|
|
|
TroubleOccurred::dispatch($logs); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|