python中通过pyVim和pyVmomi库文件获取ESXI虚拟机的cpu和内存使用率
from pyVim import connect
from pyVmomi import vim
import ssl
from pyVim.connect import SmartConnectNoSSL, Disconnect
import atexit
import pyVim
def get_vm_perf(self):
server_ip=self.server_ip
username=self.username
password=self.password
port=self.port
error_count=30;l=0;
while 1:
try:
si = SmartConnectNoSSL(
host=server_ip,
user=username,
pwd=password,
port=443)
atexit.register(Disconnect, si)
if not si:
raise SystemExit("Unable to connect to host with supplied info.")
content = si.RetrieveContent()
perfManager = content.perfManager
# create a mapping from performance stats to their counterIDs
# counterInfo: [performance stat => counterId]
# performance stat example: cpu.usagemhz.LATEST
# counterId example: 6
counterInfo = {}
for c in perfManager.perfCounter:
&nbs