码迷,mamicode.com
首页 > 系统相关 > 详细

shell之服务异常监测案例

时间:2018-03-27 18:45:49      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:服务检查   shell脚本   邮件警报   

例题一:web服务器nginx脚本服务监控
[root@oldboy43-01/server/scripts]#cat jiance_ng.sh
#!/bin/bash
if ! ss -lntup|grep [n]ginx &>/dev/null
then
/etc/init.d/nginx start
echo "nginx dead,but it‘s OK"|mail -s "whw11" 11392XXX@qq.com
fi

http_num=$(curl -I www.oldboyedu.com -s -w %{http_code} -o /dev/null)
if [ ${http_num} -ne 200 ];then
/etc/init.d/nginx start
echo "nginx dead,but it‘s OK"|mail -s "whw11" 11392XXX@qq.com
fi


例题二:mysql服务器服务监控
[root@web03 scripts]# cat jiance_db.sh
#!/bin/bash
if ! ps -ef|grep [m]ysqld &>/dev/null
then
systemctl start mariadb.service &>/dev/null
echo "mysql dead,but it is OK"|mail -s "whw111" 1139210923@qq.com
fi


例题三:memcache缓存服务器监控

  1. [root@web03 scripts]# cat jiance_mem2.sh****
    #!/bin/bash

port_status=$(ss -lntup|grep 11211|wc -l)
key1="oldboy0987"
printf "set key011 0 0 10\r\n${key1}\r\n"|nc 10.0.0.21 11211 &>/dev/null
key2=$(printf "get key011\r\n"|nc 10.0.0.21 11211|sed -n ‘2p‘|tr -d ‘\r‘)

if [ $port_status -eq 0 ];then
systemctl stop memcached.service
systemctl start memcached.service
if ! [ ${key1} == ${key2} ];then
systemctl restart memcached.service
echo "memcache dead,but it‘s OK"|mail -s "whw11" 1139210923@qq.com
fi
fi


例题四:打印选择菜单,按照选择一键安装不同的Web服务****
[root@oldboy43-01/server/scripts]#cat yijian.sh
#!/bin/bash
cat <<END
1.[install lamp]
2.[install lnmp]
3.exit
END

read -p "请输入选项:" num
. /etc/init.d/functions
case $num in
1)
if [ -x /server/scripts/lamp.sh ];then
/server/scripts/lamp.sh
action "start installing lamp" /bin/true
action "lamp is installed" /bin/true
else
echo "一键脚本不存在或无执行权限"
fi
;;
2)
if [ -x /server/scripts/lnmp.sh ];then
/server/scripts/lnmp.sh
action "start installing lnmp" /bin/true
action "lnmp is installed" /bin/true
else
echo "一键脚本不存在或无执行权限"
fi
;;
3)
exit 3
;;
*)
echo "你的输入有误"
esac


例题五:Rsync启动停止脚本
[root@oldboy43-01/server/scripts]#cat rsync_on.sh
#!/bin/bash
if ! [ -f /etc/rsyncd.conf ];then
echo "请先创建rsync配置文件"
exit 2
fi

read -p "请输入要进行的操作:" cmd

. /etc/init.d/functions

case $cmd in
start)
if ss -lntup|grep rsync &>/dev/null;then
echo "rsync服务已启动"
exit 2
else
rsync --daemon
action "rsync start" /bin/true
fi
;;
stop)
if ss -lntup|grep rsync &>/dev/null;then
killall rsync
sleep 3
action "rsync stop" /bin/true
else
echo "rsync是停止状态"
fi
;;
restart)
if ss -lntup|grep rsync &>/dev/null;then
pkill -f rsync
rsync --daemon
action "rsync stop" /bin/true
action "rsync start" /bin/true
else
rsync --daemon
action "rsync stop" /bin/false
action "rsync start" /bin/true
fi
;;
*)
echo "输入指令有误:(start|stop|restart)"
esac

shell之服务异常监测案例

标签:服务检查   shell脚本   邮件警报   

原文地址:http://blog.51cto.com/13520774/2091718

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!