def get_cpuinfo_byvirsh(): cpuinfo = {} CMD = "cat /proc/cpuinfo | grep processor | wc -l" ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None cpuinfo["vcpuTotal"] = int(data) * 4 CMD = "virsh list --all | sed -n '3,$p' | sed '$d'" ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None if data: vcpuAlloc = 0 vcpuUsed = 0 domainList = data.split('\n') for domain in domainList: domid = domain.split()[1] CMD = "virsh dominfo %s" % domid ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) continue cpus = 0 state = "" data = data.split('\n') for line in data: if 'CPU(s)' in line: cpus = int(line.split()[1]) if 'State' in line: state = line.split()[1] vcpuAlloc += cpus if state == 'running': vcpuUsed += cpus cpuinfo['vcpuUsed'] = vcpuUsed cpuinfo['vcpuAlloc'] = vcpuAlloc else: cpuinfo['vcpuUsed'] = 0 cpuinfo['vcpuAlloc'] = 0 return cpuinfo
def get_meminfo_byvirsh(): meminfo = {} CMD = "virsh list --all | sed -n '3,$p' | sed '$d'" ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None if data: vmTotalMem = 0 vmUsedMem = 0 domainList = data.split('\n') for domain in domainList: domid = domain.split()[1] CMD = "virsh dominfo %s" % domid ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) continue maxMem = 0 usedMem = 0 data = data.split('\n') for line in data: if 'Max memory' in line: #unit KB maxMem = int(line.split()[2]) if 'Used memory' in line: #unit KB usedMem = int(line.split()[2]) vmTotalMem += maxMem vmUsedMem += usedMem meminfo['vmTotalMem'] = vmTotalMem * 1024 #unit Byte meminfo['vmUsedMem'] = vmUsedMem * 1024 #unit Byte else: meminfo['vmTotalMem'] = 0 meminfo['vmUsedMem'] = 0 CMD = 'cat /proc/meminfo | grep MemTotal' ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None meminfo["totalMem"] = int(data.split()[1]) * 1024 return meminfo
def get_cpu_cores(): ret, data = OCT_SYSTEM("cat /proc/cpuinfo | grep -w processor | wc -l") if ret != 0: ERROR("get processor number error") return 1 return int(data.replace("\n", ""))
def get_max_processes(): ret, data = OCT_SYSTEM("ulimit -p") if ret != 0: print(ret) print(data) return "ulimited" return data.split("\n")[0]
def get_cores_per_cpu(): ret, data = OCT_SYSTEM("cat %s | grep 'cpu cores'| uniq" % HW_CPUINFO_FILE) if ret != 0: ERROR("get processor number error") return 2 return int(data.replace("\n", ""))
def get_irq_info(): irq = { "irq": 0, "softIrq": 0, } DSTAT_TOOL = "/usr/bin/dstat" if not os.path.exists(DSTAT_TOOL): ERROR("dstat tool not installed") return irq ret, data = OCT_SYSTEM("%s -c --nocolor --noheaders 1 5" % DSTAT_TOOL) if ret != 0: ERROR("get dstat info error %d" % ret) return irq hi = 0 si = 0 lines = data.split("\n") for line in lines: segs = line.split() if len(segs) != 6 or segs[0] == "usr": continue hi = hi + int(segs[4]) si = si + int(segs[5]) irq["irq"] = round(hi / 5, 2) irq["softIrq"] = round(si / 5, 2) return irq
def get_cpu_number(): ret, data = OCT_SYSTEM( "cat %s | grep 'physical id' | sort | uniq | wc -l" % HW_CPUINFO_FILE) if ret != 0: ERROR("get processor number error") return 1 return int(data.replace("\n", ""))
def get_cpu_cores(): ret, data = OCT_SYSTEM("cat %s | grep -w processor | wc -l" % HW_CPUINFO_FILE) if ret != 0: ERROR("get processor number error") return 1 return int(data.replace("\n", ""))
def get_debian_version(): DEBIAN_VERSION_FILE = "/etc/debian_version" if not os.path.exists(DEBIAN_VERSION_FILE): return "UNKNOWN" ret, data = OCT_SYSTEM("cat %s" % DEBIAN_VERSION_FILE) if ret != 0: return "UNKNOWN" return data.split("\n")[-1]
def get_mem_info(): mem_flags = { "totalmemory": "total", "usedmemory": "used", "available": "available", "inactivememory": "cache", "freememory": "free", "buffermemory": "buffer", "swapcache": "swapCache", "totalswap": "totalSwap", "usedswap": "usedSwap", "freeswap": "freeSwap" } info = {} if not os.path.exists(VMSTAT_TOOL): ERROR("vmstat tool not exist") return info ret, data = OCT_SYSTEM(MEMINFO_CMD) if ret != 0: ERROR("get meminfo error with cmd %s, %d\n" % (MEMINFO_CMD, ret)) return info lines = data.split("\n") for line in lines: segs = line.replace(" ", "").split("K") flag = mem_flags.get(segs[1]) if not flag: continue info[flag] = int(segs[0]) * 1024 info["shared"] = 0 # TBD info["available"] = info["free"] + info["buffer"] if not info["totalSwap"]: info["pfreeSwap"] = 0 info["pusedSwap"] = 0 else: info["pfreeSwap"] = round(info["freeSwap"] / info["totalSwap"] * 100.0, 2) info["pusedSwap"] = round(info["usedSwap"] / info["totalSwap"] * 100.0, 2) if not info["total"]: info["pused"] = 0 info["pavailable"] = 0 else: info["pused"] = round( (info["total"] - info["free"]) / info["total"] * 100.0, 2) info["pavailable"] = round(info["available"] / info["total"] * 100.0, 2) return info
def get_vmnumber_byvirsh(): info = {} CMD = "%s list --all | sed -n '3,$p' | sed '$d' | wc -l" % VIRSH_UTIL ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None info["totalVm"] = (int(data)) CMD = "%s list | sed -n '3,$p' | sed '$d' | wc -l" % VIRSH_UTIL ret, data = OCT_SYSTEM(CMD) if ret: ERROR("exec cmdline [%s] error" % CMD) return None info["runningVm"] = (int(data)) return info
def get_cpu_time(): timeinfo = { "user": 0, "system": 0, "nice": 0, "idle": 100.00, "steal": 0, "iowait": 0, } IOSTAT_TOOL = "/usr/bin/iostat" if not os.path.exists(IOSTAT_TOOL): ERROR("iostat tool not installed") return timeinfo ret, data = OCT_SYSTEM("%s -c 1 5" % IOSTAT_TOOL) if ret != 0: ERROR("get iostat info error %d" % ret) return timeinfo user = 0 nice = 0 system = 0 iowait = 0 steal = 0 idle = 0 lines = data.split("\n") for line in lines: segs = line.split() if len(segs) != 6 or not segs[0] or segs[0] == "Linux": continue user = user + float(segs[0]) nice = nice + float(segs[1]) system = system + float(segs[2]) iowait = iowait + float(segs[3]) steal = steal + float(segs[4]) idle = idle + float(segs[5]) timeinfo["user"] = round(user / 5, 2) timeinfo["nice"] = round(nice / 5, 2) timeinfo["system"] = round(system / 5, 2) timeinfo["iowait"] = round(iowait / 5, 2) timeinfo["steal"] = round(steal / 5, 2) timeinfo["idle"] = round(idle / 5, 2) return timeinfo
def parse_disk_stat(): info = {} if not os.path.exists(IOSTAT_TOOL): ERROR("iostat tool not installed") return stat ret, data = OCT_SYSTEM("iostat -d -k 1 5") if ret != 0: ERROR("exec iostat command error") return stat paras = data.split("Device:")[1:] dev_num = len([tmp for tmp in paras[0].split("\n")[1:] if tmp]) devices = [] for i in range(dev_num): device = [] for para in paras: lines = [tmp for tmp in para.split("\n")[1:] if tmp] device.append(lines[i]) devices.append(device) for device in devices: tps_total = 0 read_total = 0 write_total = 0 for i in device: tps_total += float(i.split()[1]) read_total += float(i.split()[2]) write_total += float(i.split()[3]) dev_name = i.split()[0] dev = { "tps": round(tps_total/5, 2), "readSpeed": round(read_total/5, 2), # unit KB/s "writeSpeed": round(write_total/5, 2) # unit KB/s } info[dev_name] = dev return info
def parse_disk_info(): diskinfo = {} ret, data = OCT_SYSTEM("df") if ret != 0: ERROR("exec cmdline df error") return diskinfo data = data.split("\n")[1:] for i in data: filesystem = i.split()[0] disk = { "total": int(i.split()[1]), "used": int(i.split()[2]), "available": int(i.split()[3]), "pused": i.split()[4], "mount": i.split()[5] } diskinfo[filesystem] = disk return diskinfo
def get_kernel_info(): ret, data = OCT_SYSTEM("uname -r -v -m") if ret != 0: return "UNKNOWN" return data.split("\n")[0]
def get_max_open_files(): ret, data = OCT_SYSTEM("ulimit -n") if ret != 0: return "ulimited" return data.split("\n")[0]