File: 0.02.77a/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: 24: 25: function icmpChecksum($data) 26: { 27: if (strlen($data)%2) 28: $data .= "\x00"; 29: 30: $bit = unpack('n*', $data); 31: $sum = array_sum($bit); 32: 33: while ($sum >> 16) 34: $sum = ($sum >> 16) + ($sum & 0xffff); 35: 36: return pack('n*', ~$sum); 37: } 38: 39: function PingTest($host,$ctimeout=-1) 40: { 41: global $NATS; 42: $FreeNATS_Timer=new TFNTimer(); 43: // Make Package 44: $type= "\x08"; 45: $code= "\x00"; 46: $checksum= "\x00\x00"; 47: $identifier = "\x00\x00"; 48: $seqNumber = "\x00\x00"; 49: $data= "FreeNATS"; 50: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 51: $checksum = icmpChecksum($package); // Calculate the checksum 52: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 53: 54: // Return s or ms(s*1000) 55: $returnsecs=true; 56: if (isset($NATS)) 57: { 58: if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false; 59: } 60: 61: // Timeout Values 62: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10); 63: else $timeout=10; 64: if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed 65: if ($timeout<=0) $timeout=10; // catch-all for defaults bug 66: 67: // Create Socket 68: $socket = @socket_create(AF_INET, SOCK_RAW, 1); 69: //or die(socket_strerror(socket_last_error())); 70: if (!$socket) return 0; 71: 72: // Set Non-Blocking 73: @socket_set_nonblock($socket); 74: 75: // Connect Socket 76: $sconn=@socket_connect($socket, $host, null); 77: if (!$sconn) return 0; 78: 79: // Send Data 80: @socket_send($socket, $package, strLen($package), 0); 81: 82: // Start Timer 83: $FreeNATS_Timer->Start(); 84: $startTime = microtime(true); // need this for the looping section 85: 86: 87: // Read Data 88: $keepon=true; 89: 90: while( (!(@socket_read($socket, 255))) && $keepon) 91: { // basically just kill time 92: // consider putting some sort of sleepy thing here to lower load but would f* with figures! 93: 94: if ( (microtime(true) - $startTime) > $timeout ) 95: $keepon=false; 96: } 97: 98: if ($keepon) // didn't time out - read data 99: { 100: $elapsed=$FreeNATS_Timer->Stop(); 101: if ($returnsecs) $elapsed=round($elapsed,4); 102: else $elapsed=round( ($elapsed*1000),4 ); 103: @socket_close($socket); 104: // $ret=round(microtime(true) - $startTime, 4); -- old method 105: if ($elapsed<=0) $elapsed="0.0001"; // safety catch-all 106: return $elapsed; 107: /* 108: if ($ret==0) return "0.0001"; 109: else 110: { 111: if ($ret<=0) return "0.0001"; 112: else return $ret; 113: } 114: */ 115: } 116: 117: // Socket timed out 118: @socket_close($socket); 119: return 0; 120: } 121: 122: function WebTest($url,$timeout=-1) 123: { 124: global $NATS; 125: if ($timeout<=0) // use NATS or env 126: { 127: if (isset($NATS)) 128: { 129: $nto=$NATS->Cfg->Get("test.http.timeout",-1); 130: if ($nto>0) $timeout=$nto; // use NATS timeout 131: } 132: } 133: if ($timeout>0) // use the set timeout 134: $oldtimeout=ini_set("default_socket_timeout",$timeout); 135: 136: if (function_exists("curl_getinfo")) // use CURL if present 137: { 138: $ch=curl_init(); 139: curl_setopt($ch,CURLOPT_URL,$url); 140: curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 141: curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); 142: curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); 143: if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 144: if (!$output=curl_exec($ch)) 145: { 146: $ctr=-1; // failed 147: } 148: else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2); 149: curl_close($ch); 150: 151: } 152: else 153: { // no CURL - use fopen() 154: $fp=@fopen($url,"r"); 155: if ($fp<=0) 156: { 157: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 158: return -1; 159: } 160: $ctr=0; 161: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body); 162: @fclose($fp); 163: } 164: 165: 166: 167: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 168: return $ctr; 169: } 170: 171: function DoTest($test,$param,$hostname="",$timeout=-1) 172: { 173: switch ($test) 174: { 175: case "web": case "wsize": 176: return WebTest($param,$timeout); 177: break; 178: case "tcp": // nb TCP does not support timeouts currently 179: $fp=@fsockopen($hostname,$param); 180: if ($fp<=0) return 0; 181: @fclose($fp); 182: return 1; 183: break; 184: case "wtime": 185: $timer=new TFNTimer(); 186: $timer->Start(); 187: $r=WebTest($param,$timeout); 188: $elapsedTime=$timer->Stop(); 189: $elapsedTime=round($elapsedTime,4); 190: if ($r<0) return -1; // open failed 191: if ($r==0) return -2; // no chars shown as returned 192: if ($elapsedTime<=0) return 0.0001; 193: return $elapsedTime; 194: break; 195: case "testloop": 196: return $param; 197: break; 198: 199: case "testrand": 200: mt_srand(microtime()*1000000); 201: if ( ($param=="") || ($param==0) ) $param=100; 202: return mt_rand(0,$param); 203: break; 204: 205: case "ping": 206: return PingTest($param,$timeout); 207: break; 208: } 209: return -1; 210: } 211: 212: function SimpleEval($test,$result) 213: { 214: switch($test) 215: { 216: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping) 217: if ($result<=0) return 2; 218: return 0; 219: case "web": case "wsize": 220: if ($result<=0) return 2; 221: return 0; 222: case "tcp": 223: if ($result==1) return 0; 224: return 2; 225: case "wtime": 226: if ($result<0) return 2; 227: return 0; 228: case "testloop": 229: return 0; 230: case "testrand": 231: return 0; 232: } 233: return -1; 234: } 235: 236: function aText($al) 237: { 238: switch($al) 239: { 240: case -1: return "Untested"; 241: case 0: return "Passed"; 242: case 1: return "Warning"; 243: case 2: return "Failed"; 244: default: return "Unknown"; 245: } 246: } 247: ?>