|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EricMakesStuff\ServerMonitor\Monitors; |
|
4
|
|
|
|
|
5
|
|
|
use EricMakesStuff\ServerMonitor\Events\HttpPingDown; |
|
6
|
|
|
use EricMakesStuff\ServerMonitor\Events\HttpPingUp; |
|
7
|
|
|
use EricMakesStuff\ServerMonitor\Exceptions\InvalidConfiguration; |
|
8
|
|
|
use GuzzleHttp\Client as Guzzle; |
|
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
class HttpPingMonitor extends BaseMonitor |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var int */ |
|
15
|
|
|
protected $responseCode; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $responseContent; |
|
19
|
|
|
|
|
20
|
|
|
/** @var bool */ |
|
21
|
|
|
protected $responseContainsPhrase = false; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $url; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool|string */ |
|
27
|
|
|
protected $checkPhrase = false; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
protected $timeout = 5; |
|
31
|
|
|
|
|
32
|
|
|
/** @var bool */ |
|
33
|
|
|
protected $allowRedirects = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $config |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(array $config) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!empty($config['url'])) { |
|
41
|
|
|
$this->url = $config['url']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (!empty($config['checkPhrase'])) { |
|
45
|
|
|
$this->checkPhrase = $config['checkPhrase']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($config['timeout'])) { |
|
49
|
|
|
$this->timeout = $config['timeout']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($config['allowRedirects'])) { |
|
53
|
|
|
$this->allowRedirects = $config['allowRedirects']; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @throws InvalidConfiguration |
|
59
|
|
|
*/ |
|
60
|
|
|
public function runMonitor() |
|
61
|
|
|
{ |
|
62
|
|
|
if (empty($this->url)) { |
|
63
|
|
|
throw InvalidConfiguration::noUrlConfigured(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$guzzle = new Guzzle([ |
|
68
|
|
|
'timeout' => $this->timeout, |
|
69
|
|
|
'allow_redirects' => $this->allowRedirects, |
|
70
|
|
|
]); |
|
71
|
|
|
$response = $guzzle->get($this->url); |
|
72
|
|
|
$this->responseCode = $response->getStatusCode(); |
|
73
|
|
|
$this->responseContent = (string)$response->getBody(); |
|
74
|
|
|
} catch (RequestException $e) { |
|
75
|
|
|
$response = $e->getResponse(); |
|
76
|
|
|
|
|
77
|
|
|
if ($response instanceof ResponseInterface) { |
|
78
|
|
|
$this->responseCode = $response->getStatusCode(); |
|
79
|
|
|
$this->responseContent = (string)$response->getBody(); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->setResponseCodeAndContentOnException($e); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
$this->setResponseCodeAndContentOnException($e); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($this->responseCode != '200' |
|
89
|
|
|
|| ! $this->checkResponseContains($this->responseContent, $this->checkPhrase)) { |
|
90
|
|
|
event(new HttpPingDown($this)); |
|
91
|
|
|
} else { |
|
92
|
|
|
event(new HttpPingUp($this)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param \Exception $e |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function setResponseCodeAndContentOnException(\Exception $e) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->responseCode = null; |
|
102
|
|
|
$this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function checkResponseContains($html, $phrase) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$phrase) { |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->responseContainsPhrase = str_contains($html, $phrase); |
|
112
|
|
|
|
|
113
|
|
|
return $this->responseContainsPhrase; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getResponseContainsPhrase() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->responseContainsPhrase; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getCheckPhrase() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->checkPhrase; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getResponseCode() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->responseCode; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getResponseContent() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->responseContent; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getUrl() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->url; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|