function DoTest($testname, $param, $hostname, $timeout, $params) { $timer = new TFNTimer(); // First initialise DNS query object $dnsserver = $params[1]; if ($dnsserver == "") { return -3; } $dnsserver = ip_lookup($dnsserver); $url = $param; $dns_delay = $params[2]; if ($dns_delay == 0 || !is_numeric($dns_delay)) { $dns_delay = 0; } // default no extra delay if ($timeout <= 0) { $timeout = 60; } $udp = true; // initial setting $port = 53; $dns_query = new DNSQuery($dnsserver, $port, $timeout, $udp, false); // run with debug off $type = "A"; $matches = ""; $out = preg_match("@^(?:http[s]*://)?([^/|\\?|:]+)@i", $url, $matches); $hostname = $matches[1]; // strip out hostname for FQDN lookup $host_no_dots = str_replace(".", "", $hostname); if (is_numeric($host_no_dots)) { $is_ip_address = true; } else { $is_ip_address = false; } $timer->Start(); if (!$is_ip_address) { $answer = $dns_query->Query($hostname, $type); //echo "DNS"; if ($answer === false || $dns_query->error) { $udp = false; // switch to TCP $dns_query->udp = $udp; // wait! while ($timer->Stop() < $dns_delay) { usleep(100); } $answer = $dns_query->Query($hostname, $type); //echo "DNS2"; } if ($answer->count <= 0) { return -1; } // no records returned if ($answer === false) { return -1; } // object is false if ($dns_query->error) { return -1; } // DNS object error $dns_time_taken = $timer->Stop(); // if we get this far the DNS has worked $ip_address = url_lookup($url); // pre-cache DNS } else { $dns_time_taken = 0; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 32); if ($timeout > 0) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); } if ($timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } // restart timer $timer->Start(); if (!($output = curl_exec($ch))) { $ctr = -1; // failed } else { $ctr = round(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) / 1024, 2); } $fetch_time_taken = $timer->Stop(); curl_close($ch); if ($ctr <= 0) { return -2; } // URL request failed return $dns_time_taken + $fetch_time_taken; // return elapsed time taken }
function extended_page_checker($url, $text, $notext, $user = "", $pass = "", $timeout = -1) { global $NATS; $timer = new TFNTimer(); // initialise the timer url_lookup($url); // pre-resolve the DNS into cache $output = ""; // output buffer if ($user != "") { $pos = strpos($url, "://"); if ($pos === false) { return -1; } // not a valid URL $protocol = substr($url, 0, $pos + 3); // protocol section $uri = substr($url, $pos + 3); // uri section $url = $protocol . $user . ":" . $pass . "@" . $uri; // make http://user:pass@uri } if ($timeout <= 0) { if (isset($NATS)) { $nto = $NATS->Cfg->Get("test.http.timeout", -1); if ($nto > 0) { $timeout = $nto; } // use NATS timeout } } if ($timeout > 0) { // use the set timeout $oldtimeout = ini_set("default_socket_timeout", $timeout); } $timer->Start(); if (function_exists("curl_getinfo")) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HEADER, 1); if ($timeout > 0) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); } if ($timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } if (!($output = curl_exec($ch))) { $ctr = -1; // failed } else { $ctr = round(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) / 1024, 2); } curl_close($ch); if ($ctr == 0) { $ctr = "0.0001"; } } else { // no CURL - use fopen() $fp = @fopen($url, "r"); if ($fp <= 0) { if ($timeout > 0) { ini_set("default_socket_timeout", $oldtimeout); } return -1; } $ctr = 0; while ($output .= @fgets($fp, 1024)) { $ctr += sizeof($body); } @fclose($fp); } if ($ctr < 0) { return $ctr; } // negative number (-1) failed to open $elapsed = $timer->Stop(); if ($timeout > 0) { ini_set("default_socket_timeout", $oldtimeout); } // now to check the actual text if (is_array($text)) { foreach ($text as $findthis) { if ($findthis != "") { if (strpos($output, $findthis) === false) { return -2; } } // text to be found not found } } if (is_array($notext)) { foreach ($notext as $donotfindthis) { if ($donotfindthis != "") { if (strpos($output, $donotfindthis) !== false) { return -3; } } // text not to find found } } return $elapsed; }
function DoTest($test, $param, $hostname = "", $timeout = -1, $params = 0, $nodeid = "") { global $NATS; if (!is_array($params)) { $params = array(); for ($a = 0; $a < 10; $a++) { $params[$a] = ""; } } switch ($test) { case "web": case "wsize": // Don't bother with pre-resolution as size only return WebTest($param, $timeout); break; /* -- modularised case "tcp": // nb TCP does not support timeouts currently $ip=ip_lookup($hostname); if ($ip=="0") return 0; $fp=@fsockopen($ip,$param); if ($fp<=0) return 0; @fclose($fp); return 1; break; */ /* -- modularised case "tcp": // nb TCP does not support timeouts currently $ip=ip_lookup($hostname); if ($ip=="0") return 0; $fp=@fsockopen($ip,$param); if ($fp<=0) return 0; @fclose($fp); return 1; break; */ case "wtime": $timer = new TFNTimer(); // Do a pre-lookup $ip = url_lookup($param); if ($ip == "0") { return -1; } // dns lookup failed $timer->Start(); $r = WebTest($param, $timeout); $elapsedTime = $timer->Stop(); $elapsedTime = round($elapsedTime, 4); if ($r < 0) { return -1; } // open failed if ($r == 0) { return -2; } // no chars shown as returned if ($elapsedTime <= 0) { return 0.0001; } return $elapsedTime; break; case "host": $timer = new TFNTimer(); if (preg_match("/[a-zA-Z]/", $param) > 0) { $is_ip = false; } else { $is_ip = true; } $timer->Start(); if ($is_ip) { $result = gethostbyaddr($param); } else { $result = gethostbyname($param); } $elapsedTime = $timer->Stop(); if ($result == $param) { // lookup failed return -1; } if ($result == "") { // lookup failed return -1; } $elapsedTime = round($elapsedTime, 4); if ($elapsedTime <= 0) { return 0.0001; } return $elapsedTime; break; case "testloop": return $param; break; case "testrand": mt_srand(microtime() * 1000000); if ($param == "" || $param == 0) { $param = 100; } return mt_rand(0, $param); break; case "ping": return PingTest($param, $timeout); break; default: if (isset($NATS)) { if (isset($NATS->Tests->QuickList[$test])) { $NATS->Tests->Tests[$test]->Create(); return $NATS->Tests->Tests[$test]->instance->DoTest($test, $param, $hostname, $timeout, $params); } } } return -1; // did not run any test so untested }
function DoTest($testname, $param, $hostname, $timeout, $params) { global $NATS; /* parameters: 0: url (FQDN URL can include user:pass@host or host:port formats) 1: proxy server 2: proxy port 3: proxy username 4: proxy password 5: proxy type (http or socks5) 6: 7: 8: 9: */ // setup variables $url = $params[0]; $proxy = $params[1]; $proxy_port = $params[2]; if ($proxy != "") { $use_proxy = true; } else { $use_proxy = false; } $proxy_user = $params[3]; $proxy_pass = $params[4]; if ($proxy_user != "") { $proxy_auth = true; } else { $proxy_auth = false; } $timer = new TFNTimer(); // initialise the timer url_lookup($url); // pre-resolve the DNS into cache $output = ""; // output buffer if ($timeout <= 0) { if (isset($NATS)) { $nto = $NATS->Cfg->Get("test.http.timeout", -1); if ($nto > 0) { $timeout = $nto; } // use NATS timeout } } if ($timeout > 0) { // use the set timeout $oldtimeout = ini_set("default_socket_timeout", $timeout); } $timer->Start(); // Requires CURL Now $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HEADER, 1); if ($timeout > 0) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); } if ($timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } if (!($output = curl_exec($ch))) { $ctr = -1; // failed } else { $ctr = round(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) / 1024, 2); } curl_close($ch); if ($ctr == 0) { $ctr = "0.0001"; } if ($ctr < 0) { return $ctr; } // negative number (-1) failed to open $elapsed = $timer->Stop(); if ($timeout > 0) { ini_set("default_socket_timeout", $oldtimeout); } // now to check the actual text if (is_array($text)) { foreach ($text as $findthis) { if ($findthis != "") { if (strpos($output, $findthis) === false) { return -2; } } // text to be found not found } } if (is_array($notext)) { foreach ($notext as $donotfindthis) { if ($donotfindthis != "") { if (strpos($output, $donotfindthis) !== false) { return -3; } } // text not to find found } } return $elapsed; }