apache的安装及监控script

#!/bin/bash

#this is apache manager script

#function: apache install

#function: apache monitoring

##variable

ID=`id|awk '{print $1}'|cut -c 5`

IP=`ifconfig eth0|grep 'inet\b'|awk '{print $2}'|awk -F: '{print $2}'`

LOG=/var/log/apache/apache.log

#test

if [ $ID -ne 0 ];then echo "need to root";fi

killall -s QUIT httpd

if [ -f /etc/init.d/httpd ];then rpm -e httpd-* --nodeps;fi

find / -name httpd --exec rm -rf {} \;

find / -name http --exec rm -rf {} \;

find / -name apache  --exec rm -rf {} \;

#

yum clean all > /dev/null 2>&1

yum clean > /dev/null 2>&1

yum list |grep http > /dev/null 2>&1

if [ $? -ne 0 ];then echo "YUM ERROR";exit;fi

yum -y install pcre-* gcc-*

#

ping -c 2 192.168.0.3 > /dev/null 2>&1

if [ $? -ne 0 ];then echo "NETWORK ERROR";exit;fi

##function

apache () {

mkdir /tmp/web;cd /tmp/web

mkdir /var/log/apache

wget http://192.168.0.3/abc/httpd-2.2.16.tar.bz2

tar jxf httpd-2.2.16.tar.bz2

cd httpd-2.2.16;./configure --prefix=/usr/local/httpd

   if [ $? -eq 0 ];then

     make

         if [ $? -eq 0 ];then

            make install

                if [ $? -eq 0 ];then

                   echo "APAche INstall success"

                else

                   echo "APAche INstall ERROR"

                fi

         else

            echo "make ERROR";exit 10

         fi

   else

      echo "APAche configure ERROR";exit 10

   fi

}


###apache 安装模块

mon () {

cat <<EOF

elinks http://$IP -dump

 if [ \$? -eq 0 ]

    then

        echo " apache running" >> $LOG

 else

        echo " apache stop" >> $LOG

        echo " restart apache now" >> $LOG

         /usr/local/httpd/bin/apachectl -k stop

         /usr/local/httpd/bin/apachectl -k start

              if [ \$? -eq 0 ]

                then

                     echo "apache start success" >> $LOG

              else

                    /usr/local/httpd/bin/apachectl -k stop

                   /usr/local/httpd/bin/apachectl -k start

                      if [ \$? -eq 0 ]

                           then

                            echo "apache start success" >> $LOG

                       else

                             echo"\$HOSTNAME apache fail"|mail root@localhost

                      fi

               fi

 fi

EOF

}    ####//////监控模块

cron () {

cat << EOF

*/1 * * * * /tmp/apache_mon.sh

EOF

}  ####制定任务定制模块

unmon () {

crontab -l > /tmp/crontab

sed -i '/apache_mon.sh/d' /tmp/crontab

crontab -u root /tmp/crontab

rm -f /tmp/crontab

}

#####取消任务定制模块

server () {

cat << EOF

#!/bin/bash

sta () {

/usr/local/httpd/bin/apachectl -k start

}

sto () {

killall -s QUIT httpd

}

statu () {

elinks http://localhost -dump > /dev/null

        if [ \$? -eq 0 ]

           then

              echo httpd running

        else

              echo httpd stop

        fi

}

res () {

killall -s QUIT httpd

sleep 3

/usr/local/httpd/bin/apachectl -k start

}

##main

case \$1 in

start) sta;;

stop) sto;;

restart) res;;

status) statu;;

*) echo "USAGE: service apache  start|stop|restart|status"

esac

EOF

}

///添加脚本到/etc/init.d 中实现service apache start模块;

####main

case $1 in

install) apache;;

mon)

mon > /tmp/apache_mon.sh;chmod 700 /tmp/apache_mon.sh

cron > /tmp/cron

crontab -u root /tmp/cron;rm -f /tmp/cron

;;

unmon) unmon;;

server) server > /etc/init.d/apache;chmod 755 /etc/init.d/apache;;

*)

 echo "USAGE ./apache_manager.sh install|mon|unmon|server"

esac

//主模块


你可能感兴趣的:(shell,script)