From 472064a521cbb0cdaba9340e2c613a38e9475863 Mon Sep 17 00:00:00 2001 From: David Mavrin Date: Mon, 24 Aug 2015 11:55:51 +0200 Subject: [PATCH] Pages response http code check --- src/Kendu/HealthCheck/Base/Pages.php | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Kendu/HealthCheck/Base/Pages.php diff --git a/src/Kendu/HealthCheck/Base/Pages.php b/src/Kendu/HealthCheck/Base/Pages.php new file mode 100644 index 0000000..57f131b --- /dev/null +++ b/src/Kendu/HealthCheck/Base/Pages.php @@ -0,0 +1,53 @@ +params = $params; + } + + public function run() + { + $baseUrl = 'http://'.$_SERVER['SERVER_NAME']; + $errors = []; + + if (isset($this->params['pages'])) { + foreach ($this->params['pages'] as $key => $page) { + $url = $baseUrl . $page; + + $handle = curl_init($url); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($handle, CURLOPT_NOBODY, true); + + /* Get the HTML or whatever is linked in $url. */ + $response = curl_exec($handle); + + $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); + + if (200 > $httpCode || $httpCode > 300) { + $errors[] = sprintf('%s not reachable. Code: "%s"', $page, $httpCode); + } + + curl_close($handle); + } + + if (!empty($errors)) { + return new \Kendu\HealthCheck\Status\Fail([ + 'message' => implode(';', $errors) + ]); + } + + return new \Kendu\HealthCheck\Status\Pass; + } + + return new \Kendu\HealthCheck\Status\Fail([ + 'message' => 'No page url defined' + ]); + } + + public function id() + { + return 'pages'; + } +} \ No newline at end of file