码迷,mamicode.com
首页 > 其他好文 > 详细

收集机房出口流量

时间:2018-02-01 20:42:41      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:settime   mail   from   val   index.php   join()   ram   []   bin   

bw_agent.py

#!/usr/bin/python

import time

def get_bandwidth_info():
    lines = open("/proc/net/dev", "r").readlines()
    columnLine = lines[1]
    _, receiveCols , transmitCols = columnLine.split("|")
    receiveCols = map(lambda a:"recv_"+a, receiveCols.split())
    transmitCols = map(lambda a:"trans_"+a, transmitCols.split())
    cols = receiveCols+transmitCols
    
    faces = {}
    for line in lines[2:]:
        if line.find(":") < 0: continue
        face, data = line.split(":")
        faceData = dict(zip(cols, data.split()))
        faces[face.strip()] = faceData

    if ‘bond0‘ in faces:
        return int(faces[‘bond0‘][‘trans_bytes‘])
    if ‘0‘ != faces[‘eth1‘][‘trans_bytes‘]:
        out = int(faces[‘eth1‘][‘trans_bytes‘])
        if ‘0‘ != faces[‘eth2‘][‘trans_bytes‘]:
            out += int(faces[‘eth2‘][‘trans_bytes‘]) 
        return out

if __name__ == ‘__main__‘:
    old_val = get_bandwidth_info()
    time.sleep(1)
    res = get_bandwidth_info() - old_val
    #res = round((val - old_val)/1024/1024,3)
    print res

bandwidth_watch.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Author:       standby@gmail.com
# Time:         2018-02-01
# Description:  Show the vidc Transmit bandwidth.

import os
import re
import sys
import time
import paramiko
from  multiprocessing import Pool
#print multiprocessing.cpu_count()

class Transmit(object):
    def __init__(self, ipfile, port=22, user=‘admin‘, key=‘/path/.ssh/known_hosts‘, timeout=5):
        self.ipfile = ipfile
        self.port = port
        self.user = user
        self.key = key
        self.timeout = timeout
        self.agent_file = ‘bw_agent.py‘
        self.remote_file = ‘/data/bw_agent.py‘
        self.log_file = ‘paramiko.log‘
        self.cmd = ‘python /data/bw_agent.py‘
        self.counter = 0

    def get_ips(self):
        if os.path.isfile(self.ipfile):
            ips = []
            with open(self.ipfile) as iplist:
                content = iplist.readlines()
                for line in content:
                    if line.startswith(‘#‘):
                        continue
                    elif Transmit.check_ip(line.strip(‘\n‘)):
                        ips.append(line.strip(‘\n‘))
                    else:
                        print ‘%s is invalid ip address!‘ % line.strip(‘\n‘)
                        continue
            return ips
        elif Transmit.check_ip(self.ipfile):
            return [ipfile]
        else:
            print ‘%s is invalid ip address!‘ % self.ipfile
            sys.exit(-1)

    def prepare(self, ip):
        paramiko.util.log_to_file (self.log_file)
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, self.port, self.user, self.key, timeout=self.timeout)
        return client
    
    def scp_agent(self, ip, client):
        sftp = paramiko.SFTPClient.from_transport(client.get_transport())
        sftp = client.open_sftp()
        sftp.put(self.agent_file, self.remote_file)
    
    def get_transmit(self, ip):
        res = {‘ip‘:ip, ‘result‘:None, ‘code‘:None}
        try:
            client = self.prepare(ip)
            self.scp_agent(ip, client)
            chan = client.get_transport().open_session()
            chan.settimeout(self.timeout)
            chan.exec_command(self.cmd)
            res[‘code‘] = chan.recv_exit_status()
            # chan.recv_ready()  chan.recv_stderr_ready()  chan.exit_status_ready()
            val = chan.recv(8192).strip()
            res[‘result‘] = val if 0 == res[‘code‘] else chan.recv_stderr(8192)
        except Exception, e:
            res[‘code‘] = -1
            res[‘result‘] = 0
        return res
    
    def callback(self, res):
        self.counter += 1
        print "IP: %s, Transmit: %s" % (res.get(‘ip‘), res.get(‘result‘, 0))

    def statistics(self, res_list):
        sum = 0
        for res in res_list:
            sum += int(res.get()[‘result‘])
        # byte -> bit
        bits = float(sum*8)
        kb = bits / 1024
        if kb >= 1024:
            Mb = kb / 1024
            if Mb >= 1024:
                Gb = Mb / 1024
                print "\n%s transmit: %.3f Gb" % (self.ipfile, Gb)
            else:
                print "\n%s transmit: %.3f Mb" % (self.ipfile, Mb)
        else:
            print "\n%s transmit: %.3f Kb" % (self.ipfile, kb)

    @staticmethod
    def check_ip(ip):
        ip_str = r‘^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$‘
        return True if re.match(ip_str, ip) else False
    

‘‘‘
Attention:
    - PicklingError: Can‘t pickle <type ‘instancemethod‘>: attribute lookup __builtin__.instancemethod failed
      So, the run() method is need, like proxy.  [https://strcpy.me/index.php/archives/318/]
    - paramiko.SSHClient().connect() parameter
        - client.connect(‘8.8.8.8‘, 22, ‘admin‘, ‘/path/.ssh/known_hosts‘, timeout=5)  is ok
        - client.connect(‘8.8.8.8‘, 22, ‘admin‘, ‘/path/.ssh/known_hosts‘, 5)          will raise exception:
              ‘int‘ object has no attribute ‘get_fingerprint‘
‘‘‘
def run(cls_instance, arg):
    return cls_instance.get_transmit(arg)

if __name__ == ‘__main__‘:
    if 2 != len(sys.argv):
        print """Usage:\n\t- %s iplist_file\n\t- %s single_ip\n""" % (sys.argv[0],sys.argv[0])
        sys.exit(-1)
    transmit = Transmit(sys.argv[1])
    ips = transmit.get_ips()
    res_list = []
    t_start = time.time()
    pool = Pool(20)
    for ip in ips:
        res = pool.apply_async(func=run, args=(transmit, ip,), callback=transmit.callback)
        res_list.append(res)
    pool.close()
    pool.join()
    # pool.terminate()
    transmit.statistics(res_list)
    print ‘Dealt %d,  Runtime is: %s‘ % (transmit.counter, (time.time()-t_start))

  

收集机房出口流量

标签:settime   mail   from   val   index.php   join()   ram   []   bin   

原文地址:https://www.cnblogs.com/standby/p/8400761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!