|
| 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\ClientException; |
| 10 | +use GuzzleHttp\Exception\ConnectException; |
| 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 (ClientException $e) { |
| 75 | + $response = $e->getResponse(); |
| 76 | + $this->responseCode = $response->getStatusCode(); |
| 77 | + } catch (ConnectException $e) { |
| 78 | + } |
| 79 | + |
| 80 | + if ($this->responseCode != '200' |
| 81 | + || ! $this->checkResponseContains($this->responseContent, $this->checkPhrase)) { |
| 82 | + event(new HttpPingDown($this)); |
| 83 | + } else { |
| 84 | + event(new HttpPingUp($this)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + protected function checkResponseContains($html, $phrase) |
| 89 | + { |
| 90 | + if (!$phrase) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + $this->responseContainsPhrase = str_contains($html, $phrase); |
| 95 | + |
| 96 | + return $this->responseContainsPhrase; |
| 97 | + } |
| 98 | + |
| 99 | + public function getResponseContainsPhrase() |
| 100 | + { |
| 101 | + return $this->responseContainsPhrase; |
| 102 | + } |
| 103 | + |
| 104 | + public function getCheckPhrase() |
| 105 | + { |
| 106 | + return $this->checkPhrase; |
| 107 | + } |
| 108 | + |
| 109 | + public function getResponseCode() |
| 110 | + { |
| 111 | + return $this->responseCode; |
| 112 | + } |
| 113 | + |
| 114 | + public function getResponseContent() |
| 115 | + { |
| 116 | + return $this->responseContent; |
| 117 | + } |
| 118 | + |
| 119 | + public function getUrl() |
| 120 | + { |
| 121 | + return $this->url; |
| 122 | + } |
| 123 | +} |
0 commit comments