File: 0.02.30a/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 Foobar. 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: function PingTest($host) 37: { 38: // Make Package 39: $type= "\x08"; 40: $code= "\x00"; 41: $checksum= "\x00\x00"; 42: $identifier = "\x00\x00"; 43: $seqNumber = "\x00\x00"; 44: $data= "FreeNATS"; 45: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 46: $checksum = icmpChecksum($package); // Calculate the checksum 47: $package = $type.$code.$checksum.$identifier.$seqNumber.$data; 48: 49: // Create Socket 50: $socket = @socket_create(AF_INET, SOCK_RAW, 1); 51: //or die(socket_strerror(socket_last_error())); 52: if (!$socket) return 0; 53: 54: // Set Non-Blocking 55: @socket_set_nonblock($socket); 56: 57: // Connect Socket 58: $sconn=@socket_connect($socket, $host, null); 59: if (!$sconn) return 0; 60: 61: // Start Timer 62: $startTime = microtime(true); 63: 64: // Send Data 65: @socket_send($socket, $package, strLen($package), 0); 66: 67: // Read Data 68: $keepon=true; 69: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10); 70: else $timeout=10; 71: while( (!(@socket_read($socket, 255))) && $keepon) 72: { // basically just kill time 73: // consider putting some sort of sleepy thing here to lower load but would f* with figures! 74: 75: if ( (microtime(true) - $startTime) > $timeout ) 76: $keepon=false; 77: } 78: 79: if ($keepon) // didn't time out - read data 80: { 81: @socket_close($socket); 82: $ret=round(microtime(true) - $startTime, 4); 83: if ($ret==0) return "0.0001"; 84: else return $ret; 85: } 86: 87: // Socket timed out 88: @socket_close($socket); 89: return 0; 90: } 91: 92: function WebTest($url) 93: { 94: $fp=@fopen($url,"r"); 95: if ($fp<=0) return 0; 96: $ctr=0; 97: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body); 98: @fclose($fp); 99: return $ctr; 100: } 101: 102: function DoTest($test,$param,$hostname="") 103: { 104: switch ($test) 105: { 106: case "web": 107: return WebTest($param); 108: break; 109: case "tcp": 110: $fp=@fsockopen($hostname,$param); 111: if ($fp<=0) return 0; 112: @fclose($fp); 113: return 1; 114: break; 115: case "wtime": 116: $startTime=microtime(true); 117: $r=WebTest($param); 118: $elapsedTime=round(microtime(true)-$startTime,4); 119: if ($r<=0) return -1; 120: if ($r==0) return 0.0001; 121: return $elapsedTime; 122: break; 123: case "testloop": 124: return $param; 125: break; 126: 127: case "testrand": 128: mt_srand(microtime()*1000000); 129: if ( ($param=="") || ($param==0) ) $param=100; 130: return mt_rand(0,$param); 131: break; 132: 133: case "ping": 134: return PingTest($param); 135: break; 136: } 137: return -1; 138: } 139: 140: function SimpleEval($test,$result) 141: { 142: switch($test) 143: { 144: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping) 145: if ($result<=0) return 2; 146: return 0; 147: case "web": 148: if ($result<=0) return 2; 149: return 0; 150: case "tcp": 151: if ($result==1) return 0; 152: return 2; 153: case "wtime": 154: if ($result<0) return 2; 155: return 0; 156: case "testloop": 157: return 0; 158: case "testrand": 159: return 0; 160: } 161: return -1; 162: } 163: 164: function aText($al) 165: { 166: switch($al) 167: { 168: case -1: return "Untested"; 169: case 0: return "Passed"; 170: case 1: return "Warning"; 171: case 2: return "Failed"; 172: default: return "Unknown"; 173: } 174: } 175: ?>