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