-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split code using lib and objects
- Loading branch information
Showing
7 changed files
with
469 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from proxmoxer import ProxmoxAPI | ||
|
||
from node import PVENode | ||
from task import PVETask | ||
|
||
class PVECluster: | ||
"""Proxmox VE Cluster""" | ||
|
||
|
||
def __init__(self, name, host, user, password, verify_ssl=False): | ||
self._api = ProxmoxAPI(host, user=user, password=password, verify_ssl=False) | ||
self.name = name | ||
self._initstatus() | ||
|
||
def _initstatus(self): | ||
self.status = self._api.cluster.status.get() | ||
self.resources = self._api.cluster.resources.get() | ||
|
||
self.nodes = [] | ||
for node in self._api.nodes.get(): | ||
self.nodes.append(PVENode(self._api, node["node"], node["status"], node)) | ||
|
||
self.tasks = [] | ||
for task in self._api.cluster.tasks.get(): | ||
self.tasks.append(PVETask(self._api, task["upid"])) | ||
|
||
def refresh(self): | ||
self._initstatus() | ||
|
||
def __str__(self): | ||
output = "Proxmox VE Cluster %s\n"%self.name | ||
output += " Status: " + str(self.status) + "\n" | ||
output += " Resources: " + str(self.resources) + "\n" | ||
output += " Nodes:\n" | ||
for node in self.nodes: | ||
output += str(node) + "\n" | ||
return output | ||
|
||
def vms(self): | ||
"""Return all vms on this cluster""" | ||
vms = [] | ||
for node in self.nodes: | ||
for vm in node.vms: | ||
vms.append(vm) | ||
return vms | ||
|
||
def find_node(self, nodename): | ||
"""Check for node is running on this cluster""" | ||
for node in self.nodes: | ||
if node.node == nodename: | ||
return node | ||
return False | ||
|
||
def find_task(self, upid): | ||
"""Return a task by upid""" | ||
for task in self.tasks: | ||
if task.upid == upid: | ||
return task | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from enum import Enum | ||
|
||
from vm import PVEVm | ||
from vm import VmStatus | ||
|
||
class NodeStatus(Enum): | ||
unknown = 0 | ||
online = 1 | ||
offline = 2 | ||
|
||
class PVENode: | ||
"""A proxmox VE Node""" | ||
_api = None | ||
|
||
def __init__(self, api, node, status, input = {}): | ||
self.node = node | ||
self.status = NodeStatus[status] | ||
self._api = api | ||
self.cpu = 0 | ||
self.allocatedcpu = 0 | ||
self.maxcpu = 0 | ||
self.mem = 0 | ||
self.allocatedmem = 0 | ||
self.maxmem = 0 | ||
self.disk = 0 | ||
self.maxdisk = 0 | ||
for k in input: | ||
if k == "cpu": | ||
self.cpu = input[k] | ||
elif k == "maxcpu": | ||
self.maxcpu = input[k] | ||
elif k == "mem": | ||
self.mem = input[k] | ||
elif k == "maxmem": | ||
self.maxmem = input[k] | ||
elif k == "disk": | ||
self.disk = input[k] | ||
elif k == "maxdisk": | ||
self.maxdisk = input[k] | ||
self._init_vms() | ||
self._init_allocatedmem() | ||
self._init_allocatedcpu() | ||
|
||
def __str__(self): | ||
output = "Node: " + self.node + "\n" | ||
output += "Status: " + str(self.status) + "\n" | ||
output += "CPU: " + str(self.cpu) + "/" + str(self.allocatedcpu) + "/" + str(self.maxcpu) + "\n" | ||
output += "Mem: " + str(self.mem) + "/" + str(self.allocatedmem) + "/" + str(self.maxmem) + "\n" | ||
output += "Disk: " + str(self.disk) + "/" + str(self.maxdisk) + "\n" | ||
output += "VMs: \n" | ||
for vm in self.vms: | ||
output += " - " + str(vm) + "\n" | ||
return output | ||
|
||
def _init_vms(self): | ||
self.vms = [] | ||
if self.status == NodeStatus.online: | ||
self.vms = [ PVEVm(self._api, self.node, vm["vmid"], vm["status"], vm) for vm in self._api.nodes(self.node).qemu.get() ] | ||
|
||
def _init_allocatedmem(self): | ||
"""Compute the amount of memory allocated to running VMs""" | ||
self.allocatedmem = 0 | ||
for vm in self.vms: | ||
if vm.status != VmStatus.running: | ||
continue | ||
# This is in MB in configuration | ||
self.allocatedmem += int(vm.config['memory']) * 1024 * 1024 | ||
|
||
def _init_allocatedcpu(self): | ||
"""Compute the amount of cpu allocated to running VMs""" | ||
self.allocatedcpu = 0 | ||
for vm in self.vms: | ||
if vm.status!= VmStatus.running: | ||
continue | ||
if "sockets" in vm.config: | ||
self.allocatedcpu += vm.config['sockets'] * vm.config['cores'] | ||
else: | ||
self.allocatedcpu += vm.config['cores'] | ||
|
||
# def __contains__(self, item): | ||
# """Check if a VM is running on this node""" | ||
# for vm in self.vms: | ||
# if vm.vmid == item: | ||
# return True | ||
# return False |
Oops, something went wrong.