|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\UptimeMonitor\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\UptimeMonitor\Events\SiteDown; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Spatie\UptimeMonitor\Events\SiteRestored; |
|
9
|
|
|
use Spatie\UptimeMonitor\Events\SiteUp; |
|
10
|
|
|
use UrlSigner; |
|
11
|
|
|
|
|
12
|
|
|
class UptimeMonitor extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
const STATUS_UP = 'online'; |
|
15
|
|
|
const STATUS_DOWN = 'offline'; |
|
16
|
|
|
const STATUS_NEVER_CHECKED = 'never checked'; |
|
17
|
|
|
|
|
18
|
|
|
protected $guarded = []; |
|
19
|
|
|
|
|
20
|
|
|
protected $dates = [ |
|
21
|
|
|
'last_checked_on', |
|
22
|
|
|
'last_status_change_on', |
|
23
|
|
|
'ssl_certificate_valid_until', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
public static function boot() |
|
27
|
|
|
{ |
|
28
|
|
|
static::saving(function (UptimeMonitor $uptimeMonitor) { |
|
29
|
|
|
if ($uptimeMonitor->getOriginal('status') != $uptimeMonitor->status) { |
|
30
|
|
|
$uptimeMonitor->last_status_change_on = Carbon::now(); |
|
31
|
|
|
}; |
|
32
|
|
|
}); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function shouldCheck() : bool |
|
36
|
|
|
{ |
|
37
|
|
|
if (! $this->enabled) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($this->status = static::STATUS_NEVER_CHECKED) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ($this->status === static::STATUS_DOWN) { |
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->last_checked_on->diffInMinutes() >= $this->ping_every_minutes; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function pingSucceeded($responseHtml) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!$this->lookForStringPresentOnResponse($responseHtml)) { |
|
55
|
|
|
$this->siteIsDown("String `{$this->look_for_string}` was not found on the response"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->siteIsUp(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function pingFailed(string $reason) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->siteIsDown($reason); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function siteIsUp() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->status = self::STATUS_UP; |
|
69
|
|
|
$this->last_failure_reason = ''; |
|
70
|
|
|
|
|
71
|
|
|
$wasFailing = $this->times_failed_in_a_row > 0; |
|
72
|
|
|
|
|
73
|
|
|
$this->times_failed_in_a_row = 0; |
|
74
|
|
|
$this->last_checked_on = Carbon::now(); |
|
75
|
|
|
|
|
76
|
|
|
$this->save(); |
|
77
|
|
|
|
|
78
|
|
|
$eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class); |
|
79
|
|
|
|
|
80
|
|
|
event(new $eventClass($this)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function siteIsDown(string $reason) |
|
84
|
|
|
{ |
|
85
|
|
|
$previousStatus = $this->status; |
|
86
|
|
|
|
|
87
|
|
|
$this->status = static::STATUS_DOWN; |
|
88
|
|
|
|
|
89
|
|
|
$this->times_failed_in_a_row++; |
|
90
|
|
|
|
|
91
|
|
|
$this->last_checked_on = Carbon::now(); |
|
92
|
|
|
|
|
93
|
|
|
$this->last_failure_reason = $reason; |
|
94
|
|
|
|
|
95
|
|
|
$this->save(); |
|
96
|
|
|
|
|
97
|
|
|
if ($this->shouldFireDownEvent($previousStatus)) { |
|
98
|
|
|
|
|
99
|
|
|
event(new SiteDown($this)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function lookForStringPresentOnResponse(string $responseHtml = '') : bool |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->look_for_string == '') { |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return str_contains($responseHtml, $this->look_for_string); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getPingRequestMethod() : string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->look_for_string == '' ? 'HEAD' : 'GET'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function shouldFireDownEvent($previousStatus): bool |
|
119
|
|
|
{ |
|
120
|
|
|
if ($previousStatus != static::STATUS_DOWN) { |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (Carbon::now()->diffInMinutes() >= config('resend_down_notification_every_minutes')) { |
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|