Checkapache

From Ezee.co.uk

Jump to: navigation, search

This is a quick script that checks apache is running by reading a file and checking for a string.
This is an old version, The new version does some additional tests and fixes more problems but is currently got a bug in it so not posting it here just now.

The line here kills off any semaphores that may have been held open which was a common problem on plesk servers running on FC2

ipcs -s | grep apache | gawk '{ print $2 }' | xargs -n 1 ipcrm sem

kills processes that are holding ports open.

kill $2 `/usr/sbin/lsof -i :80 | grep -v PID | awk '{print $2}'`

Main Script

#!/usr/bin/php -q
<script language="php">

echo "Checking Apache\n\n";
$url="http://XXX.XXX.XXX.XXX/testapache.html";
$err=0;
echo "Opening file $url\n";
if($fd=fopen($url,'r')) {
        echo "Connection Opened\n";

        } else {
        echo "Connection Failed\n";
        $err=1;
        }

echo "Reading from file\n";
$file=fread($fd,1024);

if(eregi("TEST OK",$file)) {
        echo "Apache Connection OK\n";
        } else {
                echo "Apache Failed\n";
                $err=2;
                }
#Now we have to do the hard work

# if $err is not 0 then send logs

if ($err!=0) {
        echo "### Restarting Apache ###\n";
        exec ("ipcs -s | grep apache | gawk '{ print $2 }' | xargs -n 1 ipcrm sem");
        exec ("kill $2 `/usr/sbin/lsof -i :80 | grep -v PID | awk '{print $2}'`");
        exec ("/etc/rc.d/init.d/httpd restart");
        mail ("youremail@somewhere.com","apache restart","apache restarted");
        }
</script>
Personal tools