File: 1.01.4b/server/base/tests/mysql.inc.php (View as HTML)

  1: <?php // mysql.inc.php -- MySQL Test
  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 fnmysql_error($error)
 24: {
 25: global $NATS;
 26: if (!isset($NATS)) return false;
 27: $NATS->Event("MySQL Test: ".$error,2,"Test","MySQL");
 28: }
 29: 
 30: function mysql_test_rows($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false)
 31: {
 32: global $NATS;
 33: if ($timeout>0) $timeout=$timeout; // use specific for test if set
 34: else
 35: 	{
 36: 	// otherwise use system if available
 37: 	if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.mysql.timeout",0);
 38: 	if ($timeout<=0) $timeout=0; // unset specifically or in environment
 39: 	}
 40: // this will return a 0 at any stage if connect etc is ok but no rows are returned
 41: // negative if something actually fails
 42: if ($timeout>0)
 43: 	{
 44: 	$oldtimeout=get_ini("mysql.timeout");
 45: 	set_ini("mysql.timeout",$timeout);
 46: 	}
 47: 
 48: if ($debug) echo "mysql://".$user.":".$pass."@".$host."/".$database."\n";
 49: 
 50: $sql=@mysql_connect($host,$user,$pass,true);
 51: 
 52: if ((!$sql)||($database==""))
 53: 	{
 54: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 55: 	if (!$sql) 
 56: 		{
 57: 		if ($debug) echo "Connect Error: Failed to Connect\n";
 58: 		fnmysql_error("Failed to Connect");
 59: 		return -1; // total connect failed
 60: 		}
 61: 	// otherwise is no database so close and return 0
 62: 	@mysql_close($sql);
 63: 	return 0;
 64: 	}
 65: 
 66: @mysql_select_db($database,$sql);
 67: 
 68: if (mysql_errno($sql)!=0) // failed to select DB
 69: 	{
 70: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 71: 	fnmysql_error(mysql_error($sql));
 72: 	@mysql_close($sql);
 73: 	return -2;	// select database failed
 74: 	}
 75: 
 76: if ($query=="")
 77: 	{ // no query to perform
 78: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);	
 79: 	@mysql_close($sql);
 80: 	return 0; // all ok but no query/rows
 81: 	}
 82: 	
 83: $r=@mysql_query($query,$sql);
 84: if (mysql_errno($sql)==0) // successful query
 85: 	{
 86: 	if (is_bool($r)) // didn't return any daya
 87: 		{
 88: 		$return=mysql_affected_rows($sql);
 89: 		}
 90: 	else 
 91: 		{
 92: 		$return=mysql_num_rows($r);
 93: 		@mysql_free_result($r,$sql); // free if a result
 94: 		}
 95: 	}
 96: else 
 97: 	{
 98: 	fnmysql_error(mysql_error($sql));
 99: 	$return=-3; // query failed
100: 	}
101: 
102: @mysql_close($sql);
103: if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
104: return $return;
105: }
106: 
107: function mysql_test_time($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false)
108: {
109: $timer=new TFNTimer();
110: $timer->Start();
111: $val=mysql_test_rows($host,$user,$pass,$database,$timeout,$query,$debug);
112: $time=$timer->Stop();
113: 
114: if ($val<0) return $val; // connect/select/query failed
115: 
116: // if $val is 0 then nothing was returned - maybe check the query here? Complicates the idea
117: // though for the user so left. Will have to do two tests for both time and rows 0 as fails
118: 
119: $time=round($time,4);
120: if ($time==0) return "0.0001";
121: return $time;
122: }
123: 
124: 
125: if (isset($NATS))
126: {
127: class FreeNATS_MySQL_Test extends FreeNATS_Local_Test
128: 	{
129: 	function DoTest($testname,$param,$hostname,$timeout,$params)
130: 		{ // 0: host, 1: user, 2: pass, 3: database, 4: query
131: 		
132: 		if ($testname=="mysql")
133: 			{
134: 			$ip=ip_lookup($param);
135: 			if ($ip=="0") return -1; // cache only as 127.0.0.1 is not the same connection as localhost for MySQL auth!
136: 	
137: 			return mysql_test_time($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
138: 			}
139: 			
140: 		else if ($testname=="mysqlrows")
141: 			{	
142: 			$ip=ip_lookup($param);
143: 			if ($ip=="0") return -1; // cache only - see above
144: 			return mysql_test_rows($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
145: 			}
146: 			
147: 		else return -1;
148: 		
149: 		}
150: 	function Evaluate($result) 
151: 		{ // same for both types
152: 		if ($result<0) return 2; // failed
153: 		return 0; // passed
154: 		}
155: 	
156: 	function ProtectOutput(&$test)
157: 		{
158: 		$test['testparam2']="";
159: 		}
160: 		
161: 	function DisplayForm(&$row)
162: 		{
163: 		echo "<table border=0>";
164: 		echo "<tr><td align=left>";
165: 		echo "Hostname :";
166: 		echo "</td><td align=left>";
167: 		echo "<input type=text name=testparam size=30 maxlength=128 value=\"".$row['testparam']."\">";
168: 		echo "</td></tr>";
169: 		echo "<tr><td align=left>";
170: 		echo "Username :";
171: 		echo "</td><td align=left>";
172: 		echo "<input type=text name=testparam1 size=30 maxlength=128 value=\"".$row['testparam1']."\">";
173: 		echo "</td></tr>";
174: 		echo "<tr><td align=left>";
175: 		echo "Password :";
176: 		echo "</td><td align=left>";
177: 		echo "<input type=text name=testparam2 size=30 maxlength=128 value=\"\">";
178: 		echo "<input type=hidden name=keepparam2 value=1>";
179: 		echo "</td></tr>";
180: 		echo "<tr><td colspan=2><i>Leave blank to not change or <input type=checkbox name=clearparam2 value=1> click to clear</i></td></tr>";
181: 		echo "<tr><td align=left>";
182: 		echo "Database :";
183: 		echo "</td><td align=left>";
184: 		echo "<input type=text name=testparam3 size=30 maxlength=128 value=\"".$row['testparam3']."\">";
185: 		echo "</td></tr>";
186: 		echo "<tr><td colspan=2><i>Optional - leave blank to not bother with select_db</td></tr>";
187: 		echo "<tr><td align=left>";
188: 		echo "Query :";
189: 		echo "</td><td align=left>";
190: 		echo "<input type=text name=testparam4 size=30 maxlength=128 value=\"".$row['testparam4']."\">";
191: 		echo "</td></tr>";
192: 		echo "<tr><td colspan=2><i>Optional - leave blank to not bother with a query</td></tr>";
193: 		echo "</table>";
194: 		}
195: 		
196: 	}
197: $params=array();
198: $NATS->Tests->Register("mysql","FreeNATS_MySQL_Test",$params,"MySQL Connect",1,"FreeNATS MySQL Tester");
199: $NATS->Tests->SetUnits("mysql","Seconds","s");
200: $NATS->Tests->Register("mysqlrows","FreeNATS_MySQL_Test",$params,"MySQL Rows",1,"FreeNATS MySQL Tester");
201: $NATS->Tests->SetUnits("mysqlrows","Rows","rows");
202: }
203: 
204: 
205: ?>