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