@@ -232,39 +232,19 @@ function parse_cmdline( $args ) {
232232# top of this file.
233233# ============================================================================
234234function ss_get_mysql_stats ( $ options ) {
235- # Process connection options and connect to MySQL .
235+ # Process connection options.
236236 global $ debug , $ mysql_user , $ mysql_pass , $ cache_dir , $ poll_time , $ chk_options ,
237237 $ mysql_port , $ mysql_ssl , $ mysql_ssl_key , $ mysql_ssl_cert , $ mysql_ssl_ca ,
238238 $ heartbeat , $ heartbeat_table , $ heartbeat_server_id , $ heartbeat_utc ;
239239
240- # Connect to MySQL.
241240 $ user = isset ($ options ['user ' ]) ? $ options ['user ' ] : $ mysql_user ;
242241 $ pass = isset ($ options ['pass ' ]) ? $ options ['pass ' ] : $ mysql_pass ;
243242 $ host = $ options ['host ' ];
244243 $ port = isset ($ options ['port ' ]) ? $ options ['port ' ] : $ mysql_port ;
245244 $ heartbeat_server_id = isset ($ options ['server-id ' ]) ? $ options ['server-id ' ] : $ heartbeat_server_id ;
246- debug (array ('connecting to ' , $ host , $ port , $ user , $ pass ));
247- if ( !extension_loaded ('mysqli ' ) ) {
248- debug ("The MySQLi extension is not loaded " );
249- die ("The MySQLi extension is not loaded " );
250- }
251- if ( $ mysql_ssl ) {
252- $ conn = mysqli_init ();
253- mysqli_ssl_set ($ conn , $ mysql_ssl_key , $ mysql_ssl_cert , $ mysql_ssl_ca , NULL , NULL );
254- mysqli_real_connect ($ conn , $ host , $ user , $ pass , NULL , $ port );
255- }
256- else {
257- $ conn = mysqli_connect ($ host , $ user , $ pass , NULL , $ port );
258- }
259- if ( !$ conn ) {
260- debug ("MySQL connection failed: " . mysqli_error ());
261- die ("MySQL: " . mysqli_error ());
262- }
263245
264- $ sanitized_host
265- = str_replace (array (": " , "/ " ), array ("" , "_ " ), $ options ['host ' ]);
266- $ cache_file = "$ cache_dir/ $ sanitized_host-mysql_cacti_stats.txt "
267- . (isset ($ options ['port ' ]) || $ port != 3306 ? ": $ port " : '' );
246+ $ sanitized_host = str_replace (array (": " , "/ " ), array ("" , "_ " ), $ host );
247+ $ cache_file = "$ cache_dir/ $ sanitized_host-mysql_cacti_stats.txt " . ($ port != 3306 ? ": $ port " : '' );
268248 debug ("Cache file is $ cache_file " );
269249
270250 # First, check the cache.
@@ -314,6 +294,25 @@ function ss_get_mysql_stats( $options ) {
314294 debug ("Not using the cache file " );
315295 }
316296
297+ # Connect to MySQL.
298+ debug (array ('Connecting to ' , $ host , $ port , $ user , $ pass ));
299+ if ( !extension_loaded ('mysqli ' ) ) {
300+ debug ("PHP MySQLi extension is not loaded " );
301+ die ("PHP MySQLi extension is not loaded " );
302+ }
303+ if ( $ mysql_ssl ) {
304+ $ conn = mysqli_init ();
305+ mysqli_ssl_set ($ conn , $ mysql_ssl_key , $ mysql_ssl_cert , $ mysql_ssl_ca , NULL , NULL );
306+ mysqli_real_connect ($ conn , $ host , $ user , $ pass , NULL , $ port );
307+ }
308+ else {
309+ $ conn = mysqli_connect ($ host , $ user , $ pass , NULL , $ port );
310+ }
311+ if ( mysqli_connect_errno () ) {
312+ debug ("MySQL connection failed: " . mysqli_connect_error ());
313+ die ("ERROR: " . mysqli_connect_error ());
314+ }
315+
317316 # Set up variables.
318317 $ status = array ( # Holds the result of SHOW STATUS, SHOW INNODB STATUS, etc
319318 # Define some indexes so they don't cause errors with += operations.
0 commit comments