From 594d7895aa47340e2d80482702bbf92b5b9c28c9 Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Fri, 25 Apr 2014 23:51:48 +0200 Subject: [PATCH 1/8] First test of ping check --- src/lang/da_DK.lang.php | 1 + src/lang/en_US.lang.php | 1 + .../Controller/ServerController.class.php | 3 +- src/psm/Util/Install/Installer.class.php | 2 +- src/psm/Util/Updater/StatusUpdater.class.php | 69 +++++++++++++++++++ src/templates/server/server.tpl.html | 1 + 6 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/lang/da_DK.lang.php b/src/lang/da_DK.lang.php index aa6e8f42..8390057b 100644 --- a/src/lang/da_DK.lang.php +++ b/src/lang/da_DK.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Hjemmeside', 'type_service' => 'Tjeneste', + 'type_ping' => 'Ping', 'pattern' => 'Søge streng/mønster', 'pattern_description' => 'Hvis dette mønster ikke findes på hjemmesiden, vil serveren blive markeret offline. Regulære udtryk er tilladt.', 'last_check' => 'Sidst kontrolleret', diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index e8f1dff0..8ec22924 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Search string/pattern', 'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.', 'last_check' => 'Last check', diff --git a/src/psm/Module/Server/Controller/ServerController.class.php b/src/psm/Module/Server/Controller/ServerController.class.php index 2b39f89f..6cf8848d 100755 --- a/src/psm/Module/Server/Controller/ServerController.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -208,7 +208,7 @@ class ServerController extends AbstractServerController { 'label' => strip_tags($_POST['label']), 'ip' => strip_tags($_POST['ip']), 'port' => intval($_POST['port']), - 'type' => in_array($_POST['type'], array('website', 'service')) ? $_POST['type'] : 'website', + 'type' => in_array($_POST['type'], array('website', 'service', 'ping')) ? $_POST['type'] : 'website', 'pattern' => $_POST['pattern'], 'warning_threshold' => intval($_POST['warning_threshold']), 'active' => in_array($_POST['active'], array('yes', 'no')) ? $_POST['active'] : 'no', @@ -342,6 +342,7 @@ class ServerController extends AbstractServerController { 'label_type' => psm_get_lang('servers', 'type'), 'label_website' => psm_get_lang('servers', 'type_website'), 'label_service' => psm_get_lang('servers', 'type_service'), + 'label_ping' => psm_get_lang('servers', 'type_ping'), 'label_type' => psm_get_lang('servers', 'type'), 'label_pattern' => psm_get_lang('servers', 'pattern'), 'label_pattern_description' => psm_get_lang('servers', 'pattern_description'), diff --git a/src/psm/Util/Install/Installer.class.php b/src/psm/Util/Install/Installer.class.php index 2198d3bd..ca567c7d 100644 --- a/src/psm/Util/Install/Installer.class.php +++ b/src/psm/Util/Install/Installer.class.php @@ -200,7 +200,7 @@ class Installer { `ip` varchar(100) NOT NULL, `port` int(5) unsigned NOT NULL, `label` varchar(255) NOT NULL, - `type` enum('service','website') NOT NULL default 'service', + `type` enum('service','website','ping') NOT NULL default 'service', `pattern` varchar(255) NOT NULL, `status` enum('on','off') NOT NULL default 'on', `error` varchar(255) NULL, diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index dacd784a..8ad44bfb 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -95,6 +95,9 @@ class StatusUpdater { case 'website': $this->status_new = $this->updateWebsite($max_runs); break; + case 'ping': + $this->status_new = $this->updatePing($max_runs); + break; } // update server status @@ -217,6 +220,53 @@ class StatusUpdater { return $result; } + + /** + * Check the current server with a ping and hope to get a pong + * @param int $max_runs + * @param int $run + * @return boolean + */ + protected function updatePing($max_runs, $run = 1) { + $errno = 0; + $timeout = 1; + $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost"; /* ICMP ping packet with a pre-calculated checksum */ + + // save response time + $starttime = microtime(true); + + /* Only run if is cron + * socket_create() need to run as root :( + * ugly hack cli hack i know + */ + //if(psm_is_cli()) { + + // IPv6 ready + if ($this->is_ipv6($this->server['ip'])) { + $socket = socket_create(AF_INET6, SOCK_RAW, 1); + } else { + $socket = socket_create(AF_INET, SOCK_RAW, 1); + } + + + socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); + socket_connect($socket, $this->server['ip'], null); + socket_send($socket, $package, strLen($package), 0); + + // if ping fails it returns false + $status = (socket_read($socket, 255)) ? false : true; + $this->rtime = (microtime(true) - $starttime); + + socket_close($socket); + + // check if server is available and rerun if asked. + if(!$status && $run < $max_runs) { + return $this->updatePing($max_runs, $run + 1); + } + + return $status; + //} + } /** * Get the error returned by the update function @@ -235,4 +285,23 @@ class StatusUpdater { public function getRtime() { return $this->rtime; } + + /** + * Test if ip is IPv6 + * @param string $ip + * @return boolean + */ + private function is_ipv6($ip) { + // If it contains anything other than hex characters, periods, colons or a / it's not IPV6 + if (!preg_match("/^([0-9a-f\.\/:]+)$/",strtolower($ip))) { return false; } + + // An IPV6 address needs at minimum two colons in it + if (substr_count($ip,":") < 2) { return false; } + + // If any of the "octets" are longer than 4 characters it's not valid + $part = preg_split("/[:\/]/",$ip); + foreach ($part as $i) { if (strlen($i) > 4) { return false; } } + + return true; + } } diff --git a/src/templates/server/server.tpl.html b/src/templates/server/server.tpl.html index e62a2b26..bf8c1149 100755 --- a/src/templates/server/server.tpl.html +++ b/src/templates/server/server.tpl.html @@ -98,6 +98,7 @@ From 034d960e019645b0305c22f781b8ad555e220cc9 Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sat, 26 Apr 2014 00:06:59 +0200 Subject: [PATCH 2/8] I have to learn whats true or false, it works great when running sudo php -f status.cron.php, but fails when updating true website --- src/psm/Util/Updater/StatusUpdater.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index 8ad44bfb..57d50ac4 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -237,7 +237,8 @@ class StatusUpdater { /* Only run if is cron * socket_create() need to run as root :( - * ugly hack cli hack i know + * ugly cli hack i know + * might be a better way still have not found a solution when updating true website */ //if(psm_is_cli()) { @@ -254,7 +255,7 @@ class StatusUpdater { socket_send($socket, $package, strLen($package), 0); // if ping fails it returns false - $status = (socket_read($socket, 255)) ? false : true; + $status = (socket_read($socket, 255)) ? true : false; $this->rtime = (microtime(true) - $starttime); socket_close($socket); From f525f31d6a662a92acb81728beac3f1f33eaea9f Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sat, 26 Apr 2014 00:22:55 +0200 Subject: [PATCH 3/8] Updatede all language files --- src/lang/bg_BG.lang.php | 1 + src/lang/de_DE.lang.php | 1 + src/lang/es_ES.lang.php | 1 + src/lang/fr_FR.lang.php | 1 + src/lang/it_IT.lang.php | 1 + src/lang/ko_KR.lang.php | 1 + src/lang/nl_NL.lang.php | 1 + src/lang/pt_BR.lang.php | 1 + src/lang/zh_CN.lang.php | 1 + 9 files changed, 9 insertions(+) diff --git a/src/lang/bg_BG.lang.php b/src/lang/bg_BG.lang.php index 3cc3276a..931b49b4 100644 --- a/src/lang/bg_BG.lang.php +++ b/src/lang/bg_BG.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Тип', 'type_website' => 'Сайт', 'type_service' => 'Услуга', + 'type_ping' => 'Ping', 'pattern' => 'Търсене на стринг/образец', 'pattern_description' => 'Ако този текст не е намерен в интернет страницата (когато имате добавен сайт), той ще бъде маркиран като Офлайн. Регулярните изрази са разрешени.', 'last_check' => 'Последна проверка', diff --git a/src/lang/de_DE.lang.php b/src/lang/de_DE.lang.php index 319bba6b..74d6136a 100644 --- a/src/lang/de_DE.lang.php +++ b/src/lang/de_DE.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Search string/pattern', 'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.', 'last_check' => 'Letzter Check', diff --git a/src/lang/es_ES.lang.php b/src/lang/es_ES.lang.php index 40b53092..19c7b81e 100644 --- a/src/lang/es_ES.lang.php +++ b/src/lang/es_ES.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Tipo', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Search string/pattern', 'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.', 'last_check' => 'Ultima verificación', diff --git a/src/lang/fr_FR.lang.php b/src/lang/fr_FR.lang.php index da3abc34..d6c3aa4c 100644 --- a/src/lang/fr_FR.lang.php +++ b/src/lang/fr_FR.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Site Web', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Rechercher un texte/motif', 'pattern_description' => 'Si ce texte n\'est par retrouvé sur le site web, le serveur est marqué hors-service. Les expressions réguliaires sont autorisées.', 'last_check' => 'Dernière vérification', diff --git a/src/lang/it_IT.lang.php b/src/lang/it_IT.lang.php index 139d649d..8c09336d 100644 --- a/src/lang/it_IT.lang.php +++ b/src/lang/it_IT.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Tipo', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Search string/pattern', 'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.', 'last_check' => 'Ultimo Controllo', diff --git a/src/lang/ko_KR.lang.php b/src/lang/ko_KR.lang.php index 1a549e5b..99e08048 100644 --- a/src/lang/ko_KR.lang.php +++ b/src/lang/ko_KR.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Search string/regex', 'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.', 'last_check' => '최근체크', diff --git a/src/lang/nl_NL.lang.php b/src/lang/nl_NL.lang.php index 745869e7..d8192d69 100644 --- a/src/lang/nl_NL.lang.php +++ b/src/lang/nl_NL.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Type', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Zoek voor tekst/regex', 'pattern_description' => 'Als dit patroon niet gevonden wordt op de website, zal de server als offline gemarkeerd worden. Regular expressions zijn toegestaan.', 'last_check' => 'Laatst gecontroleerd', diff --git a/src/lang/pt_BR.lang.php b/src/lang/pt_BR.lang.php index 03d3e0e7..356d2156 100644 --- a/src/lang/pt_BR.lang.php +++ b/src/lang/pt_BR.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => 'Tipo', 'type_website' => 'Website', 'type_service' => 'Service', + 'type_ping' => 'Ping', 'pattern' => 'Pesquisa palavra/padrão', 'pattern_description' => 'Se esse padrão não for encontrado no site, o servidor será marcado offline. As expressões regulares são permitidas.', 'last_check' => 'Última verificação', diff --git a/src/lang/zh_CN.lang.php b/src/lang/zh_CN.lang.php index 024a8d3f..d5d712ad 100644 --- a/src/lang/zh_CN.lang.php +++ b/src/lang/zh_CN.lang.php @@ -115,6 +115,7 @@ $sm_lang = array( 'type' => '类型', 'type_website' => '网站', 'type_service' => '服务', + 'type_ping' => 'Ping', 'pattern' => '字符串/正则匹配', 'pattern_description' => '如果在网站上未找到对应匹配内容, 则标记该网站为离线. 支持正则表达式.', 'last_check' => '最后检查', From ce5e1d8bc6d8c2ba358eb4cfed17ea71767750eb Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sat, 26 Apr 2014 09:22:57 +0200 Subject: [PATCH 4/8] Moved ipv6 validation in to function.class.php and made the validate code a lot prettier --- src/includes/functions.inc.php | 15 ++++++++++++++ src/psm/Util/Updater/StatusUpdater.class.php | 21 +------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 2c8a948d..a2d807c8 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -529,6 +529,21 @@ function psm_is_cli() { return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))); } +/** + * Check if ip is IPv6 or not + * + * @param string $ip + * @return boolean + */ +function psm_validate_ipv6($ip) { + // if $ip is a valid ipv6 address it returns true + if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){ + return true; + } else { + return false; + } +} + ############################################### # # Debug functions diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index 57d50ac4..fcfcd354 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -243,7 +243,7 @@ class StatusUpdater { //if(psm_is_cli()) { // IPv6 ready - if ($this->is_ipv6($this->server['ip'])) { + if (psm_validate_ipv6($this->server['ip'])) { $socket = socket_create(AF_INET6, SOCK_RAW, 1); } else { $socket = socket_create(AF_INET, SOCK_RAW, 1); @@ -286,23 +286,4 @@ class StatusUpdater { public function getRtime() { return $this->rtime; } - - /** - * Test if ip is IPv6 - * @param string $ip - * @return boolean - */ - private function is_ipv6($ip) { - // If it contains anything other than hex characters, periods, colons or a / it's not IPV6 - if (!preg_match("/^([0-9a-f\.\/:]+)$/",strtolower($ip))) { return false; } - - // An IPV6 address needs at minimum two colons in it - if (substr_count($ip,":") < 2) { return false; } - - // If any of the "octets" are longer than 4 characters it's not valid - $part = preg_split("/[:\/]/",$ip); - foreach ($part as $i) { if (strlen($i) > 4) { return false; } } - - return true; - } } From 11907c7047eaca6a8a68c89caa69f1e84c002fcf Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sun, 27 Apr 2014 09:46:54 +0200 Subject: [PATCH 5/8] Ipv6 and small updates More ipv6 check and functions Small update to servercontroller --- src/includes/functions.inc.php | 7 ++- .../Controller/ServerController.class.php | 10 +++-- src/psm/Util/Updater/StatusUpdater.class.php | 45 ++++++++++++++----- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index a2d807c8..269b7c29 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -277,6 +277,7 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = 10, $add_ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_URL, $href); + if($add_agent) { curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; phpservermon/'.PSM_VERSION.'; +http://www.phpservermonitor.org)'); } @@ -536,9 +537,11 @@ function psm_is_cli() { * @return boolean */ function psm_validate_ipv6($ip) { - // if $ip is a valid ipv6 address it returns true + // Need to remove [] on ipv6 address before we can test + $ip = trim($ip, '[]'); + if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){ - return true; + return true; } else { return false; } diff --git a/src/psm/Module/Server/Controller/ServerController.class.php b/src/psm/Module/Server/Controller/ServerController.class.php index 6cf8848d..cfa06d0f 100755 --- a/src/psm/Module/Server/Controller/ServerController.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -108,7 +108,7 @@ class ServerController extends AbstractServerController { $servers[$x]['type_icon'] = 'icon-globe'; // add link to label $ip = $servers[$x]['ip']; - if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80)) { + if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80) && ($servers[$x]['port'] != 443)) { $ip .= ' : ' . $servers[$x]['port']; } $servers[$x]['ip'] = ''.$ip.''; @@ -215,10 +215,12 @@ class ServerController extends AbstractServerController { 'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no', 'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no', ); - // make sure websites start with http:// - if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') { - $clean['ip'] = 'http://' . $clean['ip']; + + // Make sure websites start with http:// or https:// if port is 443 + if($clean['type'] == 'website' && !preg_match('#^http(s)?://#', $clean['ip'])) { + $clean['ip'] = ($clean['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip']; } + // check for edit or add if($this->server_id > 0) { diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index fcfcd354..4ee9605e 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', '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; @@ -146,7 +146,7 @@ class StatusUpdater { $errno = 0; // save response time $starttime = microtime(true); - + $fp = fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10); $status = ($fp === false) ? false : true; @@ -171,10 +171,22 @@ class StatusUpdater { 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 + + // Test if ip is ipv6 or ipv4 + if (psm_validate_ipv6($clean['ip'])) { + $clean['ip'] = '['. $clean['ip'] .']'; + } + + // Add http(s) again + $this->server['ip'] = ($this->server['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip']; + // 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['ip'].':'.$this->server['port'], true, ($this->server['pattern'] == '' ? false : true) ); @@ -235,21 +247,23 @@ class StatusUpdater { // save response time $starttime = microtime(true); - /* Only run if is cron - * socket_create() need to run as root :( - * ugly cli hack i know - * might be a better way still have not found a solution when updating true website + /** + * Only run if is cron + * socket_create() need to run as root :( + * ugly cli hack i know + * might be a better way still have not found a solution when updating true website */ - //if(psm_is_cli()) { - - // IPv6 ready + if(psm_is_cli()) { + + // if ipv6 we have to use AF_INET6 if (psm_validate_ipv6($this->server['ip'])) { + // Need to remove [] on ipv6 address + $this->server['ip'] = trim($this->server['ip'], '[]'); $socket = socket_create(AF_INET6, SOCK_RAW, 1); } else { $socket = socket_create(AF_INET, SOCK_RAW, 1); } - socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); socket_connect($socket, $this->server['ip'], null); socket_send($socket, $package, strLen($package), 0); @@ -266,7 +280,14 @@ class StatusUpdater { } return $status; - //} + // If state on last update was 'on' and the update request is comming from the website + } 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']; + return true; + } else { + return false; + } } /** From bb459adf8a07c1ab07f6245c7b9bd6f468038fe1 Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sun, 27 Apr 2014 22:45:11 +0200 Subject: [PATCH 6/8] 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; From d554982b413ad8bef73c846aeb487d0c8617ae8e Mon Sep 17 00:00:00 2001 From: "michael@ilovecode.dk" Date: Sun, 27 Apr 2014 22:52:18 +0200 Subject: [PATCH 7/8] Fixed ipv6 and website plus you can now test web server on different ports (:8080, :8443) --- src/psm/Util/Updater/StatusUpdater.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index fae55e30..78818643 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -294,7 +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.'; + $this->error = 'Update skipped, status will be updated on next cron script run.'; return true; } else { return false; From 8543581972a84965947bfec36efe1a313fea1927 Mon Sep 17 00:00:00 2001 From: nerdalertdk Date: Mon, 28 Apr 2014 09:31:29 +0200 Subject: [PATCH 8/8] Update StatusUpdater.class.php --- src/psm/Util/Updater/StatusUpdater.class.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/psm/Util/Updater/StatusUpdater.class.php b/src/psm/Util/Updater/StatusUpdater.class.php index 78818643..f9b4f36e 100644 --- a/src/psm/Util/Updater/StatusUpdater.class.php +++ b/src/psm/Util/Updater/StatusUpdater.class.php @@ -182,15 +182,11 @@ class StatusUpdater { * * 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); + // Sets port number + $url['port'] = (isset($url['port']) ? $url['port'] : $this->server['port']) + // Update Server[ip] + $this->server['ip'] = http_build_url('', $url); + */ // We're only interested in the header, because that should tell us plenty!