File: 0.02.54a/server/base/tests.inc.php (View as HTML)

  1: <?php // tests.inc.php
  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:     while( (!(@socket_read($socket, 255))) && $keepon)
 75:     	{ // basically just kill time
 76:     	// consider putting some sort of sleepy thing here to lower load but would f* with figures!
 77:     	
 78:     	if ( (microtime(true) - $startTime) > $timeout )
 79:     		$keepon=false;
 80: 		}
 81:     	
 82: 	if ($keepon) // didn't time out - read data
 83:     	{
 84: 	    @socket_close($socket);
 85:     	$ret=round(microtime(true) - $startTime, 4);
 86:     	if ($ret==0) return "0.0001";
 87:     	else 
 88:     		{
 89: 	    	if ($ret<=0) return "0.0001";
 90: 	    	else return $ret;
 91:     		}
 92:     	}
 93:     	
 94:     // Socket timed out
 95:     @socket_close($socket);
 96:     return 0;
 97: 	}
 98: 
 99: function WebTest($url,$timeout=-1)
100: 	{
101: 	global $NATS;
102: 	if ($timeout<=0) // use NATS or env
103: 		{
104: 		if (isset($NATS))
105: 			{
106: 			$nto=$NATS->Cfg->Get("test.http.timeout",-1);
107: 			if ($nto>0) $timeout=$nto; // use NATS timeout
108: 			}
109: 		}
110: 	if ($timeout>0) // use the set timeout
111: 		$oldtimeout=ini_set("default_socket_timeout",$timeout);
112: 	$fp=@fopen($url,"r");
113: 	if ($fp<=0)
114: 		{
115: 		if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
116: 		return -1;
117: 		}
118: 	$ctr=0;
119: 	while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
120: 	@fclose($fp);
121: 	if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
122: 	return $ctr;
123: 	}
124: 	
125: function DoTest($test,$param,$hostname="",$timeout=-1)
126: {
127: switch ($test)
128: 	{
129: 	case "web":
130: 		return WebTest($param,$timeout);
131: 		break;
132: 	case "tcp": // nb TCP does not support timeouts currently
133: 		$fp=@fsockopen($hostname,$param);
134: 		if ($fp<=0) return 0;
135: 		@fclose($fp);
136: 		return 1;
137: 		break;
138: 	case "wtime":
139: 		$startTime=microtime(true);
140: 		$r=WebTest($param,$timeout);
141: 		$elapsedTime=round(microtime(true)-$startTime,4);
142: 		if ($r<0) return -1; // fopen failed
143: 		if ($r==0) return -2; // no chars shown as returned
144: 		if ($elapsedTime<=0) return 0.0001;
145: 		return $elapsedTime;
146: 		break;
147: 	case "testloop":
148: 		return $param;
149: 		break;
150: 		
151: 	case "testrand":
152: 		mt_srand(microtime()*1000000);
153: 		if ( ($param=="") || ($param==0) ) $param=100;
154: 		return mt_rand(0,$param);
155: 		break;
156: 		
157: 	case "ping":
158: 		return PingTest($param,$timeout);
159: 		break;
160: 	}
161: return -1;
162: }
163: 
164: function SimpleEval($test,$result)
165: {
166: switch($test)
167: 	{
168: 	case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
169: 		if ($result<=0) return 2;
170: 		return 0;
171: 	case "web":
172: 		if ($result<=0) return 2;
173: 		return 0;
174: 	case "tcp":
175: 		if ($result==1) return 0;
176: 		return 2;
177: 	case "wtime":
178: 		if ($result<0) return 2;
179: 		return 0;
180: 	case "testloop":
181: 		return 0;
182: 	case "testrand":
183: 		return 0;
184: 	}
185: return -1;
186: }
187: 
188: function aText($al)
189: {
190: switch($al)
191: 	{
192: 	case -1: return "Untested";
193: 	case 0: return "Passed";
194: 	case 1: return "Warning";
195: 	case 2: return "Failed";
196: 	default: return "Unknown";
197: 	}
198: }
199: ?>