Пример #1
0
    def __set_bases(self):
        """
        Calculate base stats for I/O at start of the node.
        Used to calculate difference to get per/s stats.
        """
        psutil.cpu_percent()
        psutil.cpu_percent(percpu=True)

        for interface in psutil.net_io_counters(True):

            netint = psutil.net_io_counters(True)[interface]
            total_bytes = netint.bytes_recv + netint.bytes_sent
            total_packages = netint.packets_sent + netint.packets_recv

            self.__bandwidth_base[interface] = total_bytes
            self.__msg_freq_base[interface] = total_packages

        dev_names = []
        for disk in psutil.disk_partitions():
            if all(['cdrom' not in disk.opts, 'sr' not in disk.device]):
                dev_names.append(disk.device)

        for key in psutil.disk_io_counters(True):
            if key in dev_names:
                disk = psutil.disk_io_counters(True)[key]
                self.__disk_read_base[key] = disk.read_bytes
                self.__disk_write_base[key] = disk.write_bytes
Пример #2
0
def api_net_per_io(interval=1):
    net_io_before = psutil.net_io_counters(pernic=True)
    sleep(interval)
    net_io_after = psutil.net_io_counters(pernic=True)

    nics = list()

    for nic_name in net_io_after:
        nic_after = net_io_after[nic_name]
        nic_before = net_io_before[nic_name]

        bytes_sent_per = nic_after.bytes_sent - nic_before.bytes_sent
        bytes_recv_per = nic_after.bytes_recv - nic_before.bytes_recv

        package_sent_per = nic_after.packets_sent - nic_before.packets_sent
        package_recv_per = nic_after.packets_recv - nic_before.packets_recv

        nics.append({
            nic_name: {
                'bytes_recv': bytes_recv_per / interval,
                'bytes_sent': bytes_sent_per / interval,
                'package_sent': int(package_sent_per / interval),
                'package_recv': int(package_recv_per / interval),
            }
        })

    return jsonify({
        'status': True,
        'data': nics
    })
def Traccia_e_Invio_TS():
    cpu_pc = psutil.cpu_percent()#variabile percentuale cpu occupata
    mem_usata = psutil.virtual_memory().percent #variabile memoria usata
    pacchetti_inv = psutil.net_io_counters().packets_sent #variabile pacchetti inviati
    pacchetti_ric = psutil.net_io_counters().packets_recv #variabile pacchetti ricevuti

    #invio al server tramite la api key personale e riempio i vari campi
    params = urllib.urlencode ({'field1': cpu_pc, 'field2': mem_usata, 'field3': pacchetti_inv,'field4':pacchetti_ric,'key':'7BNU1GRMMFADSQ9H'}) 
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} 
    conn = httplib.HTTPConnection("api.thingspeak.com:80")
    
    #stampo nel terminale i valori e lo stato della risposta
    try:
        print (bcolors.BOLD+bcolors.WARNING+"Monitoraggio uso cpu e memoria libera della macchina virtuale " +(socket.gethostname()))+bcolors.ENDC+"\n"#nome della macchina --> modifica
        conn.request("POST", "/update", params, headers)
        response = conn.getresponse()
        print bcolors.BOLD+bcolors.OKBLUE+"Cpu occupata: "+bcolors.ENDC,cpu_pc, "%"+"\n"
        print bcolors.BOLD+bcolors.OKBLUE+"Memoria virtuale usata: "+bcolors.ENDC,mem_usata,"%"+"\n"
        print bcolors.BOLD+bcolors.OKBLUE+"Numero pacchetti inviati: "+bcolors.ENDC,pacchetti_inv,"\n"
        print bcolors.BOLD+bcolors.OKBLUE+"Numero pacchetti ricevuti: "+bcolors.ENDC,pacchetti_ric,"\n"
        print bcolors.BOLD+bcolors.OKBLUE+"Timestamp: "+bcolors.ENDC,strftime("%a, %d %b %Y %H:%M:%S", localtime()),"\n"
        print bcolors.BOLD+bcolors.OKBLUE+"Risposta del server: "+bcolors.ENDC,response.status, response.reason,"\n"
        data = response.read()
        conn.close()
        if response.reason==("OK"):
            print (bcolors.BOLD+bcolors.OKGREEN+"La richiesta è andata a buon fine!"+bcolors.ENDC),"\n\n"
    except:
        print bcolors.BOLD+bcolors.FAIL+"Connessione fallita, riprovare." +bcolors.ENDC  
Пример #4
0
    def __get_resource_stats(self):
        """
        Get network and disk counters
        :return: tuple
        """
        if not self.__counters_ts:
            self.__disk_counters = psutil.disk_io_counters()
            self.__net_counters = psutil.net_io_counters()
            self.__counters_ts = datetime.datetime.now()
            time.sleep(0.2)  # small enough for human, big enough for machine

        now = datetime.datetime.now()
        interval = (now - self.__counters_ts).total_seconds()

        net = psutil.net_io_counters()
        tx_bytes = (net.bytes_sent - self.__net_counters.bytes_sent) / interval
        rx_bytes = (net.bytes_recv - self.__net_counters.bytes_recv) / interval
        self.__net_counters = net

        disk = psutil.disk_io_counters()
        dru = (disk.read_bytes - self.__disk_counters.read_bytes) / interval
        dwu = (disk.write_bytes - self.__disk_counters.write_bytes) / interval
        self.__disk_counters = disk

        self.__counters_ts = now
        return rx_bytes, tx_bytes, dru, dwu
Пример #5
0
  def test():

    log_info = {}
    total_start = time.time()
    interfaces = ['eth0']
    bytes_recv_start = 0
    bytes_sent_start = 0
    for interface in interfaces:
      bytes_recv_start += psutil.net_io_counters(pernic=True)[interface].bytes_recv
      bytes_sent_start += psutil.net_io_counters(pernic=True)[interface].bytes_sent
    # test common urls
    print bytes_recv_start
    print bytes_sent_start
    for m in NetworkMonitor.Common_urls:
      log_info[m] = {}
      try:
        start = time.time()
        httpcode = 0
        httpcode = NetworkMonitor.Opener.open(m).getcode()
      except urllib2.URLError,e:
        print 'here'
        print 'Exception:'
        print e #urlopen error timed out
        pass
      finally:
Пример #6
0
def network():
    ip = get_my_ip()
    text = ip + '\n'
    text = text + '==================\n'
    total_network = psutil.net_io_counters()
    total_bytes_sent = '%.2f M' % (total_network[0]/1024.0/1024.0)
    total_bytes_recv = '%.2f M' % (total_network[1]/1024.0/1024.0)
    text = text + u"★总发送流量:%s  总接收流量:%s\n"%(total_bytes_sent,total_bytes_recv)
    text = text + u"★总发送数据包数:%s  总接收数据包数:%s\n"%(total_network[2],total_network[3])
    network = psutil.net_io_counters(pernic=True)
    network_keys = network.keys()
    print
    for i in range(len(network_keys)):
        this_network = network.get(network_keys[i])
        this_network_name = (network_keys[i]).decode('gbk')
        bytes_sent = this_network[0]
        bytes_recv = this_network[1]
        packets_sent = this_network[2]
        packets_recv = this_network[3]

        this_bytes_sent = '%.2f M' % (bytes_sent/1024.0/1024.0)
        this_bytes_recv = '%.2f M' % (bytes_recv/1024.0/1024.0)
        this_packets_sent = '%d' % (packets_sent)
        this_packets_recv = '%d' % (packets_recv)
        text = text + '==================\n'
        text = text +u'网卡:'+ this_network_name +'\n'
        text = text + u"发送流量:%s  接收流量:%s\n"%(this_bytes_sent,this_bytes_recv)
        text = text + u"发送数据包数:%s  接收数据包数:%s\n"%(this_packets_sent,this_packets_recv)


    sysinfo.label2.setText(sysinfo.tr(text))
Пример #7
0
	def collect_network(self, stat):
		n = psutil.net_io_counters(pernic=False)
		stat['psutil_net'] = {	'bytes_sent':n.bytes_sent,
				'bytes_recv':n.bytes_recv,
				'packets_sent':n.packets_sent,
				'packets_recv':n.packets_recv,
				'errin':n.errin,
				'errout':n.errout,
				'dropin':n.dropin,
				'dropout':n.dropout,
				} 

		ns = psutil.net_io_counters(pernic=True)
		for k, n in ns.items():
			if k == 'lo': # skip loopback
				continue

			stat['psutil_net-%s' % k] = {	'bytes_sent':n.bytes_sent,
						'bytes_recv':n.bytes_recv,
						'packets_sent':n.packets_sent,
						'packets_recv':n.packets_recv,
						'errin':n.errin,
						'errout':n.errout,
						'dropin':n.dropin,
						'dropout':n.dropout,
						}
Пример #8
0
def Sysinfo():  
    Hostname = platform.node()
    Sys_version = platform.platform()
    Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(psutil.boot_time()))    
    time.sleep(0.5)  
    Cpu_usage = psutil.cpu_percent()  
    RAM = int(psutil.virtual_memory().total/(1027*1024))  
    RAM_percent = psutil.virtual_memory().percent  
    Swap = int(psutil.swap_memory().total/(1027*1024))  
    Swap_percent = psutil.swap_memory().percent  
    Net_sent = psutil.net_io_counters().bytes_sent  
    Net_recv = psutil.net_io_counters().bytes_recv  
    Net_spkg = psutil.net_io_counters().packets_sent  
    Net_rpkg = psutil.net_io_counters().packets_recv  
    BFH = r'%'
    print " \033[1;32m主机名: %s\033[1;m" % Hostname  
    print " \033[1;32m系统版本: %s\033[1;m" % Sys_version  
    print " \033[1;32m开机时间:%s\033[1;m"  % Boot_Start  
    print " \033[1;32m当前CPU使用率:%s%s\033[1;m" % (Cpu_usage,BFH)  
    print " \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (RAM,RAM_percent,BFH)  
    print " \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (Swap,Swap_percent,BFH)  
    print " \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (Net_sent,Net_spkg)  
    print " \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (Net_recv,Net_rpkg)  
  
    for i in psutil.disk_partitions():  
        print " \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0],i[1],psutil.disk_usage(i[1])[3],BFH)  
    for key in psutil.net_if_addrs().keys():  
        print " \033[1;32m网卡: %s IP: %s\033[1;m" % (key,psutil.net_if_addrs()[key][0][1])  
Пример #9
0
	def get_net(self):
		allnetio = psutil.net_io_counters() # 获取网络总的IO信息
		onenetio = psutil.net_io_counters(pernic=True) # 输出每个网络接口的IO信息
		res = dict(
			allnetio=dict(
			bytes_sent=allnetio.bytes_sent, # 发送字节数
			bytes_recv=allnetio.bytes_recv, # 接受字节数
			packets_sent=allnetio.packets_sent, # 发送数据包数
			packets_recv=allnetio.packets_recv, # 接受数据包数
			errin=allnetio.errin,
			errout=allnetio.errout,
			dropin=allnetio.dropin,
			dropout=allnetio.dropout
			),
			onenetio=[
				dict(
					name=v[0],
					bytes_sent=v[1].bytes_sent, # 发送字节数
					bytes_recv=v[1].bytes_recv, # 接受字节数
					packets_sent=v[1].packets_sent, # 发送数据包数
					packets_recv=v[1].packets_recv, # 接受数据包数
					errin=v[1].errin,
					errout=v[1].errout,
					dropin=v[1].dropin,
					dropout=v[1].dropout
			)
			for v in onenetio.iteritems()
			]
		)
Пример #10
0
def NetworkUsageLinux():
        theArray0 = []
        theArray1 = []
        en0 = psutil.net_io_counters(pernic=True).get('eth0')
        en1 = psutil.net_io_counters(pernic=True).get('wlan0')

        en0Dict = {
                'mb_sent'                                               :en0[0]/1024/1024,
                'mb_recv'                                               :en0[1]/1024/1024,
                'packets_sent'                                  :en0[2],
                'packets_recv'                                  :en0[3],
                'errin'                                                 :en0[4],
                'errout'                                                :en0[5],
                'dropin'                                                :en0[6],
                'dropout'                                               :en0[7]
        }
        en1Dict = {
                'mb_sent'                                               :en1[0]/1024/1024,
                'mb_recv'                                               :en1[1]/1024/1024,
                'packets_sent'                                  :en1[2],
                'packets_recv'                                  :en1[3],
                'errin'                                                 :en1[4],
                'errout'                                                :en1[5],
                'dropin'                                                :en1[6],
                'dropout'                                               :en1[7]
        }
        return en0Dict, en1Dict
Пример #11
0
def poll(interval):
    tot_before = psutil.net_io_counters()
    pnic_before = psutil.net_io_counters(pernic=True)
    time.sleep(interval)
    tot_after = psutil.net_io_counters()
    pnic_after = psutil.net_io_counters(pernic=True)
    return (tot_before, tot_after, pnic_before, pnic_after)
Пример #12
0
    def netio_info(self):
        pernic_counters_before = psutil.net_io_counters(pernic=True)
        time.sleep(1)
        pernic_counters_after = psutil.net_io_counters(pernic=True)
        net_list = [ ]

        for iface in pernic_counters_before:
            after = pernic_counters_after[iface]
            before = pernic_counters_before[iface]
            net_list.append({ 
                'name': iface,
                'total': {
                    'sent': {
                        'human': sizeof_fmt(after.bytes_sent),
                        'bytes': after.bytes_sent
                        },
                    'recv': {
                        'human': sizeof_fmt(after.bytes_recv),
                        'bytes': after.bytes_recv
                        }
                    },
                'per-sec': {
                    'sent': {
                        'human': sizeof_fmt((after.bytes_sent - before.bytes_sent)),
                        'bytes': (before.bytes_sent - after.bytes_sent)
                        },
                    'recv': {
                        'human': sizeof_fmt((after.bytes_recv - before.bytes_recv)),
                        'bytes': (before.bytes_recv - after.bytes_recv)
                        }
                    },
                'name': iface
            })

        return net_list
Пример #13
0
def Inet_Info(device):
    rx = [float(psutil.net_io_counters(pernic=True)['eth0'].bytes_recv),float(psutil.net_io_counters(pernic=True)['eth0'].bytes_sent)]
    time.sleep(1)
    tx = [float(psutil.net_io_counters(pernic=True)['eth0'].bytes_recv),float(psutil.net_io_counters(pernic=True)['eth0'].bytes_sent)]
    speed_in = round((tx[0] - rx[0]) / 1024, 2)
    speed_out = round((tx[1] - rx[1]) /1024, 2)
    return str(speed_in),str(speed_out)
Пример #14
0
    def test_net_io_counters(self):
        def check_ntuple(nt):
            self.assertEqual(nt[0], nt.bytes_sent)
            self.assertEqual(nt[1], nt.bytes_recv)
            self.assertEqual(nt[2], nt.packets_sent)
            self.assertEqual(nt[3], nt.packets_recv)
            self.assertEqual(nt[4], nt.errin)
            self.assertEqual(nt[5], nt.errout)
            self.assertEqual(nt[6], nt.dropin)
            self.assertEqual(nt[7], nt.dropout)
            assert nt.bytes_sent >= 0, nt
            assert nt.bytes_recv >= 0, nt
            assert nt.packets_sent >= 0, nt
            assert nt.packets_recv >= 0, nt
            assert nt.errin >= 0, nt
            assert nt.errout >= 0, nt
            assert nt.dropin >= 0, nt
            assert nt.dropout >= 0, nt

        ret = psutil.net_io_counters(pernic=False)
        check_ntuple(ret)
        ret = psutil.net_io_counters(pernic=True)
        self.assertNotEqual(ret, [])
        for key in ret:
            self.assertTrue(key)
            self.assertIsInstance(key, str)
            check_ntuple(ret[key])
Пример #15
0
def __nic_poll_stat():
    """Retrieve raw stats within an interval window."""
    pnic_before = psutil.net_io_counters(pernic=True)
    # sleep some time
    time.sleep(1)
    pnic_after = psutil.net_io_counters(pernic=True)
    return [pnic_before, pnic_after]
Пример #16
0
def pollForAttacks():

    MAX_NET_USAGE = 400000
    MAX_ATTACKS = 4
    attack = 0
    counter = 0

    while attack <= MAX_ATTACKS:
        sleep(4)
        counter = counter + 1
        # Check the cpu usage
        if cpu_percent(interval = 1) > 70:
            attack = attack + 1
        # Check the net usage
        neti1 = net_io_counters()[1]
        neto1 = net_io_counters()[0]
        sleep(1)
        neti2 = net_io_counters()[1]
        neto2 = net_io_counters()[0]
        # Calculate the bytes per second
        net = ((neti2+neto2) - (neti1+neto1))/2
        print 'neti1 = {0}'.format(neti1)
        print 'neto1 = {0}'.format(neto1)
        print 'neti2 = {0}'.format(neti2)
        print 'neto2 = {0}'.format(neto2)
        
        print 'net = {0}'.format(net)
        if net > MAX_NET_USAGE:
            attack = attack + 1
        if counter > 25:
            attack = 0
            counter = 0
Пример #17
0
def sysinfo():
    boot_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
    time.sleep(0.5)
    cpu_usage = psutil.cpu_percent()
    ram = int(psutil.virtual_memory().total / (1027 * 1024))
    ram_percent = psutil.virtual_memory().percent
    swap = int(psutil.swap_memory().total / (1027 * 1024))
    swap_percent = psutil.swap_memory().percent
    net_sent = psutil.net_io_counters().bytes_sent
    net_recv = psutil.net_io_counters().bytes_recv
    net_spkg = psutil.net_io_counters().packets_sent
    net_rpkg = psutil.net_io_counters().packets_recv
    sysinfo_string = ""
    if __name__ == "__main__":
        bfh = r'%'
        print(" \033[1;32m开机时间:%s\033[1;m" % boot_start)
        print(" \033[1;32m当前CPU使用率:%s%s\033[1;m" % (cpu_usage, bfh))
        print(" \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (ram, ram_percent, bfh))
        print(" \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (swap, swap_percent, bfh))
        print(" \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (net_sent, net_spkg))
        print(" \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (net_recv, net_rpkg))
        for i in psutil.disk_partitions():
            print(" \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0], i[1], psutil.disk_usage(i[1])[3], bfh))

        sysinfo_string = '开机时间: ' + boot_start + '\n'
        sysinfo_string += '当前CPU使用率: ' + str(cpu_usage) + '%\n'
        sysinfo_string += '物理内存: ' + str(ram) + 'M\t使用率: ' + str(ram_percent) + '%\t'
        sysinfo_string += 'Swap内存: ' + str(swap) + 'M\t使用率: ' + str(swap_percent) + '%\n'
        return sysinfo_string

    else:
        file = open("sysinfo.log", "a")
        file.write("CPU:%s   \tRAM:%s\tnet_recv:%d\tNet_sent:%d\r\n" % (cpu_usage, ram_percent, net_recv, net_sent))
        file.flush()
        file.close()
Пример #18
0
def getSysInfo(id):
		# hostInfo=None
		# if hostAddr:
		# 	hostInfo = hostSysInfo(self.hostAddr)
		global version
		cpu_percent = psutil.cpu_percent(interval=None)
		mem_usage = psutil.virtual_memory().percent
		license_key = os.getenv('license_key')
		if NETWORK_INTERFACE in psutil.net_if_addrs():
			ip = psutil.net_if_addrs()[NETWORK_INTERFACE][0].address
			mask = psutil.net_if_addrs()[NETWORK_INTERFACE][0].netmask
			mac = psutil.net_if_addrs()[NETWORK_INTERFACE][getMacAddressIndex()].address
			ipv6address = getIpv6Address()
		else:
			ip = None
			mask = None
			mac = None
			ipv6address = []
		bytes_sent = psutil.net_io_counters().bytes_sent
		bytes_recv = psutil.net_io_counters().bytes_recv
		bytes_sent_avg = round(float(bytes_sent-lastNetIo["sent"])/HEARTBEAT_INTERVAL/1024,2) if bytes_sent-lastNetIo["sent"] > 0 else 0  #Kbytes/s
		bytes_recv_avg = round(float(bytes_recv-lastNetIo["recv"])/HEARTBEAT_INTERVAL/1024,2) if bytes_recv-lastNetIo["recv"] > 0 else 0  #Kbytes/s
		lastNetIo["sent"] = bytes_sent
		lastNetIo["recv"] = bytes_recv
		task_processes = getTaskProcessInfo()
		topTotal,processs = getProgress()
		probeInfo = dict(cpu = cpu_percent, memory = mem_usage,ipaddress=ip,device_type = 'soft',ipv6address=ipv6address,toptotal=topTotal,topprocess=processs,license_key=license_key,netmask=mask,macaddress=mac,arpNeighbors=arpNeighbors,bytesSent=bytes_sent_avg,bytesRecv=bytes_recv_avg,task_processes=task_processes)
		result = dict(clientId=id,type="heartbeat",time=time.time(),content=probeInfo)
		#result['currentVersion'] = gitutil.getCurrentVersion(config.APPLICATION_PATH).__str__()
		result['currentVersion'] = version
		return result
Пример #19
0
    def get(self, *args):
        ans = {}
        ans.update(self.defaults)
        ans.update(dict(name=self.env.name,
                   host=self.env.host,
                   ram=psutil.virtual_memory().percent,
                   cpu=psutil.cpu_percent(),
                   sent=psutil.net_io_counters().bytes_sent,
                   recv=psutil.net_io_counters().bytes_recv,
                   disk_usage=list(map(lambda p: (p.mountpoint, psutil.disk_usage(p.mountpoint).percent), psutil.disk_partitions())),
                   states=[],
                   issues=[]))

        if ans['ram'] > 90:
            ans['issues'].append('High RAM usage')
        if ans['cpu'] > 90:
            ans['issues'].append('High CPU usage')
        for du in ans['disk_usage']:
            if du[1] > 90:
                ans['issues'].append('High HDD usage at ' + du[0])

        for callback in HttpMonitor._callbacks:
            ans['states'].append(callback())

        self.set_status(200)
        self.set_header('Access-Control-Allow-Origin', '*')
        self.set_header('Access-Control-Expose-Headers', 'Access-Control-Allow-Origin')
        self.set_header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
        self.set_header('Content-Type', 'application/json')
        self.write(json.dumps(ans, indent=2))
        self.finish()
Пример #20
0
def networkInfo():
    print 'Showing network status (M/s)...'
    while (True):
       nwBefore = psutil.net_io_counters()
       time.sleep(1)
       nwAfter = psutil.net_io_counters()
       setValue(float(nwAfter.bytes_recv - nwBefore.bytes_recv)/ float(1000000))
Пример #21
0
def get_net_iostat(nic=None):
	if not nic:
		if 'net_io_counters' in dir(ps):
			return ps.net_io_counters(pernic=False)
		return ps.network_io_counters(pernic=False)
	if 'net_io_counters' in dir(ps):
		return ps.net_io_counters(pernic=False)
	return ps.network_io_counters(pernic=True).get(nic)
Пример #22
0
def update_rrd():

	total_in = psutil.net_io_counters()[1]
	total_out = psutil.net_io_counters()[0]	
	starttime = int(time.time())
	update = rrdtool.updatev('/usr/local/src/Flow.rrd','%s:%s:%s')	% (str(starttime),str(total_in),str(total_out))

	print update
Пример #23
0
def get_net_state():
    tot_before = psutil.net_io_counters()
    # sleep some time  
    time.sleep(interval)  
    tot_after = psutil.net_io_counters()  
    
    return {"net_send": bytes2human((tot_after.bytes_sent - tot_before.bytes_sent)/ interval), 
            "net_recv": bytes2human((tot_after.bytes_recv - tot_before.bytes_recv)/ interval)}
Пример #24
0
 def test_net_io_counters_no_nics(self):
     # Emulate a case where no NICs are installed, see:
     # https://github.com/giampaolo/psutil/issues/1062
     with mock.patch('psutil._psplatform.net_io_counters',
                     return_value={}) as m:
         self.assertIsNone(psutil.net_io_counters(pernic=False))
         self.assertEqual(psutil.net_io_counters(pernic=True), {})
         assert m.called
Пример #25
0
 def update(self):
     """Get the latest system information."""
     import psutil
     if self.type == 'disk_use_percent':
         self._state = psutil.disk_usage(self.argument).percent
     elif self.type == 'disk_use':
         self._state = round(psutil.disk_usage(self.argument).used /
                             1024**3, 1)
     elif self.type == 'disk_free':
         self._state = round(psutil.disk_usage(self.argument).free /
                             1024**3, 1)
     elif self.type == 'memory_use_percent':
         self._state = psutil.virtual_memory().percent
     elif self.type == 'memory_use':
         self._state = round((psutil.virtual_memory().total -
                              psutil.virtual_memory().available) /
                             1024**2, 1)
     elif self.type == 'memory_free':
         self._state = round(psutil.virtual_memory().available / 1024**2, 1)
     elif self.type == 'swap_use_percent':
         self._state = psutil.swap_memory().percent
     elif self.type == 'swap_use':
         self._state = round(psutil.swap_memory().used / 1024**3, 1)
     elif self.type == 'swap_free':
         self._state = round(psutil.swap_memory().free / 1024**3, 1)
     elif self.type == 'processor_use':
         self._state = round(psutil.cpu_percent(interval=None))
     elif self.type == 'process':
         if any(self.argument in l.name() for l in psutil.process_iter()):
             self._state = STATE_ON
         else:
             self._state = STATE_OFF
     elif self.type == 'network_out' or self.type == 'network_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             counter = counters[self.argument][IO_COUNTER[self.type]]
             self._state = round(counter / 1024**2, 1)
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'packets_out' or self.type == 'packets_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             self._state = counters[self.argument][IO_COUNTER[self.type]]
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'ipv4_address' or self.type == 'ipv6_address':
         addresses = psutil.net_if_addrs()
         if self.argument in addresses:
             self._state = addresses[self.argument][IF_ADDRS[self.type]][1]
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'last_boot':
         self._state = dt_util.as_local(
             dt_util.utc_from_timestamp(psutil.boot_time())
         ).date().isoformat()
     elif self.type == 'since_last_boot':
         self._state = dt_util.utcnow() - dt_util.utc_from_timestamp(
             psutil.boot_time())
Пример #26
0
 def net_use_now(self, interval=0.2):
   n = psutil.net_io_counters()
   old_net = n.bytes_sent + n.bytes_recv
   time.sleep(interval)
   n = psutil.net_io_counters()
   new_net = n.bytes_sent + n.bytes_recv
   dif_net = new_net - old_net
   use_rate = dif_net / interval 
   return use_rate
Пример #27
0
def mode_bars():
    while True:
        cpu = psutil.cpu_percent()
        mem = psutil.virtual_memory().percent
        netin = psutil.net_io_counters(pernic=False).bytes_recv / 1024
        netout = psutil.net_io_counters(pernic=False).bytes_sent / 1024
        frame = render_system_usage_bars(cpu, mem, netin, netout)
        write_frame(frame)
        sleep(0.3)
Пример #28
0
 def get_net(self):
     # 网络流量
     nw = ni.gateways()['default'][2][1]
     current_bytes = psutil.net_io_counters(pernic=True)[nw][1]
     time.sleep(60)
     previous, current_bytes = current_bytes, psutil.net_io_counters(pernic=True)[nw][1]
     # 网络连接数
     connections = len(psutil.net_connections())
     return connections
Пример #29
0
def poll(interval):
    """Retrieve raw stats within an interval window."""
    tot_before = psutil.net_io_counters()
    pnic_before = psutil.net_io_counters(pernic=True)
    # sleep some time
    time.sleep(interval)
    tot_after = psutil.net_io_counters()
    pnic_after = psutil.net_io_counters(pernic=True)
    return (tot_before, tot_after, pnic_before, pnic_after)
Пример #30
0
def get_sys_info():
    result = {
        "cpu_usage": psutil.cpu_percent(),
        "mem_usage": psutil.virtual_memory().percent,
        "net_in": psutil.net_io_counters().bytes_recv,
        "net_out": psutil.net_io_counters().bytes_sent,
    }

    return result
Пример #31
0
        --verbose - Enables printed output for the developer.

    Example usage:
        python sysmon.py --output=stats.csv --interface=eno1
""")
	exit(1)

info = {
	'cpu_load' : 0,
	'mem_free' : 0
}

info['cpu_load'] = psutil.cpu_percent(interval=0.5) # Grab 0.5 sec worth of sample time
info['mem_free'] = psutil.virtual_memory().free # .percent is also a thing
info['disk_free'] = psutil.disk_usage(args['partition']).free / (1024.0 ** 3) # Convert to gigabyte
packet_info = psutil.net_io_counters(pernic=True)[args['interface']]
info['packets_sent'] = packet_info.bytes_sent
info['packets_recv'] = packet_info.bytes_recv

if args['verbose']:
	print(json.dumps(info, indent=4))

if args['output']:
	birth = False
	if not os.path.isfile(args['output']):
		birth = True
	with open(args['output'], 'a') as output:
		if birth:
			output.write(','.join(["snapshot_time"]+[str(x[0]) for x in sorted(info.items(), key=lambda kv: kv[0])])+'\n')
		# Join the JSON struct into a CSV line (Sorted by keys so they stay the same)
		#         Separator                      str(value)  key,val              sorting key=[key,val][0] <- aka key and not value
Пример #32
0
def mon():
    global last_info_m_time, last_info_m_usage
    global last_info_c_time, last_info_c_usage
    global last_info_n_time, last_info_n_usage
    global last_info_d_time, last_info_d_usage
    global format

    info_time = time.time()

    # 1.- Check Memory
    meminfo = psutil.virtual_memory()
    info_m_usage = meminfo[2]

    info_delta = math.fabs(info_m_usage - last_info_m_usage)
    if info_delta >= delta:
        data = {
            "type": "memory",
            "timestamp": info_time,
            "timedelta": info_time - last_info_m_time,
            "usagepercent": last_info_m_usage,
            "usageabsolute": meminfo[0] - meminfo[1]
        }
        print_record(format, data)

        last_info_m_time = info_time
        last_info_m_usage = info_m_usage

    # 2.- Check CPU
    cpuinfo = psutil.cpu_percent()
    info_c_usage = cpuinfo

    info_delta = math.fabs(info_c_usage - last_info_c_usage)
    if info_delta >= delta:
        info_ncores = multiprocessing.cpu_count()

        info_cpufreq = 0.0
        proc = subprocess.Popen(["cat", "/proc/cpuinfo"],
                                stdout=subprocess.PIPE)
        out, err = proc.communicate()
        for line in out.split("\n"):
            if "cpu MHz" in line:
                info_cpufreq = info_cpufreq + float(line.split(":")[1])
        info_cpufreq = info_cpufreq / info_ncores

        # CPU freq * time * CPU usage * # cores
        data = {
            "type":
            "compute",
            "timestamp":
            info_time,
            "cpufreq":
            info_cpufreq,
            "timedelta":
            info_time - last_info_c_time,
            "usagepercent":
            last_info_c_usage,
            "usageabsolute":
            info_cpufreq * (info_time - last_info_c_time) * last_info_c_usage *
            info_ncores,
            "ncores":
            info_ncores
        }
        print_record(format, data)

        last_info_c_time = info_time
        last_info_c_usage = info_c_usage

    # 3.- Check Network
    netinfo = psutil.net_io_counters()
    info_n_usage = (netinfo.bytes_sent + netinfo.bytes_recv) / (1024 * 1024)

    info_delta = 100 * math.fabs(info_n_usage -
                                 last_info_n_usage) / (last_info_n_usage + 1)
    if info_delta >= delta:
        data = {
            "type": "network",
            "timestamp": info_time,
            "timedelta": info_time - last_info_n_time,
            "usageabsolute": last_info_n_usage
        }
        print_record(format, data)

        last_info_n_time = info_time
        last_info_n_usage = info_n_usage

    # 4.- Check Disk
    diskinfo = psutil.disk_io_counters()
    info_d_usage = (diskinfo.read_bytes + diskinfo.write_bytes) / (1024 * 1024)

    info_delta = 100 * math.fabs(info_d_usage -
                                 last_info_d_usage) / (last_info_d_usage + 1)
    if info_delta >= delta:
        data = {
            "type": "disk",
            "timestamp": info_time,
            "timedelta": info_time - last_info_d_time,
            "usageabsolute": last_info_d_usage
        }
        print_record(format, data)

        last_info_d_time = info_time
        last_info_d_usage = info_d_usage

    # 5.- Set next checking...
    threading.Timer(rrate, mon).start()
Пример #33
0
        elif _.endswith('sniffer.py'):
            servers.append([
                basename(_)[:-3],
                Popen([
                    'python', _, '--filter', 'not port 9999', '--interfac',
                    'eth0', '--logs', 'db', '--docker'
                ])
            ])

    while True:
        try:
            _servers = {}
            logs.info([
                'system', {
                    'type': 'network',
                    'bytes_sent': net_io_counters().bytes_sent,
                    'bytes_recv': net_io_counters().bytes_recv,
                    'packets_sent': net_io_counters().packets_sent,
                    'packets_recv': net_io_counters().packets_recv
                }
            ])
            for _ in servers:
                _servers[_[0]] = {
                    'memory': Process(_[1].pid).memory_percent(),
                    'cpu': Process(_[1].pid).cpu_percent()
                }
            logs.info(['system', _servers])
            stdout.flush()
        except BaseException:
            pass
        sleep(20)
Пример #34
0
import psutil

print(psutil.cpu_count())
print(psutil.cpu_count(logical=False))

print(psutil.virtual_memory())
print(psutil.swap_memory())

print(psutil.disk_partitions())
print(psutil.disk_usage('/'))
print(psutil.disk_io_counters())

print(psutil.net_io_counters())
print(psutil.net_if_addrs())
print(psutil.net_if_stats())
print(psutil.net_connections())

print(psutil.pids())
p = psutil.Process(4)
print(p.name())
Пример #35
0
 def update(self):
     """Get the latest system information."""
     import psutil
     if self.type == 'disk_use_percent':
         self._state = psutil.disk_usage(self.argument).percent
     elif self.type == 'disk_use':
         self._state = round(
             psutil.disk_usage(self.argument).used / 1024**3, 1)
     elif self.type == 'disk_free':
         self._state = round(
             psutil.disk_usage(self.argument).free / 1024**3, 1)
     elif self.type == 'memory_use_percent':
         self._state = psutil.virtual_memory().percent
     elif self.type == 'memory_use':
         virtual_memory = psutil.virtual_memory()
         self._state = round(
             (virtual_memory.total - virtual_memory.available) / 1024**2, 1)
     elif self.type == 'memory_free':
         self._state = round(psutil.virtual_memory().available / 1024**2, 1)
     elif self.type == 'swap_use_percent':
         self._state = psutil.swap_memory().percent
     elif self.type == 'swap_use':
         self._state = round(psutil.swap_memory().used / 1024**2, 1)
     elif self.type == 'swap_free':
         self._state = round(psutil.swap_memory().free / 1024**2, 1)
     elif self.type == 'processor_use':
         self._state = round(psutil.cpu_percent(interval=None))
     elif self.type == 'process':
         for proc in psutil.process_iter():
             try:
                 if self.argument == proc.name():
                     self._state = STATE_ON
                     return
             except psutil.NoSuchProcess as err:
                 _LOGGER.warning(
                     "Failed to load process with id: %s, old name: %s",
                     err.pid, err.name)
         self._state = STATE_OFF
     elif self.type == 'network_out' or self.type == 'network_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             counter = counters[self.argument][IO_COUNTER[self.type]]
             self._state = round(counter / 1024**2, 1)
         else:
             self._state = None
     elif self.type == 'packets_out' or self.type == 'packets_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             self._state = counters[self.argument][IO_COUNTER[self.type]]
         else:
             self._state = None
     elif self.type == 'ipv4_address' or self.type == 'ipv6_address':
         addresses = psutil.net_if_addrs()
         if self.argument in addresses:
             for addr in addresses[self.argument]:
                 if addr.family == IF_ADDRS_FAMILY[self.type]:
                     self._state = addr.address
         else:
             self._state = None
     elif self.type == 'last_boot':
         self._state = dt_util.as_local(
             dt_util.utc_from_timestamp(psutil.boot_time())).isoformat()
     elif self.type == 'load_1m':
         self._state = os.getloadavg()[0]
     elif self.type == 'load_5m':
         self._state = os.getloadavg()[1]
     elif self.type == 'load_15m':
         self._state = os.getloadavg()[2]
Пример #36
0
def monitor_host(profile):
    host_stats = {}

    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print "host monitor starts: " + str(timestamp)

    first = False
    if host_info["time"] == 0.0:
        first = True

    # cpu_percent
    host_stats["cpu_percent"] = psutil.cpu_percent(interval=0.5)

    tm = time.time()

    # cpu_times
    user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice = psutil.cpu_times(
    )

    if first == False:
        host_stats["user_time"] = str(
            (user - host_info["user_time"]) / (tm - host_info["time"]))
        host_stats["nice_time"] = str(
            (nice - host_info["nice_time"]) / (tm - host_info["time"]))
        host_stats["system_time"] = str(
            (system - host_info["system_time"]) / (tm - host_info["time"]))
        host_stats["idle_time"] = str(
            (idle - host_info["idle_time"]) / (tm - host_info["time"]))
        host_stats["iowait_time"] = str(
            (iowait - host_info["iowait_time"]) / (tm - host_info["time"]))
        host_stats["irq_time"] = str(
            (irq - host_info["irq_time"]) / (tm - host_info["time"]))
        host_stats["softirq_time"] = str(
            (softirq - host_info["softirq_time"]) / (tm - host_info["time"]))
        host_stats["steal_time"] = str(
            (steal - host_info["steal_time"]) / (tm - host_info["time"]))
        host_stats["guest_time"] = str(
            (guest - host_info["guest_time"]) / (tm - host_info["time"]))
        host_stats["guest_nice_time"] = str(
            (guest_nice - host_info["guest_nice_time"]) /
            (tm - host_info["time"]))

    host_info["user_time"] = user
    host_info["nice_time"] = nice
    host_info["system_time"] = system
    host_info["idle_time"] = idle
    host_info["iowait_time"] = iowait
    host_info["irq_time"] = irq
    host_info["softirq_time"] = softirq
    host_info["steal_time"] = steal
    host_info["guest_time"] = guest
    host_info["guest_nice_time"] = guest_nice

    # mem_percent, total_mem, used_mem, available_mem
    total_mem, available_mem, mem_percent, used_mem, free_mem, \
        active_mem, inactive_mem, buffers_mem, cached_mem = psutil.virtual_memory()

    host_stats["mem_percent"] = str(mem_percent)

    host_stats["total_mem"] = str(total_mem / (1024. * 1024.))
    host_stats["available_mem"] = str(available_mem / (1024. * 1024.))
    host_stats["used_mem"] = str(used_mem / (1024. * 1024.))
    host_stats["free_mem"] = str(free_mem / (1024. * 1024.))
    host_stats["active_mem"] = str(active_mem / (1024. * 1024.))
    host_stats["inactive_mem"] = str(inactive_mem / (1024. * 1024.))
    host_stats["buffers_mem"] = str(buffers_mem / (1024. * 1024.))
    host_stats["cached_mem"] = str(cached_mem / (1024. * 1024.))

    read_count, write_count, read_bytes, write_bytes, read_time, write_time = psutil.disk_io_counters(
    )

    if first == False:
        host_stats["read_count"] = str(
            (read_count * 1.0 - host_info["read_count"]) /
            (tm - host_info["time"]))
        host_stats["read_bytes"] = str(
            (read_bytes * 1.0 - host_info["read_bytes"]) /
            (tm - host_info["time"]))
        host_stats["write_count"] = str(
            (write_count * 1.0 - host_info["write_count"]) /
            (tm - host_info["time"]))
        host_stats["write_bytes"] = str(
            (write_bytes * 1.0 - host_info["write_bytes"]) /
            (tm - host_info["time"]))

    host_info["read_count"] = read_count * 1.0
    host_info["read_bytes"] = read_bytes * 1.0
    host_info["write_count"] = write_count * 1.0
    host_info["write_bytes"] = write_bytes * 1.0

    if first == False:
        database.host_info(timestamp, host_stats)

    interfaces = psutil.net_io_counters(pernic=True)
    for interface in interfaces:
        host_nets = {}

        if interface == profile["inbound"] or interface == profile["outbound"]:
            bytes_sent, bytes_recv, packets_sent, packets_recv, \
                errin, errout, dropin, dropout = interfaces[interface]

            if interface in host_nic:
                host_nets["interface"] = interface
                host_nets["packets_recv"] = str(((packets_recv - host_nic[interface]["packets_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_recv"] = str(((bytes_recv - host_nic[interface]["bytes_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["packets_sent"] = str(((packets_sent - host_nic[interface]["packets_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_sent"] = str(((bytes_sent - host_nic[interface]["bytes_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
            else:
                host_nic[interface] = {}

            host_nic[interface]["packets_recv"] = packets_recv
            host_nic[interface]["bytes_recv"] = bytes_recv
            host_nic[interface]["packets_sent"] = packets_sent
            host_nic[interface]["bytes_sent"] = bytes_sent

            if "interface" in host_nets:
                database.host_net(timestamp, host_nets)

        elif "vnet" in interface:
            bytes_sent, bytes_recv, packets_sent, packets_recv, \
                errin, errout, dropin, dropout = interfaces[interface]

            if interface in host_nic:
                host_nets["interface"] = interface
                host_nets["packets_recv"] = str(((packets_recv - host_nic[interface]["packets_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_recv"] = str(((bytes_recv - host_nic[interface]["bytes_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["packets_sent"] = str(((packets_sent - host_nic[interface]["packets_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_sent"] = str(((bytes_sent - host_nic[interface]["bytes_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
            else:
                host_nic[interface] = {}

            host_nic[interface]["packets_recv"] = packets_recv
            host_nic[interface]["bytes_recv"] = bytes_recv
            host_nic[interface]["packets_sent"] = packets_sent
            host_nic[interface]["bytes_sent"] = bytes_sent

            if "interface" in host_nets:
                database.host_net(timestamp, host_nets)

        elif "tap" in interface:
            bytes_sent, bytes_recv, packets_sent, packets_recv, \
                errin, errout, dropin, dropout = interfaces[interface]

            if interface in host_nic:
                host_nets["interface"] = interface
                host_nets["packets_recv"] = str(((packets_recv - host_nic[interface]["packets_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_recv"] = str(((bytes_recv - host_nic[interface]["bytes_recv"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["packets_sent"] = str(((packets_sent - host_nic[interface]["packets_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
                host_nets["bytes_sent"] = str(((bytes_sent - host_nic[interface]["bytes_sent"]) * 1.0) \
                                                  / (tm - host_info["time"]))
            else:
                host_nic[interface] = {}

            host_nic[interface]["packets_recv"] = packets_recv
            host_nic[interface]["bytes_recv"] = bytes_recv
            host_nic[interface]["packets_sent"] = packets_sent
            host_nic[interface]["bytes_sent"] = bytes_sent

            if "interface" in host_nets:
                database.host_net(timestamp, host_nets)

    host_info["time"] = tm

    return
Пример #37
0
"""
监控代码:监控服务器的内存、cpu、网络、磁盘等。与租车系统部署到一起。
"""
from datetime import datetime
from time import sleep

import psutil

print(psutil.cpu_percent())  # 获取cpu信息
print(psutil.virtual_memory())  # 获取虚拟内存
print(psutil.virtual_memory().percent)  # 虚拟内存百分比
print(psutil.disk_usage("E:/"))  # 租车系统所在的磁盘
print(psutil.disk_usage("E:/").percent)  # 租车系统所在的磁盘的百分百
print(psutil.net_io_counters())  # 网络
print(psutil.net_io_counters().bytes_sent)  # 发送的字节数
print(psutil.net_io_counters().bytes_recv)  # 接收的字节数

# 达到类似serveragent的效果,在性能测试期间,获取cpu、内存的趋势
# 死循环,每隔3s读一次,把读的结果写到文件中,测试结束后分析文件,试用excel等生成图表。
# 时间戳   CPU%  内存%   磁盘%   发送字节数    接收字节数
with open("E:/资源占用情况.txt", encoding='utf-8', mode='a') as file:
    file.write("时间戳\tcpu%\t内存%\t磁盘%\t发送字节数\t接收字节数")
    while True:
        print("监控中。。。。。。。。。。。。。。。。")
        # strftime 当前时间转字符串
        file.write(
            datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") + "\t")
        file.write(str(psutil.cpu_percent()) + "%\t")
        file.write(str(psutil.disk_usage("E:/").percent) + "%\t")
        file.write(str(psutil.net_io_counters().bytes_sent) + "%\t")
        file.write(str(psutil.net_io_counters().bytes_recv) + "%\t")
Пример #38
0
def recebidos():
    return conversor(psutil.net_io_counters().bytes_recv)
Пример #39
0
def enviados():
    return conversor(psutil.net_io_counters().bytes_sent)
Пример #40
0
def heroku_usage():
    if HEROKU_API is None and HEROKU_APP_NAME is None:
        return False, "You do not use heroku, bruh!"
    user_id = Heroku.account().id
    headers = {
        "User-Agent": choice(some_random_headers),
        "Authorization": f"Bearer {heroku_api}",
        "Accept": "application/vnd.heroku+json; version=3.account-quotas",
    }
    her_url = f"https://api.heroku.com/accounts/{user_id}/actions/get-quota"
    r = requests.get(her_url, headers=headers)
    if r.status_code != 200:
        return (
            True,
            f"**ERROR**\n`{r.reason}`",
        )
    result = r.json()
    quota = result["account_quota"]
    quota_used = result["quota_used"]
    remaining_quota = quota - quota_used
    percentage = math.floor(remaining_quota / quota * 100)
    minutes_remaining = remaining_quota / 60
    hours = math.floor(minutes_remaining / 60)
    minutes = math.floor(minutes_remaining % 60)
    App = result["apps"]
    try:
        App[0]["quota_used"]
    except IndexError:
        AppQuotaUsed = 0
        AppPercentage = 0
    else:
        AppQuotaUsed = App[0]["quota_used"] / 60
        AppPercentage = math.floor(App[0]["quota_used"] * 100 / quota)
    AppHours = math.floor(AppQuotaUsed / 60)
    AppMinutes = math.floor(AppQuotaUsed % 60)
    total, used, free = shutil.disk_usage(".")
    cpuUsage = psutil.cpu_percent()
    memory = psutil.virtual_memory().percent
    disk = psutil.disk_usage("/").percent
    upload = humanbytes(psutil.net_io_counters().bytes_sent)
    down = humanbytes(psutil.net_io_counters().bytes_recv)
    TOTAL = humanbytes(total)
    USED = humanbytes(used)
    FREE = humanbytes(free)
    return True, get_string("usage").format(
        Var.HEROKU_APP_NAME,
        AppHours,
        AppMinutes,
        AppPercentage,
        hours,
        minutes,
        percentage,
        TOTAL,
        USED,
        FREE,
        upload,
        down,
        cpuUsage,
        memory,
        disk,
    )
Пример #41
0
def get_interface_node():
    if_children = [make_if_nodes(x) for x in list(ps.net_io_counters(pernic=True).keys())]
    return ParentNode('interface', children=if_children)
Пример #42
0
    def _update_widgets(self, widgets):
        interfaces = [
            i for i in netifaces.interfaces()
            if not i.startswith(self._exclude)
        ]

        del widgets[:]

        counters = psutil.net_io_counters(pernic=True)
        now = time.time()
        timediff = now - (self._lastcheck if self._lastcheck else now)
        if timediff <= 0: timediff = 1
        self._lastcheck = now
        for interface in interfaces:
            if self._graphlen > 0:
                if interface not in self._graphdata:
                    self._graphdata[interface] = {
                        "rx": [0] * self._graphlen,
                        "tx": [0] * self._graphlen
                    }
            if not interface: interface = "lo"
            state = "down"
            if len(self.get_addresses(interface)) > 0:
                state = "up"
            elif bumblebee.util.asbool(self.parameter("hide_down", True)):
                continue

            if len(self._states["exclude"]
                   ) > 0 and state in self._states["exclude"]:
                continue
            if len(self._states["include"]
                   ) > 0 and state not in self._states["include"]:
                continue

            data = {
                "rx": counters[interface].bytes_recv,
                "tx": counters[interface].bytes_sent,
            }

            name = "traffic-{}".format(interface)

            if self._showname:
                self.create_widget(widgets, name, interface)

            for direction in ["rx", "tx"]:
                name = "traffic.{}-{}".format(direction, interface)
                widget = self.create_widget(
                    widgets,
                    name,
                    attributes={"theme.minwidth": self.get_minwidth_str()})
                prev = self._prev.get(name, 0)
                bspeed = (int(data[direction]) - int(prev)) / timediff
                speed = bumblebee.util.bytefmt(bspeed, self._format)
                txtspeed = '{0}/s'.format(speed)
                if self._graphlen > 0:
                    # skip first value returned by psutil, because it is
                    # giant and ruins the grapth ratio until it gets pushed
                    # out of saved list
                    if self._first_run is True:
                        self._first_run = False
                    else:
                        self._graphdata[interface][
                            direction] = self._graphdata[interface][direction][
                                1:]
                        self._graphdata[interface][direction].append(bspeed)
                    txtspeed = "{}{}".format(
                        bumblebee.output.bgraph(
                            self._graphdata[interface][direction]), txtspeed)
                widget.full_text(txtspeed)
                self._prev[name] = data[direction]
Пример #43
0
def netload():
    netrcv = int((psutil.net_io_counters().bytes_recv) / 1000000)
    netsent = int((psutil.net_io_counters().bytes_sent) / 1000000)
    return (netrcv, netsent)
Пример #44
0
# sdiskpart(device="E:\\", mountpoint="E:\\", fstype="NTFS", opts="rw,fixed"),
# sdiskpart(device="F:\\", mountpoint="F:\\", fstype="NTFS", opts="rw,fixed")]

print(psutil.disk_usage("/")) # 磁盘使用情况
# sdiskusage(total=500090007552, used=204505022464, free=295584985088, percent=40.9)

print(psutil.disk_io_counters()) # 磁盘IO
# sdiskio(
# read_count=4929858, write_count=8759548,
# read_bytes=321037917184, write_bytes=347494014976,
# read_time=4129, write_time=4759)


# psutil可以获取网络接口和网络连接信息
print()
print(psutil.net_io_counters()) # 获取网络读写字节/包的个数
# snetio(bytes_sent=68468541570, bytes_recv=121054712924,
# packets_sent=112391551, packets_recv=133056843,
# errin=0, errout=15, dropin=0, dropout=0)
print()
print(psutil.net_if_addrs() )# 获取网络接口信息
print()
print(psutil.net_if_stats()) # 获取网络接口状态

# 要获取当前网络连接信息, 使用net_connections()
print()
print(psutil.net_connections())


# 通过psutil可以获取到所有进程的详细信息
print()
Пример #45
0
    def update(self):
        """Update network stats using the input method.

        Stats is a list of dict (one dict per interface)
        """
        # Init new stats
        stats = self.get_init_value()

        if self.input_method == 'local':
            # Update stats using the standard system lib

            # Grab network interface stat using the psutil net_io_counter method
            try:
                netiocounters = psutil.net_io_counters(pernic=True)
            except UnicodeDecodeError as e:
                logger.debug(
                    'Can not get network interface counters ({})'.format(e))
                return self.stats

            # Grab interface's status (issue #765)
            # Grab interface's speed (issue #718)
            netstatus = {}
            try:
                netstatus = psutil.net_if_stats()
            except OSError as e:
                # see psutil #797/glances #1106
                logger.debug(
                    'Can not get network interface status ({})'.format(e))

            # Previous network interface stats are stored in the network_old variable
            if not hasattr(self, 'network_old'):
                # First call, we init the network_old var
                try:
                    self.network_old = netiocounters
                except (IOError, UnboundLocalError):
                    pass
                return self.stats

            # By storing time data we enable Rx/s and Tx/s calculations in the
            # XML/RPC API, which would otherwise be overly difficult work
            # for users of the API
            time_since_update = getTimeSinceLastUpdate('net')

            # Loop over interfaces
            network_new = netiocounters
            for net in network_new:
                # Do not take hidden interface into account
                # or KeyError: 'eth0' when interface is not connected #1348
                if self.is_hide(net) or net not in netstatus:
                    continue
                try:
                    cumulative_rx = network_new[net].bytes_recv
                    cumulative_tx = network_new[net].bytes_sent
                    cumulative_cx = cumulative_rx + cumulative_tx
                    rx = cumulative_rx - self.network_old[net].bytes_recv
                    tx = cumulative_tx - self.network_old[net].bytes_sent
                    cx = rx + tx
                    netstat = {
                        'interface_name': n(net),
                        'time_since_update': time_since_update,
                        'cumulative_rx': cumulative_rx,
                        'rx': rx,
                        'cumulative_tx': cumulative_tx,
                        'tx': tx,
                        'cumulative_cx': cumulative_cx,
                        'cx': cx,
                        # Interface status
                        'is_up': netstatus[net].isup,
                        # Interface speed in Mbps, convert it to bps
                        # Can be always 0 on some OSes
                        'speed': netstatus[net].speed * 1048576,
                        # Set the key for the dict
                        'key': self.get_key()
                    }
                except KeyError:
                    continue
                else:
                    # Append the interface stats to the list
                    stats.append(netstat)

            # Save stats to compute next bitrate
            self.network_old = network_new

        elif self.input_method == 'snmp':
            # Update stats using SNMP

            # SNMP bulk command to get all network interface in one shot
            try:
                netiocounters = self.get_stats_snmp(
                    snmp_oid=snmp_oid[self.short_system_name], bulk=True)
            except KeyError:
                netiocounters = self.get_stats_snmp(
                    snmp_oid=snmp_oid['default'], bulk=True)

            # Previous network interface stats are stored in the network_old variable
            if not hasattr(self, 'network_old'):
                # First call, we init the network_old var
                try:
                    self.network_old = netiocounters
                except (IOError, UnboundLocalError):
                    pass
            else:
                # See description in the 'local' block
                time_since_update = getTimeSinceLastUpdate('net')

                # Loop over interfaces
                network_new = netiocounters

                for net in network_new:
                    # Do not take hidden interface into account
                    if self.is_hide(net):
                        continue

                    try:
                        # Windows: a tips is needed to convert HEX to TXT
                        # http://blogs.technet.com/b/networking/archive/2009/12/18/how-to-query-the-list-of-network-interfaces-using-snmp-via-the-ifdescr-counter.aspx
                        if self.short_system_name == 'windows':
                            try:
                                interface_name = str(
                                    base64.b16decode(net[2:-2].upper()))
                            except TypeError:
                                interface_name = net
                        else:
                            interface_name = net

                        cumulative_rx = float(
                            network_new[net]['cumulative_rx'])
                        cumulative_tx = float(
                            network_new[net]['cumulative_tx'])
                        cumulative_cx = cumulative_rx + cumulative_tx
                        rx = cumulative_rx - float(
                            self.network_old[net]['cumulative_rx'])
                        tx = cumulative_tx - float(
                            self.network_old[net]['cumulative_tx'])
                        cx = rx + tx
                        netstat = {
                            'interface_name': interface_name,
                            'time_since_update': time_since_update,
                            'cumulative_rx': cumulative_rx,
                            'rx': rx,
                            'cumulative_tx': cumulative_tx,
                            'tx': tx,
                            'cumulative_cx': cumulative_cx,
                            'cx': cx
                        }
                    except KeyError:
                        continue
                    else:
                        netstat['key'] = self.get_key()
                        stats.append(netstat)

                # Save stats to compute next bitrate
                self.network_old = network_new

        # Update the stats
        self.stats = stats

        return self.stats
Пример #46
0
 def update(self):
     self.if_addrs = psutil.net_if_addrs()
     self.net_io = psutil.net_io_counters()
Пример #47
0
async def info(request):
    """HTTP Method to retun node state to caller"""
    log.debug("info request")
    app = request.app
    answer = {}
    # copy relevant entries from state dictionary to response
    node = {}
    node['id'] = request.app['id']
    node['type'] = request.app['node_type']
    node['start_time'] =  app["start_time"] #unixTimeToUTC(app['start_time'])
    node['state'] = app['node_state'] 
    node['node_number'] = app['node_number']
    node['node_count'] = app['node_count']
    
    answer["node"] = node
    # psutil info
    # see: http://pythonhosted.org/psutil/ for description of different fields
    cpu = {}
    cpu["percent"] = psutil.cpu_percent()
    cpu["cores"] = psutil.cpu_count()
    answer["cpu"] = cpu
    diskio = psutil.disk_io_counters()
    disk_stats = {}
    disk_stats["read_count"] = diskio.read_count
    disk_stats["read_time"] = diskio.read_time
    disk_stats["read_bytes"] = diskio.read_bytes
    disk_stats["write_count"] = diskio.write_count
    disk_stats["write_time"] = diskio.write_time
    disk_stats["write_bytes"] = diskio.write_bytes
    answer["diskio"] = disk_stats
    netio = psutil.net_io_counters()
    net_stats = {}
    net_stats["bytes_sent"] = netio.bytes_sent
    net_stats["bytes_sent"] = netio.bytes_recv
    net_stats["packets_sent"] = netio.packets_sent
    net_stats["packets_recv"] = netio.packets_recv
    net_stats["errin"] = netio.errin
    net_stats["errout"] = netio.errout
    net_stats["dropin"] = netio.dropin
    net_stats["dropout"] = netio.dropout
    answer["netio"] = net_stats
    mem_stats = {}
    svmem = psutil.virtual_memory()
    mem_stats["phys_total"] = svmem.total
    mem_stats["phys_available"] = svmem.available
    sswap = psutil.swap_memory()
    mem_stats["swap_total"] = sswap.total
    mem_stats["swap_used"] = sswap.used
    mem_stats["swap_free"] = sswap.free
    mem_stats["percent"] = sswap.percent
    answer["memory"] = mem_stats
    disk_stats = {}
    sdiskusage = psutil.disk_usage('/')
    disk_stats["total"] = sdiskusage.total
    disk_stats["used"] = sdiskusage.used
    disk_stats["free"] = sdiskusage.free
    disk_stats["percent"] = sdiskusage.percent
    answer["disk"] = disk_stats
    answer["log_stats"] = app["log_count"]
    answer["req_count"] = app["req_count"]
    answer["s3_stats"] = app["s3_stats"]
    mc_stats = {}
    if "meta_cache" in app:
        mc = app["meta_cache"]  # only DN nodes have this
        mc_stats["count"] = len(mc)
        mc_stats["dirty_count"] = mc.dirtyCount
        mc_stats["utililization_per"] = mc.cacheUtilizationPercent
        mc_stats["mem_used"] = mc.memUsed
        mc_stats["mem_target"] = mc.memTarget
    answer["meta_cache_stats"] = mc_stats
    cc_stats = {}
    if "chunk_cache" in app:
        cc = app["chunk_cache"]  # only DN nodes have this
        cc_stats["count"] = len(cc)
        cc_stats["dirty_count"] = cc.dirtyCount
        cc_stats["utililization_per"] = cc.cacheUtilizationPercent
        cc_stats["mem_used"] = cc.memUsed
        cc_stats["mem_target"] = cc.memTarget
    answer["chunk_cache_stats"] = cc_stats
    dc_stats = {}
    if "domain_cache" in app:
        dc = app["domain_cache"]  # only DN nodes have this
        dc_stats["count"] = len(dc)
        dc_stats["dirty_count"] = dc.dirtyCount
        dc_stats["utililization_per"] = dc.cacheUtilizationPercent
        dc_stats["mem_used"] = dc.memUsed
        dc_stats["mem_target"] = dc.memTarget
    answer["domain_cache_stats"] = dc_stats
        
    resp = await jsonResponse(request, answer) 
    log.response(request, resp=resp)
    return resp
Пример #48
0
		else:
			options = {"org": organization, "type": deviceType, "id": deviceId, "auth-method": authMethod, "auth-token": authToken}
		client = wiotp.sdk.device.DeviceClient(options)
		client.commandCallback = commandProcessor
		client.connect()
	except Exception as e:
		print(str(e))
		sys.exit(1)
	

	print("(Press Ctrl+C to disconnect)")
	
	# Take initial reading
	psutil.cpu_percent(percpu=False)
	ioBefore_ts = time.time()
	ioBefore = psutil.net_io_counters()

	while True:
		time.sleep(interval)
		ioAfter_ts = time.time()
		ioAfter = psutil.net_io_counters()
		
		# Calculate the time taken between IO checks
		ioDuration = ioAfter_ts - ioBefore_ts

		data = { 
			'name' : deviceName,
			'cpu' : psutil.cpu_percent(percpu=False),
			'mem' : psutil.virtual_memory().percent,
			'network': {
				'up': round( (ioAfter.bytes_sent - ioBefore.bytes_sent) / (ioDuration*1024), 2 ), 
Пример #49
0
def get(config):
    """Get all network information"""
    netInfo = {}
    interfaces = config.get('agent', "interfaces").split(",")
    for intf in interfaces:
        nicInfo = netInfo.setdefault(intf, {})
        vlan_range = config.get(intf, "vlans")
        vlan_min = config.get(intf, "vlan_min")
        vlan_max = config.get(intf, "vlan_max")
        switch_port = config.get(intf, "port")
        switch = config.get(intf, "switch")
        shared_interface = config.get(intf, "shared")
        if config.has_option(intf, 'isAlias'):
            nicInfo['isAlias'] = config.get(intf, 'isAlias')
        if config.has_option(intf, "ips"):
            nicInfo['ipv4-floatingip-pool'] = config.get(intf, "ips")
        nicInfo['vlan_range'] = vlan_range
        nicInfo['min_bandwidth'] = int(vlan_min)
        nicInfo['max_bandwidth'] = int(vlan_max)
        nicInfo['switch_port'] = str(switch_port).replace('/', '_')
        nicInfo['switch'] = str(switch)
        nicInfo['shared'] = str2bool(shared_interface)
        nicInfo['vlans'] = {}
        # TODO. It should calculate available capacity, depending on installed vlans.
        # Currently we set it same as max_bandwidth.
        nicInfo['available_bandwidth'] = int(vlan_max)  # TODO
        # TODO. It should also calculate reservable capacity depending on installed vlans;
        # Currently we set it to max available;
        nicInfo['reservable_bandwidth'] = int(vlan_max)  # TODO
    print netInfo
    tmpifAddr = psutil.net_if_addrs()
    tmpifStats = psutil.net_if_stats()
    tmpIOCount = psutil.net_io_counters(pernic=True)
    foundInterfaces = []
    for nic, addrs in tmpifAddr.items():
        # TODO: Check with configuration of which vlans are provisioned;
        # Currently it is a hack. if it is a vlan, I assume it is provisioned by orchestrator;
        nicSplit = nic.split('.')
        nicInfo = netInfo.setdefault(nicSplit[0], {'vlans': {}})
        if len(nicSplit) == 2:
            nicInfo = nicInfo['vlans'].setdefault(nic, {})
            nicInfo['provisioned'] = True
            nicInfo['vlanid'] = nicSplit[1]
        else:
            nicInfo['provisioned'] = False
        foundInterfaces.append(nic)
        for vals in addrs:
            familyInfo = nicInfo.setdefault(str(vals.family), {})
            # vals - family=2, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None
            # For family more information look here: http://lxr.free-electrons.com/source/include/linux/socket.h#L160
            familyInfo["family"] = vals.family
            familyInfo["address"] = vals.address
            familyInfo["netmask"] = vals.netmask
            if int(vals.family) in [2, 10] and vals.address and vals.netmask:
                try:
                    ipwithnetmask = ipaddress.ip_interface(
                        u"%s/%s" % (vals.address, vals.netmask))
                    if isinstance(ipwithnetmask, ipaddress.IPv4Interface):
                        familyInfo["ipv4-address"] = str(ipwithnetmask)
                    elif isinstance(ipwithnetmask, ipaddress.IPv6Interface):
                        familyInfo["ipv6-address"] = str(ipwithnetmask)
                    else:
                        print "This type was not understood by the system. Type: %s and value: %s" % (
                            type(ipwithnetmask), str(ipwithnetmask))
                except ValueError as ex:
                    print 'Got an exception %s' % ex
            elif int(vals.family) in [17]:
                familyInfo["mac-address"] = vals.address
            familyInfo["broadcast"] = vals.broadcast
            familyInfo["ptp"] = vals.ptp
            # tmpifStats - snicstats(isup=True, duplex=0, speed=0, mtu=1500)
            if vals.family == 2:
                familyInfo["UP"] = tmpifStats[nic].isup
                familyInfo["duplex"] = tmpifStats[nic].duplex
                familyInfo["speed"] = tmpifStats[nic].speed
                familyInfo["MTU"] = tmpifStats[nic].mtu
                # tmpIOCount - (bytes_sent=13839798, bytes_recv=690754706, packets_sent=151186,
                #               packets_recv=630590, errin=0, errout=0, dropin=0, dropout=0)
                familyInfo["bytes_sent"] = tmpIOCount[nic].bytes_sent
                familyInfo["bytes_received"] = tmpIOCount[nic].bytes_recv
                familyInfo["packets_sent"] = tmpIOCount[nic].packets_sent
                familyInfo["packets_recv"] = tmpIOCount[nic].packets_recv
                familyInfo["errin"] = tmpIOCount[nic].errin
                familyInfo["errout"] = tmpIOCount[nic].errout
                familyInfo["dropin"] = tmpIOCount[nic].dropin
                familyInfo["dropout"] = tmpIOCount[nic].dropout
                # Additional info which is not provided by psutil so far...
                # More detail information about all types here:
                # http://lxr.free-electrons.qcom/source/include/uapi/linux/if_arp.h
                nicType = externalCommand('cat /sys/class/net/' + nic +
                                          "/type")
                familyInfo["Type"] = nicType[0].strip()
                txQueueLen = externalCommand('cat /sys/class/net/' + nic +
                                             "/tx_queue_len")
                familyInfo["txqueuelen"] = txQueueLen[0].strip()
    # Check in the end which interfaces where defined in config but not available...
    outputForFE = {"interfaces": {}, "routes": []}
    for intfName, intfDict in netInfo.iteritems():
        if intfName.split('.')[0] not in foundInterfaces:
            print 'This interface was defined in configuration, but not available. Will not add it to final output'
            print intfName, intfDict
        else:
            outputForFE["interfaces"][intfName] = intfDict
    # Get Routing Information
    outputForFE["routes"] = getRoutes(config)
    return outputForFE
old_errout = None
old_dropin = None
old_dropout = None
old_dt = None
first = True
while True:
    LOGGER.debug("waiting 10s...")
    time.sleep(10)
    client = TelegrafUnixSocketClient(SOCKET_PATH)
    try:
        client.connect()
    except Exception:
        LOGGER.warning("can't connect to %s, wait 10s and try again...",
                       SOCKET_PATH)
        continue
    stats = psutil.net_io_counters(pernic=False)
    new_dt = datetime.now()
    new_bytes_sent = stats.bytes_sent
    new_bytes_recv = stats.bytes_recv
    new_packets_sent = stats.packets_sent
    new_packets_recv = stats.packets_recv
    new_errin = stats.errin
    new_errout = stats.errout
    new_dropin = stats.dropin
    new_dropout = stats.dropout

    if first:
        first = False
    else:
        delta = (new_dt - old_dt).total_seconds()
        bytes_sent_per_second = (new_bytes_sent - old_bytes_sent) / delta
Пример #51
0
def net_out():
    netOut = size(
        psutil.net_io_counters(pernic=True,
                               nowrap=True)[eth_interface].bytes_sent)
    return netOut
Пример #52
0
def net_in():
    netIn = size(
        psutil.net_io_counters(pernic=True,
                               nowrap=True)[eth_interface].bytes_recv)
    return netIn
Пример #53
0
 def test_net_io_counters(self):
     # Duplicate of test_system.py. Keep it anyway.
     for ifname, _ in psutil.net_io_counters(pernic=True).items():
         self.assertIsInstance(ifname, str)
Пример #54
0
import psutil
import time

addr = psutil.net_if_addrs()
ifaceList  = addr.keys()

index = 0
cardList = []
for iface in ifaceList:
    net = iface
    ip = addr[iface][0][1]
    try:
        ipcheck = ip.split(".")[3]
        status = "<span style='color:#00d600'>Up</span>"
        cardList.append(iface)
    except:
        status = "<span style='color:red'>Down</span>"

    # print(iface)
byte = psutil.net_io_counters(pernic=True)
# print(byte)
i = 0
for card in cardList:
    print(str(i)+"||"+card + "||"+str((byte[card][0]/8/1024-int(byte[card][0]/8/1024)))+"||"+str((byte[card][1]/8/1024/1000))+"|||")   
    i = i+1


Пример #55
0
    def update_counters(self, interface):
        import psutil

        self.pnic_before = self.pnic
        counters = psutil.net_io_counters(pernic=True)
        self.pnic = counters[interface] if interface in counters else None
Пример #56
0
from smtplib import SMTP_SSL
from email.header import Header
from datetime import datetime
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import psutil
import socket
from hurry.filesize import size

#   the remote ip address. When remote connected, I shall be able to curl to www.google.com from China.

nic = 'eth0'

byte_send = psutil.net_io_counters(pernic=True)[nic][0]
byte_recv = psutil.net_io_counters(pernic=True)[nic][1]

#send_traffic = size(byte_send)
#	recv_traffic = size(byte_recv)


def format_bytes(size):
    # 2**10 = 1024
    power = 2**10
    n = 0
    power_labels = {0: '', 1: 'kilo', 2: 'mega', 3: 'giga', 4: 'tera'}
    while size > power:
        size /= power
        n += 1
    return size, power_labels[n] + 'bytes'
Пример #57
0
 def update(self):
     """Get the latest system information."""
     if self.type == "disk_use_percent":
         self._state = psutil.disk_usage(self.argument).percent
     elif self.type == "disk_use":
         self._state = round(
             psutil.disk_usage(self.argument).used / 1024**3, 1)
     elif self.type == "disk_free":
         self._state = round(
             psutil.disk_usage(self.argument).free / 1024**3, 1)
     elif self.type == "memory_use_percent":
         self._state = psutil.virtual_memory().percent
     elif self.type == "memory_use":
         virtual_memory = psutil.virtual_memory()
         self._state = round(
             (virtual_memory.total - virtual_memory.available) / 1024**2, 1)
     elif self.type == "memory_free":
         self._state = round(psutil.virtual_memory().available / 1024**2, 1)
     elif self.type == "swap_use_percent":
         self._state = psutil.swap_memory().percent
     elif self.type == "swap_use":
         self._state = round(psutil.swap_memory().used / 1024**2, 1)
     elif self.type == "swap_free":
         self._state = round(psutil.swap_memory().free / 1024**2, 1)
     elif self.type == "processor_use":
         self._state = round(psutil.cpu_percent(interval=None))
     elif self.type == "processor_temperature":
         self._state = self.read_cpu_temperature()
     elif self.type == "process":
         for proc in psutil.process_iter():
             try:
                 if self.argument == proc.name():
                     self._state = STATE_ON
                     return
             except psutil.NoSuchProcess as err:
                 _LOGGER.warning(
                     "Failed to load process with id: %s, old name: %s",
                     err.pid,
                     err.name,
                 )
         self._state = STATE_OFF
     elif self.type == "network_out" or self.type == "network_in":
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             counter = counters[self.argument][IO_COUNTER[self.type]]
             self._state = round(counter / 1024**2, 1)
         else:
             self._state = None
     elif self.type == "packets_out" or self.type == "packets_in":
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             self._state = counters[self.argument][IO_COUNTER[self.type]]
         else:
             self._state = None
     elif (self.type == "throughput_network_out"
           or self.type == "throughput_network_in"):
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             counter = counters[self.argument][IO_COUNTER[self.type]]
             now = dt_util.utcnow()
             if self._last_value and self._last_value < counter:
                 self._state = round(
                     (counter - self._last_value) / 1000**2 /
                     (now - self._last_update_time).seconds,
                     3,
                 )
             else:
                 self._state = None
             self._last_update_time = now
             self._last_value = counter
         else:
             self._state = None
     elif self.type == "ipv4_address" or self.type == "ipv6_address":
         addresses = psutil.net_if_addrs()
         if self.argument in addresses:
             for addr in addresses[self.argument]:
                 if addr.family == IF_ADDRS_FAMILY[self.type]:
                     self._state = addr.address
         else:
             self._state = None
     elif self.type == "last_boot":
         self._state = dt_util.as_local(
             dt_util.utc_from_timestamp(psutil.boot_time())).isoformat()
     elif self.type == "load_1m":
         self._state = round(os.getloadavg()[0], 2)
     elif self.type == "load_5m":
         self._state = round(os.getloadavg()[1], 2)
     elif self.type == "load_15m":
         self._state = round(os.getloadavg()[2], 2)
Пример #58
0
def net_stats():
    netStats = psutil.net_io_counters(pernic=True, nowrap=True)[eth_interface]
    return netStats
# cpu使用率
print(psutil.cpu_percent(interval=0.5))
# 获取每个核心的使用率
print(psutil.cpu_percent(interval=0.5, percpu=True))

# 获取内存信息
# 内存整体信息
print(psutil.virtual_memory())
# 内存使用率
print(psutil.virtual_memory().percent)

# 获取硬盘信息
# 硬盘分区信息
print(psutil.disk_partitions())
# 指定目录磁盘信息
print(psutil.disk_usage("/"))
# 硬盘使用率
print(psutil.disk_usage("/").percent)

# 获取网络信息
# 收到的数据包数量
print(psutil.net_io_counters().bytes_recv)
# 发送的数据包数量
print(psutil.net_io_counters().bytes_sent)

# 获取开机时间
print(psutil.boot_time()) # 1970年到现在的秒数

# 获取活动用户
print(psutil.users())
Пример #60
0
def _get_stats():
    libvirt = util_libvirt.get_libvirt()
    conn = libvirt.open('qemu:///system')

    # What's special about this node?
    retval = {
        'is_etcd_master': config.NODE_IS_ETCD_MASTER,
        'is_hypervisor': config.NODE_IS_HYPERVISOR,
        'is_network_node': config.NODE_IS_NETWORK_NODE,
    }

    # CPU info
    present_cpus, _, available_cpus = conn.getCPUMap()
    retval.update({
        'cpu_max': present_cpus,
        'cpu_available': available_cpus,
    })

    retval['cpu_max_per_instance'] = conn.getMaxVcpus(None)

    # This is disabled as data we don't currently use
    # for i in range(present_cpus):
    #    per_cpu_stats = conn.getCPUStats(i)
    #    for key in per_cpu_stats:
    #        retval['cpu_core%d_%s' % (i, key)] = per_cpu_stats[key]

    try:
        load_1, load_5, load_15 = psutil.getloadavg()
        retval.update({
            'cpu_load_1': load_1,
            'cpu_load_5': load_5,
            'cpu_load_15': load_15,
        })
    except Exception as e:
        util_general.ignore_exception('load average', e)

    # System memory info, converting bytes to mb
    stats = psutil.virtual_memory()
    retval.update({
        'memory_max': stats.total // 1024 // 1024,
        'memory_available': stats.available // 1024 // 1024
    })

    # libvirt memory info, converting kb to mb
    memory_status = conn.getMemoryStats(
        libvirt.VIR_NODE_MEMORY_STATS_ALL_CELLS)
    retval.update({
        'memory_max_libvirt': memory_status['total'] // 1024,
        'memory_available_libvirt': memory_status['free'] // 1024,
    })

    # Kernel Shared Memory (KSM) information
    ksm_details = {}
    for ent in os.listdir('/sys/kernel/mm/ksm'):
        with open('/sys/kernel/mm/ksm/%s' % ent) as f:
            ksm_details['memory_ksm_%s' % ent] = int(f.read().rstrip())
    retval.update(ksm_details)

    # Disk info. There could be more than one filesystem here, so we track
    # all of the paths we're fond of.
    fsids = []
    minimum = -1
    total = 0
    used = 0

    for path in ['', 'blobs', 'image_cache', 'instances', 'uploads']:
        # We need to make the paths we check if they don't exist, otherwise
        # they wont be included in the metrics and things get confused.
        fullpath = os.path.join(config.STORAGE_PATH, path)
        os.makedirs(fullpath, exist_ok=True)
        s = os.statvfs(fullpath)
        free = s.f_frsize * s.f_bavail

        if s.f_fsid not in fsids:
            total += s.f_frsize * s.f_blocks
            used += s.f_frsize * (s.f_blocks - s.f_bfree)
            if minimum == -1 or free < minimum:
                minimum = free

        if path == '':
            path = 'sfroot'
        retval['disk_free_%s' % path] = free

    retval.update({
        'disk_total': total,
        'disk_free': minimum,
        'disk_used': used
    })

    disk_counters = psutil.disk_io_counters()
    retval.update({
        'disk_read_bytes': disk_counters.read_bytes,
        'disk_write_bytes': disk_counters.write_bytes,
    })

    # Network info
    net_counters = psutil.net_io_counters()
    retval.update({
        'network_read_bytes': net_counters.bytes_recv,
        'network_write_bytes': net_counters.bytes_sent,
    })

    # Virtual machine consumption info
    total_instances = 0
    total_active_instances = 0
    total_instance_max_memory = 0
    total_instance_actual_memory = 0
    total_instance_vcpus = 0
    total_instance_cpu_time = 0

    for guest in conn.listAllDomains():
        try:
            active = guest.isActive() == 1
            if active:
                _, maxmem, mem, cpus, cpu_time = guest.info()

        except libvirt.libvirtError as e:
            LOG.debug('During resource calc ignored libvirt error: %s' % e)
            active = False

        if active:
            total_instances += 1
            total_active_instances += 1
            total_instance_max_memory += maxmem
            total_instance_actual_memory += mem
            total_instance_vcpus += cpus
            total_instance_cpu_time += cpu_time

    # Queue health statistics
    node_queue_processing, node_queue_waiting = etcd.get_queue_length(
        config.NODE_NAME)

    retval.update({
        'cpu_total_instance_vcpus':
        total_instance_vcpus,
        'cpu_total_instance_cpu_time':
        total_instance_cpu_time,
        'memory_total_instance_max':
        total_instance_max_memory // 1024,
        'memory_total_instance_actual':
        total_instance_actual_memory // 1024,
        'instances_total':
        total_instances,
        'instances_active':
        total_active_instances,
        'node_queue_processing':
        node_queue_processing,
        'node_queue_waiting':
        node_queue_waiting,
    })

    if config.NODE_IS_NETWORK_NODE:
        network_queue_processing, network_queue_waiting = etcd.get_queue_length(
            'networknode')

        retval.update({
            'network_queue_processing': network_queue_processing,
            'network_queue_waiting': network_queue_waiting,
        })

    return retval