def systemInfo(self): ''' system:系统名称,node:主机名,release:系统发行版本,version:版本,machine:系统架构,processor:cpu架构,runTime:开机时长(float),freeRate:空置率 ''' info = SystemInfo.uname() runInfo = SystemStat.uptime_stat() info['runTime']= runInfo['runTime'] info['freeRate'] = runInfo['freeRate'] return info
def systemMem(self): ''' mem['MemTotal'] 内存总数 kB mem['MemUsed'] 已使用总数 kB mem['MemFree'] 空闲 mem['Buffers'] 缓存 mem['Cached'] cache mem['MemUsedPerc'] 已使用百分比 ''' return SystemStat.memory_stat()
def systemDiskList(self): ''' df -h出来的内容 disk['name'] 盘符 disk['totalSize'] 磁盘总容量 disk['usedSize'] 已使用 disk['unusedSize'] 未审批 disk['usedRate'] 已使用百分比 disk['mount'] 挂载点 ''' return SystemStat.disk_stat()
def systemPerformance(self): ''' MemTotal:内存总数,MemUsed:已使用,MemUsedPerc:已使用百分比,CpuUsedPerc:cpu使用百分比 ''' mem = SystemStat.memory_stat() if self.cpu['stat'] == None : self.cpu['stat'] = SystemStat.cpu_stat() self.cpu['time'] = time.time() time.sleep(0.2) if time.time() - self.cpu['time'] > self.cpu['interval'] : self.cpu['stat'] = SystemStat.cpu_stat() self.cpu['time'] = time.time() cpu2 = SystemStat.cpu_stat() cpuUsedPerc = SystemStat.cpu_rate(self.cpu['stat'], cpu2) info = {} info['MemTotal'] = mem['MemTotal'] info['MemUsed'] = mem['MemUsed'] info['MemUsedPerc'] = mem['MemUsedPerc'] info['CpuUsedPerc'] = cpuUsedPerc return info
def vm_info(self, vms=None): "" infos = [] # 如果没有vm,默认查询所有的vm信息 if vms == None: vms = [] vm_infos = self.vm.info_vms() for vm in vm_infos: vms.append(vm['vm_name'].encode('iso8859-1')) # 查询app信息 app_info = {} app_infos = self.app.app_info() for app in app_infos: app_info[app['vmName'].encode( 'iso8859-1')] = app['instanceId'].encode('iso8859-1') # 查询运行vm top 信息 tops = {} vm_info = os.popen( "virt-top -b -n 2 -d 0.5 --stream 2>/dev/null").read() num = 0 for tmp in vm_info.split("\n"): if tmp == "": continue top = tmp.split() if top[0] == "virt-top": num += 1 if top[0] == "virt-top" or top[0] == "ID": continue if num == 2: temp = {} cpu = top[6] mem = top[7] temp['cpuRate'] = cpu temp['memRate'] = mem tops[top[9]] = temp # 网卡流量 net_tmp = SystemStat.net_rate() net_rate = {} for rate in net_tmp: net_rate[rate['interface']] = rate['rate'] # 循环查询vm信息 for vmName in vms: info = {} # 1 vm name info['vmName'] = vmName # 2 vm stat state = vm_state(vmName)[vmName] info['vmState'] = state # 8 time info['eventTime'] = time.strftime("%Y-%m-%d %H:%M:%S") # 3 instanceId if vmName in app_info: info['instanceId'] = app_info[vmName] # 暂不发送 # 4 disk info # disk_info = os.popen('virt-df -d %s 2>/dev/null'%vmName).read() # diskInfo = [] # for tmp in disk_info.split("\n"): # lis = {} # if(tmp != ""): # disk = tmp.split() # if(disk[0] == "Filesystem"): # continue # content = {} # # content['used'] = disk[2] # content['available'] = disk[3] # content['rate'] = disk[4] # diskName = disk[0].split(":")[1] # lis[diskName] = content # diskInfo.append(lis) # info['diskInfo'] = diskInfo # 判断是否运行 if state != 'running': infos.append(info) continue # 5 net stat # 第一种方法 #info['netStats'] = vm_netstats(vmName)[vmName] # 第二种方法 netStats = [] # 可用‘ virsh domiflist test ’或者 ‘virsh domifstat test port22106’获取 nics = get_nics(vmName) for mac, attrs in nics.items(): tmp = {} if 'target' in attrs: dev = attrs['target'] if dev in net_rate: tmp['interface'] = dev.encode('iso8859-1') tmp['flow'] = net_rate[dev] netStats.append(tmp) info['netStats'] = netStats # 6 disk stat info['diskStats'] = vm_diskstats(vmName)[vmName] # 7 cpu mem if vmName in tops: info['cpuRate'] = tops[vmName]['cpuRate'] info['memRate'] = tops[vmName]['memRate'] infos.append(info) self.infos = infos
def systemNetList(self): ''' interface表示网口,ReceiveBytes表示收的字节数,ReceivePackets表示收的包数,ReceiveErrs表示接收的错误包数,ReceiveDrop表示收丢弃的包量 ''' return SystemStat.net_stat()
def systemCpuLoadavg(self): ''' lavg_1:1分钟内的平均进程数、lavg_5:5、lavg_15:15,nr:正在运行的进程/总进程数, last_pid:分钟内的平均进程数, ''' return SystemStat.load_stat()
def cpuinfoList(self): ''' processor:核序号,physical id物理cpu序号,cpu MHz主频,model name型号 ''' cpuList = SystemStat.cpuinfo_stat() return cpuList
def systemNetInterface(self,ifname=None): ''' 每秒流量位pbs(每秒比特数),此函数会耗时一秒钟 ''' return SystemStat.net_rate(ifname);