File: 1.11.2a/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-2010 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: // FreeNATS_Local_Test Base Class - Local Tests (run from FreeNATS server) must extend from this
 27: 
 28: class FreeNATS_Local_Test
 29: {
 30: function DoTest($testname,$param,$hostname="",$timeout=-1,$params=false)
 31: 	{
 32: 	return 0;
 33: 	}
 34: 	
 35: function Evaluate($result)
 36: 	{
 37: 	return -1;
 38: 	}
 39: 	
 40: function DisplayForm(&$row)
 41: 	{
 42: 	return false;
 43: 	}
 44: 	
 45: function ProtectOutput(&$row)
 46: 	{
 47: 	return false;
 48: 	}
 49: }
 50: 
 51: // ------------------------------------------------------------------------------------------------
 52: require($BaseDir."tests/tcp.inc.php");
 53: require($BaseDir."tests/udp.inc.php");
 54: require($BaseDir."tests/mysql.inc.php");
 55: require($BaseDir."tests/imap.inc.php");
 56: require($BaseDir."tests/smtp.inc.php");
 57: 
 58: require($BaseDir."tests/dns.inc.php");
 59: require($BaseDir."tests/nats-dns.inc.php"); // the wrapper module
 60: 
 61: require($BaseDir."tests/smb.inc.php");
 62: 
 63: require($BaseDir."tests/ppping.inc.php");
 64: 
 65: function ip_lookup($hostname) // "safe" DNS lookup function to call with a hostname, URL or IP - returns 0 if unsuccessful
 66: {
 67: // Is it already an IP adress?
 68: $out=str_replace(".","",$hostname);
 69: if (is_numeric($out)) return $hostname; // yes it is
 70: 
 71: // No it is not
 72: $ip=@gethostbyname($hostname);
 73: if ($ip==$hostname) return 0; // unmodified host - lookup failed
 74: 
 75: return $ip;
 76: }
 77: 
 78: function next_run_x($interval)
 79: {
 80: if ($interval<1) return time();
 81: return time()+(($interval)*60)-30;
 82: }
 83: 
 84: function test_sleep()
 85: {
 86: global $NATS;
 87: if (!isset($NATS)) return false;
 88: $sleep=$NATS->Cfg->Get("test.interval",0);
 89: if ($sleep<=0) return false;
 90: $sleep=$sleep*1000000; // convert to usec
 91: usleep($sleep);
 92: return true;
 93: }
 94: 	
 95: function url_lookup($url)
 96: {
 97: // Sod regular expressions here as we'd have to do it twice or with cleverness I lack
 98: // Is it a URL?
 99: $colon=strpos($url,":");
100: if ($colon != 0) // exists so it a URL
101: 	{
102: 	$out=preg_match("@^(?:http[s]*://)?([^/|\?|:]+)@i",$url,$matches);
103: 	$hostname=$matches[1];
104: 	}
105: else $hostname=$url; // try direct
106: 	
107: return ip_lookup($hostname);
108: }
109: 	
110: 	
111: function bin_str_dump($s,$count=0)
112: {
113: //$s = base_convert($s,10,2);
114: $data = unpack('C*',$s);
115: foreach($data as $item)
116: 	{
117: 	echo ord($item)." ";
118: 	}
119: echo "\n";
120: }
121: 
122: 
123: 
124: function PingTest($host,$ctimeout=-1)
125: 	{
126: 	global $NATS;
127: 
128:     $returnsecs=true;
129:     if (isset($NATS))
130:     	{
131: 	    if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false;
132:     	}
133: 
134: 	// Timeout Values
135:     if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
136:     else $timeout=10;
137:     if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed
138:     if ($timeout<=0) $timeout=10; // catch-all for defaults bug
139:     
140: 	$ping = new PPPing();
141: 	$ping->hostname = $host;
142: 	$ping->timeout = $timeout;
143: 	
144: 	$result=$ping->Ping();
145: 	
146: 	if ($result<0) // error condition
147: 		{
148: 		return $result;
149: 		}
150: 	else if ($result==0) // zero time
151: 		{
152: 		$result="0.0001";
153: 		}
154: 	
155: 	if ($returnsecs)
156: 		{
157: 		$result = round($result/1000,3); // convert to seconds
158: 		if ($result==0) $result=0.0001;
159: 		}
160: 		
161: 	return $result;
162: 	}
163: 
164: function WebTest($url,$timeout=-1)
165: 	{
166: 	global $NATS;
167: 	if ($timeout<=0) // use NATS or env
168: 		{
169: 		if (isset($NATS))
170: 			{
171: 			$nto=$NATS->Cfg->Get("test.http.timeout",-1);
172: 			if ($nto>0) $timeout=$nto; // use NATS timeout
173: 			}
174: 		}
175: 	if ($timeout>0) // use the set timeout
176: 		$oldtimeout=ini_set("default_socket_timeout",$timeout);
177: 		
178: 	if (function_exists("curl_getinfo")) // use CURL if present
179: 		{
180: 		$ch=curl_init();
181: 		curl_setopt($ch,CURLOPT_URL,$url);
182: 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
183: 		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
184: 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
185: 		curl_setopt($ch,CURLOPT_HEADER,1);
186: 		if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
187: 		if ($timeout>0) curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
188: 		if (!$output=curl_exec($ch))
189: 			{
190: 			$ctr=-1; // failed
191: 			}
192: 		else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
193: 		curl_close($ch);
194: 		
195: 		if ($ctr==0) $ctr="0.0001";
196: 		
197: 		}
198: 	else
199: 		{	// no CURL - use fopen()	
200: 		$oldagent=ini_set("user_agent","MSIE 4\.0b2;"); // MSIE 4.0b2 is HTTP/1.0 only just like fopen http wrapper
201: 		$fp=@fopen($url,"r");
202: 		if ($fp<=0)
203: 			{
204: 			if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
205: 			ini_set("user_agent",$oldagent);
206: 			return -1;
207: 			}
208: 		$ctr=0;
209: 		while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
210: 		@fclose($fp);
211: 		ini_set("user_agent",$oldagent);
212: 		}
213: 	
214: 	
215: 	
216: 	if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
217: 	return $ctr;
218: 	}
219: 	
220: function DoTest($test,$param,$hostname="",$timeout=-1,$params=0,$nodeid="")
221: {
222: global $NATS;
223: if (!is_array($params))
224: 	{
225: 	$params=array();
226: 	for ($a=0; $a<10; $a++) $params[$a]="";
227: 	}
228: 	
229: switch ($test)
230: 	{
231: 	case "web": case "wsize":
232: 		// Don't bother with pre-resolution as size only
233: 		return WebTest($param,$timeout);
234: 		break;
235: 	/* -- modularised
236: 	case "tcp": // nb TCP does not support timeouts currently
237: 		$ip=ip_lookup($hostname);
238: 		if ($ip=="0") return 0;
239: 		$fp=@fsockopen($ip,$param);
240: 		if ($fp<=0) return 0;
241: 		@fclose($fp);
242: 		return 1;
243: 		break;
244: 	*/
245: 	case "wtime":
246: 		$timer=new TFNTimer();
247: 		// Do a pre-lookup
248: 		$ip=url_lookup($param);
249: 		if ($ip=="0") return -1; // dns lookup failed
250: 		$timer->Start();
251: 		$r=WebTest($param,$timeout);
252: 		$elapsedTime=$timer->Stop();
253: 		$elapsedTime=round($elapsedTime,4);
254: 		if ($r<0) return -1; // open failed
255: 		if ($r==0) return -2; // no chars shown as returned
256: 		if ($elapsedTime<=0) return 0.0001;
257: 		return $elapsedTime;
258: 		break;
259: 		
260: 	case "host":
261: 		$timer=new TFNTimer();
262: 		if (preg_match("/[a-zA-Z]/",$param)>0) $is_ip=false;
263: 		else $is_ip=true;
264: 		
265: 		$timer->Start();
266: 		if ($is_ip) $result=gethostbyaddr($param);
267: 		else $result=gethostbyname($param);
268: 		$elapsedTime=$timer->Stop();
269: 		
270: 		if ($result==$param) // lookup failed
271: 			return -1;
272: 			
273: 		if ($result=="")	// lookup failed
274: 			return -1;
275: 			
276: 		$elapsedTime=round($elapsedTime,4);
277: 		if ($elapsedTime<=0) return 0.0001;
278: 		return $elapsedTime;
279: 		break;
280: 
281: 	case "testloop":
282: 		return $param;
283: 		break;
284: 		
285: 	case "testrand":
286: 		mt_srand(microtime()*1000000);
287: 		if ( ($param=="") || ($param==0) ) $param=100;
288: 		return mt_rand(0,$param);
289: 		break;
290: 		
291: 	case "ping":
292: 		return PingTest($param,$timeout);
293: 		break;
294: 
295: 	default:
296: 		if (isset($NATS)) // try and see if a test is registered
297: 			{
298: 			if (isset($NATS->Tests->QuickList[$test])) // exists
299: 				{
300: 				$NATS->Tests->Tests[$test]->Create();
301: 				return $NATS->Tests->Tests[$test]->instance->DoTest($test,$param,$hostname,$timeout,$params);
302: 				}
303: 			}
304: 		
305: 	}
306: return -1; // did not run any test so untested
307: }
308: 
309: function SimpleEval($test,$result)
310: {
311: global $NATS;
312: switch($test)
313: 	{
314: 	case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
315: 		if ($result<=0) return 2;
316: 		return 0;
317: 	case "web": case "wsize":
318: 		if ($result<=0) return 2;
319: 		return 0;
320: 	/*
321: 	case "tcp":
322: 		if ($result==1) return 0;
323: 		return 2;
324: 	*/
325: 	case "wtime":
326: 		if ($result<0) return 2;
327: 		return 0;
328: 	/*
329: 	case "mysql":
330: 		if ($result<=0) return 2;
331: 		return 0;
332: 		
333: 	case "mysqlrows":
334: 		if ($result<=0) return 2; // no rows returned or error
335: 		return 0;
336: 	*/	
337: 	case "host": case "dns":
338: 		if ($result<=0) return 2; // no records returned or error
339: 		
340: 	case "testloop":
341: 		return 0;
342: 	case "testrand":
343: 		return 0;
344: 		
345: 	default:
346: 	if (isset($NATS))
347: 		{
348: 		if (isset($NATS->Tests->QuickList[$test]))
349: 			{
350: 			$NATS->Tests->Tests[$test]->Create();
351: 			return $NATS->Tests->Tests[$test]->instance->Evaluate($result);
352: 			}
353: 		}
354: 	}
355: return -1; // untested if we don't know WTF the result was
356: }
357: 
358: function aText($al)
359: {
360: return oText($al); // uses function in tests.inc.php with site config support	
361: /* -- depreciated
362: switch($al)
363: 	{
364: 	case -1: return "Untested";
365: 	case 0: return "Passed";
366: 	case 1: return "Warning";
367: 	case 2: return "Failed";
368: 	default: return "Unknown";
369: 	}
370: */
371: }
372: ?>