1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\ServerMonitor\Models\Check; |
7
|
|
|
use Spatie\ServerMonitor\Models\Host; |
8
|
|
|
|
9
|
|
|
class ListHosts extends BaseCommand |
10
|
|
|
{ |
11
|
|
|
protected $signature = 'server-monitor:list-hosts |
12
|
|
|
{--host= : Filter hosts by name} |
13
|
|
|
{--check= : Filter checks by type}'; |
14
|
|
|
|
15
|
|
|
protected $description = 'List all hosts with their checks'; |
16
|
|
|
|
17
|
|
|
public function handle() |
18
|
|
|
{ |
19
|
|
|
if ($this->determineHostModelClass()::count() === 0) { |
|
|
|
|
20
|
|
|
return $this->info('There are no hosts configured'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->table( |
24
|
|
|
['Host', 'Health', 'Checks'], |
25
|
|
|
$this->getTableRows($this->determineHostModelClass()::all()) |
|
|
|
|
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getTableRows(Collection $hosts): array |
30
|
|
|
{ |
31
|
|
|
if ($hostName = $this->option('host')) { |
32
|
|
|
$hosts = $hosts->filter(function (Host $host) use ($hostName) { |
33
|
|
|
return $host->name === $hostName; |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $hosts |
38
|
|
|
->map(function (Host $host) { |
39
|
|
|
return [ |
40
|
|
|
'name' => $host->name, |
41
|
|
|
'health' => $host->health_as_emoji, |
42
|
|
|
'checks' => $this->getChecksSummary($host, $this->option('check')), |
43
|
|
|
]; |
44
|
|
|
}) |
45
|
|
|
->toArray(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getChecksSummary(Host $host, ?string $typeFilter): string |
49
|
|
|
{ |
50
|
|
|
return $host->checks |
51
|
|
|
->filter(function (Check $check) use ($typeFilter) { |
52
|
|
|
if (is_null($typeFilter)) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $check->type === $typeFilter; |
57
|
|
|
}) |
58
|
|
|
->map(function (Check $check) { |
59
|
|
|
return $check->summary; |
60
|
|
|
}) |
61
|
|
|
->implode(PHP_EOL); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.