环境说明:

公司是做在线教育的互联网企业,WEB架构为:前端使用LVS + Heartbeat做负载均衡,后端主要是Apache/Nginx + Tomcat,缓存有redis和Memcached,数据库使用的Oracle和Mysql。

脚本实现目的:

通过查看tomcat日志来检测服务是否正常。

脚本思路:

检测tomcat的当前连接数以及与数据库的连接数,若超过规定值则重启应用。

在脚本中事先定义好错误关键字,每次检测前提取日志的最后一万行到一个临时文件中。若检测到指定的错误,则触发指定的操作,操作完成后备份日志,并清空当前日志(避免下次检测时重复触发操作)。所有的服务都是通过配置文件的方式传递给脚本,以方便批量部署。

脚本内容:

#!/bin/bash
#This shell-script is use for check tomcat log and connection status
#Created  in 2012-02-17
#Last changed in 2013-04-26
source ~/.bash_profile &>/dev/nulldir=/tol/app
dir2=/tol/script
cd $dir2dt2=`date +"%Y-%m-%d"`
ls $dir2/logs &>/dev/null || mkdir -p $dir2/logs &>/dev/null
log="$dir2/logs/tomcat-check-$dt2.log"
host=`/sbin/ifconfig |grep "inet addr"|cut -d ':' -f2 |awk '{print $1}'|head -1`
conf=$dir2/tomcat-check.conf
sh_name=$0function shijian () {dt=`date +"%Y-%m-%d-%H:%M:%S"`
}
#conf-check
if test ! -f $conf ; thenecho "The $conf is not exist"exit 0
fi
shijian
echo "$dt" >> $log
echo "$host" >> $logwhile read line
do
shijian
tom=`echo $line |awk -F\; '{print $1}'`
con_num=`echo $line |awk -F\; '{print $4}'`
db_port=`echo $line |awk -F\; '{print $5}'`
db_num=`echo $line |awk -F\; '{print $6}'`
function pid () {pid=''pid=`ps -elf|grep java|grep "$dir"|grep $tom|awk '{print $4}'`
}
function tom_restart () {
#stop`echo $line |awk -F\; '{print $2}'`sleep 3pidif test -n "$pid" ; thenkill -9 $pidfirm -rf $dir/$tom/work/Catalina/*rm -rf $dir/$tom/temp/*
#start`echo $line |awk -F\; '{print $3}'`
}
tail -10000 $dir/$tom/logs/catalina.out > $dir/$tom/logs/check.log
catalina=$dir/$tom/logs/check.log
catalina2=$dir/$tom/logs/catalina.out
function clean () {\cp $catalina2 $catalina2-$dt && >$catalina2
}
function chongqi () {tom_restartshijian
}
#check-logsize
shijian
size=`du -m $catalina2 | awk '{print $1}'`if test $size -ge 2000 ; then>$catalina2echo "The $dt $host $catalina2 is too much big size="$size-M"" |mail -s "$host $tom catalina.out" jiank
elseshijianecho "$dt check__$host $tom $catalina2 size__is done" >> $log#check-logkeywordcheck1="OutOfMemoryError"check2="Too many open files"check3="Heap at VM Abort"check4="Cannot get a connection"check5="timeout"for check in "$check1" "$check2" "$check3" "$check4" "$check5"dokeyword=`grep -i -c "$check" $catalina`if [ "$keyword" -gt "1" ] ; thenif [ "$check" = timeout ] ; thenif [ "$keyword" -gt "500" ] ; thencleanchongqiecho "$dt $tom "$check" and restart by $sh_name newpid=$pid" >> $logecho "$host $tom "$check" by $sh_name" |mail -s "$host $tom will check!!" jiankfielsecleanchongqipidif test -n "$pid" ; thenecho "$dt $tom "$check" and restart by $sh_name newpid=$pid" >> $logecho "$dt $host $tom "$check" and restart by $sh_name" |mail -s "$host $tom will restart" jiankelseecho "$dt $tom "$check" and restart fail by $sh_name" >> $logecho "$dt $host $tom "$check" and restart fail by $sh_name" |mail -s "check $host $tom" jiankfifielseecho "$dt check__$host $tom "$check"__is done" >> $logfidone#check Oracle Io Exceptionio=`grep -i "Connection timed out" $catalina |grep -i -c "java.sql.SQLException: Io"`if [ "$io" !=  "0" ] ; thencleanchongqipidif test -n "$pid" ; thenecho "$dt $tom Oracle Io Exception and restart by $sh_name newpid=$pid" >> $logecho "$dt $host $tom Oracle Io Exception and restart by $sh_name"|mail -s "$host $tom will restart" jiankelseecho "$dt $tom Oracle Io Exception and restart fail by $sh_name" >> $logecho "$dt $host $tom Oracle Io Exception and restart fail by $sh_name" |mail -s "check $host $tom" jiankfielseecho "$dt check__$host $tom Oracle Io Exception__is done" >> $logfi#check process connect numbersconnect=`ps -eLf|grep java|grep "$dir"|grep -c $tom`if [ "$connect" -gt "$con_num" ] ; thenchongqipidif test -n "$pid" ; thenecho "$dt $tom connect more than $con_num proc and restart by $sh_name newpid=$pid" >> $logecho "$dt $host $tom connect more than $con_num proc and restart by $sh_name"| mail -s "$host $tom will restart" jiankelseecho "$dt $tom connect more than $con_num proc and restart fail by $sh_name" >> $logecho "$dt $host $tom connect more than $con_num proc and restart fail by $sh_name" |mail -s "check $host $tom" jiankfielseecho "$dt check__$host $tom connect more than $con_num proc__is done" >> $logfi#check db connect numbersif test -n "$db_port" ; thenpid=''pid=`ps -elf|grep java|grep "$dir"|grep $tom|/usr/bin/tail -1|awk '{print $4}'`dbconno=`netstat -tunp|grep "$db_port"|grep -c "$pid"`if [ "$dbconno" -gt "$db_num" ] ; thenchongqipidif test -n "$pid" ; thenecho "$dt $tom db connect more than $db_num and restart by $sh_name newpid=$pid" >> $logecho "$dt $host $tom db connect more than $db_num and restart by $sh_name"|mail -s "$host $tom will restart" jiankelseecho "$dt $tom db connect more than $db_num and restart fail by $sh_name" >> $logecho "$dt $host $tom db connect more than $db_num and restart fail by $sh_name" |mail -s "check $host $tom" jiankfielseecho "$dt check__$host $tom db connect numbers more than $db_num __is done" >> $logfifi
fi
done < $conf

脚本配置文件:

tomcata;/etc/init.d/tomcata stop;/etc/init.d/tomcata start;1000;1521;200;
tomcatb;/etc/init.d/tomcatb stop;/etc/init.d/tomcatb start;1000;1521;200;

转载于:https://blog.51cto.com/rmeos/1423420

生产环境WEB服务管理脚本之日志检测脚本相关推荐

  1. 生产环境WEB服务管理脚本之监控脚本

    环境说明: 公司是做在线教育的互联网企业,WEB架构为:前端使用LVS + Heartbeat做负载均衡,后端主要是Apache/Nginx + Tomcat,缓存有redis和Memcached,数 ...

  2. 通俗易懂的生产环境Web应用架构介绍

    前言 看见一篇非常通俗易懂且适合新手阅读的Web应用架构文章,我将其手工翻译了出来,分享给大家. 也可以去阅读英文原文,标题为,贴出链接: stephenmann.io/post/whats-- 英文 ...

  3. Python开发【项目】:生产环境下实时统计网站访问日志信息

    日志实时分析系统 生产环境下有需求:要每搁五分钟统计下这段时间内的网站访问量.UV.独立IP等信息,用直观的数据表格表现出来 环境描述: 网站为Nginx服务,系统每日凌晨会对日志进行分割,拷贝到其他 ...

  4. 记一次生产环境java服务mqtt连接线程数过多的处理过程

    项目介绍: 本项目是负责发放机设备发放商品的平台.发放机设备是厂商控制,发放机平台是我们公司负责开发和维护.发放机设备和平台是通过mtqq协议通信的. mqtt开发客户端使用的是org.eclipse ...

  5. 一文看懂SMT车间生产环境要求及管理规范

    SMT概念 SMT是表面组装技术(表面贴装技术)(SurfaceMountTechnology的缩写),称为表面贴装或表面安装技术.是目前电子组装行业里最流行的一种技术和工艺. 它是一种将无引脚或短引 ...

  6. netapp脚本保存日志_Shell脚本实战:日志关键字监控+自动告警

    原文转载于:https://os.51cto.com/art/202004/613630.htm 主要用于Linux服务器监控程序日志,如出现关键字异常则触发相应的动作或告警操作,通知到邮件联系人. ...

  7. linux日志清除脚本,linux 日志清除脚本

    linux 日志清除脚本 #!/bin/bash echo " linux clear log " echo " by knlve 2008-08-29" ec ...

  8. Linux学习总结(57)——生产环境用户权限管理规范

    一.问题现状 公司生产服务器通常上百台,甚至上千台上万台,操作人员很多(开发+运维+架构+DBA).大家使用Linux服务器时,不同职能的员工水平不同,老手和新手员工熟知度不同,如果权限控制不当(如r ...

  9. java开发的微信公众号服务端生产环境中的两个大坑

    摘要: 我们开发的公众号,由于将功能开发完毕后,未对服务进行压力测试,因此用到的组件中的参数值全是默认的,服务上线后一段时间运行得倒没什么问题,随着服务得访问量增加,一些多线程并发的问题就逐步暴露出来 ...

最新文章

  1. CGCTF-Web-md5 collision
  2. 【HDU - 2203】 亲和串 (思维题,可选KMP)
  3. MyEclipse下XFire开发Webservice实例
  4. 完美解决header,footer等HTML5标签在IE(IE6/IE7/IE8)无效的方法
  5. java基础知识---IO常用基础操作(二)
  6. java jxl 复制单元格_如何用JAVA(如poi、jxl等)读取excel文件中的下拉框单元格的值。...
  7. vue+node全栈移动商城【7】路由跳转-注册页面
  8. grdraw用法 lisp_AutoCAD 2000 Visual LISP开发
  9. 万能解压器安卓版_全能压缩软件下载
  10. POJ 3253.Fence Repair
  11. 2019安徽省程序设计竞赛 D.自驾游(最短路)
  12. 粉丝投稿!分享自己的携程后台一面+二面+HR面,希望对大家有帮助!
  13. antv g2字体阴影_antv-g2学习手册-中
  14. WPF 不要给 Window 类设置变换矩阵(分析篇):System.InvalidOperationException: 转换不可逆。
  15. Android UI换皮肤或 白天黑夜模式
  16. 大数据入门之分布式计算框架Spark(3) -- Spark Streaming
  17. 论文笔记:m6Acorr: an online tool for the correction and comparison of m6A methylation profiles
  18. GICv3软件overview手册之GICv3基本功能(1)
  19. 一个简单的安居客房屋信息爬虫
  20. 【概率论与数理统计】

热门文章

  1. SAP QM 采购货物收到第三方仓库,转库回工厂仓库之后质检之处理
  2. SAP MM Transportation of PR Release Strategy with Classification
  3. 深度丨2018年AI依然要面临解决的的技术难题
  4. 《科学》:中国科学家揭示,人脑中间神经元多样性从何而来?
  5. 喻国明:“元宇宙”背后的未来图景
  6. 从多维度解析神经科学中的视觉编码
  7. 重磅!2020年全球高被引科学家名单出炉!
  8. 把握芯片科技发展趋势 促进半导体产业创新突破
  9. 混合云关键技术能力和发展趋势
  10. 2019年度全球工程前沿研究报告