script/检查作业成功率/check_job_sucessful.sh

116 lines
4.1 KiB
Bash

#!/bin/bash
#set -x
PRG="$0"
while [ -h "$PRG" ]; do
ls=$(ls -ld "$PRG")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '.*/.*' >/dev/null; then
PRG="$link"
else
PRG=$(dirname "$PRG")/"$link"
fi
done
PRGDIR=$(dirname "$PRG")
#full path
cd $PRGDIR
ROOT_DIR=$(pwd)
cd - >/dev/null
echo "" >$ROOT_DIR/check_report.txt
#数据库的用户名和密码
MYSQLUSER="unadba"
MYSQLPWD="1223Bc@2008"
# 查询某个策略的成功率
function check_sucessful(){
# 代理信息和策略名
comname=$1
compid=$2
policyname=$3
#查询
#先查询出所有的tbl_jobhistory表
SQL_TABLES="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='unary' AND TABLE_NAME regexp 'tbl_jobhistory(_2)|^tbl_jobhistory$'"
RESULT_TABLES=`mysql -u$MYSQLUSER -p$MYSQLPWD -e "$SQL_TABLES" 2>/dev/null|awk 'NR>1'`
#拼接所有的tbl_jobhistory表
ALLJOBHISTORY=""
for i in $RESULT_TABLES
do
JOBHISTORY="select operdesc,count(*) as n2 from tbl_jobhistory${i:14} where policyname='${policyname}' and compid='${compid}' group by operdesc"
# UNION ALL并到一起去
ALLJOBHISTORY=$ALLJOBHISTORY" UNION ALL "$JOBHISTORY
done
#最终拼接的表
ALLJOBHISTORY=${ALLJOBHISTORY:10}
SELECTALL="select operdesc,sum(n2) as num from ($ALLJOBHISTORY) b group by b.operdesc"
SELECTCHECK="select 1-(tb1.num1/tb2.num2) from (select sum(num) as num1 from \
($SELECTALL) t1 where operdesc like '%失败%') as tb1 \
right JOIN \
(select sum(num) as num2 from \
($SELECTALL) t2) as tb2 \
on 1=1;"
recode=`mysql -u$MYSQLUSER -p$MYSQLPWD unary -e "$SELECTCHECK" 2>/dev/null|tail -1`
# 输出结果:
echo -e "${comname}" >>$ROOT_DIR/check_report.txt
if [ ${recode} = "NULL" ]; then
errmsg="统计计算异常,请人工计算!"
echo -e "\t ${policyname} 的作业成功率为: ${errmsg} \n" >>$ROOT_DIR/check_report.txt
# 异常的将查询到的结果输出
echo "策略 ${policyname} 查询到的所有任务状态如下:" >>$ROOT_DIR/check_report.txt
mysql -u$MYSQLUSER -p$MYSQLPWD unary -e "$SELECTALL" >> $ROOT_DIR/check_report.txt
else
recode2=$(echo "scale=2;$recode * 100" | bc)
recode=${recode2:0:5}
echo -e "\t ${policyname} 的作业成功率为: ${recode}%" >>$ROOT_DIR/check_report.txt
fi
# 间隔符
echo "-----------" >>$ROOT_DIR/check_report.txt
}
# 需要检查的代理
# mh
echo "===========mh================" >>$ROOT_DIR/check_report.txt
#check_sucessful "6.9长期mh_MySQL定时备份_13.167" "811da1a9258bb690d58aaef5aa958027" "长期任务-mysql物理多通道"
check_sucessful "6.9长期mh_MySQL定时备份_13.167" "124f3e5397ec196ae43f71d188450845" "长期任务-mysql物理多通道"
check_sucessful "6.9长期mh_OraclePhysical定时备份_29.41" "effbff8590c4c69a33151fa4d4fff2e1" "长期Oracle多通道"
check_sucessful "6.9长期mh_系统定时备份_29.33" "bd22ea57feecb5a7f96e67035c204e02" "长期系统"
# ypy
echo -e "\n===========ypy===============" >>$ROOT_DIR/check_report.txt
check_sucessful "6.9长期MySQLCDM_linux_29.48_ypy" "50061b4e4f80fa802067209eb18a232f" "长期"
check_sucessful "6.9长期OracleCDM备份_linux_29.40_ypy" "46117564f229b9b7f407b21aa5d7fa1c" "长期"
check_sucessful "6.9长期分区_WIN_29.47_ypy" "d9f991a83017a2b4ad9161044f5fca8f" "长期"
check_sucessful "6.9长期OracleCLP备份_rac_ypy" "2737975f-15c3-4716-968a-fc8ab2e9f1fc" "长期"
#zry
echo -e "\n===========zry===============" >>$ROOT_DIR/check_report.txt
check_sucessful "6.9长期定时文件29.49_zry" "b1b4c1d1b276265ae78914f350e29f96" "多通道"
check_sucessful "6.9长期定时文件29.49_zry" "b1b4c1d1b276265ae78914f350e29f96" "普通"
check_sucessful "6.9长期副本文件CDM备份29.42_zry" "55037758a2f200826bff0061d6dbdb91" "文件CDM"
check_sucessful "6.9长期副本文件CDM备份29.42_zry" "55037758a2f200826bff0061d6dbdb91" "218"
check_sucessful "6.9长期VMware备份_zry" "11BBF49B7B6E4CD4A079CD33BDB5F29F" "VMware长期"
check_sucessful "6.9长期openstack备份" "76CAB9108D8747548805A209D77A9370" "长期"
echo "开始发送邮件"
python $ROOT_DIR/SendEmail.py