#!/usr/bin/python26
#filename:check_traffic.py
#the python script function  for monitoring network traffic.
'''
--help

example:

python26  check_traffic.py dev in out checknum

python26  check_traffic.py eth0 30000 30000 checknum

the limit unit is KB
'''

import sys,os,time
import psutil

if len(sys.argv) != 5:
    print __doc__
    sys.exit(3)
dev=sys.argv[1]
value_in=int(sys.argv[2])
value_out=int(sys.argv[3])
lineNum=sys.argv[4]
tmpFile=dev+"_traffic.log"
unit="KB/s"

###get script pwd
binPwd=os.path.dirname(os.path.realpath(__file__))
os.chdir(binPwd)
###get current  network traffic
def getCurrentTraffic(dev):
    device=dev
    current=psutil.network_io_counters(pernic=True)
    time.sleep(1)
    current1=psutil.network_io_counters(pernic=True)
    c_send=current[device][0]
    c_rev=current[device][1]
    sc_send=current1[device][0]
    sc_rev=current1[device][1]
    send=(sc_send-c_send)/1024
    rev=(sc_rev-c_rev)/1024
    witeLog(send,rev)
    return send,rev

###write log file the network traffic
def witeLog(send,rev):
    now=time.strftime("%Y-%m-%d %X")
    f=file(tmpFile,'a')
    f.write("%s,%s,%s\n" % (rev,send,now))
    f.close()
    
def isTraffic():
    X=[]
    Y=[]
    if len(open(tmpFile,'rU').readlines()) < int(lineNum):
        print "The check num too less."
        sys.exit(0)
    lastData=os.popen("cat "+tmpFile+"|tail -n " + lineNum)
    for tmp in lastData.readlines():
        X.append(int(tmp.split(",")[0].strip('\n')))
        Y.append(int(tmp.split(",")[1].strip('\n')))
    X.sort()
    Y.sort()
    return X[0],Y[0]

###check tmpFile size
def isTmpFileSzie():
    os.system("touch %s" % tmpFile)
    if os.path.getsize(tmpFile)/1024 > 65535:
        os.remove(tmpFile)

###check network traffic the value of is normal
def monitor():
    getCurrentTraffic(dev)
    isCheck=isTraffic()
    if isCheck[0] < value_in and isCheck[1] < value_out:
        print "network traffic ok, in: %s %s out: %s %s" % (isCheck[0],unit,isCheck[1],unit)
        sys.exit(0)
    elif isCheck[0] >= value_in or isCheck[1] >= value_out:
        print "network traffic warning, in: %s %s out: %s %s" % (isCheck[0],unit,isCheck[1],unit)
        sys.exit(1)
    else:
        print "UNKNOWN."
         sys.exit(3)

def main():
        isTmpFileSzie()
        monitor()

if __name__=='__main__':
    main()

最新文章

  1. CSS3D效果
  2. Redis在windows下的安装使用
  3. mysq错误(1)空用户创建库
  4. java io知识点汇总FIle类
  5. 树-伸展树(Splay Tree)
  6. Android——用户登陆及用户名和密码的保存
  7. vim下使用YouCompleteMe实现代码提示、补全以及跳转设置
  8. python3.4 安装 scrapy 报错 VS2010
  9. VSCode自定义配色方案
  10. SpringMVC--入门案例
  11. 如何用jmeter进行数据库性能测试
  12. JQUERY-自定义插件-ajax-跨域访问
  13. linux audit审计(7-1)--读懂audit日志
  14. asp.net mvc 多文件上传
  15. Spring Boot 2.x 学习专栏
  16. Linux服务器tomcat启动maven项目
  17. python中的zip、lambda、map操作
  18. NALU数据打RTP包流程详解
  19. Javascript 对象创建多种方式 原型链
  20. Node.js实战(二)之HelloWorld示例

热门文章

  1. Web应急:网站被批量挂黑页
  2. Delphi 10.3.2 社区版的安装
  3. react+umi+netcore+signalR BS和客户端设备 简单通讯
  4. 浅谈Semaphore类-示例
  5. [转].NET Core前后端分离快速开发框架(Core.3.0+AntdVue)
  6. 学习CSS Grid布局
  7. Visual Studio Code创建C#项目
  8. Java自学-I/O 字节流
  9. 架构师小跟班:推荐46个非常经典的Linux面试题
  10. 在ubuntu更新时,出现错误E: Some index files failed to download, they have been ignored, or old ones used inst