shell+Python实现简单的链路监控

简介:

背景游戏公司,服务器上有充值服,世界服,经分服务器等,和前端的game有链接通信,为防止链接通信故障导致线上业务中断,需要一个小脚本时刻监控线上链接状况。

涉及:shell、python2.6、126免费邮箱

配置

1
2
3
4
5
6
7
vim  /usr/lightserver/server/operationanalysisserver/config .xml -->环境不同,这里只做范例
<?xml version= "1.0"  encoding= "utf-8"  ?>
<Root>
<Connect index= "0"  GSIP= "192.168.0.100"  GSPort= "8002"  DBIP= "192.168.0.110"  DBPort= "3306"  LoginName= "game"  Password= "game"  DBName= "oaserver1" />
<Connect index= "1"  GSIP= "192.168.0.100"  GSPort= "8004"  DBIP= "192.168.0.110"  DBPort= "3306"  LoginName= "game"  Password= "game"  DBName= "oaserver2" />
<Server IP= "192.168.0.110" />
< /Root >

shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash 
WORK_DIR= "/usr/lightserver"
SERVER= "worldserver gmserver operationanalysisserver chargeserver"
MAIL_LIST= "*********@wo.cn *********@126.com"
while  :
do
         NUM=0
         sleep  5
         for  in  $SERVER
         do
           IP=` awk  -F "\""  '/GSIP/{print $4}'  $WORK_DIR /server/operationanalysisserver/config .xml`  --> #(这里没有使用$SERVER是因为配置文件可能有差异,但是IP是相同的)
 
             for  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  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
         fi
done

python脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
#coding: utf-8
__author__  =  'Yong'
__version__  =  '1.0.0'
 
 
import  os
import  sys
import  smtplib
from  email.mime.text  import  MIMEText
from  email.header  import  Header
 
 
def  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邮箱。也可使用微信报警更多扩展需要博友们开拓,笔者不才就不一一实现了。




本文转自 z永 51CTO博客,原文链接:http://blog.51cto.com/pangge/1423038
相关文章
|
1月前
|
Shell Python
python 和shell 变量互相传递
python 和shell 变量互相传递
29 0
|
1月前
|
监控 Python
【python】实现cpu/内存监控的功能(非常简单)
【python】实现cpu/内存监控的功能(非常简单)
|
3天前
|
监控 数据可视化 数据库
【python项目推荐】键盘监控--统计打字频率
【python项目推荐】键盘监控--统计打字频率
37 13
|
1月前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
22 1
|
1月前
|
机器学习/深度学习 编解码 监控
利用Python实现监控视频的超分辨率提升
利用Python实现监控视频的超分辨率提升
29 2
|
1月前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
1月前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
1月前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
1月前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
1月前
|
监控 Shell
Shell脚本监控CPU、内存和硬盘利用率
Shell脚本监控CPU、内存和硬盘利用率