我正在开发openstack,我想监视虚拟机的CPU使用情况。为此,我想通过父(中央)openstack实例找到他们的PID。 我用过
ps aux | grep
我确实收到了输出。但是我想确认这是否是正确的PID。他们有什么方法可以查看这个吗?
或者他们是否有其他方法可以找到虚拟机的PID?
更新。 此命令不起作用。它给了我一个总是在变化的PID。它不是恒定的。 谢谢
答案 0 :(得分:1)
好的libvirt有一些接口。这里有一些python将数据提取到数据结构中:
#!/usr/bin/env python
# Modules
import subprocess
import traceback
import commands
import signal
import time
import sys
import re
import os
import getopt
import pprint
try:
import libvirt
except:
print "no libvirt detected"
sys.exit(0)
from xml.dom.minidom import parseString
global instances
global virt_conn
global tick
global virt_exist
def virtstats():
global virt_exist
global virt_conn
global instances
cpu_stats = []
if virt_exist == True:
if virt_conn == None:
print 'Failed to open connection to the hypervisor'
virt_exist = False
if virt_exist == True:
virt_info = virt_conn.getInfo()
for x in range(0, virt_info[2]):
cpu_stats.append(virt_conn.getCPUStats(x,0))
virt_capabilities = virt_conn.getCapabilities()
domcpustats = 0
# domcpustats = virDomain::GetcpuSTATS()
totmem = 0
totvcpu = 0
totcount = 0
vcpu_stats = []
for id in virt_conn.listDomainsID():
dom = virt_conn.lookupByID(id)
totvcpu += dom.maxVcpus()
vcpu_stats.append(dom.vcpus())
totmem += dom.maxMemory()
totcount += 1
dom = parseString(virt_capabilities)
xmlTag = dom.getElementsByTagName('model')[0].toxml()
xmlData=xmlTag.replace('<model>','').replace('</model>','')
for info in virt_info:
print info
for stat in cpu_stats:
print "cpu %s" % stat
for vstat in vcpu_stats:
print "vcpu:\n"
pprint.pprint(vstat)
print "CPU ( %s ) Use - %s vCPUS ( %s logical processors )" % (xmlData, totvcpu, virt_info[2])
sys.exit(0)
def main():
try:
global virt_conn
global virt_exist
virt_conn = libvirt.openReadOnly(None)
virt_exist = True
except:
virt_exist = False
print "OK: not a compute node"
sys.exit(0)
virtstats()
if __name__ == "__main__":
main()
现在你从使用方面得到的是cpu time。
vcpu块基本上采用这种布局:
1st: vCPU number, starting from 0. 2nd: vCPU state. 0: offline 1: running 2: blocked on resource 3rd: CPU time used in nanoseconds 4th: real CPU number
一旦你意识到libvirt中存在的问题,CPU块就很明显了。
希望有所帮助!
答案 1 :(得分:1)
通过使用libvirt,python,lxml和lsof,如果您的虚拟实例(域)具有显示输出集,则可以恢复pid。 (VNC,Spice,......)
以下是代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lxml import etree
import libvirt
from subprocess import check_output
def get_port_from_XML(xml_desc):
tree = etree.fromstring(xml_desc)
ports = tree.xpath('//graphics/@port')
if len(ports):
return ports[0]
return None
def get_pid_from_listen_port(port):
if port is None:
return ''
return check_output(['lsof', '-i:%s' % port, '-t']).replace('\n','')
conn = libvirt.openReadOnly('')
if conn is None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
for domain_id in conn.listDomainsID():
domain_instance = conn.lookupByID(domain_id)
name = domain_instance.name()
xml_desc = domain_instance.XMLDesc(0)
port = get_port_from_XML(xml_desc)
pid = get_pid_from_listen_port(port)
print '%s (port:%s) (pid:%s)' % (name, port, pid)
答案 2 :(得分:0)
grep "79d87652-8c8e-4afa-8c13-32fbcbf98e76" --include=libvirt.xml /path/to/nova/instances -r -A 2 | grep "<name" | cut -d " " -f 3
允许查找&#34;实例 - &#34;可以映射到&#34; -name&#34;的ps aux输出;参数。所以你可以将openstack实例id映射到pid。
答案 3 :(得分:0)
最简单的方法是使用cgroups:
在Ubuntu中:
cat /sys/fs/cgroup/cpuset/libvirt/qemu/<machine-name>/tasks