码迷,mamicode.com
首页 > 编程语言 > 详细

Python 之 获取Host Esxi 主机信息

时间:2019-02-14 23:43:46      阅读:687      评论:0      收藏:0      [点我收藏+]

标签:获取   root   blog   serve   https   roo   imp   username   vpd   

代码
#Author Kang
#获取ESXI主机的CPU和内存使用情况,并统计百分比

import ssl
import pysphere
from pysphere import VIServer,MORTypes,VIProperty
from pysphere.resources import VimService_services as VI

ssl._create_default_https_context = ssl._create_unverified_context
host_ip = ‘10.3.151.2‘      
username = ‘root‘           
passwd = ‘password‘         

s = VIServer()      
s.connect(host_ip,username,passwd)

hosts = s.get_hosts()

for host in hosts:
    p = VIProperty(s,host)
    HostName = p.name
    HostCpuUsage = p.summary.quickStats.overallCpuUsage
    HostMemoryUsage = p.summary.quickStats.overallMemoryUsage
    HostNumCpuCores = p.summary.hardware.numCpuCores
    HostMhzPerCore = p.summary.hardware.cpuMhz
    HostCpuTotal = (HostNumCpuCores * HostMhzPerCore)
    HostTotalMemory = p.summary.hardware.memorySize/ 1048576
    HostCpuUsagePercent = ((HostCpuUsage * 100) / HostCpuTotal)
    HostMemoryUsagePercent = ((HostMemoryUsage * 100) / HostTotalMemory)

    print "%s | CpuUsage=%s MemoryUsage=%s" %(HostName,str(HostCpuUsage),str(HostMemoryUsage))
    print "%s | TotalCpu=%s TotalMemory=%s" %(HostName,str(HostCpuTotal),str(HostTotalMemory))
    print "%s | CpuUsagePercent=%s MemoryUsagePercent=%s" % (HostName,str(HostCpuUsagePercent), str(HostMemoryUsagePercent))

s.disconnect()

结果:
10.3.151.9 | CpuUsage=2029 MemoryUsage=95515
10.3.151.9 | TotalCpu=46000 TotalMemory=130845
10.3.151.9 | CpuUsagePercent=4 MemoryUsagePercent=72

技术图片

Python 之 获取Host Esxi 主机信息

标签:获取   root   blog   serve   https   roo   imp   username   vpd   

原文地址:http://blog.51cto.com/12965094/2350355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!