From bb459adf8a07c1ab07f6245c7b9bd6f468038fe1 Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sun, 27 Apr 2014 22:45:11 +0200 Subject: [PATCH] Fixed ipv6 and website plus you can now test web server on different ports (:8080, :8443) --- src/psm/Util/Updater/StatusUpdater.class.php | 35 +++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index 4ee9605e..fae55e30 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -82,7 +82,7 @@ class StatusUpdater { $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array( 'server_id' => $server_id, ), array( - 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status','rtime', 'active', 'warning_threshold', 'warning_threshold_counter', + 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'rtime', 'active', 'warning_threshold', 'warning_threshold_counter', )); if(empty($this->server)) { return false; @@ -169,24 +169,34 @@ class StatusUpdater { * @return boolean */ protected function updateWebsite($max_runs, $run = 1) { + $starttime = microtime(true); - - // Removes http(s) from ip address so we can test if its an ipv6 adress - $clean['ip'] = preg_replace('#^http(s)?://#i', '', rtrim($this->server['ip'],'/')); // removes http(s) from ip address + // Parse a URL and return its components + $url = parse_url($this->server['ip']); - // Test if ip is ipv6 or ipv4 - if (psm_validate_ipv6($clean['ip'])) { - $clean['ip'] = '['. $clean['ip'] .']'; - } + // Build url + $this->server['ip'] = $url['scheme'] . '://' . (psm_validate_ipv6($url['host']) ? '['. $url['host'] .']' : $url['host']) . ':'.$this->server['port'] . (isset($url['path']) ? $url['path'] : '') . (isset($url['query']) ? '?'.$url['query'] : ''); - // Add http(s) again - $this->server['ip'] = ($this->server['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip']; + /** + * + * Need php_http.dll extensions but might be a better tool for the job + * http://stackoverflow.com/questions/14056977/function-http-build-url + + $this->server['ip'] = http_build_url($this->server['ip'], + array( + "scheme" => $url['scheme'], + "host" => (psm_validate_ipv6($url['host']) ? '['. $url['host'] .']' : $url['host']), + "port" => $this->server['port'], + "path" => (isset($url['path']) ? $url['path'] : ''), + "query" => (isset($url['query']) ? '?'.$url['query'] : '') + ), HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT); + */ // We're only interested in the header, because that should tell us plenty! // unless we have a pattern to search for! $curl_result = psm_curl_get( - $this->server['ip'].':'.$this->server['port'], + $this->server['ip'], true, ($this->server['pattern'] == '' ? false : true) ); @@ -203,7 +213,7 @@ class StatusUpdater { if(empty($code_matches[0])) { // somehow we dont have a proper response. - $this->error = 'no response from server'; + $this->error = 'No response from server.'; $result = false; } else { $code = $code_matches[1][0]; @@ -284,6 +294,7 @@ class StatusUpdater { } elseif ($this->server['status'] == 'on') { // need to set rtime to the value from last update, if not the latency will be 0 $this->rtime = $this->server['rtime']; + $this->error = 'Update skipped, must run from cron script.'; return true; } else { return false;