案例五:shell腳本實現定時監控http服務的運行狀態


注意:監控方法可以為端口、進程、URL模擬訪問方式,或者三種方法綜合。

說明:由於截止到目前僅講了if語句,因此,就請大家用if語句來實現。

  [root@oldboy-B scripts]# cat apachemon

  #!/bin/sh

  #created by oldboy 20110523

  . /etc/init.d/functions

  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`

  #if [ $HTTPPRONUM -lt 1 ];then

  if [[ $HTTPPRONUM -lt 1 ]];then

  action “httpd is not running” /bin/false

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi
  [root@oldboy-B scripts]# apachemon

  httpd is running [確定]

  [root@oldboy-B scripts]# pkill httpd

  [root@oldboy-B scripts]# ps -ef |grep http |grep -v grep

  [root@oldboy-B scripts]# apachemon

  httpd is not running [失敗]

  httpd is restart [確定]

  [root@oldboy-B scripts]# ps -ef|grep http|grep -v grep

  root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

  apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
 
腳本改進
  [root@oldboy-B /]# echo oldboytest >/var/www/html/index.htm

  [root@oldboy-B /]# wget –quiet –spider http://10.0.0.161/index.htm

  [root@oldboy-B /]# echo $?

  0

  [root@oldboy-B /]# ll index.htm

  ls: index.htm: 沒有那個文件或目錄

  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  

  . /etc/init.d/functions

  #HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` #=====>這個是基於http方式進行判斷

  wget –quiet –spider http://10.0.0.161/index.htm #=====>這個是基於WGET URL方式進行判斷

  if [ $? -ne 0 ];then

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true >>/tmp/httpd.log

  mail -s “`uname -n`’s httpd restarted at `(date)`” mail@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi
真正使用時,有些輸出是不需要的就去掉
  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  #

  . /etc/init.d/functions

  wget –quiet –spider http://10.0.0.161/index.htm #=====>這個是基於WGET URL方式進行判斷

  if [ $? -ne 0 ];then

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true >>/tmp/httpd.log

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  fi
多條件判斷的寫法
  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  #

  . /etc/init.d/functions

  HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`

  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`

  wget –quiet –spider http://10.0.0.161/index.htm && RETVAL=$?

  if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then

  #if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \< "1" ] ;then

  action “httpd is not running” /bin/false

  action “httpd is not running” /bin/false >/tmp/httpd.log

  httpdctl restart >/dev/null 2>&1

  action “httpd is restart” /bin/true

  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM