#!/bin/ksh
#程序名称:ocs_license
#作者姓名:
#开发时间:2007-08-10
#功能说明:本程序每晚根据CBE自动备份数据,统计当前系统中所有激活用户,
# 统计结果保存到smp的w_uvs_scpconfig表,同时将数据同步到所有SCP的w_uvs_scpconfig表。
cd $HOME/smp_run/smpser/bin
#包含公共函数集
. public.sh.rc
#日志文件
LogFile=${LogDir}/ocs_license.log
#TEMP目录路径
TmpDir=$HOME/smp_run/smpser/temp
#存放从w_uvs_scpconfig表中卸载出来的数据的文件
DataFile=$TmpDir/w_uvs_scpconfig.unl
#用于消息收发的变量定义
synccmdfile="sync_ocs.cmd"
smapcmdfile="smap_ocs.cmd"
#当天日期
DATE=`date '+%Y%m%d'`
#增加支持oracle数据库
#=============================================================
#
#UpdateLicense()
#功能 根据传递进来的SCPNO,业务键和用户数进行判断
# 生成加密数据,形成sql语句并将其写入到文件servicenumber.sql
#输入
# $1 scp号
# $2 业务键
# $3 当前用户数
#输出
# 更新数据库的sql语句被最加到servicenumber.sql文件中
#返回
# 0 操作成功
# 1 操作失败
#
UpdateLicense()
{
SCPNO=$1
servicekind=$2
service_numbernow=$3
if [ "-$service_numbernow" = "-" ];then
service_numbernow=0
fi
#对当前License进行加密
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
dbaccess $SMPDBNAME -<<EOF >/dev/null 2>&1
unload to $TmpDir/license_info.tmp
select argument1,argument2 from w_uvs_scpconfig where
scpno = $SCPNO and servicekind = $servicekind;
EOF
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
PASSWD="`getpwd $SMPDBNAME`" #取得数据库密码
#在这里调用平台的myunload
myunload $SMPDBNAME/$PASSWD $TmpDir/license_info.tmp '|' "select argument1,argument2 from w_uvs_scpconfig where scpno = $SCPNO and servicekind = $servicekind"
fi
if [ $? -ne 0 ]; then
Log "ERROR:get license info of SCPNO:$SCPNO servicekind:$servicekind from database FAILED !"
#删除遗留的临时报表/清单文件
rm -f servicenumber.sql.tmp>/dev/null 2>&1
Log "servicenumber FAILED !"
return 1
fi
#如果不存在该业务的license数据,不进行处理
licenseinfo=`cat $TmpDir/license_info.tmp`
if [ "x$licenseinfo" = "x" ]; then
rm -f servicenumber.sql.tmp>/dev/null 2>&1
return 0
fi
read Argument1 Argument2 < $TmpDir/license_info.tmp
len1=`/bin/echo ${Argument1} | awk '{print length($0)}'`
len2=`/bin/echo ${Argument2} | awk '{print length($0)}'`
if [ ${len2} -ne 36 -o ${len1} -eq 1 ];then
Log "ERROR:License of SCP:${SCPNO} SERVICE:$servicekind in w_uvs_scpconfig is wrong!"
return 1
fi
cd ${HOME}/smp_run/smpser/bin
new_Argument2=`./rpt_pps_scp $Argument2 $Argument1 $service_numbernow`
cd ${TmpDir}
len3=`echo ${new_Argument2} | awk '{print length($0)}'`
if [ ${len3} -eq 36 ];then
echo "update w_uvs_scpconfig set Argument2='${new_Argument2}' where servicekind = $servicekind and scpno = $SCPNO;" >> servicenumber.sql
else
Log "WARNING:Fail to execute rpt_pps_scp of SCP:${SCPNO}."
return 1
fi
return 0
}
#更新smp上的license
smp_license()
{
filelist=${TmpDir}/actfilelist
rm -rf $filelist
cd ${TmpDir}
ls -l | grep ${DATE}_actuser_list_ | grep unl > $filelist
#如果文件为空,报错
if [ ! -s ${filelist} ]; then
Log "ERROR:Not find any ${DATE}_actuser_list file!"
return 1
fi
ActiveUser=0
#读一个文件
while read oneline
do
name=`echo $oneline | awk '{printf "%s", $NF}'`
#统计记录数
num=`wc -l ${name}|awk '{print $1}'`
Log "$name:$num"
ActiveUser=$(echo "scale=0; ${ActiveUser}+${num}" | bc -l)
done < $filelist
Log "Total:$ActiveUser"
oldIFS=$IFS
IFS="|"
while read SCPNO other
do
UpdateLicense $SCPNO 1010 $ActiveUser
if [ $? -ne 0 ]; then
Log "ERROR:Update License in $SCPNO FAILED !"
return 1
fi
done < $SCPNOLIST
IFS=$oldIFS
#判断SQL文件是否存在,不存在直接退出
if [ ! -f servicenumber.sql ];then
Log "WARNING:Not process SCP,exit 0"
return 1
fi
#执行update语句,更新w_uvs_scpconfig表数据
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
dbaccess $SMPDBNAME servicenumber.sql >/dev/null 2>&1
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
PASSWD="`getpwd $SMPDBNAME`" #取得数据库密码
cat servicenumber.sql | sqlplus.sh $SMPDBNAME/$PASSWD >> $LogFile 2>&1
ST=$?
fi
if [ $? -ne 0 ]; then
rm -f servicenumber.sql >/dev/null 2>&1
Log "ERROR:UPDATE license in database FAILED !"
return 1
fi
}
#获取emsampid号
get_emsmapid()
{
smapid=970
count=0
while [ $smapid -lt 999 ];
do
realsmapid=$smapid
dtms shownodeinfo | cut -c 5-9 | grep $realsmapid | wc -l | read count
if [ $count -eq 0 ]; then
smapnotool add $realsmapid
SYNCSMAPID=$smapid
return 0
fi
smapid=`expr $smapid + 1`
done
Log "The smapid is used up."
exit 1
}
#安全执行SQL语句函数
SafeExecSQL()
{
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
echo "$*" | dbaccess $SMPDBNAME >/dev/null 2>&1
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
PASSWD="`getpwd $SMPDBNAME`" #取得数据库密码
cat "$*" | sqlplus.sh $SMPDBNAME/$PASSWD >/dev/null 2>&1
fi
if [ $? -ne 0 ]; then
Log "ERROR:EXEC $* FAILED !"
exit 1
fi
}
#保证系统中同时只能有一个ocs_license运行
CheckUnique()
{
Log "Begin to check unique ..."
pid=$$
ps -ef|grep ocs_license|grep $LOGNAME>tempfile
cat tempfile|grep -v grep|grep -v $pid |grep -v root |grep -v "ocs_license.log" |grep -v "c ocs_license" | grep -v "tail " | grep -v su |read result
rm -f tempfile >/dev/null 2>&1
if [ "-${result}" = "-" ]; then
Log "Only one ocs_license is running!"
else
Log "Another ocs_license is already running!!"
LogEnd
exit 1
fi
Log "Check unique OK!"
}
# 函数:waitmsg(InitWaitTime, FinishWait)
# 功能:等待同步完毕。
# 输入:
# InitWaitTime 初始化等待时间
# FinishWait 完成等待时间
# 输出:
# 0 成功
# 1 失败
waitsync()
{
# 状态文件格式:
# INIT SYNC
# START SCP 100
# END SCP 100
# START SCP 102
# END SCP 102
# FINISH OK/FAIL
#
# 等待方法:
# 1. 等待文件非空, 最多等待InitWaitTime秒;
# 2. 等待出现FINISH字样, 最多等待FinishWaitTime秒;
# 3. 对读到的任何文本, 均显示.
# 4. 检测是否独到FINISH字样, 显示同步完成状况.
InitWaitTime=$1
FinishWaitTime=$2
if [ "-$InitWaitTime" = "-" ]; then
InitWaitTime=120
fi
if [ "-$FinishWaitTime" = "-" ]; then
FinishWaitTime=3600
fi
# 1. 取当时时间, 计算最终等待时间
StartTime=`GetTime`
WaitTime=`expr $StartTime + $InitWaitTime`
# 2. 检测文件非空.
while true
do
# 2.1 等待2秒
sleep 2
# 2.2 读一行
fileline=`cat $statusfile | wc -l`
# 2.3 如果非空, 退出检查
if [ $fileline -gt 0 ]; then
break
fi
# 2.4 看时间到了没有.
CurrentTime=`GetTime`
if [ $CurrentTime -gt $WaitTime ]; then
Log "Smpker not response, sync fail."
return 1
fi
echo " Waiting smpker response ..."
done
# 3. 计算新的等待最终时间.
WaitTime=`expr $StartTime + $FinishWaitTime`
# 4. 文件行数初始为0
LineCount=0
# 5. 开始显示状态并检测结束标志.
while true
do
# 5.1 先等待2秒
sleep 2
# 5.2 计算新行数
typeset -i NewCount
NewCount=`cat $statusfile | wc -l`
Finish=`cat $statusfile | grep FINISH`
# 5.3 显示新增加的行
if [ $NewCount -gt $LineCount ]; then
TailCount=`expr $NewCount - $LineCount`
echo "TailCount=$TailCount"
echo "statusfile=$statusfile"
tail -$TailCount $statusfile
LineCount=$NewCount
StartTime=`GetTime`
WaitTime=`expr $StartTime + $FinishWaitTime`
fi
# 5.4 取完成状态
if [ "-$Finish" != "-x" ]; then
oldIFS=$IFS
IFS=" "
echo $Finish | read FinishFlag FinishStatus
IFS=$oldIFS
if [ "-$FinishFlag" = "-FINISH" ]; then
if [ "-$FinishStatus" = "-OK" ]; then
return 0
else
return 1
fi
fi
fi
# 5.5 看时间到了没有
CurrentTime=`GetTime`
if [ $CurrentTime -gt $WaitTime ]; then
Log "Sync time out, abort sync."
return 1
fi
done
return 1
}
# 函数:GetTime()
# 功能:取系统时间,以秒计算的时间输出到标准输出。
# 输入:
# 无
# 其它变量:
# 无
# 输出:
# 0 成功
# 1 失败
GetTime()
{
dtms show_dt2time `date '+%Y%m%d%H%M%S'`
return $?
}
# 函数:create_querycmd(cmdfile)
# 功能:建立UNLOAD消息文件
# 输入:
# 其它变量:
# execwhere 指定在SCP执行,还是在SMP执行
# scpid 指定从哪个SCP取数据;
# filename 报表数据文件名;
# tablesql 生成报表的SQL语句。
#
# 输出:
# 0 成功
# 1 失败
# 注意:
# 本函数建立了statusfile,在其后的流程中,必须删除该文件。
#
create_querycmd()
{
tablesql=""
#生成构造消息用的SQL语句
oldIFS=$IFS
IFS="|"
while read Servicekind Argument1 Argument2 Others
do
#检查Servicekind的有效性
if [ "-$Servicekind" = "-" ]; then
Log "ERROR:Servicekind is null !"
exit 1
fi
#检查Argument2的有效性
if [ "-$Argument2" = "-" ]; then
Log "ERROR:Argument2 is null !"
exit 1
fi
TempSQL="update w_uvs_scpconfig set Argument2='${Argument2}' where servicekind=${Servicekind};"
#拼装发送消息的UPDATE语句
tablesql=${tablesql}${TempSQL}
done <$DataFile
IFS=$oldIFS
#在发送消息的SQL语句最后加上UNLOAD 语句(程序需要)。
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
TempSQL="unload to ${filename} select * from w_uvs_scpconfig;"
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
#在这里调用平台的myunload
#TempSQL="!myunload / ${filename} ' ' 'select * from w_uvs_scpconfig'"
#TempSQL="unload to ${filename} select * from w_uvs_scpconfig"
TempSQL="sqlplus / ;set echo off;set feedback off;set heading off;set trimout off;set trimout on;spool ${filename};select * from w_uvs_scpconfig;spool off;exit;"
fi
tablesql=${tablesql}${TempSQL}
echo "tablesql=$tablesql"
#echo "tablesql=$tablesql"
statusfile=`GetTime`
statusfile=$TmpDir/${statusfile}.$$
rm -f $statusfile
rm -f $synccmdfile
touch $statusfile
cat <<EOF > $synccmdfile
7
BEGIN
command=data_unload|
flagwheretodo=scponly|
flagsql=sql|
scpid=$SCPNO|
statusfile=$statusfile|
sqlstmt=$tablesql|
filename=$filename|
;
END
EOF
return $?
}
#函数:create_smapcmd_sync(synccmdfile)
# 功能:建立SMAP命令行文件
# 输入:
# synccmdfile 命令行文件名
# 其它变量:
# 无
# 输出:
# 0 成功
# 1 失败
create_smapcmd_sync()
{
rm -f $smapcmdfile
cat <<EOF > $smapcmdfile
g < $synccmdfile
EOF
#cat $synccmdfile
return $?
}
# 函数:domsg()
# 功能:执行消息命令,等待消息执行完毕。
# 输入:
# 无
# 输出:
# 无
# 返回:
# 0 成功
# 1 失败
domsg()
{
emsmap $SYNCSMAPID exec $smapcmdfile >> $LogFile
waitsync 10 30
result=$?
rm -f $statusfile
return $result
}
#完成数据同步功能
syncdata()
{
Log "Begin to syncdate..."
#同步w_uvs_scpconfig
oldIFS=$IFS
IFS="|"
while read SCPNO other
do
#定义从SCP传回来的文件名
filename=w_uvs_scpconfig.${SCPNO}
#从w_uvs_scpconfig表中卸载SCPNO的相关统计数据
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
SafeExecSQL "unload to $DataFile delimiter '|' select servicekind,argument1,argument2 from w_uvs_scpconfig where scpno = $SCPNO;"
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
PASSWD="`getpwd $SMPDBNAME`" #取得数据库密码
#在这里调用平台的myunload
myunload $SMPDBNAME/$PASSWD $DataFile '|' "select servicekind,argument1,argument2 from w_uvs_scpconfig where scpno = $SCPNO"
fi
#切换到emsmap所在的smp_run/bin目录下
cd ${HOME}/smp_run/bin
create_querycmd $SCPNO
create_smapcmd_sync
domsg
if [ $? -ne 0 ]; then
Log "ERROR:SCPNO $SCPNO:scpconfig EXEC domsg FAILED !"
else
Log "SCPNO $SCPNO:scpconfig EXEC domsg OK !"
fi
rm -f $DataFile
done < $SCPNOLIST
IFS=$oldIFS
}
#获取scpno
get_scpno()
{
#取得所有SCPNO
SCPNOLIST=$TmpDir/allscp.list
if [ "-$DBSERVERTYPE" = "-INFORMIX" ]; then
dbaccess $SMPDBNAME - <<EOF >/dev/null
unload to $SCPNOLIST delimiter "|"
select SCPNo from w_uvs_scpconfig;
EOF
elif [ "-$DBSERVERTYPE" = "-ORACLE" ]; then
PASSWD="`getpwd $SMPDBNAME`" #取得数据库密码
#在这里调用平台的myunload
myunload $SMPDBNAME/$PASSWD $SCPNOLIST '|' "select SCPNo from w_uvs_scpconfig"
fi
if [ $? -ne 0 ]; then
return 1
fi
}
# ====== main =========
ProgName=`basename $0`
#程序启动日志
LogStart $ProgName
#唯一性检查
CheckUnique
#获取所有scpno
get_scpno
if [ $? -ne 0 ]; then
Log "ERROR:Select SCPNo from w_uvs_scpconfig failed!"
LogEnd $ProgName
exit 1
fi
#更新smp上的license
smp_license
if [ $? -ne 0 ]; then
Log "ERROR:Update w_uvs_scpconfig in SMP failed!"
LogEnd $ProgName
exit 1
fi
#获取smapid
get_emsmapid
#更新SCP上w_uvs_scpconfig表中数据
syncdata
if [ -f ${TmpDir}/servicenumber.sql ];then
rm -f ${TmpDir}/servicenumber.sql
fi
if [ -f $TmpDir/license_info.tmp ];then
rm -f $TmpDir/license_info.tmp
fi
#结束日志
LogEnd $ProgName
exit 0