1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models; |
4
|
|
|
|
5
|
|
|
use Spatie\SslCertificate\SslCertificate; |
6
|
|
|
use Spatie\UptimeMonitor\Events\InvalidSslCertificateFound; |
7
|
|
|
use Spatie\UptimeMonitor\Events\SiteDown; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Spatie\UptimeMonitor\Events\SiteRestored; |
11
|
|
|
use Spatie\UptimeMonitor\Events\SiteUp; |
12
|
|
|
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus; |
13
|
|
|
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus; |
14
|
|
|
use Spatie\UptimeMonitor\Models\Presenters\SitePresenter; |
15
|
|
|
use Spatie\Url\Url; |
16
|
|
|
|
17
|
|
|
class Site extends Model |
18
|
|
|
{ |
19
|
|
|
use SitePresenter; |
20
|
|
|
|
21
|
|
|
protected $guarded = []; |
22
|
|
|
|
23
|
|
|
protected $dates = [ |
24
|
|
|
'uptime_last_checked_on', |
25
|
|
|
'last_uptime_status_change_on', |
26
|
|
|
'ssl_certificate_expiration_date', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function scopeEnabled($query) |
30
|
|
|
{ |
31
|
|
|
return $query->where('enabled', true); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getUrlAttribute() |
35
|
|
|
{ |
36
|
|
|
return Url::fromString($this->attributes['url']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function boot() |
40
|
|
|
{ |
41
|
|
|
static::saving(function (Site $site) { |
42
|
|
|
if ($site->getOriginal('status') != $site->status) { |
43
|
|
|
$site->last_uptime_status_change_on = Carbon::now(); |
44
|
|
|
} |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function shouldCheckUptime() : bool |
49
|
|
|
{ |
50
|
|
|
if (! $this->enabled) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->uptime_status === UptimeStatus::DOWN) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->uptime_last_checked_on->diffInMinutes() >= $this->ping_every_minutes; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function pingSucceeded($responseHtml) |
66
|
|
|
{ |
67
|
|
|
if (! $this->lookForStringPresentOnResponse($responseHtml)) { |
68
|
|
|
$this->siteIsDown("String `{$this->look_for_string}` was not found on the response"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->siteIsUp(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function pingFailed(string $reason) |
75
|
|
|
{ |
76
|
|
|
$this->siteIsDown($reason); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function siteIsUp() |
80
|
|
|
{ |
81
|
|
|
$this->uptime_status = UptimeStatus::UP; |
82
|
|
|
$this->last_failure_reason = ''; |
83
|
|
|
|
84
|
|
|
$wasFailing = $this->times_failed_in_a_row > 0; |
85
|
|
|
|
86
|
|
|
$this->times_failed_in_a_row = 0; |
87
|
|
|
$this->uptime_last_checked_on = Carbon::now(); |
88
|
|
|
|
89
|
|
|
$this->save(); |
90
|
|
|
|
91
|
|
|
$eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class); |
92
|
|
|
|
93
|
|
|
event(new $eventClass($this)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function siteIsDown(string $reason) |
97
|
|
|
{ |
98
|
|
|
$previousStatus = $this->uptime_status; |
99
|
|
|
|
100
|
|
|
$this->uptime_status = UptimeStatus::DOWN; |
101
|
|
|
|
102
|
|
|
$this->times_failed_in_a_row++; |
103
|
|
|
|
104
|
|
|
$this->uptime_last_checked_on = Carbon::now(); |
105
|
|
|
|
106
|
|
|
$this->last_failure_reason = $reason; |
107
|
|
|
|
108
|
|
|
$this->save(); |
109
|
|
|
|
110
|
|
|
if ($this->shouldFireDownEvent($previousStatus)) { |
111
|
|
|
event(new SiteDown($this)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function lookForStringPresentOnResponse(string $responseHtml = '') : bool |
116
|
|
|
{ |
117
|
|
|
if ($this->look_for_string == '') { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return str_contains($responseHtml, $this->look_for_string); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getPingRequestMethod() : string |
125
|
|
|
{ |
126
|
|
|
return $this->look_for_string == '' ? 'HEAD' : 'GET'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function shouldFireDownEvent($previousStatus): bool |
130
|
|
|
{ |
131
|
|
|
if ($previousStatus != UptimeStatus::DOWN) { |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (Carbon::now()->diffInMinutes() >= config('resend_down_notification_every_minutes')) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isHealthy() |
143
|
|
|
{ |
144
|
|
|
if (in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function updateWithCertificate(SslCertificate $certificate) |
156
|
|
|
{ |
157
|
|
|
$this->ssl_certificate_status = $certificate->isValid() |
158
|
|
|
? SslCertificateStatus::VALID |
159
|
|
|
: SslCertificateStatus::INVALID; |
160
|
|
|
|
161
|
|
|
$this->ssl_certificate_expiration_date = $certificate->expirationDate(); |
162
|
|
|
|
163
|
|
|
$this->ssl_certificate_issuer = $certificate->getIssuer(); |
164
|
|
|
|
165
|
|
|
$this->save(); |
166
|
|
|
|
167
|
|
|
event(new ValidSslCertificateFound($this)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function updateWithCertificateException($exception) |
171
|
|
|
{ |
172
|
|
|
$this->ssl_certificate_status = SslCertificateStatus::INVALID; |
173
|
|
|
$this->ssl_certificate_expiration_date = null; |
174
|
|
|
$this->ssl_certificate_issuer = ''; |
175
|
|
|
|
176
|
|
|
$this->save(); |
177
|
|
|
|
178
|
|
|
event(new InvalidSslCertificateFound($this)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|