File: 1.13.3b/server/base/tests/mysql.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: 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: 31: 32: // Data Test 33: function mysql_test_data($host,$user,$pass,$database="",$timeout=0,$query="",$column=0,$debug=false) 34: { 35: global $NATS; 36: if ($timeout>0) $timeout=$timeout; // use specific for test if set 37: else 38: { 39: // otherwise use system if available 40: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.mysql.timeout",0); 41: if ($timeout<=0) $timeout=0; // unset specifically or in environment 42: } 43: // this will return a 0 at any stage if connect etc is ok but no rows are returned 44: // negative if something actually fails 45: if ($timeout>0) 46: { 47: $oldtimeout=ini_get("mysql.timeout"); 48: ini_set("mysql.timeout",$timeout); 49: } 50: 51: if (!is_numeric($column)) $column=0; 52: if ($column<0) $column=0; 53: 54: $sql=@mysql_connect($host,$user,$pass,true); 55: 56: if ((!$sql)||($database=="")) 57: { 58: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 59: if (!$sql) 60: { 61: if ($debug) echo "Connect Error: Failed to Connect\n"; 62: fnmysql_error("Failed to Connect"); 63: return -1; // total connect failed 64: } 65: // otherwise is no database so close and return -1 to indicate failure (for the data requires a DB+qry etc) 66: @mysql_close($sql); 67: return -1; 68: } 69: 70: @mysql_select_db($database,$sql); 71: 72: if (mysql_errno($sql)!=0) // failed to select DB 73: { 74: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 75: fnmysql_error(mysql_error($sql)); 76: @mysql_close($sql); 77: return -2; // select database failed 78: } 79: 80: if ($query=="") 81: { // no query to perform 82: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 83: @mysql_close($sql); 84: return -1; // all ok but no query/rows 85: } 86: 87: $r=@mysql_query($query,$sql); 88: if (mysql_errno($sql)==0) // successful query 89: { 90: if (is_bool($r)) // didn't return any data 91: { 92: $return=-4; // so for this purpose (data) the query failed 93: } 94: else 95: { 96: if ($row=mysql_fetch_array($r)) 97: { // got data ok 98: if (isset($row[$column])) $return=$row[$column]; 99: else $return=$row[0]; 100: } 101: else $return=-5; // query seemed to succeed but no data at all here 102: @mysql_free_result($r,$sql); // free if a result 103: } 104: } 105: else 106: { 107: fnmysql_error(mysql_error($sql)); 108: $return=-3; // query failed 109: } 110: 111: @mysql_close($sql); 112: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 113: return $return; 114: } 115: 116: 117: 118: // Row Test 119: function mysql_test_rows($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false) 120: { 121: global $NATS; 122: if ($timeout>0) $timeout=$timeout; // use specific for test if set 123: else 124: { 125: // otherwise use system if available 126: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.mysql.timeout",0); 127: if ($timeout<=0) $timeout=0; // unset specifically or in environment 128: } 129: // this will return a 0 at any stage if connect etc is ok but no rows are returned 130: // negative if something actually fails 131: if ($timeout>0) 132: { 133: $oldtimeout=ini_get("mysql.timeout"); 134: ini_set("mysql.timeout",$timeout); 135: } 136: 137: if ($debug) echo "mysql://".$user.":".$pass."@".$host."/".$database."\n"; 138: 139: $sql=@mysql_connect($host,$user,$pass,true); 140: 141: if ((!$sql)||($database=="")) 142: { 143: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 144: if (!$sql) 145: { 146: if ($debug) echo "Connect Error: Failed to Connect\n"; 147: fnmysql_error("Failed to Connect"); 148: return -1; // total connect failed 149: } 150: // otherwise is no database so close and return 0 151: @mysql_close($sql); 152: return 0; 153: } 154: 155: @mysql_select_db($database,$sql); 156: 157: if (mysql_errno($sql)!=0) // failed to select DB 158: { 159: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 160: fnmysql_error(mysql_error($sql)); 161: @mysql_close($sql); 162: return -2; // select database failed 163: } 164: 165: if ($query=="") 166: { // no query to perform 167: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 168: @mysql_close($sql); 169: return 0; // all ok but no query/rows 170: } 171: 172: $r=@mysql_query($query,$sql); 173: if (mysql_errno($sql)==0) // successful query 174: { 175: if (is_bool($r)) // didn't return any daya 176: { 177: $return=mysql_affected_rows($sql); 178: } 179: else 180: { 181: $return=mysql_num_rows($r); 182: @mysql_free_result($r,$sql); // free if a result 183: } 184: } 185: else 186: { 187: fnmysql_error(mysql_error($sql)); 188: $return=-3; // query failed 189: } 190: 191: @mysql_close($sql); 192: if ($timeout>0) ini_set("mysql.timeout",$oldtimeout); 193: return $return; 194: } 195: 196: function mysql_test_time($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false) 197: { 198: $timer=new TFNTimer(); 199: $timer->Start(); 200: $val=mysql_test_rows($host,$user,$pass,$database,$timeout,$query,$debug); 201: $time=$timer->Stop(); 202: 203: if ($val<0) return $val; // connect/select/query failed 204: 205: // if $val is 0 then nothing was returned - maybe check the query here? Complicates the idea 206: // though for the user so left. Will have to do two tests for both time and rows 0 as fails 207: 208: $time=round($time,4); 209: if ($time==0) return "0.0001"; 210: return $time; 211: } 212: 213: 214: if (isset($NATS)) 215: { 216: class FreeNATS_MySQL_Test extends FreeNATS_Local_Test 217: { 218: function DoTest($testname,$param,$hostname,$timeout,$params) 219: { // 0: host, 1: user, 2: pass, 3: database, 4: query 220: 221: if ($testname=="mysql") 222: { 223: $ip=ip_lookup($param); 224: if ($ip=="0") return -1; // cache only as 127.0.0.1 is not the same connection as localhost for MySQL auth! 225: 226: return mysql_test_time($param,$params[1],$params[2],$params[3],$timeout,$params[4]); 227: } 228: 229: else if ($testname=="mysqlrows") 230: { 231: $ip=ip_lookup($param); 232: if ($ip=="0") return -1; // cache only - see above 233: return mysql_test_rows($param,$params[1],$params[2],$params[3],$timeout,$params[4]); 234: } 235: 236: else if ($testname=="mysqldata") 237: { 238: $ip=ip_lookup($param); 239: if ($ip=="0") return -1; 240: return mysql_test_data($param,$params[1],$params[2],$params[3],$timeout,$params[4],$params[5]); 241: } 242: 243: else return -1; 244: 245: } 246: function Evaluate($result) 247: { // same for all types 248: if ($result<0) return 2; // failed 249: return 0; // passed 250: } 251: 252: function ProtectOutput(&$test) 253: { 254: $test['testparam2']=""; 255: } 256: 257: function DisplayForm(&$row) 258: { 259: $optional=true; 260: if ($row['testtype']=="mysqldata") $optional=false; 261: echo ""; 262: echo ""; 267: echo ""; 272: echo ""; 278: echo ""; 279: echo ""; 284: if ($optional) echo ""; 285: echo ""; 290: if ($optional) echo ""; 291: if ($row['testtype']=="mysqldata") 292: { 293: echo ""; 298: echo ""; 299: } 300: echo "
"; 263: echo "Hostname :"; 264: echo ""; 265: echo ""; 266: echo "
"; 268: echo "Username :"; 269: echo ""; 270: echo ""; 271: echo "
"; 273: echo "Password :"; 274: echo ""; 275: echo ""; 276: echo ""; 277: echo "
Leave blank to not change or click to clear
"; 280: echo "Database :"; 281: echo ""; 282: echo ""; 283: echo "
Optional - leave blank to not bother with select_db
"; 286: echo "Query :"; 287: echo ""; 288: echo ""; 289: echo "
Optional - leave blank to not bother with a query
"; 294: echo "Column :"; 295: echo ""; 296: echo ""; 297: echo "
Which field (0 is first and the default) of the first record to use
";
301: } 302: 303: } 304: $params=array(); 305: $NATS->Tests->Register("mysql","FreeNATS_MySQL_Test",$params,"MySQL Connect",2,"FreeNATS MySQL Tester"); 306: $NATS->Tests->SetUnits("mysql","Seconds","s"); 307: $NATS->Tests->Register("mysqlrows","FreeNATS_MySQL_Test",$params,"MySQL Rows",2,"FreeNATS MySQL Tester"); 308: $NATS->Tests->SetUnits("mysqlrows","Rows","rows"); 309: $NATS->Tests->Register("mysqldata","FreeNATS_MySQL_Test",$params,"MySQL Data",2,"FreeNATS MySQL Tester"); 310: } 311: 312: 313: ?>