function DoTest($testname, $param, $hostname, $timeout, $params) { echo "Called for " . $hostname . " port " . $param . " timeout " . $timeout . "\n"; $timer = new TFNTimer(); $ip = ip_lookup($hostname); echo $hostname . " => " . $ip . "\n"; if ($ip == "0") { return -2; } // lookup failed echo "Lookup Successful\n"; $errno = 0; $errstr = ""; $timer->Start(); echo "Doing fsockopen()\n"; $fp = @fsockopen($ip, $param, $errno, $errstr, $timeout); $elapsed = $timer->Stop(); echo "FP is : "; echo $fp; echo "\n"; if ($fp === false) { return -1; } // open failed echo "Closing\n"; @fclose($fp); return $elapsed; }
function DoTest($testname, $param, $hostname, $timeout, $params) { global $NATS; $timer = new TFNTimer(); if ($timeout <= 0) { $timeout = $NATS->Cfg->Get("test.tcp.timeout", 0); } // if no test-specific param use sys default if ($timeout <= 0) { $timeout = 60; } // if sys default is <=0 then default to 60 seconds $ip = ip_lookup($hostname); if ($ip == "0") { return -2; } // lookup failed $errno = 0; $errstr = ""; $timer->Start(); $fp = @fsockopen($ip, $param, $errno, $errstr, $timeout); $elapsed = $timer->Stop(); if ($fp === false) { return -1; } // open failed @fclose($fp); return $elapsed; }
function imap_test_time($host,$user,$pass,$timeout=-1,$protocol="imap",$port=-1,$ssl=false,$debug=false) { $timer=new TFNTimer(); $timer->Start(); $res=imap_test_connect($host,$user,$pass,$timeout,$protocol,$port,$ssl,$debug); $time=$timer->Stop(); if ($res<=0) return $res; // test failed to connect $time=round($time,4); if ($time==0) $time=0.0001; return $time; }
function smtp_test_time($hostname,$port=25,$timeout=-1,$debug=false) { $timer=new TFNTimer(); $timer->Start(); $res=smtp_test_connect($hostname,$port,$timeout,$debug); $time=$timer->Stop(); if ($res<=0) return $res; // connect failed $time=round($time,4); if ($time==0) $time=0.0001; return $time; }
function smb_connect_time($share, $username, $password) { global $smbclientBinary, $NATS; $timer = new TFNTimer(); $cmd = $smbclientBinary . " "; if ($password != "") { $username = $username . "%" . $password; } if ($username != "") { $cmd .= "--user " . $username . " "; } $cmd .= "-N "; $cmd .= "-c exit"; $cmd .= " " . $share; $timer->Start(); //echo $cmd; $output = array(); $line = exec($cmd); //echo $line; $time = $timer->Stop(); // Domain= if (strlen($line) > 7) { $dom = substr($line, 0, 7); if ($dom == "Domain=") { $result = true; } else { $result = false; } } else { if ($line == "") { $result = true; // blank weird output } else { $result = false; if (isset($NATS)) { $NATS->Event("SMB Failed: " . $line, 2, "Test", "SMB"); } } } if (!$result) { return -1; } // connect failed $time = round($time, 4); if ($time == 0) { $time = 0.0001; } return $time; }
function DoTest($testname, $param, $hostname, $timeout, $params) { $timer = new TFNTimer(); if ($param == "") { $dnsserver = $hostname; } else { $dnsserver = $param; } if ($dnsserver == "") { return -3; } if ($timeout <= 0) { $timeout = 60; } if ($params[4] == 1) { $udp = false; } else { $udp = true; } if ($params[3] == "" || $params[3] == 0) { $port = 53; } else { $port = $params[3]; } $dns_query = new DNSQuery($dnsserver, $port, $timeout, $udp, false); // run with debug off $type = $params[2]; $query = $params[1]; $timer->Start(); $answer = $dns_query->Query($query, $type); $elapsedTime = $timer->Stop(); if ($answer === false || $dns_query->error) { return -2; } // query error if ($answer->count <= 0) { return -1; } // no records returned // otherwise we've got some results ok $elapsedTime = round($elapsedTime, 4); if ($elapsedTime <= 0) { return 0.0001; } return $elapsedTime; break; }
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 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; if ($timeout <= 0) { $timeout = $NATS->Cfg->Get("test.udp.timeout", 0); } // if no test-specific param use sys default if ($timeout <= 0) { $timeout = 20; } // if sys default is <=0 then default to 60 seconds if ($params[1] != "") { $package = $params[1]; } else { $package = ""; } if ($params[2] == 1) { $reqresponse = true; } else { $reqresponse = false; } $timer = new TFNTimer(); $ip = ip_lookup($hostname); if ($ip == "0") { return -2; } // lookup failed $connstr = "udp://" . $ip; $errno = 0; $errstr = ""; $timer->Start(); $fp = @fsockopen($connstr, $param, $errno, $errstr, $timeout); if ($fp === false) { return -1; } // open failed stream_set_timeout($fp, $timeout); $write = fwrite($fp, $package); // send some data if (!$write) { return -3; } // failed to send data $read = fgets($fp); @fclose($fp); $elapsed = $timer->Stop(); if (!$read) { if ($reqresponse) { return -4; } else { if (round($elapsed, 0) < $timeout) { return -5; // looks like a hard reject e.g. ICMP port unreachable } } } if ($elapsed == 0) { $elapsed = "0.001"; } return $elapsed; }
function mysql_test_time($host, $user, $pass, $database = "", $timeout = 0, $query = "", $debug = false) { $timer = new TFNTimer(); $timer->Start(); $val = mysql_test_rows($host, $user, $pass, $database, $timeout, $query, $debug); $time = $timer->Stop(); if ($val < 0) { return $val; } // connect/select/query failed // if $val is 0 then nothing was returned - maybe check the query here? Complicates the idea // though for the user so left. Will have to do two tests for both time and rows 0 as fails $time = round($time, 4); if ($time == 0) { return "0.0001"; } return $time; }
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($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; }