歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Shell+Python實現簡單的鏈路監控

Shell+Python實現簡單的鏈路監控

日期:2017/3/1 9:40:49   编辑:SHELL編程

背景:游戲公司,服務器上有充值服,世界服,經分服務器等,和前端的game有鏈接通信,為防止鏈接通信故障導致線上業務中斷,需要一個小腳本時刻監控線上鏈接狀況。

涉及:shell、python2.6、126免費郵箱

配置

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腳本

#!/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腳本

#!/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郵箱。也可使用微信報警更多擴展需要博友們開拓,筆者不才就不一一實現了。

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved