zoukankan      html  css  js  c++  java
  • 网卡监控脚本--nagios

    #!/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 " % (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(' ')))
            Y.append(int(tmp.split(",")[1].strip(' ')))
        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()

    ----------当你发现自己的才华撑不起野心时,就请安静下来学习吧---------
  • 相关阅读:
    render :template 和 render :parital
    routes.rb和link_to的一些规则
    rails程序文件名命名规范
    Ruby的模型关系随笔
    Ruby的require相关知识
    rails中的form_for
    rails中的session
    RubyMine不能调试Rails项目的问题
    序列化后的json串与解析json串实体间的关系
    mysql还原数据库
  • 原文地址:https://www.cnblogs.com/Qing-840/p/5501461.html
Copyright © 2011-2022 走看看