背景:游戏公司,服务器上有充值服,世界服,经分服务器等,和前端的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
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
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