The Hypervisor class allows you to query physical hypervisor hosts and discover all virtual machines running on them, including their owners and configurations.
Get all virtual machines running on a specific hypervisor:
from myos.hypervisor import Hypervisorhv = Hypervisor(name='hv1.matrix.net')servers = hv.serversprint(f"Servers on {hv.hostname}: {len(servers)}")for server in servers: print(f" {server.name} ({server.id})")
from myos.hypervisor import Hypervisorhv = Hypervisor(name='hv1.matrix.net')servers = hv.serversfor server in servers: user = server.user print(f"{server.name} {server.id} {user.name} {user.id} {user.email}")
Each server object provides full access to its owner’s information through the user property.
from myos.cloud import Cloudcloud = Cloud("admin")hypervisors = cloud.hypervisors# Find hypervisors with more than 20 VMsbusy_hvs = hypervisors.filter(lambda hv: len(hv.servers) > 20)# Sort by number of servers (descending)busy_hvs = busy_hvs.sort(lambda hv: -len(hv.servers))print(f"Busy hypervisors: {len(busy_hvs)}")for hv in busy_hvs: print(f" {hv.hostname}: {len(hv.servers)} servers")
Find which hypervisors are hosting servers from a specific project:
from myos.project import Projectproject = Project(name='Condor')servers = project.servers# Group servers by hypervisorhv_map = {}for server in servers: hv_name = server.hypervisor.hostname if hv_name not in hv_map: hv_map[hv_name] = [] hv_map[hv_name].append(server.name)print(f"Project '{project.name}' servers by hypervisor:")for hv_name, server_names in sorted(hv_map.items()): print(f"\n{hv_name} ({len(server_names)} servers):") for name in server_names: print(f" - {name}")
Identify servers on a hypervisor for maintenance planning:
from myos.hypervisor import Hypervisordef plan_maintenance(hostname): """List all stakeholders affected by hypervisor maintenance""" hv = Hypervisor(name=hostname) servers = hv.servers print(f"Maintenance Planning for: {hv.hostname}") print(f"Affected Servers: {len(servers)}") # Get unique user emails for notifications users = set() for server in servers: users.add(server.user.email) print(f"\nUsers to Notify: {len(users)}") for email in sorted(users): user_servers = [s for s in servers if s.user.email == email] print(f" {email}: {len(user_servers)} server(s)") for server in user_servers: print(f" - {server.name}")# Plan maintenanceplan_maintenance('hv1.matrix.net')