1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Spatie\ServerMonitor\Events\CheckFailed; |
12
|
|
|
use Spatie\ServerMonitor\Events\CheckWarning; |
13
|
|
|
use Spatie\ServerMonitor\Events\CheckRestored; |
14
|
|
|
use Spatie\ServerMonitor\Events\CheckSucceeded; |
15
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
16
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
17
|
|
|
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition; |
18
|
|
|
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter; |
19
|
|
|
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition; |
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 getAttribute($key) |
42
|
|
|
{ |
43
|
|
|
if (array_key_exists($key, $this->attributes)) { |
44
|
|
|
return parent::getAttribute($key); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$properties = json_decode($this->attributes['properties'], true); |
48
|
|
|
|
49
|
|
|
return Arr::get($properties, $key, parent::getAttribute($key)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function shouldRun(): bool |
53
|
|
|
{ |
54
|
|
|
if (! $this->enabled) { |
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (is_null($this->checked_at)) { |
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->checked_at |
|
|
|
|
63
|
|
|
->addMinutes($this->next_check_in_minutes) |
|
|
|
|
64
|
|
|
->isPast(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getDefinition(): CheckDefinition |
68
|
|
|
{ |
69
|
|
|
if (! $definitionClass = config("server-monitor.checks.{$this->type}")) { |
|
|
|
|
70
|
|
|
throw InvalidCheckDefinition::unknownCheckType($this); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (! class_exists($definitionClass)) { |
74
|
|
|
throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return app($definitionClass)->setCheck($this); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getProcess(): Process |
81
|
|
|
{ |
82
|
|
|
static $processes = []; |
83
|
|
|
|
84
|
|
|
if (! isset($processes[$this->id])) { |
|
|
|
|
85
|
|
|
$processes[$this->id] = new Process($this->getProcessCommand()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $processes[$this->id]; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getProcessCommand(): string |
92
|
|
|
{ |
93
|
|
|
$delimiter = 'EOF-LARAVEL-SERVER-MONITOR'; |
94
|
|
|
|
95
|
|
|
$definition = $this->getDefinition(); |
96
|
|
|
|
97
|
|
|
$portArgument = empty($this->host->port) ? '' : "-p {$this->host->port}"; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return "ssh {$this->getTarget()} {$portArgument} 'bash -se <<$delimiter".PHP_EOL |
100
|
|
|
.'set -e'.PHP_EOL |
101
|
|
|
.$definition->getCommand().PHP_EOL |
102
|
|
|
.$delimiter."'"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function getTarget(): string |
106
|
|
|
{ |
107
|
|
|
$target = $this->host->name; |
|
|
|
|
108
|
|
|
|
109
|
|
|
if ($this->host->ssh_user) { |
|
|
|
|
110
|
|
|
$target = $this->host->ssh_user.'@'.$target; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $target; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function succeeded(string $message = '') |
117
|
|
|
{ |
118
|
|
|
$this->status = CheckStatus::SUCCESS; |
|
|
|
|
119
|
|
|
$this->message = $message; |
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->save(); |
122
|
|
|
|
123
|
|
|
event(new CheckSucceeded($this, $message)); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function warn(string $warningMessage = '') |
129
|
|
|
{ |
130
|
|
|
$this->status = CheckStatus::WARNING; |
|
|
|
|
131
|
|
|
$this->message = $warningMessage; |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->save(); |
134
|
|
|
|
135
|
|
|
event(new CheckWarning($this, $warningMessage)); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function failed(string $failureReason = '') |
141
|
|
|
{ |
142
|
|
|
$this->status = CheckStatus::FAILED; |
|
|
|
|
143
|
|
|
$this->message = $failureReason; |
|
|
|
|
144
|
|
|
|
145
|
|
|
$this->save(); |
146
|
|
|
|
147
|
|
|
event(new CheckFailed($this, $failureReason)); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function scopeEnabled(Builder $query) |
153
|
|
|
{ |
154
|
|
|
$query->where('enabled', 1); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function handleFinishedProcess() |
158
|
|
|
{ |
159
|
|
|
$originalStatus = $this->status; |
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->getDefinition()->handleFinishedProcess($this->getProcess()); |
162
|
|
|
|
163
|
|
|
$this->scheduleNextRun(); |
164
|
|
|
|
165
|
|
|
if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) { |
|
|
|
|
166
|
|
|
event(new CheckRestored($this, $this->message)); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function scheduleNextRun() |
173
|
|
|
{ |
174
|
|
|
$this->checked_at = Carbon::now(); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$this->next_check_in_minutes = $this->getDefinition()->performNextRunInMinutes(); |
|
|
|
|
177
|
|
|
$this->save(); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function hasStatus(string $status): bool |
183
|
|
|
{ |
184
|
|
|
return $this->status === $status; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus) |
188
|
|
|
{ |
189
|
|
|
if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $newStatus === CheckStatus::SUCCESS; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function storeProcessOutput(Process $process) |
197
|
|
|
{ |
198
|
|
|
$this->process_output = [ |
|
|
|
|
199
|
|
|
'output' => $process->getOutput(), |
200
|
|
|
'error_output' => $process->getErrorOutput(), |
201
|
|
|
'exit_code' => $process->getExitCode(), |
202
|
|
|
'exit_code_text' => $process->getExitCodeText(), |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
$this->save(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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.