Issues (22)

src/Commands/MonitorCommand.php (7 issues)

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;
0 ignored issues
show
Bug Best Practice introduced by
The property cpu does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        $this->memory = $memory;
0 ignored issues
show
Bug Best Practice introduced by
The property memory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
        $this->network = $network;
0 ignored issues
show
Bug Best Practice introduced by
The property network does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->webServer = $webServer;
0 ignored issues
show
Bug Best Practice introduced by
The property webServer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        $this->storage = $storage;
0 ignored issues
show
Bug Best Practice introduced by
The property storage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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);
0 ignored issues
show
The call to MohsenAbrishami\Stethosc...acades\Record::record() has too few arguments starting with memoryUsage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        Record::/** @scrutinizer ignore-call */ 
77
                record($logs);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
$logs of type array is incompatible with the type integer expected by parameter $cpuUsage of MohsenAbrishami\Stethosc...acades\Record::record(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        Record::record(/** @scrutinizer ignore-type */ $logs);
Loading history...
77
78
        if (! empty($logs)) {
79
            $logs['signature'] = $this->signature;
80
            TroubleOccurred::dispatch($logs);
81
        }
82
    }
83
}
84