Location: PHPKode > scripts > Database servers down alarm > database-servers-down-alarm/as_dbserver_check.en.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head><title>as_dbserver_check: Checking / alarming Alive state of Database server(s)
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body     { font-family:verdana,arial,helvetica;font-size:12px; }
pre { font-family: verdana,arial; font-size:11px; color:#0000FF;
      background-color: #EFEFF0; border: 1px solid #D0D0F0; padding:6px;
}
td   { font-family:arial,helvetica;font-size:12px;}
tr.odd   { background-color: #F0F0F8; color:#000000; }
tr.even  { background-color: #E0E0F0; color:#000000; }
h4       { background-color: #E0E0F0; color:#000000;
           text-align: left; }
h5       { background-color: #E0E0F0; color:#000000;
           text-align: left; font-size:11px; padding-left:8px;}
-->
</style>
</head>
<body>
<!-- DOC BEGIN -->

<h3>as_dbserver_check: Checking / alarming Alive state of Database server(s)</h3>

Sometimes your ISP's MySQL or other DB database servers may go down.
This can be critical for your business, and you surely want to know about it ASAP.

<br />This module's task is doing what you want: it checks out alive or 'dead' state of all
registered DBMS servers (different kinds of DBMS supported).
If some server seems to get down, you (or your web-admin) receive email notification,
 and your ISP support team receives another one (message for ISP can contain your client info like
agreement number or Client ID, that support team usualy wants to know).

<br /><br />
You schedule a call of your checking script with appropriate time interval (say 10 minutes),
and since this moment your DB servers are monitored by you, not only by your ISP techicians.

<br /><br />
When DB server return to operational mode, you receive respective "I'm back" notification.
<br />Supported Databases are:
<ul>
  <li>MySQL</li>
  <li>mSQL</li>
  <li>PostgreSQL</li>
  <li>Oracle (Oracle oci extension must be loaded)</li>
  <li>Microsoft SQL (MS SQL extension must be loaded)</li>
  <li>DB2</li>
  <li>Any other DB that can be connected by your PHP function</li>
</ul>

The only used and tested by me is MySQL. The rest databases can be tested by anyone, and if something wrong please let me know.

<br /><br />
One more thing: may be you have an engine for sending SMS from your site and would like to get instant SMS messages 
about DB server problems.
Then you can "register" your function that will use SMS engine and send alert messages to your mobile device.


<h4>Installation on the site</h4>
<ul>
  <li>Place php module <b>as_dbserver_check.php</b> into your site's folder,</li>
  <li>Edit <b>checkdb.php</b> or create your own script, filling it with all your parameters for the job.
  </li>
  <li>Tune your CRON or other scheduling module to run this checking script periodically.
  </li>
</ul>

<h4>Simple code example</h4>

<pre>require_once('as_dbserver_check.php');

$checker = new CDbChecker('My tiny site','','My client ID is 7777, agreement 10101/2009');
$checker-&gt;SetEmails('hide@address.com','hide@address.com');

$checker-&gt;AddServer(DBTYPE_MYSQL,'localhost','username','password');
$checker-&gt;CheckDbState();

</pre>

<h4>Using the class CDbChecker</h4>

All checking jobs are performed in CheckDbState() method, so ALL needed parameters must be set before calling it.
<br />Here is a full method list with descriptions.

<br /><br />
<b>CDbChecker($title='', $homefolder='', $isptext='')</b> - class constructor.
<br /><br /><b>$title</b> can contain any short description of your site - it's inserted in email message that will be sent to you or
defined admins in your company.

<br /><br /><b>$homefolder</b> - folder on the local server, that will hold "flag" files. These flags created to avoid repetitive "spamming"
when server is down for a long time (for example: your checking runs every 5 minutes, and DB server down 3 hour. To avoid sending
"alarm" every 5 minutes, small "flag" file is created. When DB server goes online, this file will be deleted, and you'll receive
a message "server is UP"). No need to say, this folder must be writable from PHP scripts. Default value - current folder, './'

<br /><br /><b>$isptext</b> is additional text body for your ISP support. It can contain your client id or anything else you want. 
This text will be prepended to the email message.

<br /><br /><b>SetEmails($adminemail,$ispemail='', $emailcset='')</b> registers two email addresses:
you company's web-admin (may be you) and your ISP support address.
When some DB server down detected, the alarm message is sent to both addresses (if both passed).
And when server gets up, the message is sent only to you or your admin ($adminemail address).

<br /><br />
<b>AddServer($dbtype,$address,$login='',$passwd='',$connfunc=false,$downtime='')</b> - method registers one database server to check out.

<br /><br /><b>$dbtype</b> is one of database types from DBTYPE_MYSQL,DBTYPE_MSQL,DBTYPE_POSTGRESQL,DBTYPE_ORACLE,DBTYPE_MSSQL,DBTYPE_DB2,
or it can be any other code (non-listed DB type).

<br /><br /><b>$address</b> is a server address. In MySQL case it can be in "host:port" form.
<br />For PostgreSQL it can be a full connect string like "host=localhost port=5432 dbname=mary" (new pg_connect format)- 
in that case parameters <b>$login</b> and <b>$password</b> not used. Otherwise connection string will be constructed from passed parameters.
So if your server has non-standard port, you should pass full connect string <b>$address</b>.

<br /><br /><b>$connfunc</b> is optional "make connection" function's name, that is used for "undefined" data types.
Besides it will be used (if passed) for MS SQL databases, when mssql extension not loaded. The same is correct for Oracle databases.
<b>$connfunc</b> should return non-empty value if connection successfully established. Any empty returned value (0, false) is treated as "connect fail".

<br /><br /><b>$downtime</b> can be used if your ISP turns down databases for service tasks every day at the same time.
For example since 02 to 04 AM your DB can be down due to ISP service jobs. So checking database state at this interval must be paused.
In that case pass non-empty string in <b>$downtime</b> in format "hh:mm-hh:mm", holding beginning and ending time (hh-hours, mm-minutes).
In our case it should be "02:00-04:00" string.
<br />Of course, this pause can be defined in cron schedule string. But in that case it will pause the whole job, instead of "per-connection" apply.

<br /><br /><b>SetSMSEngine($smsfunc='')</b> registers your function that can prepare and send SMS messages.
In fact, it can be any function that will be called if some server state changes detected (DB server goes down or up).
This function must receive a text to send in first parameter.

<br /><br /><b>Please note</b>: CDbChecker knows nothing about SMS formats, supported character sets or maximal message length, 
it just calls function whose name was registered !

<br /><br /><b>CheckDbState()</b> finally this is called to do the job. All registered DB connections are checked,
if some state has changed since last check, respective message will be composed.
"Server is Down alarms" and all "Server is UP" are gathered in two independent text block, so two independent messages can be called:
one about servers that get down, and another one if some servers get up.
If defined, "SMS sending" function will be called with each of these messages as parameter.
<br />Method returns true if all registered DB servers are OK, and false if something is down.

<div style="text-align:right">Distributed under BSD License</div>

<H4>Version History</H4>

<h5>1.00.001 (06/18/2009)</h5>
<ul>
  <li>First release published</li>
</ul>

<!-- DOC END -->
<br />
<hr>
<div align=center><font size=-2>Copyright &copy; Alexander Selifonov, <a href="http://www.selifan.ru">www.selifan.ru</a>
</font></div>
</body></html>
Return current item: Database servers down alarm