Chinaunix首页 | 论坛 | 博客
  • 博客访问: 825298
  • 博文数量: 167
  • 博客积分: 7173
  • 博客等级: 少将
  • 技术积分: 1671
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 23:07
文章分类

全部博文(167)

文章存档

2018年(1)

2017年(11)

2012年(2)

2011年(27)

2010年(88)

2009年(38)

分类: 系统运维

2010-06-30 11:52:12

今天早上回来导数据,闲着没事干,(因为要转resin的开源版,要修改一些resin的源码和做一些pro版
功能的替代)

resin ping功能的解析

child of cluster

Starts a thread that periodically makes a request to the server, and restarts Resin if it fails.

This facility is used to increase server reliability - if there is a problem with the server

 (perhaps from a deadlock or an exhaustion of resources), the server is restarted.

A failure occurs if a request to the url returns an HTTP status that is not 200.

Since the local process is restarted, it does not make sense to specify a url that does not

get serviced by the instance of Resin that has the ping configuration. Most configurations

 use url's that specify 'localhost' as the host.

This pinging only catches some problems because it's running in the same process as

Resin itself. If the entire JDK freezes, this thread will freeze as well. Assuming the

 JDK doesn't freeze, the PingThread will catch errors like deadlocks.


现要做的效果是:

应用自动登录到数据库里面(只登陆不做任何操作);发现登录不了数据库就发告警邮件;一个小时内

不能连接的次数超过10次 就进行封端口和重启应用的操作。


因为resin的启动脚本是自己书写,所以本脚本有一些变量是依赖于我自己的启动脚本

因为有一套运维系统,所以脚本中有一些返回值,给运维系统进行画图,告警等一系列操作,所以

可以不看这些输出值。

我在resin_home下建立了line_plugin目录并将一些jar插件放在里面;

对于本脚本  用了mysql 和 oracle数据库的检测程序  为了以后增加功能所以将他们分开

做jar包 其实代码是一样。


#!/bin/bash
#author Sky
PATH="/var/PROGRAM/MANAGEMENT/modules/xbash:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:"
LANG=zh_CN
export PATH LANG


JAVAHOME=`cat /etc/init.d/$1  | grep "JAVA_HOME=" | awk -F"=" '{print $2}'`
RESINHOME=`cat /etc/init.d/$1  | grep "RESIN_HOME=" | awk -F"=" '{print $2}'`
CONF_NAME=`cat /etc/init.d/$1  | grep "SEARCH_STR=" | awk -F"=" '{print $2}' | sed 's/\"//g'`
PLUGIN_MYSQL="app_chk_mysqldb.jar"
PLUGIN_ORACLE="app_chk_oracledb.jar"



######agent 初始化输出###########
if [[ $# -eq 0 ]] ; then
   echo "appdbchk_=0"
   exit 1
fi

if [[ ! -e /etc/init.d/$1 ]] ; then
   echo "appdbchk_=0"
   exit 1
fi
#################################


###########环境初始化############
[[ ! -e $JAVAHOME/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar ]] && cp $RESINHOME/lib/mysql-connector-java-5.1.10-bin.jar $JAVAHOME/jre/lib/ext/
[[ ! -e $JAVAHOME/jre/lib/ext/ojdbc14.jar ]] && cp $RESINHOME/lib/ojdbc14.jar $JAVAHOME/jre/lib/ext/
#################################

##########DB URL OPTION##########
DBURL=""
DBUSER=""
DBPASSWD=""
#################################

############
# 主机信息 #
############
INTF=$(netstat -rn | tail -1 | awk '{print $NF}')
IP=$(ifconfig $INTF | awk '/inet addr/{ split($2,tmp,":") ; print tmp[2] }')
HOST_NAME=$(hostname --short)
###########
##########
#邮件环境#
##########
#CHARTSET="zh_CN."
MAIL_CLIENT=""
MAIL_SENDER="
MAIL_SERVER=""
case $IP in
        192.168.230.*|192.168.1[0-1].*|192.168.238.*|202..*)
        MAIL_SERVER=""
        ;;
        *)
        MAIL_SERVER=""
        ;;
esac
##########

cd $RESINHOME

open=0 #读开关
topen=0 #检测数据库开关
wlog=0  #写log的标签
dbtap=0   #数据库状态

cat $RESINHOME/conf/$CONF_NAME |
while read line
do
  if echo $line | grep -q "    then
    open=1
  fi
  if echo $line | grep -q "
    then
    open=0
  fi
  if [[ $open -eq 1 ]] ; then
    if echo $line |  grep -q "       then
       DBURL=`echo $line | sed 's///g' |  sed 's/<\/url>//g'`
       topen=$(($topen+1))
    fi
    if echo $line |  grep -q "       then
       DBUSER=`echo $line | sed 's///g' |  sed 's/<\/user>//g'`
       topen=$(($topen+1))
    fi   
    if echo $line |  grep -q "       then
       DBPASSWD=`echo $line | sed 's///g' |  sed 's/<\/password>//g'`
       topen=$(($topen+1))
    fi    
  fi
  if [[ $topen -eq 3 ]] ; then
      if echo $DBURL | grep -q "mysql"
        then     
         dbtap=`$JAVAHOME/bin/java -jar $RESINHOME/line_plugin/$PLUGIN_MYSQL $DBURL $DBUSER $DBPASSWD`
      else
         dbtap=`$JAVAHOME/bin/java -jar $RESINHOME/line_plugin/$PLUGIN_ORACLE $DBURL $DBUSER $DBPASSWD`
      fi

      appdbname=`echo "$DBURL" | awk -F":" '{print $NF}'`
     
      if echo $dbtap | grep -q "DB is BAD"
           then
           echo "appdbchk_$appdbname=1"
         else
           echo "appdbchk_$appdbname=0"
      fi
 
      if echo $dbtap | grep -q "DB is BAD"
        then
        dbname=`echo "$DBURL" | awk -F"/" '{print $NF}'`
        if [[ -e $RESINHOME/line_plugin/$dbname_$DBUSER.log ]] ; then
           GHOUR=`awk '{print $1}' $RESINHOME/line_plugin/$dbname_$DBUSER.log`
           NHOUR=$(date +%k)
           BADNUM=`awk '{print $2}' $RESINHOME/line_plugin/$dbname_$DBUSER.log`
           if echo "$GHOUR" | grep -q "$NHOUR"
             then
              BADNUM=$(($BADNUM+1))
              if [[ $BADNUM -gt 10 ]] ; then
                 echo "连接$DBURL失败已超十次" > /tmp/.dbdisconnection
                 #########发送邮件############
                 env MAILRC=/dev/null charset="$CHARTSET" from="$MAIL_SENDER" smtp="$MAIL_SERVER" \
                 nail -n -s "$HOST_NAME($IP)$1数据库连接失败超10次" $MAIL_CLIENT < /tmp/.dbdisconnection
                else
                 echo "连接$DBURL失败" > /tmp/.dbdisconnection
                 #########发送邮件############
                 env MAILRC=/dev/null charset="$CHARTSET" from="$MAIL_SENDER" smtp="$MAIL_SERVER" \
                 nail -n -s "$HOST_NAME($IP)$1数据库连接失败" $MAIL_CLIENT < /tmp/.dbdisconnection
              fi
              echo "$GHOUR $BADNUM" > $RESINHOME/line_plugin/$dbname_$DBUSER.log
              ################
              ####重启应用代码
              ################
           fi
        else
           NHOUR=$(date +%k)
           echo "$NHOUR 0" > $RESINHOME/line_plugin/$dbname_$DBUSER.log
        fi
      else
            NHOUR=$(date +%k)
           echo "$NHOUR 0" > $RESINHOME/line_plugin/$dbname_$DBUSER.log
      fi

      topen=0
  fi
 
 
done



文件:app_db_chk_for_resin.zip
大小:7KB
下载:下载

阅读(1422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~