背景:游戏公司,服务器上有充值服,世界服,经分服务器等,和前端的game有链接通信,为防止链接通信故障导致线上业务中断,需要一个小脚本时刻监控线上链接状况。
涉及:shell、python2.6、126免费邮箱
配置:
vim /usr/lightserver/server/operationanalysisserver/config.xml -->环境不同,这里只做范例
shell脚本:
#!/bin/bash WORK_DIR="/usr/lightserver"SERVER="worldserver gmserver operationanalysisserver chargeserver"MAIL_LIST="*********@wo.cn *********@126.com"while :do NUM=0 sleep 5 for i in $SERVER do IP=`awk -F"\"" '/GSIP/{print $4}' $WORK_DIR/server/operationanalysisserver/config.xml` -->#(这里没有使用$SERVER是因为配置文件可能有差异,但是IP是相同的) for j in $IP do NUM=$(($NUM+1)) PID=`ps aux|grep $WORK_DIR/server/$i/unix/$i|grep -v grep|awk '{print $2}'|sed -n 1p` if [ -z $PID ] ;then PID=0 fi if lsof -p $PID|grep -v grep|grep -v mysql|grep $j|grep ESTABL > /dev/null 2>&1;then continue else echo "`date "+%Y-%m-%d %X"` $j link disconnected" echo "-----------------------------------------------------------------" if [ -f /usr/local/check_server/lock/link_alert.lck ];then -->#短信通知有限制,1天最多只能发送10条,所以这里做了限制本次错误只发送1条,若无限制短信发送可直接省略此段,就是每5秒发一次。 continue else mkdir -p /usr/local/check_server/lock touch /usr/local/check_server/lock/link_alert.lck for m in $MAIL_LIST do python /usr/local/check_server/send_mail.py "Server Fault" "Links may be a problem,`date "+%Y-%m-%d %X"` $j link disconnected" "$m" -->#这里的收件人和平常邮件接收人一样。 done fi fi done done if [ $NUM -le `netstat -nat|grep -v grep|grep ESTABL|awk '{print $5}'|grep -v 0.0.0.0|egrep '(8002|8004)'|wc -l` ];then if [ -f /usr/local/check_server/lock/link_alert.lck ];then rm -rf /usr/local/check_server/lock/link_alert.lck fi fidone
python脚本:
#!/usr/bin/env python#coding: utf-8__author__ = 'Yong'__version__ = '1.0.0'import osimport sysimport smtplibfrom email.mime.text import MIMETextfrom email.header import Headerdef send_mail(s, i, r): #Subject = 'python test mail' Subject = s #mail_info = 'test from python3' mail_info = i Receiver = r Smtp_Server = 'smtp.126.com' --> #这里用的是126的服务器,也可用公司的,做发件方使用 Username = '*******' --> #邮箱名, Passwd = '*******' --> #邮箱密码 if Username.find('@') < 0: Sender = Username + '@126.com' else: Sender = Username msg = MIMEText(mail_info, 'plain', 'utf-8') msg['Subject'] = Header(Subject, 'utf-8') smtp = smtplib.SMTP() smtp.connect(Smtp_Server) smtp.login(Username, Passwd) smtp.sendmail(Sender, Receiver, msg.as_string()) smtp.quit()if __name__ == '__main__': if len(sys.argv) != 4: print 'Usage:{0} 邮件主题 邮件内容 收件人地址\n'.format(sys.argv[0]) sys.exit(1) send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
小建议:联通的手机可用186邮箱,移动的可使用139邮箱。也可使用微信报警更多扩展需要博友们开拓,笔者不才就不一一实现了。