File:
0.04.09a/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: if (!isset($BaseDir)) $BaseDir="../base/"; //
24: require_once($BaseDir."timer.inc.php"); // just in case standalone
25:
26: require($BaseDir."tests/mysql.inc.php");
27: require($BaseDir."tests/imap.inc.php");
28: require($BaseDir."tests/smtp.inc.php");
29:
30: function ip_lookup($hostname) // "safe" DNS lookup function to call with a hostname, URL or IP - returns 0 if unsuccessful
31: {
32: // Is it already an IP adress?
33: $out=str_replace(".","",$hostname);
34: if (is_numeric($out)) return $hostname; // yes it is
35:
36: // No it is not
37: $ip=@gethostbyname($hostname);
38: if ($ip==$hostname) return 0; // unmodified host - lookup failed
39:
40: return $ip;
41: }
42:
43:
44:
45: function url_lookup($url)
46: {
47: // Sod regular expressions here as we'd have to do it twice or with cleverness I lack
48: // Is it a URL?
49: $colon=strpos($url,":");
50: if ($colon != 0) // exists so it a URL
51: {
52: $out=preg_match("@^(?:http[s]*://)?([^/|\?|:]+)@i",$url,$matches);
53: $hostname=$matches[1];
54: }
55: else $hostname=$url; // try direct
56:
57: return ip_lookup($hostname);
58: }
59:
60:
61:
62:
63:
64:
65:
66: function icmpChecksum($data)
67: {
68: if (strlen($data)%2)
69: $data .= "\x00";
70:
71: $bit = unpack('n*', $data);
72: $sum = array_sum($bit);
73:
74: while ($sum >> 16)
75: $sum = ($sum >> 16) + ($sum & 0xffff);
76:
77: return pack('n*', ~$sum);
78: }
79:
80: function PingTest($host,$ctimeout=-1)
81: {
82: global $NATS;
83: $FreeNATS_Timer=new TFNTimer();
84: // Make Package
85: $type= "\x08";
86: $code= "\x00";
87: $checksum= "\x00\x00";
88: $identifier = "\x00\x00";
89: $seqNumber = "\x00\x00";
90: $data= "FreeNATS";
91: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
92: $checksum = icmpChecksum($package); // Calculate the checksum
93: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
94:
95: // Return s or ms(s*1000)
96: $returnsecs=true;
97: if (isset($NATS))
98: {
99: if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false;
100: }
101:
102: // Timeout Values
103: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
104: else $timeout=10;
105: if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed
106: if ($timeout<=0) $timeout=10; // catch-all for defaults bug
107:
108: // Create Socket
109: $socket = @socket_create(AF_INET, SOCK_RAW, 1);
110: //or die(socket_strerror(socket_last_error()));
111: if (!$socket) return 0;
112:
113: // Set Non-Blocking
114: @socket_set_nonblock($socket);
115:
116: // Connect Socket
117: $sconn=@socket_connect($socket, $host, null);
118: if (!$sconn) return 0;
119:
120: // Send Data
121: @socket_send($socket, $package, strLen($package), 0);
122:
123: // Start Timer
124: $FreeNATS_Timer->Start();
125: $startTime = microtime(true); // need this for the looping section
126:
127:
128: // Read Data
129: $keepon=true;
130:
131: while( (!(@socket_read($socket, 255))) && $keepon)
132: { // basically just kill time
133: // consider putting some sort of sleepy thing here to lower load but would f* with figures!
134:
135: if ( (microtime(true) - $startTime) > $timeout )
136: $keepon=false;
137: }
138:
139: if ($keepon) // didn't time out - read data
140: {
141: $elapsed=$FreeNATS_Timer->Stop();
142: if ($returnsecs) $elapsed=round($elapsed,4);
143: else $elapsed=round( ($elapsed*1000),4 );
144: @socket_close($socket);
145: // $ret=round(microtime(true) - $startTime, 4); -- old method
146: if ($elapsed<=0) $elapsed="0.0001"; // safety catch-all
147: return $elapsed;
148: /*
149: if ($ret==0) return "0.0001";
150: else
151: {
152: if ($ret<=0) return "0.0001";
153: else return $ret;
154: }
155: */
156: }
157:
158: // Socket timed out
159: @socket_close($socket);
160: return 0;
161: }
162:
163: function WebTest($url,$timeout=-1)
164: {
165: global $NATS;
166: if ($timeout<=0) // use NATS or env
167: {
168: if (isset($NATS))
169: {
170: $nto=$NATS->Cfg->Get("test.http.timeout",-1);
171: if ($nto>0) $timeout=$nto; // use NATS timeout
172: }
173: }
174: if ($timeout>0) // use the set timeout
175: $oldtimeout=ini_set("default_socket_timeout",$timeout);
176:
177: if (function_exists("curl_getinfo")) // use CURL if present
178: {
179: $ch=curl_init();
180: curl_setopt($ch,CURLOPT_URL,$url);
181: curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
182: curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
183: curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
184: curl_setopt($ch,CURLOPT_HEADER,1);
185: if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
186: if (!$output=curl_exec($ch))
187: {
188: $ctr=-1; // failed
189: }
190: else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
191: curl_close($ch);
192:
193: if ($ctr==0) $ctr="0.0001";
194:
195: }
196: else
197: { // no CURL - use fopen()
198: $fp=@fopen($url,"r");
199: if ($fp<=0)
200: {
201: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
202: return -1;
203: }
204: $ctr=0;
205: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
206: @fclose($fp);
207: }
208:
209:
210:
211: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
212: return $ctr;
213: }
214:
215: function DoTest($test,$param,$hostname="",$timeout=-1,$params=0)
216: {
217: if (!is_array($params))
218: {
219: $params=array();
220: for ($a=0; $a<10; $a++) $params[$a]="";
221: }
222:
223: switch ($test)
224: {
225: case "web": case "wsize":
226: // Don't bother with pre-resolution as size only
227: return WebTest($param,$timeout);
228: break;
229: case "tcp": // nb TCP does not support timeouts currently
230: $ip=ip_lookup($hostname);
231: if ($ip=="0") return 0;
232: $fp=@fsockopen($ip,$param);
233: if ($fp<=0) return 0;
234: @fclose($fp);
235: return 1;
236: break;
237: case "wtime":
238: $timer=new TFNTimer();
239: // Do a pre-lookup
240: $ip=url_lookup($param);
241: if ($ip=="0") return -1; // dns lookup failed
242: $timer->Start();
243: $r=WebTest($param,$timeout);
244: $elapsedTime=$timer->Stop();
245: $elapsedTime=round($elapsedTime,4);
246: if ($r<0) return -1; // open failed
247: if ($r==0) return -2; // no chars shown as returned
248: if ($elapsedTime<=0) return 0.0001;
249: return $elapsedTime;
250: break;
251: case "testloop":
252: return $param;
253: break;
254:
255: case "testrand":
256: mt_srand(microtime()*1000000);
257: if ( ($param=="") || ($param==0) ) $param=100;
258: return mt_rand(0,$param);
259: break;
260:
261: case "ping":
262: return PingTest($param,$timeout);
263: break;
264:
265: case "smtp":
266: $ip=ip_lookup($param);
267: if ($ip=="0") return -1;
268: return smtp_test_time($ip,$params[1],$timeout);
269: break;
270:
271: case "imap":
272: if ($params[5]==1) $ssl=true;
273: else $ssl=false;
274:
275: $ip=ip_lookup($param);
276: if ($ip=="0") return -1;
277:
278: return imap_test_time($ip,$params[1],$params[2],$timeout,$params[3],$params[4],$ssl);
279: break;
280:
281: case "mysql":
282: $ip=ip_lookup($param);
283: if ($ip=="0") return -1; // cache only as 127.0.0.1 is not the same connection as localhost for MySQL auth!
284:
285: return mysql_test_time($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
286: break;
287:
288: case "mysqlrows":
289: $ip=ip_lookup($param);
290: if ($ip=="0") return -1; // cache only - see above
291: return mysql_test_rows($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
292: break;
293:
294:
295: }
296: return -1;
297: }
298:
299: function SimpleEval($test,$result)
300: {
301: switch($test)
302: {
303: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
304: if ($result<=0) return 2;
305: return 0;
306: case "web": case "wsize":
307: if ($result<=0) return 2;
308: return 0;
309: case "tcp":
310: if ($result==1) return 0;
311: return 2;
312: case "wtime":
313: if ($result<0) return 2;
314: return 0;
315:
316: case "imap":
317: if ($result<=0) return 2;
318: return 0;
319:
320: case "smtp":
321: if ($result<=0) return 2;
322: return 0;
323:
324: case "mysql":
325: if ($result<=0) return 2;
326: return 0;
327:
328: case "mysqlrows":
329: if ($result<=0) return 2; // no rows returned or error
330: return 0;
331:
332: case "testloop":
333: return 0;
334: case "testrand":
335: return 0;
336: }
337: return -1;
338: }
339:
340: function aText($al)
341: {
342: switch($al)
343: {
344: case -1: return "Untested";
345: case 0: return "Passed";
346: case 1: return "Warning";
347: case 2: return "Failed";
348: default: return "Unknown";
349: }
350: }
351: ?>