Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions samples/get_hosts_cpuinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_cpu_info(hosts):
host_cpu_info_dict = {}
for host in hosts:
cpu_info = host.hardware.cpuInfo
host_cpu_info_dict[host] = cpu_info
return host_cpu_info_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_cpu_info_dict = get_hosts_cpu_info(hosts)
if host_cpu_info_dict is not None:
print("The hosts cpu info is:\n")
for host, cpu_info in host_cpu_info_dict.items():
print(f"Host: {host.name}\n")
print(f"Number of physical CPU cores on the host is {cpu_info.numCpuCores}")
print(f"Number of physical CPU packages on the host is {cpu_info.numCpuPackages}")
print(f"Number of physical CPU threads on the host is {cpu_info.numCpuThreads}")


# Main section
if __name__ == "__main__":
sys.exit(main())
51 changes: 51 additions & 0 deletions samples/get_hosts_fullname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_product(hosts):
host_product_dict = {}
for host in hosts:
product = host.config.product
host_product_dict[host] = product
return host_product_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_product_dict = get_hosts_product(hosts)
if host_product_dict is not None:
print("The hosts full name is:\n")
#print(host_product_dict)
for host, product in host_product_dict.items():
print(f"Host: {host.name}\n")
print(product.fullName)


# Main section
if __name__ == "__main__":
sys.exit(main())
49 changes: 49 additions & 0 deletions samples/get_hosts_memsize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_mem_size(hosts):
host_mem_size_dict = {}
for host in hosts:
mem_size = host.hardware.memorySize
host_mem_size_dict[host] = mem_size
return host_mem_size_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_mem_size_dict = get_hosts_mem_size(hosts)
if host_mem_size_dict is not None:
print("The hosts memory information is:\n")
for host, mem_size in host_mem_size_dict.items():
print(f"Host: {host.name}\n")
print(f"Memory size is {mem_size} bytes")

# Main section
if __name__ == "__main__":
sys.exit(main())
53 changes: 53 additions & 0 deletions samples/get_hosts_pnic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_pnics(hosts):
host_pnics_dict = {}
for host in hosts:
pnics = host.config.network.pnic
host_pnics_dict[host] = pnics
return host_pnics_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_pnics_dict = get_hosts_pnics(hosts)
if host_pnics_dict is not None:
print("Hosts physical network adapters:\n")
for host, pnics in host_pnics_dict.items():
print(f"Host: {host.name}\n")
for pnic in pnics:
print(pnic.device)
else:
print("No physical network adapters found")


# Main section
if __name__ == "__main__":
sys.exit(main())
58 changes: 58 additions & 0 deletions samples/get_hosts_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys

def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts

def get_host_services_status(host):
service_system = host.configManager.serviceSystem
services_info = service_system.serviceInfo.service

services_status = {}
for service in services_info:
services_status[service.key] = {
'name': service.label,
'running': service.running,
}

return services_status

def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)

if hosts:
print("Service Status on Hosts:\n")
for host in hosts:
services_status = get_host_services_status(host)
print(f"Host: {host.name}")

for service_key, service_info in services_status.items():
print(f"Service: {service_info['name']}")
print(f"Status: {'Running' if service_info['running'] else 'Not Running'}")
print()

if __name__ == "__main__":
sys.exit(main())


52 changes: 52 additions & 0 deletions samples/get_hosts_systeminfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_system_info(hosts):
host_system_info_dict = {}
for host in hosts:
system_info = host.hardware.systemInfo
host_system_info_dict[host] = system_info
return host_system_info_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_system_info_dict = get_hosts_system_info(hosts)
if host_system_info_dict is not None:
print("The hosts system info is:\n")
for host, system_info in host_system_info_dict.items():
print(f"Host: {host.name}\n")
print(f"System model identification is {system_info.model}")
print(f"The serial number is {system_info.serialNumber}")
print(f"The vendor is {system_info.vendor}")


# Main section
if __name__ == "__main__":
sys.exit(main())
58 changes: 58 additions & 0 deletions samples/get_hosts_vnic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Written by Maros Kukan
Github: https://github.com/maroskukan
Email: maros.kukan@me.com
Note: Example code For testing purposes only
This code has been released under the terms of the Apache-2.0 license
http://opensource.org/licenses/Apache-2.0
"""

from pyVmomi import vim
from tools import cli, service_instance
import sys


def get_vm_hosts(content):
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem],
True)
hosts = list(host_view.view)
host_view.Destroy()
return hosts


def get_hosts_vnics(hosts):
host_vnics_dict = {}
for host in hosts:
vnics = host.config.network.vnic
host_vnics_dict[host] = vnics
return host_vnics_dict


def main():
parser = cli.Parser()
args = parser.get_args()
si = service_instance.connect(args)
content = si.RetrieveContent()

hosts = get_vm_hosts(content)
host_vnics_dict = get_hosts_vnics(hosts)
if host_vnics_dict is not None:
print("Hosts virtual network adapters:\n")
for host, vnics in host_vnics_dict.items():
print(f"Host: {host.name}\n")
for vnic in vnics:
print(f"Device is {vnic.device}")
print(f"Portgroup is {vnic.portgroup}")
print(f"IP address is {vnic.spec.ip.ipAddress}")
print(f"Subnet mask is {vnic.spec.ip.subnetMask}")
print(f"MTU is {vnic.spec.mtu}")
print("\n")
else:
print("No virtual network adapters found")


# Main section
if __name__ == "__main__":
sys.exit(main())
Loading