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\storage; |
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, storage $storage) |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
$this->cpu = $cpu; |
|
|
|
|
21
|
|
|
$this->memory = $memory; |
|
|
|
|
22
|
|
|
$this->network = $network; |
|
|
|
|
23
|
|
|
$this->webServer = $webServer; |
|
|
|
|
24
|
|
|
$this->storage = $storage; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The name and signature of the console command. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $signature = 'stethoscope:monitor'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The console command description. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'monitor memory usage, cpu usage, network connection and nginx status'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function handle() |
47
|
|
|
{ |
48
|
|
|
$logs = []; |
49
|
|
|
|
50
|
|
|
$cpuUsage = $this->cpu->check(); |
51
|
|
|
$memoryUsage = $this->memory->check(); |
52
|
|
|
$networkStatus = $this->network->check(); |
53
|
|
|
$storageFreeSpace = $this->storage->check(); |
54
|
|
|
$webServerStatuses = $this->webServer->check(); |
55
|
|
|
|
56
|
|
|
if (config('stethoscope.monitorable_resources.cpu') && $cpuUsage > config(('stethoscope.thresholds.cpu'))) { |
57
|
|
|
$logs['cpu'] = $cpuUsage; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($memoryUsage > config(('stethoscope.thresholds.memory')) && config('stethoscope.monitorable_resources.memory')) { |
61
|
|
|
$logs['memory'] = $memoryUsage; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($networkStatus == 'disconnected' && config('stethoscope.monitorable_resources.network')) { |
65
|
|
|
$logs['network'] = $networkStatus; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($storageFreeSpace < config(('stethoscope.thresholds.storage')) && config('stethoscope.monitorable_resources.storage')) { |
69
|
|
|
$logs['storage'] = $storageFreeSpace; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($webServerStatuses != 'active' && config('stethoscope.monitorable_resources.web_server')) { |
73
|
|
|
$logs['webServer'] = $webServerStatuses; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
Record::record($logs); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (! empty($logs)) { |
79
|
|
|
$logs['signature'] = $this->signature; |
80
|
|
|
TroubleOccurred::dispatch($logs); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|