File:
0.02.78a/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: curl_setopt($ch,CURLOPT_HEADER,1);
144: if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
145: if (!$output=curl_exec($ch))
146: {
147: $ctr=-1; // failed
148: }
149: else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
150: curl_close($ch);
151:
152: if ($ctr==0) $ctr="0.0001";
153:
154: }
155: else
156: { // no CURL - use fopen()
157: $fp=@fopen($url,"r");
158: if ($fp<=0)
159: {
160: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
161: return -1;
162: }
163: $ctr=0;
164: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
165: @fclose($fp);
166: }
167:
168:
169:
170: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
171: return $ctr;
172: }
173:
174: function DoTest($test,$param,$hostname="",$timeout=-1)
175: {
176: switch ($test)
177: {
178: case "web": case "wsize":
179: return WebTest($param,$timeout);
180: break;
181: case "tcp": // nb TCP does not support timeouts currently
182: $fp=@fsockopen($hostname,$param);
183: if ($fp<=0) return 0;
184: @fclose($fp);
185: return 1;
186: break;
187: case "wtime":
188: $timer=new TFNTimer();
189: $timer->Start();
190: $r=WebTest($param,$timeout);
191: $elapsedTime=$timer->Stop();
192: $elapsedTime=round($elapsedTime,4);
193: if ($r<0) return -1; // open failed
194: if ($r==0) return -2; // no chars shown as returned
195: if ($elapsedTime<=0) return 0.0001;
196: return $elapsedTime;
197: break;
198: case "testloop":
199: return $param;
200: break;
201:
202: case "testrand":
203: mt_srand(microtime()*1000000);
204: if ( ($param=="") || ($param==0) ) $param=100;
205: return mt_rand(0,$param);
206: break;
207:
208: case "ping":
209: return PingTest($param,$timeout);
210: break;
211: }
212: return -1;
213: }
214:
215: function SimpleEval($test,$result)
216: {
217: switch($test)
218: {
219: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
220: if ($result<=0) return 2;
221: return 0;
222: case "web": case "wsize":
223: if ($result<=0) return 2;
224: return 0;
225: case "tcp":
226: if ($result==1) return 0;
227: return 2;
228: case "wtime":
229: if ($result<0) return 2;
230: return 0;
231: case "testloop":
232: return 0;
233: case "testrand":
234: return 0;
235: }
236: return -1;
237: }
238:
239: function aText($al)
240: {
241: switch($al)
242: {
243: case -1: return "Untested";
244: case 0: return "Passed";
245: case 1: return "Warning";
246: case 2: return "Failed";
247: default: return "Unknown";
248: }
249: }
250: ?>