获取前一天Tomcat的异常日志
前几天收到主管的要求,说让我把各团队负责的Tomcat前一天的异常日志信息通过邮件发送给各团队主管。那么,后来就写出了这个脚本。
思路大概是先把团队tomcat的log检索异常信息进行重定向到文件中,然后判断该文件是否有信息,如果没有,则移除;如果有,则压缩进行打包发送,发送之后进行检索下一个团队的log file。
里面的err_log.txt文件所包含的是 server_ip、host_name、以及item_name,例如长下面这样:
172.16.1.16 S-C4 tradeservice-alive1
172.16.1.17 S-C5 tradeservice-alive1
172.16.1.25 S-C15 tradeservice-alive1
172.16.1.31 S-C21 tradeservice-alive1
下面是脚本
#!/bin/bash
date=$(date "+%h %d")
day=$(date +%Y-%m-%d)
#日志文件存放目录
#tar打包后存放目录
#发送邮件后移除到old目录
log_dir="/tmp/tomcat_error/tomcat_error_log"
tar_dir="/tmp/tomcat_error/tomcat_error_tar"
old_dir="/tmp/tomcat_error/tomcat_error_old"
#grep在检索的时候所过滤掉的信息,将变量带入下面每个团队的grep中
grep_content="(WARN|null|org.apache.catalina.connector.|Mozilla|DEBUG level|isSuccess)"
#小毛团队_Tomcat_Log
#查看小毛团队的tomcat个数
tomcat_line="$(cat /data/sh/xiaomao_err_list.txt | egrep -v "(^$|^#)" | wc -l)"
xiaomao_list="/data/sh/xiaomao_err_list.txt"
tram="hero"
for (( n = 1; n <= ${tomcat_line} ; n++ ))
do
#ip_addreess为tomcat所在serverip
#host_name为tomcat所在主机名
#item_name为tomcat具体名称
ip_addres=$(sed -n ${n}p ${xiaomao_list} | awk '{print $1}')
host_name=$(sed -n ${n}p ${xiaomao_list} | awk '{print $2}')
item_name=$(sed -n ${n}p ${xiaomao_list} | awk '{print $3}')
echo ${ip_addres}_${host_name}_${item_name}
#ssh通过远程过滤日志并且将日志保存在log_dir下面
ssh root@${ip_addres} "grep -v INFO /usr/local/tomcats/${item_name}/logs/catalina.out" | egrep -v "${grep_content}" |grep -v "${date}" > ${log_dir}/${item_name}_${host_name}_error.log
#进入log_dir目录,查看日志行数
#判断日志是否为空,如果为空则表示无错误,移除。避免打包发送
cd ${log_dir} && line=$(cat ${item_name}_${host_name}_error.log | wc -l)
if [ ${line} -eq 0 ] ;then
mv ${item_name}_${host_name}_error.log ${old_dir}
fi
done
#进行打包,并且通过邮件发送给小毛团队
#发送完之后将log及tar包进行移除,避免和下个团队的log混在一起
tar jcf ${tar_dir}/${day}_${tram}_all_error.log.tbz2 *.log
echo "${day}_${tram}_all_error.log" | mail -s "${day}_${tram}_all_error.log" -a ${tar_dir}/${day}_${tram}_all_error.log.tbz2 xiaomao@xxx.com
mv ${log_dir}/* ${tar_dir}/* ${old_dir}/
#九戒团队_Tomcat_Log
tomcat_line="$(cat /data/sh/jiujie_err_list.txt | egrep -v "(^$|^#)" | wc -l)"
jiujie_list="/data/sh/jiujie_err_list.txt"
tram="qingyun"
for (( n = 1; n <= ${tomcat_line} ; n++ ))
do
ip_addres=$(sed -n ${n}p ${jiujie_list} | awk '{print $1}')
host_name=$(sed -n ${n}p ${jiujie_list} | awk '{print $2}')
item_name=$(sed -n ${n}p ${jiujie_list} | awk '{print $3}')
echo ${ip_addres}_${host_name}_${item_name}
ssh root@${ip_addres} "grep -v INFO /usr/local/tomcats/${item_name}/logs/catalina.out" | egrep -v "${grep_content}" |grep -v "${date}" > ${log_dir}/${item_name}_${host_name}_error.log
cd ${log_dir} && line=$(cat ${item_name}_${host_name}_error.log | wc -l)
if [ ${line} -eq 0 ] ;then
mv ${item_name}_${host_name}_error.log ${old_dir}
fi
done
tar jcf ${tar_dir}/${day}_${tram}_all_error.log.tbz2 *.log
echo "${day}_${tram}_all_error.log" | mail -s "${day}_${tram}_all_error.log" -a ${tar_dir}/${day}_${tram}_all_error.log.tbz2 jiujie@xxx.com
mv ${log_dir}/* ${tar_dir}/* ${old_dir}/
#晨风团队_Tomcat_Log
tomcat_line="$(cat /data/sh/chenfeng_err_list.txt | egrep -v "(^$|^#)" | wc -l)"
chenfeng_list="/data/sh/chenfeng_err_list.txt"
tram="kunlun"
for (( n = 1; n <= ${tomcat_line} ; n++ ))
do
ip_addres=$(sed -n ${n}p ${chenfeng_list} | awk '{print $1}')
host_name=$(sed -n ${n}p ${chenfeng_list} | awk '{print $2}')
item_name=$(sed -n ${n}p ${chenfeng_list} | awk '{print $3}')
echo ${ip_addres}_${host_name}_${item_name}
ssh root@${ip_addres} "grep -v INFO /usr/local/tomcats/${item_name}/logs/catalina.out" | egrep -v "${grep_content}" |grep -v "${date}" > ${log_dir}/${item_name}_${host_name}_error.log
cd ${log_dir} && line=$(cat ${item_name}_${host_name}_error.log | wc -l)
if [ ${line} -eq 0 ] ;then
mv ${item_name}_${host_name}_error.log ${old_dir}
fi
done
tar jcf ${tar_dir}/${day}_${tram}_all_error.log.tbz2 *.log
echo "${day}_${tram}_all_error.log" | mail -s "${day}_${tram}_all_error.log" -a ${tar_dir}/${day}_${tram}_all_error.log.tbz2 chenfeng@xxx.com
mv ${log_dir}/* ${tar_dir}/* ${old_dir}/
#缺哥团队_Tomcat_Log
tomcat_line="$(cat /data/sh/wuque_err_list.txt | egrep -v "(^$|^#)" | wc -l)"
wuque_list="/data/sh/wuque_err_list.txt"
tram="xiaoyao"
for (( n = 1; n <= ${tomcat_line} ; n++ ))
do
ip_addres=$(sed -n ${n}p ${wuque_list} | awk '{print $1}')
host_name=$(sed -n ${n}p ${wuque_list} | awk '{print $2}')
item_name=$(sed -n ${n}p ${wuque_list} | awk '{print $3}')
echo ${ip_addres}_${host_name}_${item_name}
ssh root@${ip_addres} "grep -v INFO /usr/local/tomcats/${item_name}/logs/catalina.out" | egrep -v "${grep_content}" |grep -v "${date}" > ${log_dir}/${item_name}_${host_name}_error.log
cd ${log_dir} && line=$(cat ${item_name}_${host_name}_error.log | wc -l)
if [ ${line} -eq 0 ] ;then
mv ${item_name}_${host_name}_error.log ${old_dir}
fi
done
tar jcf ${tar_dir}/${day}_${tram}_all_error.log.tbz2 *.log
echo "${day}_${tram}_all_error.log" | mail -s "${day}_${tram}_all_error.log" -a ${tar_dir}/${day}_${tram}_all_error.log.tbz2 wuque@xxx.com
mv ${log_dir}/* ${tar_dir}/* ${old_dir}/
#忍冬团队_Tomcat_Log
tomcat_line="$(cat /data/sh/rendong_err_list.txt | egrep -v "(^$|^#)" | wc -l)"
rendong_list="/data/sh/rendong_err_list.txt"
tram="shushan"
for (( n = 1; n <= ${tomcat_line} ; n++ ))
do
ip_addres=$(sed -n ${n}p ${rendong_list} | awk '{print $1}')
host_name=$(sed -n ${n}p ${rendong_list} | awk '{print $2}')
item_name=$(sed -n ${n}p ${rendong_list} | awk '{print $3}')
echo ${ip_addres}_${host_name}_${item_name}
ssh root@${ip_addres} "grep -v INFO /usr/local/tomcats/${item_name}/logs/catalina.out" | egrep -v "${grep_content}" |grep -v "${date}" > ${log_dir}/${item_name}_${host_name}_error.log
cd ${log_dir} && line=$(cat ${item_name}_${host_name}_error.log | wc -l)
if [ ${line} -eq 0 ] ;then
mv ${item_name}_${host_name}_error.log ${old_dir}
fi
done
tar jcf ${tar_dir}/${day}_${tram}_all_error.log.tbz2 *.log
echo "${day}_${tram}_all_error.log" | mail -s "${day}_${tram}_all_error.log" -a ${tar_dir}/${day}_${tram}_all_error.log.tbz2 rendong@xxx.com
mv ${log_dir}/* ${tar_dir}/* ${old_dir}/