File: 0.04.03a/server/base/tests.inc.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeNATS 4: 5: FreeNATS is (C) Copyright 2008 PurplePixie Systems 6: 7: FreeNATS is free software: you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation, either version 3 of the License, or 10: (at your option) any later version. 11: 12: FreeNATS is distributed in the hope that it will be useful, 13: but WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15: GNU General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with FreeNATS. If not, see www.gnu.org/licenses 19: 20: For more information see www.purplepixie.org/freenats 21: -------------------------------------------------------------- */ 22: 23: if (!isset($BaseDir)) $BaseDir="../base/"; // 24: require_once($BaseDir."timer.inc.php"); // just in case standalone 25: 26: require($BaseDir."tests/mysql.inc.php"); 27: require($BaseDir."tests/imap.inc.php"); 28: require($BaseDir."tests/smtp.inc.php"); 29: 30: function ip_lookup($hostname) // "safe" DNS lookup function to call with a hostname, URL or IP - returns 0 if unsuccessful 31: { 32: // Is it already an IP adress? 33: $out=str_replace(".","",$hostname); 34: if (is_numeric($out)) return $hostname; // yes it is 35: 36: // No it is not 37: $ip=@gethostbyname($hostname); 38: if ($ip==$hostname) return 0; // unmodified host - lookup failed 39: 40: return $ip; 41: } 42: 43: 44: 45: function url_lookup($url) 46: { 47: // Sod regular expressions here as we'd have to do it twice or with cleverness I lack 48: // Is it a URL? 49: $colon=strpos($url,":"); 50: if ($colon != 0) // exists so it a URL 51: { 52: $out=preg_match("@^(?:http[s]*://)?([^/|\?|:]+)@i",$url,$matches); 53: $hostname=$matches[1]; 54: } 55: else $hostname=$url; // try direct 56: 57: return ip_lookup($hostname); 58: } 59: 60: 61: 62: 63: 64: 65: 66: function icmpChecksum($data) 67: { 68: if (strlen($data)%2) 69: $data .= "\x00"; 70: 71: $bit = unpack('n*', $data); 72: $sum = array_sum($bit); 73: 74: while ($sum >> 16) 75: $sum = ($sum >> 16) + ($sum & 0xffff); 76: 77: return pack('n*', ~$sum); 78: } 79: 80: function PingTest($host,$ctimeout=-1) 81: { 82: global $NATS; 83: $FreeNATS_Timer=new TFNTimer(); 84: // Make Package 85: $type= "\x08"; 86: $code= "\x00"; 87: $checksum= "\x00\x00"; 88: $identifier = "\x00\x00"; 89: $seqNumber = "\x00\x00"; 90: $data= "FreeNATS"; 91: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 92: $checksum = icmpChecksum($package); // Calculate the checksum 93: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 94: 95: // Return s or ms(s*1000) 96: $returnsecs=true; 97: if (isset($NATS)) 98: { 99: if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false; 100: } 101: 102: // Timeout Values 103: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10); 104: else $timeout=10; 105: if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed 106: if ($timeout<=0) $timeout=10; // catch-all for defaults bug 107: 108: // Create Socket 109: $socket = @socket_create(AF_INET, SOCK_RAW, 1); 110: //or die(socket_strerror(socket_last_error())); 111: if (!$socket) return 0; 112: 113: // Set Non-Blocking 114: @socket_set_nonblock($socket); 115: 116: // Connect Socket 117: $sconn=@socket_connect($socket, $host, null); 118: if (!$sconn) return 0; 119: 120: // Send Data 121: @socket_send($socket, $package, strLen($package), 0); 122: 123: // Start Timer 124: $FreeNATS_Timer->Start(); 125: $startTime = microtime(true); // need this for the looping section 126: 127: 128: // Read Data 129: $keepon=true; 130: 131: while( (!(@socket_read($socket, 255))) && $keepon) 132: { // basically just kill time 133: // consider putting some sort of sleepy thing here to lower load but would f* with figures! 134: 135: if ( (microtime(true) - $startTime) > $timeout ) 136: $keepon=false; 137: } 138: 139: if ($keepon) // didn't time out - read data 140: { 141: $elapsed=$FreeNATS_Timer->Stop(); 142: if ($returnsecs) $elapsed=round($elapsed,4); 143: else $elapsed=round( ($elapsed*1000),4 ); 144: @socket_close($socket); 145: // $ret=round(microtime(true) - $startTime, 4); -- old method 146: if ($elapsed<=0) $elapsed="0.0001"; // safety catch-all 147: return $elapsed; 148: /* 149: if ($ret==0) return "0.0001"; 150: else 151: { 152: if ($ret<=0) return "0.0001"; 153: else return $ret; 154: } 155: */ 156: } 157: 158: // Socket timed out 159: @socket_close($socket); 160: return 0; 161: } 162: 163: function WebTest($url,$timeout=-1) 164: { 165: global $NATS; 166: if ($timeout<=0) // use NATS or env 167: { 168: if (isset($NATS)) 169: { 170: $nto=$NATS->Cfg->Get("test.http.timeout",-1); 171: if ($nto>0) $timeout=$nto; // use NATS timeout 172: } 173: } 174: if ($timeout>0) // use the set timeout 175: $oldtimeout=ini_set("default_socket_timeout",$timeout); 176: 177: if (function_exists("curl_getinfo")) // use CURL if present 178: { 179: $ch=curl_init(); 180: curl_setopt($ch,CURLOPT_URL,$url); 181: curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 182: curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); 183: curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); 184: curl_setopt($ch,CURLOPT_HEADER,1); 185: if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 186: if (!$output=curl_exec($ch)) 187: { 188: $ctr=-1; // failed 189: } 190: else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2); 191: curl_close($ch); 192: 193: if ($ctr==0) $ctr="0.0001"; 194: 195: } 196: else 197: { // no CURL - use fopen() 198: $fp=@fopen($url,"r"); 199: if ($fp<=0) 200: { 201: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 202: return -1; 203: } 204: $ctr=0; 205: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body); 206: @fclose($fp); 207: } 208: 209: 210: 211: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 212: return $ctr; 213: } 214: 215: function DoTest($test,$param,$hostname="",$timeout=-1,$params=0) 216: { 217: if (!is_array($params)) 218: { 219: $params=array(); 220: for ($a=0; $a<10; $a++) $params[$a]=""; 221: } 222: 223: switch ($test) 224: { 225: case "web": case "wsize": 226: return WebTest($param,$timeout); 227: break; 228: case "tcp": // nb TCP does not support timeouts currently 229: $fp=@fsockopen($hostname,$param); 230: if ($fp<=0) return 0; 231: @fclose($fp); 232: return 1; 233: break; 234: case "wtime": 235: $timer=new TFNTimer(); 236: // Do a pre-lookup 237: $ip=url_lookup($param); 238: if ($ip=="0") return -1; // dns lookup failed 239: $timer->Start(); 240: $r=WebTest($param,$timeout); 241: $elapsedTime=$timer->Stop(); 242: $elapsedTime=round($elapsedTime,4); 243: if ($r<0) return -1; // open failed 244: if ($r==0) return -2; // no chars shown as returned 245: if ($elapsedTime<=0) return 0.0001; 246: return $elapsedTime; 247: break; 248: case "testloop": 249: return $param; 250: break; 251: 252: case "testrand": 253: mt_srand(microtime()*1000000); 254: if ( ($param=="") || ($param==0) ) $param=100; 255: return mt_rand(0,$param); 256: break; 257: 258: case "ping": 259: return PingTest($param,$timeout); 260: break; 261: 262: case "smtp": 263: return smtp_test_time($param,$params[1],$timeout); 264: break; 265: 266: case "imap": 267: if ($params[5]==1) $ssl=true; 268: else $ssl=false; 269: 270: return imap_test_time($param,$params[1],$params[2],$timeout,$params[3],$params[4],$ssl); 271: break; 272: 273: case "mysql": 274: return mysql_test_time($param,$params[1],$params[2],$params[3],$timeout,$params[4]); 275: break; 276: 277: case "mysqlrows": 278: return mysql_test_rows($param,$params[1],$params[2],$params[3],$timeout,$params[4]); 279: break; 280: 281: 282: } 283: return -1; 284: } 285: 286: function SimpleEval($test,$result) 287: { 288: switch($test) 289: { 290: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping) 291: if ($result<=0) return 2; 292: return 0; 293: case "web": case "wsize": 294: if ($result<=0) return 2; 295: return 0; 296: case "tcp": 297: if ($result==1) return 0; 298: return 2; 299: case "wtime": 300: if ($result<0) return 2; 301: return 0; 302: 303: case "imap": 304: if ($result<=0) return 2; 305: return 0; 306: 307: case "smtp": 308: if ($result<=0) return 2; 309: return 0; 310: 311: case "mysql": 312: if ($result<=0) return 2; 313: return 0; 314: 315: case "mysqlrows": 316: if ($result<=0) return 2; // no rows returned or error 317: return 0; 318: 319: case "testloop": 320: return 0; 321: case "testrand": 322: return 0; 323: } 324: return -1; 325: } 326: 327: function aText($al) 328: { 329: switch($al) 330: { 331: case -1: return "Untested"; 332: case 0: return "Passed"; 333: case 1: return "Warning"; 334: case 2: return "Failed"; 335: default: return "Unknown"; 336: } 337: } 338: ?>