Database Monitoring
May 24, 2002
Once the user fills in their login credentials and clicks Submit, they are directed to a page within the application. The user is brought to the chapter5_dbMonitor.php page, although having this as an entry page to the application is not a requirement. The first action in this script is to bring in the common functions we defined earlier in chapter5_common.php.
chapter5_dbMonitor.php
<?php include ('chapter5_common.php');
Next, the dbMonitor class is defined. The class contains variables and methods that allow the user to monitor and report on the overall status of the database server. When you create an instance of this class, you supply the link ID you obtained from the database connection that was established during the chapter5_common.php portion of this script. The code is as follows:
//################################################################## //## Class: dbMonitor ## //## Description: Manage a db connection ## //################################################################## class dbMonitor { //member variables var $database; var $linkid; var $resultid; var $sqlstmt; var $rc;
//constructor function dbMonitor($alinkid) { $this->linkid = $alinkid; }
The first method in the dbMonitor class we'll look at is displayStatus(). The purpose of this method is to let the user view the current state of the database. Information such as the number of clients and the number of open tables is returned to the user, as follows:
//################################################################## //## Method: displayStatus ## //## Parameters: none ## //## Return value: array of status variables ## //################################################################## function displayStatus() { $this->sqlstmt = "show status"; $this->resultid = mysql_query ($this->sqlstmt,$this->linkid); $count = mysql_numrows($this->resultid); for ($i=0;$i < $count;$i++) { $statuslist[$i] = mysql_fetch_array ($this->resultid, MYSQL_ASSOC); } return $statuslist; }
As you can see, the displayStatus() method executes the "show status" command on the server and places the results of this command into an associative array. The actual display of the status values is handled at the point in the code where the method is called.
Next, let's look at the displayVersion() method of the dbMonitor class. The purpose of this method is to query the server to find out what version of the software is running:
//################################################################## //## Method: displayVersion ## //## Parameters: none ## //## Return value: the version number ## //################################################################## function displayVersion() { $this->sqlstmt = "select version() as Version"; $queryid = mysql_query ($this->sqlstmt,$this->linkid); $versionnumber[0] = mysql_fetch_array ($queryid, MYSQL_ASSOC); return $versionnumber; }
The version number is returned as an associative array to allow it to be displayed with the displayResultSet() function of the chapter5_common.php file. It could have just as easily been returned as a single value.
The next method of the dbMonitor class to examine is displayProcessList(). This method requests a list of active processes from the server and returns the result set as an associative array:
//################################################################## //## Method: displayProcessList ## //## Parameters: none ## //## Return value: array of active processes ## //################################################################## function displayProcessList() { $this->sqlstmt = "show processlist"; $this->resultid = mysql_query ($this->sqlstmt,$this->linkid); $count = mysql_numrows($this->resultid); for ($i=0;$i < $count;$i++) { $processlist[$i] = mysql_fetch_array ($this->resultid, MYSQL_ASSOC); } return $processlist; }
Managing the Login
Instant PHP 4
Database Monitoring (Cont.)
|