监控shell

来源:互联网 发布:jdk1.7 for mac安装包 编辑:程序博客网 时间:2024/05/02 23:26

#!/usr/bin/ksh
# @file monRapOgLimitMon.sh
# @author
# @date
# @version 0.0.1
# @brief RAP下发阀值监控程序
# Change log:
# <pre>
# author time version description
# ----------------------------------------------------------
#
# </pre>
#

f_get_limit()
{
sqlplus -s "${DBUser}/${DBPasswd}@${DBSid}" <<EOF > /dev/null 2>>${SQLLog}
set echo off
set feed off
set head off
set term off
set trimspool off
set trims off
set pagesize 100

spool ${rapNumLimit}
SELECT query_direction||','||biz_line||','||cust_or_sttl_cd||','||limit_val FROM dcsdba.rap_num_limit;
spool off

spool ${rapOgNoTimeLimit}
SELECT query_direction||','||cust_or_sttl_cd||','||time_unit ||','||limit_val FROM dcsdba.rap_og_no_time_limit;
spool off

spool ${rapOgErrNumLimit}
SELECT cust_carrier_cd||','||partner_carrier_cd||','||biz_pkg||','||biz_line||','||file_type||','||limit_val FROM dcsdba.rap_og_err_numlimit;
spool off

spool ${rapOgFileErrLimit}
SELECT limit_val FROM dcsdba.RAP_OG_FILE_ERR_LIMIT;
spool off

exit
EOF

if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "SERIOUS:sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd}!"
echo "sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd} in f_get_icFileProcState!"
return 1
fi

return 0
}

f_get_audit_info()
{
_flag=$1

if [ "${_flag}" = "rapNum" ];then
sqlplus -s "${DBUser}/${DBPasswd}@${DBSid}" <<EOF > /dev/null 2>>${SQLLog}
set echo off
set feed off
set head off
set term off
set trimspool off
set trims off
set pagesize 100

spool ${rapOgFileAuditNum}
SELECT a.cust_carrier_cd,a.sttl_centr_cd_data FROM dcsdba.RAP_OG_FILE_AUDIT a WHERE a.RAP_CREATION_TM >to_date((to_char(SYSDATE,'yyyymmdd')||'000000'),'yyyy-mm-dd hh24:mi:ss');
spool off

exit
EOF
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "SERIOUS:sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd}! in f_get_audit_info"
echo "sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd} in f_get_audit_info!"
return 1
fi

elif [ "${_flag}" = "rapOgNoTime" ];then
#QUERY_DIRECTION 漫游端 1-客户端 2-漫游端
if [ $2 -eq 1 ];then
#时间单位(TIME_UNIT)1 按天 2 按小时
if [ $3 -eq 1 ];then
_column = "a.cust_carrier_cd,round(abs(a.rap_creation_tm-sysdate))"
else
_column = "a.cust_carrier_cd,round(abs(a.rap_creation_tm-sysdate)*24)"
fi
_where = "WHERE a.cust_carrier_cd='$4'"
else
#时间单位(TIME_UNIT)1 按天 2 按小时
if [ $3 -eq 1 ];then
_column = "a.sttl_centr_cd_data,round(abs(a.rap_creation_tm-sysdate))"
else
_column = "a.sttl_centr_cd_data,round(abs(a.rap_creation_tm-sysdate)*24)"
fi
_where = "WHERE a.sttl_centr_cd_data='$4'"
fi


sqlplus -s "${DBUser}/${DBPasswd}@${DBSid}" <<EOF > /dev/null 2>>${SQLLog}
set echo off
set feed off
set head off
set term off
set trimspool off
set trims off
set pagesize 100

spool ${rapOgFileAuditOgNoTime}
SELECT ${_column} FROM dcsdba.rap_og_file_audit a
WHERE a.rap_seq =(
SELECT MAX(a.rap_seq) FROM dcsdba.rap_og_file_audit a ${_where} )
spool off

exit
EOF
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "SERIOUS:sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd}!"
echo "sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd} in f_get_audit_info!"
return 1
fi

fi
#第三个检查点
elif [ "${_flag}" = "rapOgErrNum" ];then
_custCarrier=$3
_partnerCarrier=$4
_bizLine=$5
_bizPkg=$6
#QUERY_DIRECTION 1-RAP文件 2-低版本文件
if [ $2 -eq 1 ];then
#RAP文件 查询 dcsdba.rap_og_file_audit
_tableName = "dcsdba.rap_og_file_audit"
else
_tableName = "dcsdba.rpt_og_file_audit"
fi


sqlplus -s "${DBUser}/${DBPasswd}@${DBSid}" <<EOF > /dev/null 2>>${SQLLog}
set echo off
set feed off
set head off
set term off
set trimspool off
set trims off
set pagesize 100

spool ${rapOgFileAuditOgErrNum}
SELECT CDR_COUNT FROM ${_tableName} WHERE CUST_CARRIER_CD = ${_custCarrier} AND PARTNER_CARRIER_CD = ${_partnerCarrier} AND BIZ_LINE = ${_bizLine} AND BIZ_PKG = ${_bizPkg}

spool off

exit
EOF
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "SERIOUS:sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd}!"
echo "sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd} in f_get_audit_info!"
return 1
fi
fi
#第四个检查点
elif [ "${_flag}" = "rapOgFileErr" ];then
sqlplus -s "${DBUser}/${DBPasswd}@${DBSid}" <<EOF > /dev/null 2>>${SQLLog}
set echo off
set feed off
set head off
set term off
set trimspool off
set trims off
set pagesize 100

spool ${rapOgFileAuditOgFileErr}
SELECT SUM(a.CDR_COUNT) FROM dcsdba.RAP_OG_FILE_AUDIT a WHERE a.RAP_CREATION_TM >to_date((to_char(SYSDATE,'yyyymmdd')||'000000'),'yyyy-mm-dd hh24:mi:ss');
spool off

exit
EOF
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "SERIOUS:sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd}! in f_get_audit_info"
echo "sqlplus excute failed db_user=${DBUser} db_pass=${DBPasswd} in f_get_audit_info!"
return 1
fi
fi


}

f_check1()
{
cat ${rapNumLimit}|grep -v ^$|wc -l|read _limitCnt
if [ ${_limitCnt} -gt 0 ];then
cat ${rapNumLimit}|grep -v ^$|awk -F"," '{ print $1,$2,$3,$4 }'|while read _queryDirection _bizLine _custOrSttl _limitVal
do
if [ ${_queryDirection} -eq 1 -a ${_bizLine} -eq 1 ];then
cat ${rapOgFileAuditNum}|grep -v ^$|grep ^${_custOrSttl}|wc -l|read _rapOgCnt
if [ ${_rapOgCnt} -gt ${_limitVal} ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_rapOgCnt} is greater than limit number ${_limitVal}!"
echo "统计数据大于配置的RAP门限的值"
fi
elif[ ${_queryDirection} -eq 2 -a ${_bizLine} -eq 2 ];then
cat ${rapOgFileAuditNum}|grep -v ^$|grep ${_custOrSttl}$|wc -l|read _rapOgCnt
if [ ${_rapOgCnt} -gt ${_limitVal} ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_rapOgCnt} is greater than limit number ${_limitVal}!"
fi
fi
done
fi
}

f_check2()
{
cat ${rapOgNoTimeLimit}|grep -v ^$|wc -l|read _rapOgNoCnt
if [ ${_rapOgNoCnt} -gt 0 ];then
cat ${rapOgNoTimeLimit}|grep -v ^$|awk -F"," '{ print $1,$2,$3,$4 }'|while read _queryDirection1 _custOrSttl1 _timeUnit _limitVal1
do
if [ ${_queryDirection} -eq 1 ];then
f_get_audit_info rapOgNoTime 1 ${_timeUnit} ${_custOrSttl1}
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "f_get_audit_info rapOgNoTime ${_queryDirection} ${_timeUnit} ${_custOrSttl1} exec failed!"
#exit 1
fi

cat ${rapOgFileAuditOgNoTime}|grep -v ^$|awk '{ print $2 }'|read _timeUnitLimit
if [ ${_timeUnitLimit} -gt ${_limitVal1} ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_rapOgCnt} is greater than limit number ${_limitVal}!"
echo "统计数据大于配置的RAP门限的值"
fi
elif[ ${_queryDirection} -eq 2 ];then
f_get_audit_info rapOgNoTime 2 ${_timeUnit} ${_custOrSttl1}
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "f_get_audit_info rapOgNoTime ${_queryDirection} ${_timeUnit} ${_custOrSttl1} exec failed!"
#exit 1
fi

cat ${rapOgFileAuditOgNoTime}|grep -v ^$|awk '{ print $2 }'|read _timeUnitLimit
if [ ${_timeUnitLimit} -gt ${_limitVal1} ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_rapOgCnt} is greater than limit number ${_limitVal}!"
fi
fi
done
fi
}

f_check3()
{
cat ${rapOgErrNumLimit}|grep -v ^$|wc -l|read _rapOgErrNumCnt
if [ ${_rapOgErrNumCnt} -gt 0 ];then
cat ${rapOgErrNumLimit}|grep -v ^$|awk -F"," '{ print $1,$2,$3,$4,$5,$6 }'|/
while read _custCd _partnerCd _bizPkg _bizLine _fileType _limitVal2
do
if [ ${_fileType} -eq 1 ]
then
f_get_audit_info rapOgErrNum 1 ${_custCd} ${_partnerCd} ${_bizLine} ${_bizPkg}
if [ $? -ne 0 ]
then
lgWriteLog 3 ${MySelf}.sh 10000034 "f_get_audit_info rapOgErrNum ${_queryDirection} ${_timeUnit} ${_custOrSttl1} exec failed!"
#exit 1
fi

cat ${rapOgFileAuditOgErrNum}|grep -v ^$|awk '{ print $1 }'|while read _errNumCount
do
if [ ${_errNumCount} -gt ${_limitVal2} ]
then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_errNumCount} is greater than limit number ${_limitVal2}!"
fi
done
elif[ ${_fileType} -eq 2 ]
then
f_get_audit_info rapOgErrNum 2 ${_custCd} ${_partnerCd} ${_bizLine} ${_bizPkg}
if [ $? -ne 0 ]
then
lgWriteLog 3 ${MySelf}.sh 10000034 "f_get_audit_info rapOgErrNum ${_queryDirection} ${_timeUnit} ${_custOrSttl1} exec failed!"
#exit 1
fi
cat ${rapOgFileAuditOgErrNum}|grep -v ^$|awk '{ print $1 }'|while read _errNumCount
do
if [ ${_errNumCount} -gt ${_limitVal2} ]
then
lgWriteLog 3 ${MySelf}.sh 10000034 "the statistics value ${_errNumCount} is greater than limit number ${_limitVal2}!"
fi
done
fi
done
fi
}

f_check4()
{
cat ${rapOgFileErrLimit}|grep -v ^$|wc -l|read _rapOgErrNumCnt
if [ ${_rapOgErrNumCnt} -gt 0 ];then
f_get_audit_info rapOgFileErr
cat ${rapOgFileErrLimit}|grep -v ^$|read _rapOgErrLimitVal
cat ${rapOgFileAuditOgFileErr}|grep -v ^$|read _rapOgCnt4
if [ ${_rapOgCnt4} -gt ${_rapOgErrLimitVal} ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "the value ${_rapOgCnt4} is greater than limit number ${_rapOgErrLimitVal}!"
echo "每天RAP文件下发错单数超过错单数门限值"
fi
fi

}

################# main #################################################
MySelf="monRapOgLimitMon"
ddate=`date +%Y%m%d`
if [ "$1" = "-help" -o "$1" = "-h" ];then
echo "RAP下发阀值监控程序,监控当日RAP文件相关值超过门限现象"
echo "用法:${MySelf}.sh [-h|-help] "
echo "参数说明:"
echo " [-h|-help] 打印帮助信息"
exit 0
fi

if [ -z "${MCB_HOME}" ];then
echo "MCB_HOME not set,exit!"
exit 1
fi

if [ -z "${MCB_APPID}" ];then
echo "MCB_APPID not set,exit!"
exit 1
fi

################ Add Lock File #####################

PIDLock=${MCB_HOME}/${MCB_APPID}/var/tmp/.${MySelf}.pid
if [ -r ${PIDLock} ];then
cat ${PIDLock} | read pid
kill -0 ${pid} >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "The same program ${MySelf}.sh is running now."
exit 1
fi
fi
echo $$ > ${PIDLock}

CfgFile=${MCB_HOME}/${MCB_APPID}/conf/${MySelf}.conf
LogFile=${MCB_HOME}/${MCB_APPID}/var/log/${MySelf}.log
rapNumLimit=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapNumLimit
rapOgNoTimeLimit=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgNoTimeLimit
rapOgErrNumLimit=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgErrNumLimit
rapOgFileErrLimit=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgFileErrLimit

rapOgFileAuditNum=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgFileAudit.Num
rapOgFileAuditOgNoTime=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgFileAudit.OgNoTime
rapOgFileAuditOgErrNum=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgFileAudit.OgErrNum
rapOgFileAuditOgFileErr=${MCB_HOME}/${MCB_APPID}/var/tmp/.rapOgFileAudit.OgFileErr

############## check common conf ########################
if [ ! -r ${CfgFile} ];then
echo "The conf file ${CfgFile} not open,exit!"
exit 1
fi

. cfConfigFile.sh
cfInit ${CfgFile} noLock readOnly

LogPath=$(dirname ${LogFile})

if [ ! -d ${LogPath} ];then
echo "The LogFile Path ${LogPath} not exist,exit!"
exit 1
fi

isday=1


# 包含SHELL日志接口文件
. ilogger.sh
#初试化logger
lgInit "${LogFile}" "" "${MySelf}" "" "$isday"
if [ $? -ne 0 ];then
echo "lgInit ${LogFile} ${MySelf} $isday failed!"
exit 1
fi

#运行周期,以秒为单位
interval=$(cfGetConfigItem common interval)
#数据库配置
DBSid=$(cfGetConfigItem database dbn)
DBUser=$(cfGetConfigItem database user)

if [ -z "${interval}" ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "common/interval" "is NULL"
exit 1
fi

expr ${interval} + 1 >/dev/null 2>&1
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "common/interval" "is NULL"
exit 1
fi

if [ -z "${DBSid}" ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "common/DBSid" "is NULL"
exit 1
fi

if [ -z "${DBUser}" ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "common/DBUser" "is NULL"
exit 1
fi

#DBPasswd=$(getdbpwd ${DBUser})
DBPasswd=dcsdba
echo DBPasswd=${DBPasswd}
if [ $? -ne 0 ];then
lgWriteLog 3 ${MySelf}.sh 10000034 "common/DBUser" "DBUser[${DBUser}] get dbpasswd failed!"
exit 1
fi


###################### begin to monRapOgLimitMon ####################
echo "*********the program ${MySelf} start at `date +%Y-%m-%d_%H:%M:%S`**********"
lgWriteLog 1 ${MySelf}.sh 00000000 "the program ${MySelf} ${inst} start at `date +%Y-%m-%d_%H:%M:%S`"

#取各个门限表记录
f_get_limit

####--1--检测当日客户或清算中心下发RAP文件数量超过个数门限####
#取RapOG审核表记录
f_get_audit_info rapNum

f_check1

####--2--某时间范围无RAP文件生成#
f_check2

###--3--生成单个RAP文件错单数超过错单数门限值
f_check3

###--4--每天RAP文件下发错单数超过错单数门限值,每天执行一次
checkDate=`date +%Y%m%d`
if[ "${ddate}" != "${checkDate}"];then
f_check4
ddate=${checkDate}
fi


lgWriteLog 1 ${MySelf}.sh 00000000 "*********the program ${MySelf} end at `date +%Y-%m-%d_%H:%M:%S`**********"
echo "*********the program ${MySelf} end at `date +%Y-%m-%d_%H:%M:%S`**********"
echo

原创粉丝点击