Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split code using lib and objects #3

Merged
merged 14 commits into from
Oct 1, 2024
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -45,6 +45,12 @@ clusters:
host: 10.10.10.10
user: pvecontrol@pve
password: Supers3cUre
node:
# Overcommit cpu factor. can be 1 for not overcommit
cpufactor: 2.5
# Memory to reserve for system on a node. in Bytes
memoryminimum: 81928589934592

```

Usage
59 changes: 59 additions & 0 deletions src/pvecontrol/cluster.py
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
4 changes: 4 additions & 0 deletions src/pvecontrol/config_default.yaml
Original file line number Diff line number Diff line change
@@ -5,3 +5,7 @@ clusters:
host: 127.0.0.1
user: pvecontrol@pve
password: somerandomsecret

node:
cpufactor: 2.5
memoryminimum: 81928589934592
85 changes: 85 additions & 0 deletions src/pvecontrol/node.py
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
Loading
Loading