Skip to content
26 changes: 9 additions & 17 deletions app/Check/Consumers/AliveCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,23 @@ class AliveCheck extends Check
*/
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
$client = new \GuzzleHttp\Client();

$check->beforeLastTimeout = $check->lastTimeout;
$check->lastTimeout = NULL;

$options = [
'connect_timeout' => $check::ALIVE_TIMEOUT / 1000,
'timeout' => 2 * $check::ALIVE_TIMEOUT / 1000,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create($check::ALIVE_TIMEOUT / 1000, 2 * $check::ALIVE_TIMEOUT / 1000);
if ( ! $check->followRedirect) {
$guzzleOptions = $guzzleOptions->withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Configuration\AllowRedirects::create(FALSE));
}
$client = new \GuzzleHttp\Client($guzzleOptions->config());

if ($check->siteMap && ! $this->siteMapLoader) {
$this->siteMapLoader = new \Pd\Monitoring\Check\SiteMapLoader($check->url);
}

if ( ! $check->followRedirect) {
$options['allow_redirects'] = FALSE;
}

try {
if ($this->siteMapLoader) {
while ($url = $this->siteMapLoader->getNextUrl($this->lastUrl)) {
$loaded = $this->loadUrl($client, $options, $check, $url);
$loaded = $this->loadUrl($client, $check, $url);
if ( ! $loaded) {
return FALSE;
} else {
Expand All @@ -50,7 +42,7 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool

return TRUE;
} else {
$loaded = $this->loadUrl($client, $options, $check, $check->url);
$loaded = $this->loadUrl($client, $check, $check->url);
if ($loaded) {
return TRUE;
}
Expand All @@ -71,13 +63,13 @@ protected function getCheckType(): int
}


private function loadUrl(\GuzzleHttp\Client $client, array $options, \Pd\Monitoring\Check\AliveCheck $check, string $url): bool
private function loadUrl(\GuzzleHttp\Client $client, \Pd\Monitoring\Check\AliveCheck $check, string $url): bool
{
$start = (float) \microtime(TRUE);
$this->logInfo($check, \sprintf('Začínám stahovat url "%s"', $url));

try {
$response = $client->request('GET', $url, $options);
$response = $client->get($url);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
$this->logError($check, $e->getMessage());

Expand Down
14 changes: 4 additions & 10 deletions app/Check/Consumers/CertificateCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
}

try {
$curl = new \GuzzleHttp\Client();
$options = [
'connect_timeout' => self::TIMEOUT,
'timeout' => 2 * self::TIMEOUT,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
];

$gradeResponse = \Nette\Utils\Json::decode((string) $curl->get($check->getSslLabsApiLink(), $options)->getBody(), \Nette\Utils\Json::FORCE_ARRAY);
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create(self::TIMEOUT, 2 * self::TIMEOUT);
$client = new \GuzzleHttp\Client($guzzleOptions->config());

$gradeResponse = \Nette\Utils\Json::decode((string) $client->get($check->getSslLabsApiLink())->getBody(), \Nette\Utils\Json::FORCE_ARRAY);

if ($gradeResponse['status'] === 'READY') {
$check->lastGrade = NULL;
Expand Down
1 change: 0 additions & 1 deletion app/Check/Consumers/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function process(\PhpAmqpLib\Message\AMQPMessage $message): int


/**
* @param \Pd\Monitoring\Check\Check $check
* @return bool TRUE, pokud se podařilo úspěšně provést kontrolu, jinak FALSE. Po FALSE může následovat opětovné spuštění kontroly
*/
abstract protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool;
Expand Down
13 changes: 3 additions & 10 deletions app/Check/Consumers/ErrorsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ class ErrorsCheck extends Check
*/
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
$client = new \GuzzleHttp\Client();

$check->errorsJson = NULL;

$options = [
'connect_timeout' => self::TIMEOUT,
'timeout' => 2 * self::TIMEOUT,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create(self::TIMEOUT, 2 * self::TIMEOUT);
$client = new \GuzzleHttp\Client($guzzleOptions->config());

try {
$start = (float) \microtime(TRUE);

$this->logInfo($check, \sprintf('Začínám stahovat url "%s" v čase %f', $check->url, $start));

$response = $client->request('GET', $check->url, $options);
$response = $client->get($check->url);

$this->logHeaders($check, $response);

Expand Down
7 changes: 3 additions & 4 deletions app/Check/Consumers/HttpStatusCodeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class HttpStatusCodeCheck extends Check
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
try {
$configuration =
$guzzleOptions =
\Pd\Monitoring\Check\Consumers\Client\Configuration::create(
2 * self::TIMEOUT,
self::TIMEOUT,
2 * self::TIMEOUT,
FALSE)
->withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Configuration\AllowRedirects::create(FALSE))
;

$client = new \GuzzleHttp\Client($configuration->config());
$client = new \GuzzleHttp\Client($guzzleOptions->config());

try {
$this->logInfo($check, \sprintf('Kontrola ID "%s". Začínám stahovat url "%s".', $check->id, $check->url));
Expand Down
13 changes: 3 additions & 10 deletions app/Check/Consumers/NumberValueCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ class NumberValueCheck extends Check
*/
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
$client = new \GuzzleHttp\Client();

$check->value = NULL;

$options = [
'connect_timeout' => self::TIMEOUT,
'timeout' => 2 * self::TIMEOUT,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create(self::TIMEOUT, 2 * self::TIMEOUT);
$client = new \GuzzleHttp\Client($guzzleOptions->config());

try {
$start = (float) \microtime(TRUE);

$this->logInfo($check, \sprintf('Začínám stahovat url "%s" v čase %f', $check->url, $start));

$response = $client->request('GET', $check->url, $options);
$response = $client->get($check->url);

$this->logHeaders($check, $response);

Expand Down
16 changes: 5 additions & 11 deletions app/Check/Consumers/RabbitConsumerCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
$check->lastConsumerCount = NULL;

try {
$config = [
'verify' => $check->validateHttps,
];
$client = new \GuzzleHttp\Client($config);

$options = [
'connect_timeout' => 5,
'timeout' => 5,
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create(5, 5, $check->validateHttps);
$client = new \GuzzleHttp\Client($guzzleOptions->config());

try {
$options = [];
if ( ! empty($check->login)) {
$options['auth'] = [$check->login, $check->password];
}

$this->logInfo($check, \sprintf('Kontrola ID "%s". Začínám stahovat url "%s".', $check->id, $check->url));

$response = $client->request('GET', $check->url, $options);
$response = $client->get($check->url, $options);

$this->logHeaders($check, $response);

Expand Down
16 changes: 5 additions & 11 deletions app/Check/Consumers/RabbitQueueCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
$check->lastMessageCount = NULL;

try {
$config = [
'verify' => $check->validateHttps,
];
$client = new \GuzzleHttp\Client($config);

$options = [
'connect_timeout' => 5,
'timeout' => 5,
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create(5, 5, $check->validateHttps);
$client = new \GuzzleHttp\Client($guzzleOptions->config());

try {
$options = [];
if ( ! empty($check->login)) {
$options['auth'] = [$check->login, $check->password];
}

$this->logInfo($check, \sprintf('Kontrola ID "%s". Začínám stahovat url "%s".', $check->id, $check->url));

$response = $client->request('GET', $check->url, $options);
$response = $client->get($check->url, $options);

$this->logHeaders($check, $response);

Expand Down
29 changes: 11 additions & 18 deletions app/Check/Consumers/XpathCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ final class XpathCheck extends Check
private ?string $lastUrl = NULL;


/**
* @param \Pd\Monitoring\Check\Check|\Pd\Monitoring\Check\XpathCheck $check
* @return bool
*/
protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
{
if ( ! $check instanceof \Pd\Monitoring\Check\XpathCheck) {
throw new \Pd\Monitoring\Exception();
}
$check->xpathLastResult = NULL;

$client = new \GuzzleHttp\Client();

$options = [
'connect_timeout' => $check::ALIVE_TIMEOUT / 1000,
'timeout' => 2 * $check::ALIVE_TIMEOUT / 1000,
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
'allow_redirects' => TRUE,
];
$guzzleOptions = \Pd\Monitoring\Check\Consumers\Client\Configuration::create($check::ALIVE_TIMEOUT / 1000, (int) (\round(2 * ($check::ALIVE_TIMEOUT / 1000))))
->withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Configuration\AllowRedirects::create(TRUE))
;
$client = new \GuzzleHttp\Client($guzzleOptions->config());

if ($check->siteMap && ! $this->siteMapLoader) {
$this->siteMapLoader = new \Pd\Monitoring\Check\SiteMapLoader($check->url);
Expand All @@ -37,7 +30,7 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool
if ($this->siteMapLoader) {
$withoutError = TRUE;
while ($url = $this->siteMapLoader->getNextUrl($this->lastUrl)) {
$loaded = $this->loadUrl($client, $options, $check, $url);
$loaded = $this->loadUrl($client, $check, $url);
if ( ! $loaded) {
$this->logError($check, \sprintf('Došlo k chybě na url "%s"', $url));
$withoutError = FALSE;
Expand All @@ -47,7 +40,7 @@ protected function doHardJob(\Pd\Monitoring\Check\Check $check): bool

return $withoutError;
} else {
$loaded = $this->loadUrl($client, $options, $check, $check->url);
$loaded = $this->loadUrl($client, $check, $check->url);
if ($loaded) {
return TRUE;
}
Expand All @@ -67,9 +60,9 @@ protected function getCheckType(): int
}


private function loadUrl(\GuzzleHttp\Client $client, array $options, \Pd\Monitoring\Check\XpathCheck $check, string $url): bool
private function loadUrl(\GuzzleHttp\Client $client, \Pd\Monitoring\Check\XpathCheck $check, string $url): bool
{
$response = $client->request('GET', $url, $options);
$response = $client->get($url);

if ($response->getStatusCode() !== 200) {
$this->logHeaders($check, $response);
Expand Down
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ parameters:
count: 1
path: app/Check/Consumers/AliveCheck.php

-
message: "#^Method Pd\\\\Monitoring\\\\Check\\\\Consumers\\\\AliveCheck\\:\\:loadUrl\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: app/Check/Consumers/AliveCheck.php

-
message: "#^Only booleans are allowed in a negated boolean, Pd\\\\Monitoring\\\\Check\\\\SiteMapLoader\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -460,26 +455,11 @@ parameters:
count: 1
path: app/Check/Consumers/RabbitQueueCheck.php

-
message: "#^Access to an undefined property Pd\\\\Monitoring\\\\Check\\\\Check\\:\\:\\$xpathLastResult\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Access to undefined constant Pd\\\\Monitoring\\\\Check\\\\Check\\:\\:ALIVE_TIMEOUT\\.$#"
count: 2
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Cannot call method getNextUrl\\(\\) on Pd\\\\Monitoring\\\\Check\\\\SiteMapLoader\\|null\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Method Pd\\\\Monitoring\\\\Check\\\\Consumers\\\\XpathCheck\\:\\:loadUrl\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Only booleans are allowed in a negated boolean, Pd\\\\Monitoring\\\\Check\\\\SiteMapLoader\\|null given\\.$#"
count: 1
Expand All @@ -490,11 +470,6 @@ parameters:
count: 1
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Parameter \\#3 \\$check of method Pd\\\\Monitoring\\\\Check\\\\Consumers\\\\XpathCheck\\:\\:loadUrl\\(\\) expects Pd\\\\Monitoring\\\\Check\\\\XpathCheck, Pd\\\\Monitoring\\\\Check\\\\Check given\\.$#"
count: 2
path: app/Check/Consumers/XpathCheck.php

-
message: "#^Only booleans are allowed in &&, string\\|null given on the left side\\.$#"
count: 1
Expand Down