1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\ServerMonitor\Commands\BaseCommand; |
7
|
|
|
use Spatie\ServerMonitor\Models\Check; |
8
|
|
|
use Spatie\ServerMonitor\Models\Host; |
9
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
10
|
|
|
|
11
|
|
|
class ListNotifications extends BaseCommand |
12
|
|
|
{ |
13
|
|
|
protected $signature = 'server-monitor:list-notifications |
14
|
|
|
{--host= : Only show checks for certain host}'; |
15
|
|
|
|
16
|
|
|
protected $description = 'List all notifications for host(s)'; |
17
|
|
|
|
18
|
|
|
private $channels = []; |
19
|
|
|
private $notifications = []; |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
if ($this->determineHostModelClass()::count() === 0) { |
24
|
|
|
return $this->info('There are no hosts configured'); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->channels = config('server-monitor-plugin-notification-by-host.notifications.channels'); |
28
|
|
|
$this->notifications = config('server-monitor.notifications.notifications'); |
29
|
|
|
$titles = ['Host']; |
30
|
|
|
foreach ($this->channels as $type => $channel) { |
31
|
|
|
$titles[] = $type; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$hosts = $this->determineHostModelClass()::all(); |
35
|
|
|
if ($hostName = $this->option('host')) { |
36
|
|
|
$hosts = $hosts->filter(function (Host $host) use ($hostName) { |
37
|
|
|
return $host->name === $hostName; |
|
|
|
|
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->tableWithTitle( |
42
|
|
|
'Custom notification by host', |
43
|
|
|
$titles, |
44
|
|
|
$this->getTableRows($hosts) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getTableRows(Collection $hosts): array |
49
|
|
|
{ |
50
|
|
|
$rows = []; |
51
|
|
|
$first = true; |
52
|
|
|
foreach ($hosts as $host) { |
53
|
|
|
if(!$first) { |
54
|
|
|
$rows[] = new TableSeparator(); |
55
|
|
|
} |
56
|
|
|
/* @var $host Host */ |
57
|
|
|
$rows[] = [$host->name]; |
|
|
|
|
58
|
|
|
|
59
|
|
|
$hostNotifications = $host->getCustomProperty('notifications'); |
60
|
|
|
if($hostNotifications) { |
61
|
|
|
$rows = $this->displayCustomNotification($hostNotifications, $rows); |
62
|
|
|
} |
63
|
|
|
else { |
64
|
|
|
$rows = $this->displayGlobalNotification($rows); |
65
|
|
|
} |
66
|
|
|
$first = false; |
67
|
|
|
} |
68
|
|
|
return $rows; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function displayCustomNotification(array $hostNotifications, array $rows): array { |
72
|
|
|
foreach ($this->notifications as $notification => $global) { |
73
|
|
|
$rows[] = ['']; |
74
|
|
|
$temp = [implode("\n", str_split($notification, 50))]; |
75
|
|
|
foreach ($this->channels as $type => $channel) { |
76
|
|
|
if(isset($hostNotifications[$notification]) && array_search($type, $hostNotifications[$notification]['channels']) !== false) { |
77
|
|
|
if(isset($hostNotifications['configuration'][$type])) { |
78
|
|
|
$configurationList = []; |
79
|
|
|
foreach ($hostNotifications['configuration'][$type] as $configurationAttribut => $configurationValue) { |
80
|
|
|
$configurationList[] = $configurationAttribut.':'.(is_array($configurationValue)?implode(',', $configurationValue):$configurationValue); |
81
|
|
|
} |
82
|
|
|
$temp[] = implode("\n", $configurationList); |
83
|
|
|
} |
84
|
|
|
else { |
85
|
|
|
$temp[] = 'Global'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
else { |
89
|
|
|
$temp[] = ''; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$rows[] = $temp; |
93
|
|
|
} |
94
|
|
|
return $rows; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function displayGlobalNotification(array $rows): array{ |
98
|
|
|
|
99
|
|
|
foreach ($this->notifications as $notification => $global) { |
100
|
|
|
$rows[] = ['']; |
101
|
|
|
$temp = [implode("\n", str_split($notification, 50))]; |
102
|
|
|
foreach ($this->channels as $type => $channel) { |
103
|
|
|
if (array_search($type, $global) !== false) { |
104
|
|
|
$temp[] = 'Global'; |
105
|
|
|
} |
106
|
|
|
else { |
107
|
|
|
$temp[] = ''; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
$rows[] = $temp; |
111
|
|
|
} |
112
|
|
|
return $rows; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function tableWithTitle(string $title, array $header, array $rows) |
116
|
|
|
{ |
117
|
|
|
$this->info($title); |
118
|
|
|
$this->info(str_repeat('=', strlen($title))); |
119
|
|
|
$this->table($header, $rows); |
120
|
|
|
$this->comment(''); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.