|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\ServerMonitor\Models\Check; |
|
7
|
|
|
|
|
8
|
|
|
class ListChecks extends BaseCommand |
|
9
|
|
|
{ |
|
10
|
|
|
protected $signature = 'server-monitor:list-checks |
|
11
|
|
|
{--host= : Only show checks for certain host} |
|
12
|
|
|
{--check= : Only show certain check type}'; |
|
13
|
|
|
|
|
14
|
|
|
protected $description = 'List all checks for host(s)'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle() |
|
17
|
|
|
{ |
|
18
|
|
|
if ($this->determineHostModelClass()::count() === 0) { |
|
|
|
|
|
|
19
|
|
|
return $this->info('There are no hosts configured'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$this->unhealthyChecks(); |
|
23
|
|
|
|
|
24
|
|
|
$this->healthyChecks(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
View Code Duplication |
protected function unhealthyChecks() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this->tableWithTitle( |
|
30
|
|
|
'Unhealthy checks', |
|
31
|
|
|
['Host', 'Check', 'Status', 'Message', 'Last checked', 'Next check'], |
|
32
|
|
|
$this->getTableRows($this->determineCheckModelClass()::unhealthy()->get()) |
|
|
|
|
|
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
protected function healthyChecks() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$this->tableWithTitle( |
|
39
|
|
|
'Healthy checks', |
|
40
|
|
|
['Host', 'Check', 'Message', 'Status', 'Last checked', 'Next check'], |
|
41
|
|
|
$this->getTableRows(self::determineCheckModelClass()::healthy()->get()) |
|
|
|
|
|
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function tableWithTitle(string $title, array $header, array $rows) |
|
46
|
|
|
{ |
|
47
|
|
|
if (count($rows) === 0) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->info($title); |
|
52
|
|
|
$this->info('================'); |
|
53
|
|
|
$this->table($header, $rows); |
|
54
|
|
|
$this->comment(''); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getTableRows(Collection $checks): array |
|
58
|
|
|
{ |
|
59
|
|
View Code Duplication |
if ($hostName = $this->option('host')) { |
|
|
|
|
|
|
60
|
|
|
$checks = $checks->filter(function (Check $check) use ($hostName) { |
|
61
|
|
|
return $check->host->name === $hostName; |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
if ($checkType = $this->option('check')) { |
|
|
|
|
|
|
66
|
|
|
$checks = $checks->filter(function (Check $check) use ($checkType) { |
|
67
|
|
|
return $check->type === $checkType; |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $checks |
|
72
|
|
|
->map(function (Check $check) { |
|
73
|
|
|
return [ |
|
74
|
|
|
'name' => $check->host->name, |
|
75
|
|
|
'check' => $check->type, |
|
76
|
|
|
'last_run_message' => $check->last_run_message, |
|
77
|
|
|
'status' => $check->status_as_emoji, |
|
78
|
|
|
'last_checked' => $check->getLatestRunDiffAttribute(), |
|
79
|
|
|
'next_check' => $check->getNextRunDiffAttribute(), |
|
80
|
|
|
]; |
|
81
|
|
|
}) |
|
82
|
|
|
->toArray(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.