ring-trial



web-server-48x48Last few weeks I have these problem, where my server could not handle so much connection made to the server. The server have been upgraded, and the loads have been reduced. But there is some connection that stuck, that makes my apache died. Last 2 days it happen once, when I got a LAST_ACK DDOS attack. I couldn’t pick up the IP, because if different IP. Otherwise,I can just block the IP. The web server were unaccessible until I restarted apache.

I have installed nagios and cacti on the other machine, and they work really well. But, I dont like to receive downtime notification from nagios. I like nagios working well, but I hate I’m having a problem.

I wrote these 2 scripts, PHP and a bash script that can make my life easier. The PHP script will just query mysql database, and return “OK” if nothing wrong. The other BASH script will download the PHP script through a URL, check if the content is ok. If OK, do nothing. Else, it will restart my apache.

This is my PHP script that can do the work to monitor my apache and mysql.I name it, agent.php

define('DB_NAME', 'yourdb_name');    // The name of the database
define('DB_USER', 'yourdb_username');     // Your MySQL username
define('DB_PASSWORD', 'yourdb_password'); // ...and password
define('DB_HOST', 'localhost');    // 99% chance you won't need to change this value
function conDb()
{
        global $DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME;
        mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Fail to connect server: ' . DB_HOST . '
' . mysql_error());
        mysql_select_db(DB_NAME) or die('Fail to connect data base: ' . DB_NAME . '
' . mysql_error());
}

function DisconnDb()
{
        global $DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME;
        mysql_close(mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)) or die('Fail to disconnect: ' . DB_HOST . '' . mysql_error());
}

conDb();
$count = 0;
$result = mysql_query("select * from wp_options limit 10");
if(mysql_num_rows($result) == '10'){
        echo "OK Good job";
}else{
        echo "Error";
}

For this example, I just pick one of my wordpress database, with table that surely have more than 10 records. Put it where you can access it from your web browser. Let say http:///agent.php. You should test it first, and make sure the script will report OK if apache and mysql works fine.

This is the BASH script, that will check the PHP script. I put it in /root/apache_mysql_monitor.sh

#!/bin/bash

mydate=`date +"%Y-%m-%d-%H-%M"`

cd /tmp/
wget --user-agent="anyhing you want" http:///agent.php -O status.txt

i=`tail -n 1 /tmp/status.txt`

if [ "$i" = "OK Good job" ]; then
        echo "OK"
else
        /usr/local/etc/rc.d/httpd restart
fi
rm -rf /tmp/status.txt

In this script, i have to add additional arguments for wget, to use different user-agent name, because I have block wget to access my apache through mod_rewrite. Nice right? :) Then, just place a new cronjob for root, to run it every 5 minutes.


#crontab -e -u root
*/5 * * * * /root/sh/apache_mysql_status.sh

Hope I can be useful to you as well.

No related posts.


0 Responses to “mysql down? need restart? but when?”

  1. No Comments

Leave a Reply





Get Chitika | Premium
Eye Shadow iPhone 4 Case speckcase
Eye Shadow iPhone 4 Case by MiPhoneCase
See more iphone 4 cases
Make Your Own iPhone 4 Case speckcase
Make Your Own iPhone 4 Case by mvdesigns
More iPod touch cases

Subscribe

Subscribe to my RSS Feeds