1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Spatie\ServerMonitor\Events\CheckFailed; |
10
|
|
|
use Spatie\ServerMonitor\Events\CheckWarning; |
11
|
|
|
use Spatie\ServerMonitor\Events\CheckRestored; |
12
|
|
|
use Spatie\ServerMonitor\Events\CheckSucceeded; |
13
|
|
|
use Spatie\ServerMonitor\Helpers\ConsoleOutput; |
14
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
16
|
|
|
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition; |
17
|
|
|
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter; |
18
|
|
|
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition; |
19
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties; |
20
|
|
|
|
21
|
|
|
class Check extends Model |
22
|
|
|
{ |
23
|
|
|
use CheckPresenter, HasCustomProperties; |
24
|
|
|
|
25
|
|
|
public $guarded = []; |
26
|
|
|
|
27
|
|
|
public $casts = [ |
28
|
|
|
'custom_properties' => 'array', |
29
|
|
|
'process_output' => 'array', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
public $dates = [ |
33
|
|
|
'checked_at', 'next_check_at', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function host(): BelongsTo |
37
|
|
|
{ |
38
|
|
|
return $this->belongsTo(Host::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function scopeHealthy($query) |
42
|
|
|
{ |
43
|
|
|
return $query->where('status', CheckStatus::SUCCESS); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function scopeUnhealthy($query) |
47
|
|
|
{ |
48
|
|
|
return $query->where('status', '!=', CheckStatus::SUCCESS); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getAttribute($key) |
52
|
|
|
{ |
53
|
|
|
if (array_key_exists($key, $this->attributes)) { |
54
|
|
|
return parent::getAttribute($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$properties = json_decode($this->attributes['custom_properties'], true); |
58
|
|
|
|
59
|
|
|
return array_get($properties, $key, parent::getAttribute($key)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function shouldRun(): bool |
63
|
|
|
{ |
64
|
|
|
if (! $this->enabled) { |
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_null($this->checked_at)) { |
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->checked_at |
|
|
|
|
73
|
|
|
->addMinutes($this->next_check_in_minutes) |
|
|
|
|
74
|
|
|
->isPast(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getDefinition(): CheckDefinition |
78
|
|
|
{ |
79
|
|
|
if (! $definitionClass = config("server-monitor.checks.{$this->type}")) { |
|
|
|
|
80
|
|
|
throw InvalidCheckDefinition::unknownCheckType($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (! class_exists($definitionClass)) { |
84
|
|
|
throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return app($definitionClass)->setCheck($this); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getProcess(): Process |
91
|
|
|
{ |
92
|
|
|
static $processes = []; |
93
|
|
|
|
94
|
|
|
if (! isset($processes[$this->id])) { |
|
|
|
|
95
|
|
|
$processes[$this->id] = new Process($this->getProcessCommand()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $processes[$this->id]; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getProcessCommand(): string |
102
|
|
|
{ |
103
|
|
|
$delimiter = 'EOF-LARAVEL-SERVER-MONITOR'; |
104
|
|
|
|
105
|
|
|
$definition = $this->getDefinition(); |
106
|
|
|
|
107
|
|
|
$portArgument = empty($this->host->port) ? '' : "-p {$this->host->port}"; |
|
|
|
|
108
|
|
|
|
109
|
|
|
return "ssh {$this->getTarget()} {$portArgument} 'bash -se <<$delimiter".PHP_EOL |
110
|
|
|
.'set -e'.PHP_EOL |
111
|
|
|
.$definition->getCommand().PHP_EOL |
112
|
|
|
.$delimiter."'"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function getTarget(): string |
116
|
|
|
{ |
117
|
|
|
$target = $this->host->name; |
|
|
|
|
118
|
|
|
|
119
|
|
|
if ($this->host->ssh_user) { |
|
|
|
|
120
|
|
|
$target = $this->host->ssh_user.'@'.$target; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $target; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function succeeded(string $message = '') |
127
|
|
|
{ |
128
|
|
|
$this->status = CheckStatus::SUCCESS; |
|
|
|
|
129
|
|
|
$this->message = $message; |
|
|
|
|
130
|
|
|
|
131
|
|
|
$this->save(); |
132
|
|
|
|
133
|
|
|
event(new CheckSucceeded($this)); |
134
|
|
|
ConsoleOutput::info($this->host->name.": check `{$this->type}` succeeded"); |
|
|
|
|
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function warn(string $warningMessage = '') |
140
|
|
|
{ |
141
|
|
|
$this->status = CheckStatus::WARNING; |
|
|
|
|
142
|
|
|
$this->message = $warningMessage; |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->save(); |
145
|
|
|
|
146
|
|
|
event(new CheckWarning($this)); |
147
|
|
|
|
148
|
|
|
ConsoleOutput::info($this->host->name.": check `{$this->type}` issued warning"); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function failed(string $failureReason = '') |
154
|
|
|
{ |
155
|
|
|
$this->status = CheckStatus::FAILED; |
|
|
|
|
156
|
|
|
$this->message = $failureReason; |
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->save(); |
159
|
|
|
|
160
|
|
|
event(new CheckFailed($this)); |
161
|
|
|
|
162
|
|
|
ConsoleOutput::error($this->host->name.": check `{$this->type}` failed"); |
|
|
|
|
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function scopeEnabled(Builder $query) |
168
|
|
|
{ |
169
|
|
|
$query->where('enabled', 1); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function handleFinishedProcess() |
173
|
|
|
{ |
174
|
|
|
$originalStatus = $this->status; |
|
|
|
|
175
|
|
|
|
176
|
|
|
$this->getDefinition()->handleFinishedProcess($this->getProcess()); |
177
|
|
|
|
178
|
|
|
$this->scheduleNextRun(); |
179
|
|
|
|
180
|
|
|
if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) { |
|
|
|
|
181
|
|
|
event(new CheckRestored($this)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function scheduleNextRun() |
188
|
|
|
{ |
189
|
|
|
$this->checked_at = Carbon::now(); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$this->next_check_in_minutes = $this->getDefinition()->performNextRunInMinutes(); |
|
|
|
|
192
|
|
|
$this->save(); |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function hasStatus(string $status): bool |
198
|
|
|
{ |
199
|
|
|
return $this->status === $status; |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus) |
203
|
|
|
{ |
204
|
|
|
if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $newStatus === CheckStatus::SUCCESS; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function storeProcessOutput(Process $process) |
212
|
|
|
{ |
213
|
|
|
$this->process_output = [ |
|
|
|
|
214
|
|
|
'output' => $process->getOutput(), |
215
|
|
|
'error_output' => $process->getErrorOutput(), |
216
|
|
|
'exit_code' => $process->getExitCode(), |
217
|
|
|
'exit_code_text' => $process->getExitCodeText(), |
218
|
|
|
]; |
219
|
|
|
|
220
|
|
|
$this->save(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.