环境说明:

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

脚本实现目的:

通过在服务器本地运行脚本,当检测到服务不可用时,自动重启相关服务,并发邮件通知管理员。

通过脚本检测服务关键配置文件,若文件丢失,则根据备份自动恢复文件,并重启。

脚本思路:

通过脚本访问事先定义好的监控页面,一共访问三次,每次访问间隔几秒钟,若三次访问都失败,则认为服务故障,重启服务并发邮件。配置文件的备份则是通过重启脚本来完成,所有的服务都是通过配置文件的方式传递给脚本,以方便批量部署。

脚本内容:

#!/bin/bash
#This shell-script is use for check tomcat and apache/nginx alive
#Created in 2012-11-01
#Last changed in 2013-05-25
source ~/.bash_profile &>/dev/nulldir=''
dir=/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/tomcatwget-$dt2.log"
host=`/sbin/ifconfig |grep "inet addr"|cut -d ':' -f2 |awk '{print $1}'|head -1`
wgetfile=$dir2/tomcat_wgetfile
conf=$dir2/tomcatwget.conf
conf3=$dir2/web.conf
sh_name=$0function shijian () {dt=`date +"%Y-%m-%d-%H:%M:%S"`
}
function apache_restart () {
#stopuser=`ps  -elf |grep -v grep|grep -v root|grep '/bin/httpd'|grep "$dir"|awk '{print $3}'|head -1`$dir/apache/bin/httpd -k stop  &>/dev/nullsleep 5opid=''opid=`ps -elf |grep -v grep|grep "httpd"|grep "$dir"|awk '{print $4}'`if test -n "$opid" ; thenkill -9  $opidfi
#start/usr/bin/ipcs -s | grep "$user"| gawk '{ print $2 }' | xargs -n 1 ipcrm sem &> /dev/null$dir/apache/bin/httpd -k start  &>/dev/nullnpid=`ps -elf |grep -v grep|grep "httpd"|grep root |grep "$dir"|awk '{print $4}'`shijianif test -z "$npid" ; thenecho "The $dt $host apache restart fail by $sh_name" >> $logecho "The $dt $host apache restart fail by $sh_name" |mail -s "check $host apache restart fail" jiankelseecho "The $dt $host apache is restart,the new pid=$npid" >> $logecho "The $dt $host apache is restart by $sh_name,the new pid=$npid" |mail -s "check $host apache restart" jiankfi
}
function nginx_restart () {
#stopopid=''opid=`cat $dir/nginx/logs/nginx.pid`kill -QUIT $opidsleep 3opid=`ps -elf |grep -v grep|grep process|grep nginx |awk '{print $4}'`if test ! -z "$opid" ; thenkill -9 $opidfi
#start$dir/nginx/sbin/nginx -c $dir/nginx/conf/nginx.confnpid=''npid=`ps -elf |grep -v grep|grep process|grep nginx |awk '{print $4}'`shijianif test -z "$npid" ; thenecho "The $dt $host nginx restart fail by $sh_name" >> $logecho "The $dt $host nginx restart fail by $sh_name" |mail -s "check $host nginx restart fail" jiankelseecho "The $dt $host nginx is restart,the new pid=$npid" >> $logecho "The $dt $host nginx is restart by $sh_name,the new pid=$npid" |mail -s "check $host nginx restart" jiankfi
}
function http_restart () {if test ! -f $conf3 ; thenecho "The $conf3 is not exist" >> $logapache_restartsleep 3nginx_restartelsewhile read linedodir=`echo $line |awk -F\; '{print $1}'`softname=`echo $line |awk -F\; '{print $2}'`$softname\_restartdone < $conf3fi
}
function web_reboot () {http_restartsh $dir2/tomcat-restart.sh $name &>/dev/nullexit 0
}#conf-check
if test ! -f $conf ; thenecho "The $conf is not exist"exit 0
fi
shijian
echo "$dt" >> $log
echo "$host" >> $log##Apache-check
domain=`/usr/bin/head -1 $conf |awk -F\; '{print $1}'`
if test `echo $domain |grep http` ; thenhtml="$domain"
elsehtml=http://$domain/t-s.html
ficat /dev/null > $wgetfile
/usr/bin/elinks -dump  $html >> $wgetfile 2>&1
sleep 3
/usr/bin/elinks -dump  $html >> $wgetfile 2>&1
sleep 3
/usr/bin/elinks -dump  $html >> $wgetfile 2>&1num=''
num=`grep -i -c "ok" $wgetfile`
if (( $num < 1 )) ; thenshijianecho "The $dt $host $html wget error" >> $logecho "The $dt $host $html wget error" | mail -s "check the $host wget error" jiankhttp_restart
elif (( $num < 3&&$num >= 1 )) ; thenecho "The $host $html wget oknumber is $num" |mail -s "$host wget oknumber" jiank
elseecho "The $host $html wget success" >> $log
fiwhile read line
do
shijian
file1=''
file1=`echo $line |awk -F\; '{print $2}'`
file2=''
file2=`echo $line |awk -F\; '{print $3}'`
name=''
name=`echo $line |awk -F\; '{print $4}'`
port=''
port=`echo $line |awk -F\; '{print $5}'`
function pid () {pid=''pid=`ps -elf|grep java|grep "$dir"|grep $name|awk '{print $4}'`
}
function down () {`echo $line |awk -F\; '{print $6}'`sleep 3pidif test -n "$pid" ; thenkill -9 $pidfirm -rf $dir/$name/work/Catalina/*rm -rf $dir/$name/temp/*
}
function up () {`echo $line |awk -F\; '{print $7}'`
}
#web.xml-check
if test "$file1" ; thenif test ! -f $file1 ; thenshijian\cp $file1.bak $file1 && chown tomcat.tomcat $file1 &>/dev/nullif test -f $file1 ; then echo "The $dt $file1 is lost and will be recovery" >> $logecho "The $dt $file1 is lost and will be recovery" | mail -s "check the $host $file1 lost" jiankweb_rebootelseecho "The $dt $file1 is lost and recovery fail" >> $logecho "The $dt $file1 is lost and recovery fail" | mail -s "check the $host $file1 lost" jiankfi elseecho "The $dt $file1 is ok" >> $logfi
fi
#ROOT.xml-check
if test "$file2" ; thenif test ! -f $file2 ; thenshijian\cp $file2.bak $file2 && chown tomcat.tomcat $file2 &>/dev/nullecho "The $dt $file2 do not exist" >> $logecho "The $dt $file2 do not exist" | mail -s "check the $host $file2 lost" jiankdownuppidif test -n "$pid" ; thenecho "$dt $host $file2 is lost and restart by $sh_name" |mail -s "$host $file2 is lost" jiankelseupecho "The $dt $host $file2 is lost and restart fail by $sh_name" >> $logecho "The $dt $host $file2 is lost and restart fail by $sh_name" |mail -s "check $host $name restart fail" jiankfielseecho "The $dt $file2 is ok" >> $logfi
fi
#tomcat-check
if test "$port" ; thenif test `echo $port|grep http` ; thenjsp="$port"elsejsp=http://127.0.0.1:$port/t-s.jspficat /dev/null > $wgetfile
/usr/bin/elinks -dump  $jsp >> $wgetfile 2>&1
sleep 5
/usr/bin/elinks -dump  $jsp >> $wgetfile 2>&1
sleep 5
/usr/bin/elinks -dump  $jsp >> $wgetfile 2>&1num=''
num=`grep -i -c "ok" $wgetfile`if (( $num < 1 )) ; thenshijianecho "The $dt $host $name wget error 3 times and begin to restart" >> $logdownuppidif test -n "$pid" ; thenecho "$dt Restart $name success pid= " $pid >> $logecho "$dt $host $name wget error and restart by $sh_name" |mail -s "$host $name wget error" jiankelseupecho "The $dt $host $name wget error and restart fail by $sh_name" >> $logecho "The $dt $host $name wget error and restart fail by $sh_name" |mail -s "check $host $name restart fail" jiankfi
elif (( $num < 3&&$num >= 1 )) ; thenecho "The $host $name wget oknumber is $num" |mail -s "$host tomcat wget oknumber" jiank
elseecho "The $host $name wget success" >> $log
fifi
done < $conf

脚本配置文件:

127.0.0.1;/tol/htdocs/tomcata/ROOT/WEB-INF/web.xml;/tol/app/tomcata/conf/Catalina/localhost/ROOT.xml;tomcata;7010;/etc/init.d/tomcata stop;/etc/init.d/tomcata start;
;tol/htdocs/tomcatb/ROOT/WEB-INF/web.xml;/tol/app/tomcatb/conf/Catalina/localhost/ROOT.xml;tomcatb;7011;/etc/init.d/tomcatb stop;/etc/init.d/tomcatb start;

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

生产环境WEB服务管理脚本之监控脚本相关推荐

  1. 生产环境WEB服务管理脚本之日志检测脚本

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

  2. 整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新!

    整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新! 一.ffmpeg脚本 1.1 打开进程,并判断进程数量 1.2 关闭进 ...

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

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

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

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

  5. Linux实操-网络配置、进程管理、服务管理、动态监控进程、监控网络状态

    网络配置 网络配置原理图 查看网络IP和网关 查看虚拟网络编辑器 修改虚拟网卡ip地址 查看网关 linux网络环境配置 第一种方法(自动获取) 第二种方法(指定固定的 ip) 进程管理(重点) 显示 ...

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

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

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

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

  8. shell脚本之监控脚本

    #!/bin/bash NC=$(expr $(free | grep "Mem:" | awk '{print $3}') \* 100 / $(free|grep " ...

  9. lvs服务器需要开启web服务么_如何检测 Web 服务请求丢失问题

    导读 『StabilityGuide』是阿里多位阿里技术工程师共同发起的稳定性领域的知识库开源项目,涵盖性能压测.故障演练.JVM.应用容器.服务框架.流量调度.监控.诊断等多个技术领域,以更结构化的 ...

最新文章

  1. 16第一章 ASP.Net编程基础知识
  2. 白话Elasticsearch20-深度探秘搜索技术之使用rescoring机制优化近似匹配搜索的性能
  3. 消息队列之JMS和AMQP对比
  4. show attend and tell 计算bleu分数(1到4)
  5. EXSITS应该怎么用?
  6. Spark的实战题目——寻找5亿次访问中,访问次数最多的人
  7. 一个通用数据库操作组件DBUtil(c#)、支持SqlServer、Oracle、Mysql、postgres、SQLITE
  8. [css] 在css中为什么说不建议使用@import?
  9. 用带参数的方法给空数组放元素,寻找数组里面的值是否存在。
  10. python常见的异常类有哪些_Python常见异常类型
  11. 原 jQuery基础修炼圣典—DOM篇
  12. 优秀案例|App内弹窗界面设计灵感
  13. 07_支持向量机3_统计学习方法
  14. js练习——动态控制表格中得行
  15. Maven 本地仓库更新策略
  16. fences卸载_fences是什么?fences栅栏桌面怎样安装卸载?
  17. Android实战简易教程五(ListView用法研究)
  18. 必应壁纸php,PHP版Bing壁纸下载源码
  19. 中国云计算产业渐成熟 加速传统产业转型升级
  20. 中通开放平台简介——连锁门店解决方案

热门文章

  1. oracle中的冲销日记账,OraEBSR12GL日记账业务操作09:日记账冲销处理
  2. Leetcode.463 岛屿的周长
  3. hdu6097—Mindis(计算几何)
  4. php asoft 排序,卓象科技:PHP算法之归并排序
  5. 安全基础--23--应急响应(上)
  6. node_modules包路径查找规则,依赖版本生效规则
  7. word忘记密码处理
  8. Pytorch踩坑记录:关于用net.eval()和with no grad装饰器计算结果不一样的问题
  9. BUCT-2023寒假集训-进阶训练赛(九)题解
  10. JavaScript惰性载入函数