python流量监控脚本
简介利用python监控一分钟之内的流量数据,并设置预警值,如果超过发送钉钉提醒#/usr/bin/env/python#coding=utf-8import sys,re,time,osimport jsonimport requestsurl = ''netcard = '/proc/net/dev'def get_net_data():nc = netcard or
·
简介
利用python监控一分钟之内的流量数据,并设置预警值,如果超过发送钉钉提醒
#/usr/bin/env/python
#coding=utf-8
import sys,re,time,os
import json
import requests
url = ''
netcard = '/proc/net/dev'
def get_net_data():
nc = netcard or '/proc/net/dev'
fd = open(nc, "r")
netcardstatus = False
for line in fd.readlines():
if line.find("eth0") >= 0 or line.find("enp") >= 0:
netcardstatus = True
field = line.split()
recv = field[0].split(":")[1]
recv = recv or field[1]
send = field[8]
if not netcardstatus:
fd.close()
print 'Please setup your netcard'
sys.exit()
fd.close()
return (float(recv), float(send))
def send_warn(recv_data, send_data):
content = "亚马逊服务器一分钟流量警告,recv_data: %s kb, send_data: %s kb"%(recv_data, send_data)
text = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"atMobiles": [
"156xxxx8827",
"189xxxx8325"
],
"isAtAll": False
}
}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(text), headers=headers)
print r.text
def net_loop():
recv,send = get_net_data()
while True:
time.sleep(60)
new_recv,new_send = get_net_data()
recv_data = (new_recv - recv)/1024
send_data = (new_send - send)/1024
print "recv_data: %s kb, send_data: %s kb"%(recv_data, send_data)
if recv_data/60>100 or send_data/60>10:
send_warn(recv_data, send_data)
recv,send = new_recv,new_send
if __name__ == "__main__":
net_loop()
更多推荐
已为社区贡献2条内容
所有评论(0)