{help}Script to Monitor and Auto Restart Apache Httpd Server

Discussion in 'Site & Server Administration' started by alexo, Jan 22, 2009.

Thread Status:
Not open for further replies.
  1. #1
    hi,

    As I am new to Linux server, I am facing some doubts like the Script to can monito my unmanaged server and Auto Restart Apache Httpd Server

    I'm on linux server, OS- Centos, control panel- DA.
    I setup only 5-6 my websites on this server, which don't have so much traffic; installed few php scripts, which eat a lot of memory. I setup 2gb RAm, but unfortunately the server last 2 weeks often went to offline, as server was out of memory.


    after a little research find few scripts


    1. Linux Server Hack - How to setup a Shell Script to Auto Restart Apache Httpd Server!

    Save the following as restart.sh:

        #!/bin/bash
        # Apache Process Monitor
        # Restart Apache Web Server When It Goes Down
        # -------------------------------------------------------------------------
        # Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
        # This script is licensed under GNU GPL version 2.0 or above
        # -------------------------------------------------------------------------
        # This script is part of nixCraft shell script http://en.wikipedia.org/wiki/Shell_script  collection (NSSC)
        # Visit http://bash.cyberciti.biz/ for more information.
        # -------------------------------------------------------------------------
        # RHEL / CentOS / Fedora http://en.wikipedia.org/wiki/Fedora  Linux restart command
        RESTART="/sbin/service httpd restart"
    
        # uncomment if you are using Debian / Ubuntu Linux
        #RESTART="/etc/init.d/apache2 restart"
    
        #path to pgrep command
        PGREP="/usr/bin/pgrep"
    
        # Httpd daemon name,
        # Under RHEL/CentOS/Fedora it is httpd
        # Under Debian 4.x it is apache2
        HTTPD="httpd"
    
        # find httpd pid
        $PGREP ${HTTPD}
    
        if [ $? -ne 0 ] # if apache not running
        then
         # restart apache
         $RESTART
        fi
    
    
    Then setup a cron job like this: (usually in your sa-update file under /etc/cron.d)
    
        */5 * * * * root /root/restart.sh  >/dev/null 2>&1
    Code (markup):
    source:


    2. script to monitor your server

    
    --- BEGIN ---
    #!/home/www/cgi-bin/php -q
    
    <?php
    
    // ***** OPTIONS *****
    
    // E-mail variables
    $admin_email = "to@domain.com";
    $from_email = "from@domain.com";
    $email_body = "Server: SERVERNAME\nDate: ";
    
    // CLAMD (clamav anti-virus) uncomment if you run clamav
    // $processes[0] = "/home/usr-local/sbin/clamd";
    // $action[0] = "/home/usr-local/sbin/clamd --config-file=/etc/clamav.conf";
    // $max_limit[0] = "7";
    
    // HTTPD
    $processes[1] = "httpd";
    $action[1] = "/usr/spc/httpd.restart";
    $max_limit[1] = "100";
    
    
    // STUNNEL - uncomment if you run stunnel (e.g. for ssl mail connections)
    // $processes[2] = "/home/usr-local/sbin/stunnel";
    // $action[2] = "/home/usr-local/sbin/stunnel";
    // $max_limit[2] = "10";
    
    // MySQL
    $processes[3] = "/usr/local/mysql/bin/mysqld";
    $action[3] = "";
    $max_limit[3] = "70";
    
    
    // CASP - uncomment if you run cilisoft asp
    //$processes[4] = "/home/usr-local/casp/asp-server-3000/caspd";
    //$action[4] = "";
    //$max_limit[4] = "10";
    
    $log = "Process monitor started...\n";
    $fp=popen("/bin/ps -auxw","r");
    
    while (!feof($fp)) {
    $buffer = fgets($fp, 4096);
    $output .= $buffer;
    
    }
    
    for ($n=0; $n<count($processes); $n++) {
    $log .= "checking ".$processes[$n]."... ";
    $process_count[$n] = substr_count($output, $processes[$n]);
    $log .= $process_count[$n]." processes\n";
    
    if ($process_count[$n] == 0) {
    if ($action[$n]<>"") {
    $log .= "Executing ".$action[$n]." ...\n";
    exec($action[$n]);
    $mailheaders = "From: ".$from_email."\nContent-Type: tex
    t/plain; charset=\"us-ascii\"";
    mail ($admin_email, "Restarting ".$processes[$n], $email
    _body, $mailheaders);
    } else {
    $log .= $action[$n]." not running!\n";
    $mailheaders = "From: ".$from_email."\nContent-Type: tex
    t/plain; charset=\"us-ascii\"";
    mail ($admin_email, $processes[$n]." not running!", $ema
    il_body, $mailheaders);
    }
    }
    else if ($process_count[$n] > $max_limit[$n]) {
    $log .= "Too many (".$process_count[$n].") ".$processes[$n]." pr
    ocesses. Sending notice to $admin_email\n";
    $mailheaders = "From: ".$from_email."\nContent-Type: text/plain;
    charset=\"us-ascii\"";
    mail ($admin_email, "Too many (".$process_count[$n].") ".$proces
    ses[$n]." processes", $email_body, $mailheaders);
    }
    
    }
    
    // Uncomment following line to print log to stdout
    #echo $log;
    
    ?>
    --- END --- 
    Code (markup):
    source

    3. Shell script to restart MySQL server if it is killed or not working

    
    #!/bin/bash
    # Shell script to restart MySQL server if it is killed or not working
    # due to ANY causes.
    # When script detects mysql is not running (it basically sends ping request
    # to MySQL) it try to start using /etc/init.d/mysql script; and it sends an
    # email to user indicating the status.
    # This script must be run from Cron Job so that it can monitor mysql server.
    # For more info visit following url:
    # http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html
    # --------------------------------------------------------------------------
    # Copyright (C) 2005 nixCraft project <http://cyberciti.biz/fb/>
    # This script is licensed under GNU GPL version 2.0 or above
    # -------------------------------------------------------------------------
    # This script is part of nixCraft shell script collection (NSSC)
    # Visit http://bash.cyberciti.biz/ for more information.
    # -------------------------------------------------------------------------
     
    # mysql root/admin username
    MUSER="root"
    # mysql admin/root password
    MPASS="SET-ROOT-PASSWORD"
    # mysql server hostname
    MHOST="localhost"
    #Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
    # Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
    MSTART="/etc/init.d/mysql start"
    # Email ID to send notification
    EMAILID="notification@somewhere-corp.com"
    # path to mail program
    MAILCMD="$(which mail)"
    # path mysqladmin
    MADMIN="$(which mysqladmin)"
     
    #### DO NOT CHANGE anything BELOW ####
    MAILMESSAGE="/tmp/mysql.fail.$$"
     
    # see if MySQL server is alive or not
    # 2&1 could be better but i would like to keep it simple and easy to
    # understand stuff :)
    $MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
    if [ $? -ne 0 ]; then
    	echo "" >$MAILMESSAGE
    	echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
    	echo "Hostname: $(hostname)" >>$MAILMESSAGE
    	echo "Date & Time: $(date)" >>$MAILMESSAGE
    	# try to start mysql
    	$MSTART>/dev/null
    	# see if it is started or not
    	o=$(ps cax | grep -c ' mysqld$')
    	if [ $o -eq 1 ]; then
    		sMess="MySQL Server MySQL server successfully restarted"
    	else
    		sMess="MySQL server FAILED to restart"
    	fi
    	# Email status too
    	echo "Current Status: $sMess" >>$MAILMESSAGE
    	echo "" >>$MAILMESSAGE
    	echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
    	echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
    	# send email
    	$MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
    else # MySQL is running :) and do nothing
    	:
    fi
    # remove file
    rm -f $MAILMESSAGE
    Code (markup):
    source

    4. Monit

    M/Monit is a system for automatic management and monitoring of Information Technology Systems. M/Monit can monitor and manage distributed computer systems, conduct automatic maintenance and repair and execute meaningful causal actions in error situations.

    M/Monit can start a service if it does not run, restart a service if it does not respond and suspend a service if it utilize too much resources.

    Monitor system attributes such as CPU, Load, Memory, Disk usage, files, directories and filesystems for changes. Conditional rules can be set and if a value goes outside a defined scope specific actions can be executed and notification sent.

    Information is collected from monitored systems and stored in a database. Drill-down and filter functions exist to investigate collected data.
    source


    =-----------
    So need your advice which one you recommend.

    Any advice will be appreciated
     
    alexo, Jan 22, 2009 IP
  2. thegetpr

    thegetpr Banned

    Messages:
    99
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    to execute shell program just hit

    ./filename.sh
     
    thegetpr, Jan 22, 2009 IP
  3. alexo

    alexo Well-Known Member

    Messages:
    372
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #3
    hey, did you read my post ?
     
    alexo, Jan 23, 2009 IP
Thread Status:
Not open for further replies.