File: 0.02.16a/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: return $elapsedTime; 121: break; 122: case "testloop": 123: return $param; 124: break; 125: } 126: return -1; 127: } 128: 129: function SimpleEval($test,$result) 130: { 131: switch($test) 132: { 133: case "ping": 134: if ($result<=0) return 2; 135: return 0; 136: case "web": 137: if ($result<=0) return 2; 138: return 0; 139: case "tcp": 140: if ($result==1) return 0; 141: return 2; 142: case "wtime": 143: if ($result<0) return 2; 144: return 0; 145: } 146: return -1; 147: } 148: 149: function aText($al) 150: { 151: switch($al) 152: { 153: case -1: return "Untested"; 154: case 0: return "Passed"; 155: case 1: return "Warning"; 156: case 2: return "Failed"; 157: default: return "Unknown"; 158: } 159: } 160: ?>