1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HandlesCheckResult; |
7
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasProcess; |
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\Helpers\ConsoleOutput; |
16
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
18
|
|
|
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition; |
19
|
|
|
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter; |
20
|
|
|
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition; |
21
|
|
|
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties; |
22
|
|
|
use Spatie\ServerMonitor\Models\Concerns\ThrottlesFailingNotifications; |
23
|
|
|
|
24
|
|
|
class Check extends Model |
25
|
|
|
{ |
26
|
|
|
use CheckPresenter, |
27
|
|
|
HasCustomProperties, |
28
|
|
|
ThrottlesFailingNotifications, |
29
|
|
|
HasProcess, |
30
|
|
|
HandlesCheckResult; |
31
|
|
|
|
32
|
|
|
public $guarded = []; |
33
|
|
|
|
34
|
|
|
public $casts = [ |
35
|
|
|
'custom_properties' => 'array', |
36
|
|
|
'process_output' => 'array', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public $dates = [ |
40
|
|
|
'last_ran_at', 'next_check_at', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
public function host(): BelongsTo |
44
|
|
|
{ |
45
|
|
|
return $this->belongsTo(Host::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function scopeHealthy($query) |
49
|
|
|
{ |
50
|
|
|
return $query->where('status', CheckStatus::SUCCESS); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function scopeUnhealthy($query) |
54
|
|
|
{ |
55
|
|
|
return $query->where('status', '!=', CheckStatus::SUCCESS); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function scopeEnabled(Builder $query) |
59
|
|
|
{ |
60
|
|
|
$query->where('enabled', 1); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getAttribute($key) |
64
|
|
|
{ |
65
|
|
|
if (array_key_exists($key, $this->attributes)) { |
66
|
|
|
return parent::getAttribute($key); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$properties = json_decode($this->attributes['custom_properties'], true); |
70
|
|
|
|
71
|
|
|
return array_get($properties, $key, parent::getAttribute($key)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function shouldRun(): bool |
75
|
|
|
{ |
76
|
|
|
if (! $this->enabled) { |
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (is_null($this->last_ran_at)) { |
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return ! $this->last_ran_at |
|
|
|
|
85
|
|
|
->addMinutes($this->next_run_in_minutes) |
|
|
|
|
86
|
|
|
->isFuture(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getDefinition(): CheckDefinition |
90
|
|
|
{ |
91
|
|
|
if (! $definitionClass = config("server-monitor.checks.{$this->type}")) { |
|
|
|
|
92
|
|
|
throw InvalidCheckDefinition::unknownCheckType($this); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (! class_exists($definitionClass)) { |
96
|
|
|
throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return app($definitionClass)->setCheck($this); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function handleFinishedProcess() |
103
|
|
|
{ |
104
|
|
|
$originalStatus = $this->status; |
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->getDefinition()->handleFinishedProcess($this->getProcess()); |
107
|
|
|
|
108
|
|
|
$this->scheduleNextRun(); |
109
|
|
|
|
110
|
|
|
if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) { |
|
|
|
|
111
|
|
|
event(new CheckRestored($this)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus) |
118
|
|
|
{ |
119
|
|
|
if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $newStatus === CheckStatus::SUCCESS; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function scheduleNextRun() |
127
|
|
|
{ |
128
|
|
|
$this->last_ran_at = Carbon::now(); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$this->next_run_in_minutes = $this->getDefinition()->performNextRunInMinutes(); |
|
|
|
|
131
|
|
|
$this->save(); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function hasStatus(string $status): bool |
137
|
|
|
{ |
138
|
|
|
return $this->status === $status; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function storeProcessOutput(Process $process) |
142
|
|
|
{ |
143
|
|
|
$this->process_output = [ |
|
|
|
|
144
|
|
|
'output' => $process->getOutput(), |
145
|
|
|
'error_output' => $process->getErrorOutput(), |
146
|
|
|
'exit_code' => $process->getExitCode(), |
147
|
|
|
'exit_code_text' => $process->getExitCodeText(), |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
$this->save(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.